-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathdynamic_layout.v
186 lines (170 loc) · 4.21 KB
/
dynamic_layout.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
import ui
const win_width = 800
const win_height = 600
@[heap]
struct App {
mut:
cpt int
}
fn main() {
mut app := &App{}
window := ui.window(
width: win_width
height: win_height
title: 'Dynamic layout'
mode: .resizable
layout: ui.row(
id: 'row'
spacing: 10
widths: [.4, .6]
heights: ui.stretch
children: [
ui.column(
id: 'col1'
spacing: 10
margin_: 10
children: [
ui.button(text: 'add last', on_click: app.btn_add_click),
ui.button(text: 'add two', on_click: app.btn_add_two_click),
ui.button(text: 'remove last', on_click: btn_remove_click),
ui.button(text: 'remove second', on_click: btn_remove_second_click),
ui.button(text: 'hide', on_click: btn_show_hide_click),
ui.button(text: 'deactivate', on_click: btn_show_activate_click),
ui.button(text: 'move', on_click: btn_move_click),
ui.button(text: 'text last', on_click: btn_last_text_click),
ui.button(text: 'text third', on_click: btn_third_text_click),
ui.button(text: 'text below', on_click: btn_text_below_click),
ui.button(text: 'switch', on_click: btn_switch_click),
ui.button(text: 'migrate', on_click: btn_migrate_click),
]
),
ui.column(
id: 'col2'
spacing: 10
margin_: 10
children: [
ui.button(text: 'Button'),
]
),
]
)
)
ui.run(window)
}
fn btn_switch_click(btn &ui.Button) {
window := btn.ui.window
// Without id:
// mut s := window.child() //root_layout
// if mut s is ui.Stack {
// s.move(from: 0, to: -1)
// }
mut s := window.get_or_panic[ui.Stack]('row')
s.move(from: 0, to: -1)
}
fn btn_migrate_click(btn &ui.Button) {
window := btn.ui.window
mut s := window.child(0)
mut t := window.child(1)
if mut s is ui.Stack {
if mut t is ui.Stack {
s.move(
from: 0
target: t
to: -1
)
}
}
}
fn (mut app App) btn_add_click(btn &ui.Button) {
window := btn.ui.window
mut s := window.get_or_panic[ui.Stack]('col2')
app.cpt++
s.add(
child: ui.button(text: 'Button ${app.cpt}')
widths: ui.stretch
heights: ui.compact
spacing: 10
)
}
fn (mut app App) btn_add_two_click(btn &ui.Button) {
window := btn.ui.window
mut s := window.get_or_panic[ui.Stack]('col2')
app.cpt++
s.add(
children: [ui.button(text: 'Button ${app.cpt++}'), ui.button(text: 'Button ${app.cpt}')]
widths: ui.stretch
heights: ui.compact
spacing: 10
)
}
fn btn_remove_click(btn &ui.Button) {
window := btn.ui.window
mut s := window.get_or_panic[ui.Stack]('col2')
s.remove(at: -1)
}
fn btn_show_hide_click(btn &ui.Button) {
window := btn.ui.window
mut s := window.get_or_panic[ui.Stack]('col2')
state := btn.text == 'show'
s.set_children_visible(state, 0)
mut b := unsafe { btn }
b.text = if state { 'hide' } else { 'show' }
}
fn btn_show_activate_click(btn &ui.Button) {
window := btn.ui.window
mut s := window.get_or_panic[ui.Stack]('col2')
state := btn.text == 'deactivate'
if state {
s.set_children_depth(ui.z_index_hidden, 0)
} else {
s.set_children_depth(0, 0)
}
mut b := unsafe { btn }
b.text = if state { 'activate' } else { 'deactivate' }
window.update_layout()
}
fn btn_remove_second_click(btn &ui.Button) {
window := btn.ui.window
mut s := window.get_or_panic[ui.Stack]('col2')
if s.get_children().len > 1 {
s.remove(at: 1)
} else {
ui.message_box('Second button not found')
}
}
fn btn_move_click(btn &ui.Button) {
window := btn.ui.window
mut s := window.get_or_panic[ui.Stack]('col2')
s.move(
from: 0
to: -1
)
}
fn btn_last_text_click(btn &ui.Button) {
window := btn.ui.window
mut w := window.child(1, -1)
if mut w is ui.Button {
ui.message_box('Last text button: ${w.text}')
} else {
ui.message_box('Third text button not found')
}
}
fn btn_third_text_click(btn &ui.Button) {
window := btn.ui.window
mut w := window.child(1, 2)
if mut w is ui.Button {
ui.message_box('Third text button: ${w.text}')
} else {
ui.message_box('Third text button not found')
}
}
fn btn_text_below_click(btn &ui.Button) {
mut s := btn.parent
if mut s is ui.Stack {
// An example of extracting child from stack
mut w := s.child(s.get_children().len - 2)
if mut w is ui.Button {
ui.message_box('Text below button: ${w.text}')
}
}
}