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