-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.v
263 lines (241 loc) · 5.58 KB
/
ui.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (c) 2020-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license
// that can be found in the LICENSE file.
module ui
import time
import gg
import os
import clipboard
import eventbus
const (
version = '0.0.4'
)
const (
cursor_show_delay = 100 // ms
)
pub struct UI {
pub mut:
gg &gg.Context = voidptr(0)
window &Window = voidptr(0)
show_cursor bool
last_type_time i64
clipboard &clipboard.Clipboard
btn_down [3]bool
mut:
// just_typed bool
cb_image gg.Image
circle_image gg.Image
radio_image gg.Image
selected_radio_image gg.Image
down_arrow gg.Image
redraw_requested bool
resource_cache map[string]gg.Image
closed bool
ticks int
// text styles and font set
text_styles map[string]TextStyle
fonts FontSet
}
pub enum VerticalAlignment {
top = 0
center
bottom
}
pub enum HorizontalAlignment {
left = 0
center
right
}
pub interface Widget {
mut:
id string
x int
y int
z_index int
offset_x int
offset_y int
hidden bool
init(Layout)
cleanup()
draw()
point_inside(x f64, y f64) bool
set_pos(x int, y int)
propose_size(w int, h int) (int, int)
size() (int, int)
set_visible(bool)
}
pub interface Layout {
get_ui() &UI
get_state() voidptr
size() (int, int)
get_subscriber() &eventbus.Subscriber
get_children() []Widget
mut:
draw()
resize(w int, h int)
update_layout()
}
pub enum MouseAction {
up
down
}
// MouseButton is same to sapp.MouseButton
pub enum MouseButton {
invalid = 256
left = 0
right = 1
middle = 2
}
pub struct MouseEvent {
pub:
x int
y int
button MouseButton
action MouseAction
mods KeyMod
}
pub struct ScrollEvent {
pub:
x f64
y f64
mouse_x f64
mouse_y f64
}
pub struct MouseMoveEvent {
pub:
x f64
y f64
mouse_button int
// TODO enum
}
pub enum Cursor {
hand
arrow
ibeam
}
fn (mut gui UI) idle_loop() {
// This method is called by window.run to ensure
// that the window will be redrawn slowly, and that
// the cursor will blink at a rate of 1Hz, even if
// there are no other user events.
for {
if time.ticks() - gui.last_type_time < ui.cursor_show_delay {
// Always show the cursor if the user is typing right now
gui.show_cursor = true
} else {
gui.show_cursor = !gui.show_cursor
}
gui.gg.refresh_ui()
$if macos {
if gui.gg.native_rendering {
C.darwin_window_refresh()
}
}
gui.ticks = 0
// glfw.post_empty_event()
// Sleeping for a monolithic block of 500ms means, that the thread
// in which this method is run, may react to the closing of a dialog
// 500ms after the button for closing the dialog/window was clicked.
// Instead, we sleep 50 times, for just 10ms each time, checking
// in between the sleeps, whether the dialog window had been closed.
// This guarantees that the thread will exit at most 10ms after the
// closing event.
// kek_sleep()
// time.sleep(1 * time.second)
for i := 0; i < 50; i++ {
time.sleep(10 * time.millisecond)
if gui.closed {
return
}
}
}
}
pub fn run(window &Window) {
mut gui := window.ui
gui.window = window
go gui.idle_loop()
gui.gg.run()
/*
for !window.glfw_obj.should_close() {
if window.child_window != 0 {
//gg.clear(gx.rgb(230,230,230))
if window.child_window.draw_fn != voidptr(0) {
window.child_window.draw_fn(window.child_window.state)
}
for child in window.child_window.children {
child.draw()
}
}
else {
//gg.clear(window.bg_color)
// The user can define a custom drawing function for the entire window (advanced mode)
if window.draw_fn != voidptr(0) {
window.draw_fn(window.state)
}
// Render all widgets, including Canvas
for child in window.children {
child.draw()
}
}
// Triggers a re-render in case any function requests it.
// Transitions & animations, for example.
if gui.redraw_requested {
gui.redraw_requested = false
//glfw.post_empty_event()
}
gui.gg.render()
}
gui.window.glfw_obj.destroy()
*/
gui.closed = true
// the gui.idle_loop thread checks every 10 ms if gui.closed is true;
// waiting 2x this time should be enough to ensure the gui.loop
// thread will exit before us, without using a waitgroup here too
time.sleep(20 * time.millisecond)
}
fn (mut gui UI) load_icos() {
gui.cb_image = gui.gg.create_image_from_memory(&bytes_check_png[0], bytes_check_png.len)
$if macos {
gui.circle_image = gui.gg.create_image_from_memory(&bytes_darwin_circle_png[0],
bytes_darwin_circle_png.len)
} $else {
gui.circle_image = gui.gg.create_image_from_memory(&bytes_circle_png[0], bytes_circle_png.len)
}
gui.down_arrow = gui.gg.create_image_from_memory(&bytes_arrow_png[0], bytes_arrow_png.len)
gui.selected_radio_image = gui.gg.create_image_from_memory(&bytes_selected_radio_png[0],
bytes_selected_radio_png.len)
}
pub fn open_url(url string) {
if !url.starts_with('https://') && !url.starts_with('http://') {
return
}
$if windows {
os.execute('start "$url"')
}
$if macos {
os.execute('open "$url"')
}
$if linux {
os.execute('xdg-open "$url"')
}
}
pub fn confirm(s string) bool {
return false
}
[unsafe]
pub fn (gui &UI) free() {
unsafe {
// gg &gg.Context = voidptr(0)
// window &Window = voidptr(0)
// clipboard &clipboard.Clipboard
// cb_image gg.Image
// circle_image gg.Image
// radio_image gg.Image
// selected_radio_image gg.Image
// down_arrow gg.Image
gui.resource_cache.free()
}
$if free ? {
println('\tui -> freed')
}
}