-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathprogressbar.v
More file actions
150 lines (137 loc) · 3.34 KB
/
progressbar.v
File metadata and controls
150 lines (137 loc) · 3.34 KB
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
// Copyright (c) 2020-2022 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
module ui
@[heap]
pub struct ProgressBar {
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
ui &UI = unsafe { nil }
val int
min int
max int
hidden bool
// Style
theme_style string
style ProgressBarStyle
style_params ProgressBarStyleParams
// component state for composable widget
component voidptr
// native widget handle (when native_widgets is enabled)
native_w NativeWidget
}
@[params]
pub struct ProgressBarParams {
ProgressBarStyleParams
pub:
id string
width int
height int = 16
z_index int
min int
max int
val int
theme string = no_style
}
pub fn progressbar(c ProgressBarParams) &ProgressBar {
mut pb := &ProgressBar{
id: c.id
height: c.height
width: c.width
z_index: c.z_index
min: c.min
max: c.max
val: c.val
style_params: c.ProgressBarStyleParams
ui: unsafe { nil }
}
pb.style_params.style = c.theme
return pb
}
fn (mut pb ProgressBar) init(parent Layout) {
pb.parent = parent
u := parent.get_ui()
pb.ui = u
pb.load_style()
// Create native widget if native_widgets is enabled
if pb.ui.window.native_widgets.is_enabled() {
pb.native_w = pb.ui.window.native_widgets.create_progressbar(pb.x, pb.y, pb.width,
pb.height, f64(pb.min), f64(pb.max), f64(pb.val))
}
}
@[manualfree]
pub fn (mut pb ProgressBar) cleanup() {
unsafe { pb.free() }
}
@[unsafe]
pub fn (pb &ProgressBar) free() {
$if free ? {
print('progress_bar ${pb.id}')
}
unsafe {
pb.id.free()
free(pb)
}
$if free ? {
println(' -> freed')
}
}
pub fn (mut pb ProgressBar) set_pos(x int, y int) {
pb.x = x
pb.y = y
}
pub fn (mut pb ProgressBar) size() (int, int) {
return pb.width, pb.height
}
pub fn (mut pb ProgressBar) propose_size(w int, h int) (int, int) {
/*
pb.width = w
pb.height = h
return w, h
*/
pb.width = w
pb.height = h
return pb.width, pb.height
}
fn (mut pb ProgressBar) draw() {
pb.draw_device(mut pb.ui.dd)
}
fn (mut pb ProgressBar) draw_device(mut d DrawDevice) {
// Native widget: update position/value and skip custom drawing
if pb.ui.window.native_widgets.is_enabled() && pb.native_w.handle != unsafe { nil } {
pb.ui.window.native_widgets.update_progressbar(&pb.native_w, pb.x, pb.y, pb.width,
pb.height, f64(pb.val))
return
}
offset_start(mut pb)
$if layout ? {
if pb.ui.layout_print {
println('ProgressBar(${pb.id}): (${pb.x}, ${pb.y}, ${pb.width}, ${pb.height})')
}
}
// Draw the gray background
d.draw_rect_filled(pb.x, pb.y, pb.width, pb.height, pb.style.bg_color)
d.draw_rect_empty(pb.x, pb.y, pb.width, pb.height, pb.style.bg_border_color)
// Draw the value
width := int(f64(pb.width) * (f64(pb.val) / f64(pb.max)))
d.draw_rect_empty(pb.x, pb.y, width, pb.height, pb.style.border_color) // gg.Black)
d.draw_rect_filled(pb.x, pb.y, width, pb.height, pb.style.color) // gg.Black)
$if bb ? {
debug_draw_bb_widget(mut pb, pb.ui)
}
offset_end(mut pb)
}
fn (pb &ProgressBar) point_inside(x f64, y f64) bool {
return point_inside(pb, x, y)
}
fn (mut pb ProgressBar) set_visible(state bool) {
pb.hidden = !state
}