-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_subwindow.v
322 lines (290 loc) · 7.03 KB
/
widget_subwindow.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
module ui
import eventbus
import gx
pub const (
sw_decoration = 20
sw_z_index = 10000
sw_z_index_top = 1000
sw_z_index_child = 100
)
[heap]
pub struct SubWindow {
pub mut:
id string
x int
y int
z_index int = ui.sw_z_index
z_index_children_orig []int
offset_x int
offset_y int
hidden bool
ui &UI = 0
// dragging
drag bool
dragging bool
drag_x int
drag_y int
// decoration
decoration bool
// main unique layout attached to the subwindow
layout Layout = empty_stack
is_focused bool
parent Layout = empty_stack
// component state for composable widget
component voidptr
}
[params]
pub struct SubWindowParams {
id string
x int
y int
hidden bool = true
layout Layout = empty_stack
drag bool = true
decoration bool = true
}
pub fn subwindow(c SubWindowParams) &SubWindow {
mut s := &SubWindow{
id: c.id
x: c.x
y: c.y
layout: c.layout
hidden: c.hidden
drag: c.drag
decoration: c.decoration
}
return s
}
fn (mut s SubWindow) init(parent Layout) {
s.parent = parent
pui := parent.get_ui()
s.ui = pui
// Subscriber needs here to be before initialization of all its children
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_mouse_down, sw_mouse_down, s)
subscriber.subscribe_method(events.on_mouse_move, sw_mouse_move, s)
subscriber.subscribe_method(events.on_mouse_up, sw_mouse_up, s)
s.ui.window.evt_mngr.add_receiver(s, [events.on_mouse_down])
// children initialized after so that subcribe_method
mut l := s.layout
if l is Widget {
mut w := l as Widget
w.init(s)
}
// z_index of all children
s.set_children_depth(s.z_index + ui.sw_z_index_child)
s.set_pos(s.x, s.y)
s.update_layout()
s.set_visible(!s.hidden)
}
[manualfree]
pub fn (mut s SubWindow) cleanup() {
mut subscriber := s.parent.get_subscriber()
subscriber.unsubscribe_method(events.on_mouse_down, s)
subscriber.unsubscribe_method(events.on_mouse_move, s)
subscriber.unsubscribe_method(events.on_mouse_up, s)
mut ui := s.get_ui()
ui.window.evt_mngr.rm_receiver(s, [events.on_mouse_down])
unsafe { s.free() }
}
fn (mut s SubWindow) draw() {
if s.hidden {
return
}
offset_start(mut s)
// possibly add window decoration
if s.decoration {
w, _ := s.size()
s.ui.gg.draw_rounded_rect_filled(s.x, s.y, w, ui.sw_decoration, 5, gx.black)
}
s.layout.draw()
offset_end(mut s)
}
[unsafe]
pub fn (s &SubWindow) free() {
$if free ? {
print('canvas_layout $s.id')
}
unsafe {
s.id.free()
free(s)
}
$if free ? {
println(' -> freed')
}
}
fn sw_mouse_down(mut s SubWindow, e &MouseEvent, window &Window) {
// println("sw_md: $s.id -> ${window.point_inside_receivers(events.on_mouse_down)}")
if s.hidden {
return
}
// if !window.is_top_widget(s, events.on_mouse_down) {
// return
// }
// println("sw $s.id start dragging")
if s.decoration && s.point_inside_bar(e.x, e.y) {
s.as_top_subwindow()
s.dragging = true
s.drag_x, s.drag_y = s.x - e.x, s.y - e.y
// w, h := s.size()
// println("drag down: ($s.drag_x, $s.drag_y) ($s.x, $s.y, ${s.x + w}, ${s.y + h}) }")
}
}
fn sw_mouse_up(mut s SubWindow, e &MouseEvent, window &Window) {
if s.hidden {
return
}
s.dragging = false
}
fn sw_mouse_move(mut s SubWindow, e &MouseMoveEvent, window &Window) {
// println('btn_click for window=$window.title')
if s.hidden {
return
}
if s.dragging {
w, _ := s.size()
new_x, new_y := s.drag_x + int(e.x), s.drag_y + int(e.y)
// println("($new_x, $new_y)")
if new_x + w - ui.sw_decoration >= 0 && new_y + ui.sw_decoration / 2 >= 0
&& new_x + ui.sw_decoration <= s.ui.window.width
&& new_y + ui.sw_decoration / 2 <= s.ui.window.height {
s.set_pos(new_x, new_y)
// println("sw $s.id dragging $s.x, $s.y")
s.update_layout()
// window.update_layout()
}
}
}
pub fn (mut s SubWindow) update_layout() {
s.layout.update_layout()
}
fn (mut s SubWindow) set_adjusted_size(ui &UI) {
}
fn (mut s SubWindow) point_inside_bar(x f64, y f64) bool {
// add possible decoration
if s.decoration {
w, _ := s.size()
return x > s.x && x < s.x + w && y > s.y && y < s.y + ui.sw_decoration
} else {
return false
}
}
fn (mut s SubWindow) point_inside(x f64, y f64) bool {
// add possible decoration
if s.decoration {
w, h := s.size()
// println('point_inside $s.id $w, $h')
return x > s.x && x < s.x + w && y > s.y && y < s.y + h + ui.sw_decoration
} else {
if s.layout is Widget {
mut w := s.layout as Widget
// println(" ${w.point_inside(x, y)}")
return w.point_inside(x, y)
} else {
return false
}
}
}
pub fn (mut s SubWindow) set_pos(x int, y int) {
s.x = x
s.y = y
if s.layout is Widget {
mut w := s.layout as Widget
// println("sw set_pos: $s.x, $s.y $s.decoration")
w.set_pos(x, y + if s.decoration { ui.sw_decoration } else { 0 })
}
}
pub fn (mut s SubWindow) propose_size(width int, height int) (int, int) {
if s.layout is Widget {
mut ws := s.layout as Widget
w, mut h := ws.propose_size(width, height)
if s.decoration {
h += ui.sw_decoration
}
return w, h
} else {
return -1, -1
}
}
pub fn (s SubWindow) size() (int, int) {
w, mut h := s.layout.size()
if s.decoration {
h += ui.sw_decoration
}
return w, h
}
pub fn (mut s SubWindow) set_visible(state bool) {
s.hidden = !state
if s.layout is Widget {
mut w := s.layout as Widget
w.set_visible(state)
}
if !s.hidden {
s.ui.window.update_layout()
}
}
fn (s &SubWindow) get_ui() &UI {
return s.ui
}
pub fn (s &SubWindow) get_state() voidptr {
parent := s.parent
return parent.get_state()
}
fn (s &SubWindow) get_subscriber() &eventbus.Subscriber {
parent := s.parent
return parent.get_subscriber()
}
fn (mut s SubWindow) resize(w int, h int) {
s.layout.resize(w, h)
}
pub fn (s &SubWindow) get_children() []Widget {
if s.layout is Widget {
w := s.layout as Widget
return [w]
} else {
return []
}
}
fn (mut s SubWindow) set_children_depth(z_inc int) {
s.layout.incr_children_depth(z_inc)
s.ui.window.evt_mngr.sorted_receivers(events.on_mouse_down)
}
fn (mut s SubWindow) is_top_subwindow() bool {
return s.ui.window.subwindows.map(it.id).first() == s.id
}
fn (mut s SubWindow) as_top_subwindow() {
$if atsw ? {
println('as top subw $s.id')
Layout(s).debug_show_children_tree(0)
}
mut sws := []&SubWindow{}
for sw in s.ui.window.subwindows {
if sw.id != s.id {
sws << sw
}
}
sws << s
mut win := s.ui.window
win.subwindows = sws
// println("atp sws: ${win.subwindows.map(it.id)}")
for mut sw in sws {
sw.update_depth(sw.id == s.id)
}
$if atsw ? {
println('atp end')
Layout(s).debug_show_children_tree(0)
}
}
fn (mut s SubWindow) update_depth(top bool) {
// reset first the children
s.set_children_depth(-s.z_index - ui.sw_z_index_child)
// inc z_index
s.z_index = ui.sw_z_index
if top {
s.z_index += ui.sw_z_index_top
}
// propagate to children
// println("z_index: ${s.z_index + sw_z_index_child}")
s.set_children_depth(s.z_index + ui.sw_z_index_child)
s.update_layout()
}