-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathstyles.v
More file actions
265 lines (243 loc) · 6.86 KB
/
styles.v
File metadata and controls
265 lines (243 loc) · 6.86 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
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
module ui
import gg
import os
import x.json2
// define style outside Widget definition
// all styles would be collected inside one map attached to ui
pub const no_style = '_no_style_'
pub const no_color = gg.Color{0, 0, 0, 0}
pub const transparent = gg.Color{0, 0, 0, 1}
pub struct Style {
pub mut:
win WindowStyle
btn ButtonStyle
pgbar ProgressBarStyle
cl CanvasLayoutStyle
stack StackStyle
tb TextBoxStyle
lb ListBoxStyle
cb CheckBoxStyle
radio RadioStyle
rect RectangleStyle
slider SliderStyle
dd DropdownStyle
menu MenuStyle
label LabelStyle
}
pub fn (s Style) to_json_any() json2.Any {
mut obj := map[string]json2.Any{}
obj['window'] = s.win.to_json_any()
obj['button'] = s.btn.to_json_any()
obj['progressbar'] = s.pgbar.to_json_any()
obj['slider'] = s.slider.to_json_any()
obj['dropdown'] = s.dd.to_json_any()
obj['checkbox'] = s.cb.to_json_any()
obj['radio'] = s.radio.to_json_any()
obj['rectangle'] = s.rect.to_json_any()
obj['menu'] = s.menu.to_json_any()
obj['canvaslayout'] = s.cl.to_json_any()
obj['stack'] = s.stack.to_json_any()
obj['textbox'] = s.tb.to_json_any()
obj['listbox'] = s.lb.to_json_any()
obj['label'] = s.label.to_json_any()
return obj
}
pub fn parse_style_json_file(path string) Style {
text := os.read_file(path) or {
eprintln('UI: using the default UI style, since the .json file `${path}` could not be read: ${err}')
return default_style()
}
doc := json2.decode[json2.Any](text) or {
eprintln('UI: using the default UI style, since the .json file `${path}` was invalid. JSON parse error: ${err}')
return default_style()
}
m := doc.as_map()
mut s := Style{}
s.win.from_json(m['window'] or { json2.Any(map[string]json2.Any{}) })
s.btn.from_json(m['button'] or { json2.Any(map[string]json2.Any{}) })
s.pgbar.from_json(m['progressbar'] or { json2.Any(map[string]json2.Any{}) })
s.slider.from_json(m['slider'] or { json2.Any(map[string]json2.Any{}) })
s.dd.from_json(m['dropdown'] or { json2.Any(map[string]json2.Any{}) })
s.cb.from_json(m['checkbox'] or { json2.Any(map[string]json2.Any{}) })
s.radio.from_json(m['radio'] or { json2.Any(map[string]json2.Any{}) })
s.rect.from_json(m['rectangle'] or { json2.Any(map[string]json2.Any{}) })
s.menu.from_json(m['menu'] or { json2.Any(map[string]json2.Any{}) })
s.cl.from_json(m['canvaslayout'] or { json2.Any(map[string]json2.Any{}) })
s.stack.from_json(m['stack'] or { json2.Any(map[string]json2.Any{}) })
s.tb.from_json(m['textbox'] or { json2.Any(map[string]json2.Any{}) })
s.lb.from_json(m['listbox'] or { json2.Any(map[string]json2.Any{}) })
s.label.from_json(m['label'] or { json2.Any(map[string]json2.Any{}) })
return s
}
pub fn (s Style) as_json_file(path string) {
text := json2.encode(s.to_json_any(), prettify: true)
os.write_file(path, text) or { panic(err) }
}
pub fn style_json_file(style_id string) string {
return os.join_path(settings_styles_dir, 'style_${style_id}.json')
}
// load styles
pub fn (mut gui UI) load_styles() {
// ensure some theme styles are predefined
$if no_load_styles ? {
gui.styles['default'] = default_style()
return
}
create_theme_styles()
for style_id in ['default', 'red', 'blue'] {
gui.load_style_from_file(style_id)
}
}
pub fn (mut gui UI) load_style_from_file(style_id string) {
style := parse_style_json_file(style_json_file(style_id))
// println("$style_id: $style")
gui.styles[style_id] = style
}
// predefined style
fn create_theme_styles() {
if !os.exists(settings_styles_dir) {
os.mkdir_all(settings_styles_dir) or { panic(err) }
}
if !os.exists(style_json_file('default')) {
create_default_style_file()
}
if !os.exists(style_json_file('red')) {
create_red_style_file()
}
if !os.exists(style_json_file('blue')) {
create_blue_style_file()
}
}
pub fn default_style() Style {
// "" means default
return Style{
// window
win: WindowStyle{
bg_color: default_window_color
}
// button
btn: ButtonStyle{
radius: .1
border_color: button_border_color
bg_color: gg.white
bg_color_pressed: gg.rgb(119, 119, 119)
bg_color_hover: gg.rgb(219, 219, 219)
}
// progressbar
pgbar: ProgressBarStyle{
color: gg.rgb(87, 153, 245)
border_color: gg.rgb(76, 133, 213)
bg_color: gg.rgb(219, 219, 219)
bg_border_color: gg.rgb(191, 191, 191)
}
}
}
pub fn create_default_style_file() {
default_style().as_json_file(style_json_file('default'))
}
pub fn blue_style() Style {
return Style{
// win
win: WindowStyle{
bg_color: gg.blue
}
// button
btn: ButtonStyle{
radius: .3
border_color: button_border_color
bg_color: gg.light_blue
bg_color_pressed: gg.rgb(0, 0, 119)
bg_color_hover: gg.rgb(0, 0, 219)
}
// progressbar
pgbar: ProgressBarStyle{
color: gg.rgb(87, 153, 245)
border_color: gg.rgb(76, 133, 213)
bg_color: gg.rgb(219, 219, 219)
bg_border_color: gg.rgb(191, 191, 191)
}
// canvas layout
cl: CanvasLayoutStyle{
bg_color: transparent // gg.rgb(220, 220, 255)
}
// stack
stack: StackStyle{
bg_color: transparent // gg.rgb(220, 220, 255)
}
}
}
pub fn create_blue_style_file() {
blue_style().as_json_file(os.join_path(settings_styles_dir, 'style_blue.json'))
}
pub fn red_style() Style {
return Style{
// win
win: WindowStyle{
bg_color: gg.red
}
// button
btn: ButtonStyle{
radius: .3
border_color: button_border_color
bg_color: gg.light_red
bg_color_pressed: gg.rgb(119, 0, 0)
bg_color_hover: gg.rgb(219, 0, 0)
text_color: gg.red
}
// progressbar
pgbar: ProgressBarStyle{
color: gg.rgb(245, 153, 87)
border_color: gg.rgb(213, 133, 76)
bg_color: gg.rgb(219, 219, 219)
bg_border_color: gg.rgb(191, 191, 191)
}
// slider
slider: SliderStyle{
thumb_color: gg.rgb(245, 153, 87)
}
// canvas layout
cl: CanvasLayoutStyle{
bg_color: transparent // gg.rgb(255, 220, 220)
}
// stack
stack: StackStyle{
bg_color: transparent // gg.rgb(255, 220, 220)
}
}
}
pub fn create_red_style_file() {
red_style().as_json_file(os.join_path(settings_styles_dir, 'style_red.json'))
}
// parent style
pub fn (l Layout) bg_color() gg.Color {
mut col := no_color
if l is Stack {
col = l.style.bg_color
if col in [no_color, transparent] {
return l.parent.bg_color()
}
} else if l is CanvasLayout {
col = l.style.bg_color
if col in [no_color, transparent] {
return l.parent.bg_color()
}
} else if l is Window {
col = l.bg_color
}
return col
}
// add shortcut
pub fn (mut window Window) add_shortcut_theme() {
mut sc := Shortcutable(window)
sc.add_shortcut('ctrl + t', fn (mut w Window) {
themes := ['default', 'red', 'blue']
for i, theme in themes {
if w.theme_style == theme {
w.theme_style = themes[if i + 1 == themes.len { 0 } else { i + 1 }]
break
}
}
mut l := Layout(w)
l.update_theme_style(w.theme_style)
})
}