vx11 / examples / focus.v
117 lines · 113 sloc · 3.69 KB · c4bc47d81a04ec4c69336c7c85575735cb003365
Raw
1// Rounded-window focus timer. SHAPE changes the actual server-side window region.
2module main
3
4import time
5import vx11
6import vx11.shape
7import vx11.examples.demo
8
9fn rounded_window(extension &shape.Extension, window vx11.Window, w int, h int) ! {
10 mut rectangles := []shape.Rectangle{}
11 radius := 24
12 // Horizontal scan lines approximate quarter circles without a bitmap mask.
13 for y in 0 .. h {
14 mut inset := 0
15 if y < radius || y >= h - radius {
16 dy := if y < radius { radius - y - 1 } else { y - (h - radius) }
17 for inset < radius && (radius - inset) * (radius - inset) + dy * dy > radius * radius {
18 inset++
19 }
20 }
21 rectangles << shape.Rectangle{ x: inset, y: y, width: w - 2 * inset, height: 1 }
22 }
23 extension.combine(window, .bounding, rectangles, .set)!
24}
25
26fn focus(mut c demo.Canvas, remaining int, running bool) ! {
27 c.rect(0, 0, c.width, c.height, '#192c2b')!
28 c.text(30, 40, 'STILL / FOCUS', '#b4d0c2', false)!
29 c.circle(c.width - 43, 28, 8, if running { '#c8efad' } else { '#d7b77b' })!
30 cx := c.width / 2
31 c.centered(cx, 83, 'MAKE SOME SPACE', '#7eaa99', false)!
32 c.arc(cx - 104, 110, 208, 360, '#344b42')!
33 c.arc(cx - 104, 110, 208, (1500 - remaining) * 360 / 1500, '#c8efad')!
34 minutes := remaining / 60
35 seconds := remaining % 60
36 label := '${minutes:02}:${seconds:02}'
37 c.scaled_text(cx - 60, 225, label, '#eef5df', 4)!
38 c.centered(cx, 241, if running { 'STAY WITH IT' } else { 'READY WHEN YOU ARE' }, '#a5bca8', false)!
39 c.round_rect(32, c.height - 124, c.width - 64, 46, 12, '#c8efad')!
40 c.centered(cx, c.height - 95, if running { 'PAUSE' } else { 'START' }, '#213a2e', false)!
41 c.text(30, c.height - 49, 'Space / click Start or pause', '#a5bca8', false)!
42 c.text(30, c.height - 28, 'R Reset Q / Esc Close', '#729585', false)!
43 c.present()!
44}
45
46fn main() {
47 mut display := vx11.connect('')!
48 defer { display.close() }
49 shapes := shape.bind(display)!
50 mut window := display.new_window(
51 title: 'Still - a focus timer'
52 width: 360
53 height: 470
54 events: vx11.exposure_mask | vx11.structure_notify_mask | vx11.key_press_mask | vx11.button_press_mask
55 )!
56 defer { window.destroy() }
57 demo.minimum_size(display, window, 360, 470)!
58 mut canvas := demo.new_canvas(display, window, 360, 470)!
59 defer { canvas.close() }
60 rounded_window(shapes, window.id, 360, 470)!
61 mut running := false
62 mut remaining := 1500
63 mut last := time.now().unix_milli()
64 mut dirty := true
65 window.show()!
66 focus(mut canvas, remaining, running)!
67 if canvas.finish_preview()! { return }
68 mut quit := false
69 for !quit {
70 for event in display.poll_events()! {
71 if window.should_close(event) { quit = true }
72 if event.kind == vx11.key_press {
73 key := display.key_text(event)!
74 if key in ['q', 'Q', '\x1b'] { quit = true }
75 if key == ' ' {
76 running = !running
77 last = time.now().unix_milli()
78 dirty = true
79 }
80 if key in ['r', 'R'] {
81 remaining = 1500
82 running = false
83 dirty = true
84 }
85 }
86 if button := event.button() {
87 if button.y >= canvas.height - 124 && button.y < canvas.height - 78 && button.x >= 32 && button.x < canvas.width - 32 {
88 running = !running
89 last = time.now().unix_milli()
90 dirty = true
91 }
92 }
93 if size := event.configure() {
94 canvas.resize(size.width, size.height)!
95 rounded_window(shapes, window.id, size.width, size.height)!
96 dirty = true
97 }
98 if event.kind == vx11.expose { dirty = true }
99 }
100 now := time.now().unix_milli()
101 if running && now - last >= 1000 {
102 elapsed := int((now - last) / 1000)
103 remaining -= elapsed
104 if remaining <= 0 {
105 remaining = 0
106 running = false
107 }
108 last += i64(elapsed) * 1000
109 dirty = true
110 }
111 if dirty {
112 focus(mut canvas, remaining, running)!
113 dirty = false
114 }
115 time.sleep(30 * time.millisecond)
116 }
117}
118