vendetta / src / theme.v
578 lines · 548 sloc · 19.44 KB · 77754ad9e6dc749afefba326ff89abc5ddd8c9c9
Raw
1import os
2import toml
3import strconv
4import ui2
5import libvendetta
6import libvendetta.mpv
7
8// default_theme_name is the built-in theme. It is always offered, even with no
9// theme files, and a theme file of the same name only overlays its colours.
10const default_theme_name = 'vinix-dark'
11
12// The built-in colour palette, the values the vinix-dark theme writes out.
13const slider_track_dark = 0x334155
14const slider_value_blue = 0x3b82f6
15
16const btn_blue = 0x3b82f6 // ui2 primary (buttons, value track)
17const btn_secondary = 0x64748b // ui2 muted slate (seek buttons)
18
19// The control strip's buttons: the play/pause toggle, the seek steps and the
20// mute toggle. They default to the strip's own backdrop (bg_dark), so the
21// glyphs sit on the bar without boxes of their own; each keeps a key so a
22// theme can split them apart again.
23const btn_play = btn_blue
24const btn_seek = bg_dark
25const btn_mute = bg_dark
26const text_main = u32(0xffffff) // pure white (labels, button glyphs)
27const text_muted = 0xe2e8f0 // very light slate (stats body, time labels)
28const bg_dark = 0x020617 // near-black media control bg
29const bg_surface = 0x1e293b // ui2 surface (dialogs, overlay, preview popup)
30
31// The menu bar's colours: the bar strip, its underline and the open panels'
32// outline, the titles and row labels, the disabled rows and the shortcuts
33// beside them, the hover and open highlight, and the open panel's background,
34// shadow and separators. ui2 ships a light bar for hosts that want one;
35// vinix-dark overrides all nine so the bar sits in the dark chrome with it.
36const menu_bg = u32(0x1e293b)
37const menu_border = u32(0x334155)
38const menu_text = u32(0xe2e8f0)
39const menu_disabled = u32(0x64748b)
40const menu_shortcut = u32(0x94a3b8)
41const menu_highlight = u32(0x334155)
42const menu_panel = u32(0x1e293b)
43const menu_shadow = u32(0x020617)
44const menu_separator = u32(0x334155)
45
46// The text fields' backdrop and the dialog scrim, plus the OSD and subtitle
47// colours mpv draws over the video. The first two keep the values the views
48// hardcoded before they were themed; the OSD and subtitle pairs are mpv's own
49// defaults, so theming them changes nothing until a theme says so.
50const field_bg = u32(0x000000)
51const overlay_bg = u32(0x000000)
52const osd_text = u32(0xffffff)
53const osd_border = u32(0x000000)
54const sub_text = u32(0xffffff)
55const sub_border = u32(0x000000)
56
57// Theme is every colour the window paints. A theme file can replace any of
58// them; whatever it leaves out keeps the built-in vinix-dark values.
59struct Theme {
60mut:
61 slider_track u32
62 slider_value u32
63 btn_primary u32
64 btn_secondary u32
65 // The control strip's buttons: the play/pause toggle, the seek steps and
66 // the mute toggle, each with its own key.
67 btn_play u32
68 btn_seek u32
69 btn_mute u32
70 text_main u32
71 text_muted u32
72 bg_dark u32
73 bg_surface u32
74 // The control strip's backdrop in docked mode, where it sits statically;
75 // the floating strip keeps bg_dark.
76 bg_docked u32
77 // The overlay message's toast background.
78 notice_bg u32
79 // The menu bar: the strip and its underline, the titles and row labels,
80 // the disabled rows and shortcuts, the hover highlight, and the open
81 // panel's background, shadow, outline and separators.
82 menu_bg u32
83 menu_border u32
84 menu_text u32
85 menu_disabled u32
86 menu_shortcut u32
87 menu_highlight u32
88 menu_panel u32
89 menu_shadow u32
90 menu_separator u32
91 // The text-input fields' backdrop, the scrim behind modals, and the OSD
92 // and subtitle colours mpv draws over the video.
93 field_bg u32
94 overlay_bg u32
95 osd_text u32
96 osd_border u32
97 sub_text u32
98 sub_border u32
99}
100
101// default_theme is the theme that is compiled into vendetta.
102fn default_theme() Theme {
103 return Theme{
104 slider_track: slider_track_dark
105 slider_value: slider_value_blue
106 btn_primary: btn_blue
107 btn_secondary: btn_secondary
108 btn_play: btn_play
109 btn_seek: btn_seek
110 btn_mute: btn_mute
111 text_main: text_main
112 text_muted: text_muted
113 bg_dark: bg_dark
114 bg_surface: bg_surface
115 bg_docked: bg_dark
116 notice_bg: bg_surface
117 menu_bg: menu_bg
118 menu_border: menu_border
119 menu_text: menu_text
120 menu_disabled: menu_disabled
121 menu_shortcut: menu_shortcut
122 menu_highlight: menu_highlight
123 menu_panel: menu_panel
124 menu_shadow: menu_shadow
125 menu_separator: menu_separator
126 field_bg: field_bg
127 overlay_bg: overlay_bg
128 osd_text: osd_text
129 osd_border: osd_border
130 sub_text: sub_text
131 sub_border: sub_border
132 }
133}
134
135// theme_dir is where vendetta keeps its themes: <config base>/vendetta/themes.
136fn theme_dir() string {
137 dir := config_base()
138 if dir.len == 0 {
139 return ''
140 }
141 return os.join_path(dir, 'vendetta', 'themes')
142}
143
144// theme_color reads one colour out of a theme table. It accepts a TOML
145// integer (0x334155) or a string ('334155', '#334155', '0x334155'); anything
146// else, or a missing key, keeps the fallback value.
147fn theme_color(table toml.Any, key string, fallback u32) u32 {
148 val := table.value_opt(key) or { return fallback }
149 raw := match val {
150 u64, i64, int, f32, f64 { val.u64() }
151 string {
152 mut text := (val as string).trim_space().trim_string_left('#')
153 text = text.trim_string_left('0x')
154 strconv.parse_uint(text, 16, 32) or { return fallback }
155 }
156 else {
157 return fallback
158 }
159 }
160 return u32(raw & 0xffffffff)
161}
162
163// load_theme overlays the colours declared in ~/.config/vendetta/themes/<name>.toml
164// on the built-in defaults. A missing or malformed file, a missing [colors]
165// table, or an unreadable value leaves the built-in value in place.
166fn load_theme(name string) Theme {
167 mut theme := default_theme()
168 text := os.read_file(os.join_path(theme_dir(), '${name}.toml')) or {
169 // The built-in theme has no file unless the user made one; only a
170 // named theme that cannot be found is worth a message.
171 if name != default_theme_name {
172 eprintln('vendetta: no theme ${name}, using the built-in colours')
173 }
174 return theme
175 }
176 doc := toml.parse_text(text) or {
177 eprintln('vendetta: theme ${name}: ${err}')
178 return theme
179 }
180 colors := doc.value_opt('colors') or {
181 eprintln('vendetta: theme ${name} has no [colors] table')
182 return theme
183 }
184 theme.slider_track = theme_color(colors, 'slider_track', theme.slider_track)
185 theme.slider_value = theme_color(colors, 'slider_value', theme.slider_value)
186 theme.btn_primary = theme_color(colors, 'btn_primary', theme.btn_primary)
187 theme.btn_secondary = theme_color(colors, 'btn_secondary', theme.btn_secondary)
188 theme.btn_play = theme_color(colors, 'btn_play', theme.btn_play)
189 theme.btn_seek = theme_color(colors, 'btn_seek', theme.btn_seek)
190 theme.btn_mute = theme_color(colors, 'btn_mute', theme.btn_mute)
191 theme.text_main = theme_color(colors, 'text_main', theme.text_main)
192 theme.text_muted = theme_color(colors, 'text_muted', theme.text_muted)
193 theme.bg_dark = theme_color(colors, 'bg_dark', theme.bg_dark)
194 theme.bg_surface = theme_color(colors, 'bg_surface', theme.bg_surface)
195 theme.bg_docked = theme_color(colors, 'bg_docked', theme.bg_docked)
196 theme.notice_bg = theme_color(colors, 'notice_bg', theme.notice_bg)
197 theme.menu_text = theme_color(colors, 'menu_text', theme.menu_text)
198 theme.menu_bg = theme_color(colors, 'menu_bg', theme.menu_bg)
199 theme.menu_border = theme_color(colors, 'menu_border', theme.menu_border)
200 theme.menu_disabled = theme_color(colors, 'menu_disabled', theme.menu_disabled)
201 theme.menu_shortcut = theme_color(colors, 'menu_shortcut', theme.menu_shortcut)
202 theme.menu_highlight = theme_color(colors, 'menu_highlight', theme.menu_highlight)
203 theme.menu_panel = theme_color(colors, 'menu_panel', theme.menu_panel)
204 theme.menu_shadow = theme_color(colors, 'menu_shadow', theme.menu_shadow)
205 theme.menu_separator = theme_color(colors, 'menu_separator', theme.menu_separator)
206 theme.field_bg = theme_color(colors, 'field_bg', theme.field_bg)
207 theme.overlay_bg = theme_color(colors, 'overlay_bg', theme.overlay_bg)
208 theme.osd_text = theme_color(colors, 'osd_text', theme.osd_text)
209 theme.osd_border = theme_color(colors, 'osd_border', theme.osd_border)
210 theme.sub_text = theme_color(colors, 'sub_text', theme.sub_text)
211 theme.sub_border = theme_color(colors, 'sub_border', theme.sub_border)
212 return theme
213}
214
215// available_themes lists the built-in theme first, then every theme file in
216// theme_dir by name.
217fn available_themes() []string {
218 mut custom := []string{}
219 files := os.ls(theme_dir()) or { []string{} }
220 for file in files {
221 if file.ends_with('.toml') {
222 name := file.all_before_last('.toml')
223 if name != default_theme_name {
224 custom << name
225 }
226 }
227 }
228 custom.sort()
229 return [default_theme_name, ...custom]
230}
231
232// palette_of is the widget palette for a theme.
233fn palette_of(theme Theme) libvendetta.Palette {
234 return libvendetta.Palette{
235 slider_track: theme.slider_track
236 slider_value: theme.slider_value
237 btn_primary: theme.btn_primary
238 btn_secondary: theme.btn_secondary
239 btn_play: theme.btn_play
240 btn_seek: theme.btn_seek
241 btn_mute: theme.btn_mute
242 text_main: theme.text_main
243 text_muted: theme.text_muted
244 bg_dark: theme.bg_dark
245 bg_surface: theme.bg_surface
246 bg_docked: theme.bg_docked
247 }
248}
249
250// apply_menu_colors hands a theme's menu bar palette to ui2. The bar draws
251// from ui2's globals every frame, so this is the whole hand-off; set_theme
252// and the editor's live preview both go through it.
253fn apply_menu_colors(theme Theme) {
254 ui2.set_menu_bar_background_color(theme.menu_bg)
255 ui2.set_menu_bar_border_color(theme.menu_border)
256 ui2.set_menu_bar_title_color(theme.menu_text)
257 ui2.set_menu_bar_disabled_text_color(theme.menu_disabled)
258 ui2.set_menu_bar_shortcut_text_color(theme.menu_shortcut)
259 ui2.set_menu_bar_highlight_color(theme.menu_highlight)
260 ui2.set_menu_bar_panel_background_color(theme.menu_panel)
261 ui2.set_menu_bar_panel_shadow_color(theme.menu_shadow)
262 ui2.set_menu_bar_separator_color(theme.menu_separator)
263}
264
265// apply_mpv_colors mirrors the theme's OSD and subtitle colours into mpv's
266// options. mpv reads colours as #RRGGBB (alpha optional); the theme keeps
267// 0xRRGGBB. Run on theme switches and live from the editor, so a drag in the
268// picker restyles the OSD on the video under it.
269fn (mut p Player) apply_mpv_colors() {
270 mpv.set_string_async(p.mpv_ctx, 100, 'osd-color', '#${p.theme.osd_text:06x}')
271 mpv.set_string_async(p.mpv_ctx, 101, 'osd-border-color', '#${p.theme.osd_border:06x}')
272 mpv.set_string_async(p.mpv_ctx, 102, 'sub-color', '#${p.theme.sub_text:06x}')
273 mpv.set_string_async(p.mpv_ctx, 103, 'sub-border-color', '#${p.theme.sub_border:06x}')
274}
275
276// set_theme makes the named theme the window's and the widget's palette. A
277// name with no theme file falls back to the built-in theme, with a message.
278fn (mut p Player) set_theme(name string) {
279 mut chosen := name
280 if chosen != default_theme_name && chosen !in available_themes() {
281 eprintln('vendetta: no theme ${name}, using ${default_theme_name}')
282 chosen = default_theme_name
283 }
284 theme := load_theme(chosen)
285 p.theme_name = chosen
286 p.theme = theme
287 p.palette = palette_of(theme)
288 // The bar and its panels follow the theme.
289 apply_menu_colors(theme)
290 p.apply_mpv_colors()
291}
292
293// color_keys lists the theme colours in the editor's row order.
294const color_keys = [
295 'slider_track',
296 'slider_value',
297 'btn_primary',
298 'btn_secondary',
299 'btn_play',
300 'btn_seek',
301 'btn_mute',
302 'text_main',
303 'text_muted',
304 'bg_dark',
305 'bg_docked',
306 'notice_bg',
307 'menu_bg',
308 'menu_border',
309 'menu_text',
310 'menu_disabled',
311 'menu_shortcut',
312 'menu_highlight',
313 'menu_panel',
314 'menu_shadow',
315 'menu_separator',
316 'bg_surface',
317 'field_bg',
318 'overlay_bg',
319 'osd_text',
320 'osd_border',
321 'sub_text',
322 'sub_border',
323]
324
325// color_label is a colour row's label in the editor.
326fn color_label(key string) string {
327 mut words := []string{}
328 for word in key.split('_') {
329 words << word.capitalize()
330 }
331 return words.join(' ')
332}
333
334// theme_color_of reads one colour out of a theme by key.
335fn theme_color_of(theme Theme, key string) u32 {
336 return match key {
337 'slider_track' { theme.slider_track }
338 'slider_value' { theme.slider_value }
339 'btn_primary' { theme.btn_primary }
340 'btn_secondary' { theme.btn_secondary }
341 'btn_play' { theme.btn_play }
342 'btn_seek' { theme.btn_seek }
343 'btn_mute' { theme.btn_mute }
344 'text_main' { theme.text_main }
345 'text_muted' { theme.text_muted }
346 'bg_dark' { theme.bg_dark }
347 'bg_docked' { theme.bg_docked }
348 'notice_bg' { theme.notice_bg }
349 'menu_bg' { theme.menu_bg }
350 'menu_border' { theme.menu_border }
351 'menu_text' { theme.menu_text }
352 'menu_disabled' { theme.menu_disabled }
353 'menu_shortcut' { theme.menu_shortcut }
354 'menu_highlight' { theme.menu_highlight }
355 'menu_panel' { theme.menu_panel }
356 'menu_shadow' { theme.menu_shadow }
357 'menu_separator' { theme.menu_separator }
358 'bg_surface' { theme.bg_surface }
359 'field_bg' { theme.field_bg }
360 'overlay_bg' { theme.overlay_bg }
361 'osd_text' { theme.osd_text }
362 'osd_border' { theme.osd_border }
363 'sub_text' { theme.sub_text }
364 'sub_border' { theme.sub_border }
365 else { 0 }
366 }
367}
368
369// set_color writes one colour of a theme by key.
370fn (mut theme Theme) set_color(key string, c u32) {
371 match key {
372 'slider_track' { theme.slider_track = c }
373 'slider_value' { theme.slider_value = c }
374 'btn_primary' { theme.btn_primary = c }
375 'btn_secondary' { theme.btn_secondary = c }
376 'btn_play' { theme.btn_play = c }
377 'btn_seek' { theme.btn_seek = c }
378 'btn_mute' { theme.btn_mute = c }
379 'text_main' { theme.text_main = c }
380 'text_muted' { theme.text_muted = c }
381 'bg_dark' { theme.bg_dark = c }
382 'bg_docked' { theme.bg_docked = c }
383 'notice_bg' { theme.notice_bg = c }
384 'menu_bg' { theme.menu_bg = c }
385 'menu_border' { theme.menu_border = c }
386 'menu_text' { theme.menu_text = c }
387 'menu_disabled' { theme.menu_disabled = c }
388 'menu_shortcut' { theme.menu_shortcut = c }
389 'menu_highlight' { theme.menu_highlight = c }
390 'menu_panel' { theme.menu_panel = c }
391 'menu_shadow' { theme.menu_shadow = c }
392 'menu_separator' { theme.menu_separator = c }
393 'bg_surface' { theme.bg_surface = c }
394 'field_bg' { theme.field_bg = c }
395 'overlay_bg' { theme.overlay_bg = c }
396 'osd_text' { theme.osd_text = c }
397 'osd_border' { theme.osd_border = c }
398 'sub_text' { theme.sub_text = c }
399 'sub_border' { theme.sub_border = c }
400 else {}
401 }
402}
403
404// hex_text renders a colour the way the editor's fields show it.
405fn hex_text(c u32) string {
406 return '#${c:06x}'
407}
408
409// parse_hex_color reads a colour field: '#RRGGBB', '0xRRGGBB' or 'RRGGBB'.
410// Anything else, including a partially typed value, is not a colour yet.
411fn parse_hex_color(text string) ?u32 {
412 mut t := text.trim_space().trim_string_left('#').trim_string_left('0x')
413 if t.len == 0 {
414 return none
415 }
416 raw := strconv.parse_uint(t, 16, 32) or { return none }
417 return u32(raw & 0xffffff)
418}
419
420// ThemePicker is one colour row's chooser state. ui2's picker is controlled,
421// so the app stores every change and rebuilds.
422struct ThemePicker {
423mut:
424 value ui2.ColorHsva
425 open bool
426 hex_text ?string
427 preview ?ui2.ColorHsva
428 panel ui2.ColorPickerPanel = ui2.ColorPickerPanel.spectrum
429}
430
431// open_theme_editor loads a theme into the editor panel. An empty name starts
432// a new theme from the built-in colours. It replaces the queue panel, the
433// other right-side panel.
434fn (mut p Player) open_theme_editor(name string) {
435 base := if name.len > 0 { load_theme(name) } else { default_theme() }
436 p.theme_edit_base = base
437 p.theme_edit_name = name
438 p.theme_edit_texts = map[string]string{}
439 for key in color_keys {
440 p.theme_edit_texts[key] = hex_text(theme_color_of(base, key))
441 }
442 p.theme_editor_open = true
443 for key in color_keys {
444 p.theme_pickers[key] = ThemePicker{
445 value: ui2.color_from_rgba(theme_color_of(base, key), 1)
446 }
447 }
448}
449
450// theme_picker_trigger_config is the row's chooser as it is built inside its
451// row view: the swatch at the row's right, parent-local.
452fn (p &Player) theme_picker_trigger_config(key string) ui2.ColorPickerConfig {
453 st := p.theme_pickers[key] or { ThemePicker{} }
454 return ui2.ColorPickerConfig{
455 id: 'theme_picker_${key}'
456 frame: ui2.rect(258, 3, 24, 24)
457 value: st.value
458 hex_text: st.hex_text
459 preview: st.preview
460 panel: st.panel
461 open: st.open
462 variant: .swatch
463 alpha: false
464 size: .small
465 theme: ui2.component_theme(false)
466 }
467}
468
469// theme_picker_config is the chooser as the popover and the event decoder see
470// it: the swatch's frame in the editor sidebar's coordinates, shifted by the
471// list's scroll offset, with the sidebar's window position as the origin, so
472// pointer events map onto the parts however the list is scrolled.
473fn (p &Player) theme_picker_config(key string, index int, b ui2.Rect) ui2.ColorPickerConfig {
474 scroll := ui2.scroll_offset(ui2.scrollable_scroll_id('theme_rows'))
475 return ui2.ColorPickerConfig{
476 ...p.theme_picker_trigger_config(key)
477 frame: ui2.rect(258, 95 + f64(index) * 30 - scroll, 24, 24)
478 origin_x: b.width - 348
479 origin_y: content_top()
480 window: b
481 }
482}
483
484// handle_theme_picker_event runs one of the editor's choosers against an
485// event and reports whether the event belonged to one. A drag on the area or
486// the hue slider, a swatch, or a typed hex lands in the row's field, so the
487// window previews it live.
488fn (mut p Player) handle_theme_picker_event(event string) bool {
489 b := ui2.bounds()
490 for i, key in color_keys {
491 cfg := p.theme_picker_config(key, i, b)
492 ev := ui2.color_picker_event(cfg, event) or { continue }
493 mut st := p.theme_pickers[key] or { ThemePicker{} }
494 match ev.kind {
495 .change, .select {
496 st.value = ev.value
497 st.hex_text = none
498 p.set_theme_color_row(key, ev.value.hex())
499 }
500 .hex_change {
501 typed := ui2.text(ui2.color_picker_hex_field_id(cfg.id))
502 st.hex_text = typed
503 if next := ui2.color_picker_hex_value(cfg, typed) {
504 st.value = next
505 p.set_theme_color_row(key, next.hex())
506 }
507 }
508 .panel {
509 st.panel = ev.panel
510 }
511 .preview {
512 st.preview = ev.value
513 }
514 .preview_end {
515 st.preview = none
516 }
517 .toggle, .dismiss {
518 st.open = ev.open
519 if !st.open {
520 st.preview = none
521 st.hex_text = none
522 }
523 }
524 }
525 p.theme_pickers[key] = st
526 return true
527 }
528 return false
529}
530
531// set_theme_color_row puts a colour into its row's field and the live theme.
532fn (mut p Player) set_theme_color_row(key string, hex string) {
533 p.theme_edit_texts[key] = hex
534 ui2.set_text('theme_color_input_${key}', hex)
535 p.theme_editor_preview()
536}
537
538// theme_editor_preview folds the editor's fields into the live theme, so the
539// window under the panel previews every accepted edit. Text that does not
540// parse leaves its colour alone.
541fn (mut p Player) theme_editor_preview() {
542 mut theme := p.theme_edit_base
543 for key in color_keys {
544 if c := parse_hex_color(p.theme_edit_texts[key]) {
545 theme.set_color(key, c)
546 }
547 }
548 p.theme = theme
549 p.palette = palette_of(theme)
550 // The bar and its panels draw from ui2's globals every frame.
551 apply_menu_colors(theme)
552 p.apply_mpv_colors()
553}
554
555// save_theme_from_editor writes the edited colours to <name>.toml, makes it
556// the current theme, and refreshes the theme menus. The name follows
557// save_queue's rules.
558fn (mut p Player) save_theme_from_editor() {
559 mut clean := p.theme_edit_name.trim_space()
560 if clean.len == 0 || clean.contains('/') || clean.contains('\\') || clean.contains('..') {
561 p.notify('Theme name may not be empty or contain path separators.')
562 return
563 }
564 p.theme_editor_preview()
565 mut text := '[colors]\n'
566 for key in color_keys {
567 text += '${key} = 0x${theme_color_of(p.theme, key):06x}\n'
568 }
569 os.mkdir_all(theme_dir(), os.MkdirParams{}) or {}
570 os.write_file(os.join_path(theme_dir(), '${clean}.toml'), text) or {
571 p.notify('Could not save the theme: ${err}')
572 return
573 }
574 p.theme_edit_name = clean
575 p.set_theme(clean)
576 p.notify('Theme saved as ${clean}.')
577 ui2.set_menu_bar(p.menus())
578}
579