-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_checkbox.v
238 lines (216 loc) · 5.68 KB
/
widget_checkbox.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
// Copyright (c) 2020-2022 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 gx
const (
check_mark_size = 14
cb_border_color = gx.rgb(50, 50, 50) // gx.rgb(76, 145, 244)
)
type CheckChangedFn = fn (voidptr, bool)
type CheckBowClickFn = fn (&CheckBox, voidptr)
[heap]
pub struct CheckBox {
pub mut:
id string
height int
width int
x int
y int
offset_x int
offset_y int
z_index int
parent Layout = empty_stack
is_focused bool
checked bool
ui &UI
on_click CheckBowClickFn
on_check_changed CheckChangedFn
text string
disabled bool
// text styles
text_styles TextStyles
text_size f64
text_cfg gx.TextCfg
hidden bool
// component state for composable widget
component voidptr
}
[params]
pub struct CheckBoxParams {
id string
x int
y int
z_index int
text string
on_click CheckBowClickFn
on_check_changed CheckChangedFn
checked bool
disabled bool
text_cfg gx.TextCfg
text_size f64
}
pub fn checkbox(c CheckBoxParams) &CheckBox {
mut cb := &CheckBox{
id: c.id
height: ui.check_mark_size + 5 // TODO
z_index: c.z_index
ui: 0
text: c.text
on_click: c.on_click
on_check_changed: c.on_check_changed
checked: c.checked
disabled: c.disabled
text_cfg: c.text_cfg
text_size: c.text_size
}
return cb
}
fn (mut cb CheckBox) init(parent Layout) {
cb.parent = parent
cb.ui = parent.get_ui()
cb.width = text_width(cb, cb.text) + 5 + ui.check_mark_size
cb.init_style()
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_key_down, cb_key_down, cb)
subscriber.subscribe_method(events.on_click, cb_click, cb)
}
[manualfree]
pub fn (mut cb CheckBox) cleanup() {
mut subscriber := cb.parent.get_subscriber()
subscriber.unsubscribe_method(events.on_key_down, cb)
subscriber.unsubscribe_method(events.on_click, cb)
unsafe { cb.free() }
}
[unsafe]
pub fn (cb &CheckBox) free() {
$if free ? {
print('checkbox $cb.id')
}
unsafe { free(cb) }
$if free ? {
println(' -> freed')
}
}
fn (mut cb CheckBox) init_style() {
$if nodtw ? {
if is_empty_text_cfg(cb.text_cfg) {
cb.text_cfg = cb.ui.window.text_cfg
}
if cb.text_size > 0 {
_, win_height := cb.ui.window.size()
cb.text_cfg = gx.TextCfg{
...cb.text_cfg
size: text_size_as_int(cb.text_size, win_height)
}
}
} $else {
mut dtw := DrawTextWidget(cb)
dtw.init_style()
dtw.update_text_size(cb.text_size)
}
}
fn cb_key_down(mut cb CheckBox, e &KeyEvent, window &Window) {
// println('key down $e <$e.key> <$e.codepoint> <$e.mods>')
// println('key down key=<$e.key> code=<$e.codepoint> mods=<$e.mods>')
$if cb_keydown ? {
println('cb_keydown: $cb.id -> $cb.hidden $cb.is_focused')
}
if cb.hidden {
return
}
if !cb.is_focused {
return
}
// default behavior like click for space and enter
if e.key in [.enter, .space] {
cb.checked = !cb.checked
// println("checked: $cb.checked")
if cb.on_check_changed != CheckChangedFn(0) {
cb.on_check_changed(window.state, cb.checked)
}
if cb.on_click != CheckBowClickFn(0) {
cb.on_click(cb, window.state)
}
}
}
fn cb_click(mut cb CheckBox, e &MouseEvent, window &Window) {
if cb.hidden {
return
}
if cb.point_inside(e.x, e.y) { // && e.action == 0 {
cb.checked = !cb.checked
// println("checked: $cb.checked")
if cb.on_check_changed != CheckChangedFn(0) {
cb.on_check_changed(window.state, cb.checked)
}
if cb.on_click != CheckBowClickFn(0) {
cb.on_click(cb, window.state)
}
}
}
pub fn (mut cb CheckBox) set_pos(x int, y int) {
cb.x = x
cb.y = y
}
pub fn (mut cb CheckBox) size() (int, int) {
return cb.width, cb.height
}
pub fn (mut cb CheckBox) propose_size(w int, h int) (int, int) {
cb.width = w
// TODO: fix height
// cb.height = h
// width := check_mark_size + 5 + cb.ui.ft.text_width(cb.text)
return cb.width, cb.height
}
fn (mut cb CheckBox) draw() {
offset_start(mut cb)
cb.ui.gg.draw_rect_filled(cb.x, cb.y, ui.check_mark_size, ui.check_mark_size, gx.white) // progress_bar_color)
draw_inner_border(false, cb.ui.gg, cb.x, cb.y, ui.check_mark_size, ui.check_mark_size,
false)
if cb.is_focused {
cb.ui.gg.draw_rect_empty(cb.x, cb.y, ui.check_mark_size, ui.check_mark_size, ui.cb_border_color)
}
// Draw X (TODO draw a check mark instead)
if cb.checked {
// cb.ui.gg.draw_rect_filled(cb.x + 3, cb.y + 3, 2, 2, gx.black)
/*
x0 := cb.x +2
y0 := cb.y +2
cb.ui.gg.draw_line_c(x0, y0, x0+check_mark_size -4, y0 + check_mark_size-4, gx.black)
cb.ui.gg.draw_line_c(0.5+x0, y0, -3.5 +x0+check_mark_size , y0 + check_mark_size-4, gx.black)
//
y1 := cb.y + check_mark_size - 2
cb.ui.gg.draw_line_c(x0, y1, x0+check_mark_size -4, y0, gx.black)
cb.ui.gg.draw_line_c(0.5+x0, y1, -3.5+x0+check_mark_size, y0, gx.black)
*/
cb.ui.gg.draw_image(cb.x + 3, cb.y + 3, 8, 8, cb.ui.cb_image)
}
// Text
$if nodtw ? {
cb.ui.gg.draw_text(cb.x + ui.check_mark_size + 5, cb.y, cb.text, cb.text_cfg)
} $else {
dtw := DrawTextWidget(cb)
dtw.load_style()
dtw.draw_text(cb.x + ui.check_mark_size + 5, cb.y, cb.text)
}
$if bb ? {
debug_draw_bb_widget(mut cb, cb.ui)
}
offset_end(mut cb)
}
fn (cb &CheckBox) point_inside(x f64, y f64) bool {
return point_inside(cb, x, y)
}
fn (mut cb CheckBox) mouse_move(e MouseEvent) {
}
fn (mut cb CheckBox) set_visible(state bool) {
cb.hidden = !state
}
fn (mut cb CheckBox) focus() {
mut f := Focusable(cb)
f.set_focus()
}
fn (mut cb CheckBox) unfocus() {
cb.is_focused = false
}