import os import math import toml import strconv import ui2 import libvendetta import libvendetta.mpv // default_theme_name is the built-in theme. It is always offered, even with no // theme files, and a theme file of the same name only overlays its colours. const default_theme_name = 'vendetta' // The built-in colour palette, the values the vendetta theme writes out. const slider_track_dark = 0x091029 const slider_value_blue = 0x2775f1 const btn_blue = 0x2775f1 // ui2 primary (buttons, value track) const btn_secondary = 0x06061d // near-black secondary (seek buttons) // The control strip's buttons: the play/pause toggle, the seek steps and the // mute toggle. Each keeps a key so a theme can give it its own box; the // defaults are the vendetta theme's strip. const btn_play = btn_blue const btn_seek = 0x06061d const btn_mute = 0x000000 const text_main = u32(0xffffff) // pure white (labels, button glyphs) const text_muted = 0xe2e8f0 // very light slate (stats body, time labels) const bg_dark = 0x000000 // near-black media control bg const bg_surface = 0x000000 // ui2 surface (dialogs, overlay, preview popup) // The menu bar's colours: the bar strip, its underline and the open panels' // outline, the titles and row labels, the disabled rows and the shortcuts // beside them, the hover and open highlight, and the open panel's background, // shadow and separators. ui2 ships a light bar for hosts that want one; // vendetta overrides all nine so the bar sits in the black chrome with it. const menu_bg = u32(0x000000) const menu_border = u32(0x000000) const menu_text = u32(0xbbbbbb) const menu_disabled = u32(0x3e3e3e) const menu_shortcut = u32(0x958c83) const menu_highlight = u32(0x2775f1) const menu_panel = u32(0x000000) const menu_shadow = u32(0x000000) const menu_separator = u32(0x000000) // How much of the video the floating panels hide, the vendetta theme's value; // a theme file without an [opacity] table keeps it. const default_overlay_opacity = 0.85 // The text fields' backdrop and the dialog scrim, plus the OSD and subtitle // colours mpv draws over the video. The first two keep the values the views // hardcoded before they were themed; the OSD and subtitle pairs are mpv's own // defaults, so theming them changes nothing until a theme says so. const field_bg = u32(0x000000) const overlay_bg = u32(0x000000) const osd_text = u32(0xffffff) const osd_border = u32(0x000000) const sub_text = u32(0xffffff) const sub_border = u32(0x000000) // Theme is every colour the window paints. A theme file can replace any of // them; whatever it leaves out keeps the built-in vendetta values. struct Theme { mut: slider_track u32 slider_value u32 btn_primary u32 btn_secondary u32 // The control strip's buttons: the play/pause toggle, the seek steps and // the mute toggle, each with its own key. btn_play u32 btn_seek u32 btn_mute u32 text_main u32 text_muted u32 bg_dark u32 bg_surface u32 // The control strip's backdrop in docked mode, where it sits statically; // the floating strip keeps bg_dark. bg_docked u32 // The overlay message's toast background. notice_bg u32 // The menu bar: the strip and its underline, the titles and row labels, // the disabled rows and shortcuts, the hover highlight, and the open // panel's background, shadow, outline and separators. menu_bg u32 menu_border u32 menu_text u32 menu_disabled u32 menu_shortcut u32 menu_highlight u32 menu_panel u32 menu_shadow u32 menu_separator u32 // The text-input fields' backdrop, the scrim behind modals, and the OSD // and subtitle colours mpv draws over the video. field_bg u32 overlay_bg u32 osd_text u32 osd_border u32 sub_text u32 sub_border u32 // How much of the video the panels floating over it hide, 0 to 1: dialogs, // settings, the statistics popup and the seek preview. The default is the // vendetta theme's value. overlay_opacity f64 = default_overlay_opacity } // default_theme is the theme that is compiled into vendetta. fn default_theme() Theme { return Theme{ slider_track: slider_track_dark slider_value: slider_value_blue btn_primary: btn_blue btn_secondary: btn_secondary btn_play: btn_play btn_seek: btn_seek btn_mute: btn_mute text_main: text_main text_muted: text_muted bg_dark: bg_dark bg_surface: bg_surface bg_docked: bg_dark notice_bg: bg_surface menu_bg: menu_bg menu_border: menu_border menu_text: menu_text menu_disabled: menu_disabled menu_shortcut: menu_shortcut menu_highlight: menu_highlight menu_panel: menu_panel menu_shadow: menu_shadow menu_separator: menu_separator field_bg: field_bg overlay_bg: overlay_bg osd_text: osd_text osd_border: osd_border sub_text: sub_text sub_border: sub_border } } // theme_dir is where vendetta keeps its themes: /vendetta/themes. fn theme_dir() string { dir := config_base() if dir.len == 0 { return '' } return os.join_path(dir, 'vendetta', 'themes') } // theme_color reads one colour out of a theme table. It accepts a TOML // integer (0x091029) or a string ('091029', '#091029', '0x091029'); anything // else, or a missing key, keeps the fallback value. fn theme_color(table toml.Any, key string, fallback u32) u32 { val := table.value_opt(key) or { return fallback } raw := match val { u64, i64, int, f32, f64 { val.u64() } string { mut text := (val as string).trim_space().trim_string_left('#') text = text.trim_string_left('0x') strconv.parse_uint(text, 16, 32) or { return fallback } } else { return fallback } } return u32(raw & 0xffffffff) } // load_theme overlays the colours declared in ~/.config/vendetta/themes/.toml // on the built-in defaults. A missing or malformed file, a missing [colors] // table, or an unreadable value leaves the built-in value in place. fn load_theme(name string) Theme { mut theme := default_theme() text := os.read_file(os.join_path(theme_dir(), '${name}.toml')) or { // The built-in theme has no file unless the user made one; only a // named theme that cannot be found is worth a message. if name != default_theme_name { eprintln('vendetta: no theme ${name}, using the built-in colours') } return theme } doc := toml.parse_text(text) or { eprintln('vendetta: theme ${name}: ${err}') return theme } if opacity := doc.value_opt('opacity.overlay') { theme.overlay_opacity = math.clamp(opacity.f64(), 0.0, 1.0) } colors := doc.value_opt('colors') or { eprintln('vendetta: theme ${name} has no [colors] table') return theme } theme.slider_track = theme_color(colors, 'slider_track', theme.slider_track) theme.slider_value = theme_color(colors, 'slider_value', theme.slider_value) theme.btn_primary = theme_color(colors, 'btn_primary', theme.btn_primary) theme.btn_secondary = theme_color(colors, 'btn_secondary', theme.btn_secondary) theme.btn_play = theme_color(colors, 'btn_play', theme.btn_play) theme.btn_seek = theme_color(colors, 'btn_seek', theme.btn_seek) theme.btn_mute = theme_color(colors, 'btn_mute', theme.btn_mute) theme.text_main = theme_color(colors, 'text_main', theme.text_main) theme.text_muted = theme_color(colors, 'text_muted', theme.text_muted) theme.bg_dark = theme_color(colors, 'bg_dark', theme.bg_dark) theme.bg_surface = theme_color(colors, 'bg_surface', theme.bg_surface) theme.bg_docked = theme_color(colors, 'bg_docked', theme.bg_docked) theme.notice_bg = theme_color(colors, 'notice_bg', theme.notice_bg) theme.menu_text = theme_color(colors, 'menu_text', theme.menu_text) theme.menu_bg = theme_color(colors, 'menu_bg', theme.menu_bg) theme.menu_border = theme_color(colors, 'menu_border', theme.menu_border) theme.menu_disabled = theme_color(colors, 'menu_disabled', theme.menu_disabled) theme.menu_shortcut = theme_color(colors, 'menu_shortcut', theme.menu_shortcut) theme.menu_highlight = theme_color(colors, 'menu_highlight', theme.menu_highlight) theme.menu_panel = theme_color(colors, 'menu_panel', theme.menu_panel) theme.menu_shadow = theme_color(colors, 'menu_shadow', theme.menu_shadow) theme.menu_separator = theme_color(colors, 'menu_separator', theme.menu_separator) theme.field_bg = theme_color(colors, 'field_bg', theme.field_bg) theme.overlay_bg = theme_color(colors, 'overlay_bg', theme.overlay_bg) theme.osd_text = theme_color(colors, 'osd_text', theme.osd_text) theme.osd_border = theme_color(colors, 'osd_border', theme.osd_border) theme.sub_text = theme_color(colors, 'sub_text', theme.sub_text) theme.sub_border = theme_color(colors, 'sub_border', theme.sub_border) return theme } // available_themes lists the built-in theme first, then every theme file in // theme_dir by name. fn available_themes() []string { mut custom := []string{} files := os.ls(theme_dir()) or { []string{} } for file in files { if file.ends_with('.toml') { name := file.all_before_last('.toml') if name != default_theme_name { custom << name } } } custom.sort() return [default_theme_name, ...custom] } // palette_of is the widget palette for a theme. fn palette_of(theme Theme) libvendetta.Palette { return libvendetta.Palette{ slider_track: theme.slider_track slider_value: theme.slider_value btn_primary: theme.btn_primary btn_secondary: theme.btn_secondary btn_play: theme.btn_play btn_seek: theme.btn_seek btn_mute: theme.btn_mute text_main: theme.text_main text_muted: theme.text_muted bg_dark: theme.bg_dark bg_surface: theme.bg_surface bg_docked: theme.bg_docked } } // apply_menu_colors hands a theme's menu bar palette to ui2. The bar draws // from ui2's globals every frame, so this is the whole hand-off; set_theme // and the editor's live preview both go through it. fn apply_menu_colors(theme Theme) { ui2.set_menu_bar_background_color(theme.menu_bg) ui2.set_menu_bar_border_color(theme.menu_border) ui2.set_menu_bar_title_color(theme.menu_text) ui2.set_menu_bar_disabled_text_color(theme.menu_disabled) ui2.set_menu_bar_shortcut_text_color(theme.menu_shortcut) ui2.set_menu_bar_highlight_color(theme.menu_highlight) ui2.set_menu_bar_panel_background_color(theme.menu_panel) ui2.set_menu_bar_panel_shadow_color(theme.menu_shadow) ui2.set_menu_bar_separator_color(theme.menu_separator) } // apply_mpv_colors mirrors the theme's OSD and subtitle colours into mpv's // options. mpv reads colours as #RRGGBB (alpha optional); the theme keeps // 0xRRGGBB. Run on theme switches and live from the editor, so a drag in the // picker restyles the OSD on the video under it. fn (mut p Player) apply_mpv_colors() { mpv.set_string_async(p.mpv_ctx, 100, 'osd-color', '#${p.theme.osd_text:06x}') mpv.set_string_async(p.mpv_ctx, 101, 'osd-border-color', '#${p.theme.osd_border:06x}') mpv.set_string_async(p.mpv_ctx, 102, 'sub-color', '#${p.theme.sub_text:06x}') mpv.set_string_async(p.mpv_ctx, 103, 'sub-border-color', '#${p.theme.sub_border:06x}') } // set_theme makes the named theme the window's and the widget's palette. A // name with no theme file falls back to the built-in theme, with a message. fn (mut p Player) set_theme(name string) { mut chosen := name if chosen != default_theme_name && chosen !in available_themes() { eprintln('vendetta: no theme ${name}, using ${default_theme_name}') chosen = default_theme_name } theme := load_theme(chosen) p.theme_name = chosen p.theme = theme p.palette = palette_of(theme) // The bar and its panels follow the theme. apply_menu_colors(theme) p.apply_mpv_colors() } // color_keys lists the theme colours in the editor's row order. const color_keys = [ 'slider_track', 'slider_value', 'btn_primary', 'btn_secondary', 'btn_play', 'btn_seek', 'btn_mute', 'text_main', 'text_muted', 'bg_dark', 'bg_docked', 'notice_bg', 'menu_bg', 'menu_border', 'menu_text', 'menu_disabled', 'menu_shortcut', 'menu_highlight', 'menu_panel', 'menu_shadow', 'menu_separator', 'bg_surface', 'field_bg', 'overlay_bg', 'osd_text', 'osd_border', 'sub_text', 'sub_border', ] // color_label is a colour row's label in the editor. fn color_label(key string) string { mut words := []string{} for word in key.split('_') { words << word.capitalize() } return words.join(' ') } // theme_color_of reads one colour out of a theme by key. fn theme_color_of(theme Theme, key string) u32 { return match key { 'slider_track' { theme.slider_track } 'slider_value' { theme.slider_value } 'btn_primary' { theme.btn_primary } 'btn_secondary' { theme.btn_secondary } 'btn_play' { theme.btn_play } 'btn_seek' { theme.btn_seek } 'btn_mute' { theme.btn_mute } 'text_main' { theme.text_main } 'text_muted' { theme.text_muted } 'bg_dark' { theme.bg_dark } 'bg_docked' { theme.bg_docked } 'notice_bg' { theme.notice_bg } 'menu_bg' { theme.menu_bg } 'menu_border' { theme.menu_border } 'menu_text' { theme.menu_text } 'menu_disabled' { theme.menu_disabled } 'menu_shortcut' { theme.menu_shortcut } 'menu_highlight' { theme.menu_highlight } 'menu_panel' { theme.menu_panel } 'menu_shadow' { theme.menu_shadow } 'menu_separator' { theme.menu_separator } 'bg_surface' { theme.bg_surface } 'field_bg' { theme.field_bg } 'overlay_bg' { theme.overlay_bg } 'osd_text' { theme.osd_text } 'osd_border' { theme.osd_border } 'sub_text' { theme.sub_text } 'sub_border' { theme.sub_border } else { 0 } } } // set_color writes one colour of a theme by key. fn (mut theme Theme) set_color(key string, c u32) { match key { 'slider_track' { theme.slider_track = c } 'slider_value' { theme.slider_value = c } 'btn_primary' { theme.btn_primary = c } 'btn_secondary' { theme.btn_secondary = c } 'btn_play' { theme.btn_play = c } 'btn_seek' { theme.btn_seek = c } 'btn_mute' { theme.btn_mute = c } 'text_main' { theme.text_main = c } 'text_muted' { theme.text_muted = c } 'bg_dark' { theme.bg_dark = c } 'bg_docked' { theme.bg_docked = c } 'notice_bg' { theme.notice_bg = c } 'menu_bg' { theme.menu_bg = c } 'menu_border' { theme.menu_border = c } 'menu_text' { theme.menu_text = c } 'menu_disabled' { theme.menu_disabled = c } 'menu_shortcut' { theme.menu_shortcut = c } 'menu_highlight' { theme.menu_highlight = c } 'menu_panel' { theme.menu_panel = c } 'menu_shadow' { theme.menu_shadow = c } 'menu_separator' { theme.menu_separator = c } 'bg_surface' { theme.bg_surface = c } 'field_bg' { theme.field_bg = c } 'overlay_bg' { theme.overlay_bg = c } 'osd_text' { theme.osd_text = c } 'osd_border' { theme.osd_border = c } 'sub_text' { theme.sub_text = c } 'sub_border' { theme.sub_border = c } else {} } } // hex_text renders a colour the way the editor's fields show it. fn hex_text(c u32) string { return '#${c:06x}' } // parse_hex_color reads a colour field: '#RRGGBB', '0xRRGGBB' or 'RRGGBB'. // Anything else, including a partially typed value, is not a colour yet. fn parse_hex_color(text string) ?u32 { mut t := text.trim_space().trim_string_left('#').trim_string_left('0x') if t.len == 0 { return none } raw := strconv.parse_uint(t, 16, 32) or { return none } return u32(raw & 0xffffff) } // ThemePicker is one colour row's chooser state. ui2's picker is controlled, // so the app stores every change and rebuilds. struct ThemePicker { mut: value ui2.ColorHsva open bool hex_text ?string preview ?ui2.ColorHsva panel ui2.ColorPickerPanel = ui2.ColorPickerPanel.spectrum } // open_theme_editor loads a theme into the editor panel. An empty name starts // a new theme from the built-in colours. It replaces the queue panel, the // other right-side panel. fn (mut p Player) open_theme_editor(name string) { base := if name.len > 0 { load_theme(name) } else { default_theme() } p.theme_edit_base = base p.theme_edit_name = name p.theme_edit_opacity = base.overlay_opacity p.theme_edit_texts = map[string]string{} for key in color_keys { p.theme_edit_texts[key] = hex_text(theme_color_of(base, key)) } p.theme_editor_open = true for key in color_keys { p.theme_pickers[key] = ThemePicker{ value: ui2.color_from_rgba(theme_color_of(base, key), 1) } } } // theme_picker_trigger_config is the row's chooser as it is built inside its // row view: the swatch at the row's right, parent-local. fn (p &Player) theme_picker_trigger_config(key string) ui2.ColorPickerConfig { st := p.theme_pickers[key] or { ThemePicker{} } return ui2.ColorPickerConfig{ id: 'theme_picker_${key}' frame: ui2.rect(258, 3, 24, 24) value: st.value hex_text: st.hex_text preview: st.preview panel: st.panel open: st.open variant: .swatch alpha: false size: .small theme: ui2.component_theme(false) } } // theme_picker_config is the chooser as the popover and the event decoder see // it: the swatch's frame in the editor sidebar's coordinates, shifted by the // list's scroll offset, with the sidebar's window position as the origin, so // pointer events map onto the parts however the list is scrolled. fn (p &Player) theme_picker_config(key string, index int, b ui2.Rect) ui2.ColorPickerConfig { scroll := ui2.scroll_offset(ui2.scrollable_scroll_id('theme_rows')) return ui2.ColorPickerConfig{ ...p.theme_picker_trigger_config(key) frame: ui2.rect(258, 95 + f64(index) * 30 - scroll, 24, 24) origin_x: b.width - 348 origin_y: content_top() window: b } } // handle_theme_picker_event runs one of the editor's choosers against an // event and reports whether the event belonged to one. A drag on the area or // the hue slider, a swatch, or a typed hex lands in the row's field, so the // window previews it live. fn (mut p Player) handle_theme_picker_event(event string) bool { b := ui2.bounds() for i, key in color_keys { cfg := p.theme_picker_config(key, i, b) ev := ui2.color_picker_event(cfg, event) or { continue } mut st := p.theme_pickers[key] or { ThemePicker{} } match ev.kind { .change, .select { st.value = ev.value st.hex_text = none p.set_theme_color_row(key, ev.value.hex()) } .hex_change { typed := ui2.text(ui2.color_picker_hex_field_id(cfg.id)) st.hex_text = typed if next := ui2.color_picker_hex_value(cfg, typed) { st.value = next p.set_theme_color_row(key, next.hex()) } } .panel { st.panel = ev.panel } .preview { st.preview = ev.value } .preview_end { st.preview = none } .toggle, .dismiss { st.open = ev.open if !st.open { st.preview = none st.hex_text = none } } } p.theme_pickers[key] = st return true } return false } // set_theme_color_row puts a colour into its row's field and the live theme. fn (mut p Player) set_theme_color_row(key string, hex string) { p.theme_edit_texts[key] = hex ui2.set_text('theme_color_input_${key}', hex) p.theme_editor_preview() } // theme_editor_preview folds the editor's fields into the live theme, so the // window under the panel previews every accepted edit. Text that does not // parse leaves its colour alone. fn (mut p Player) theme_editor_preview() { mut theme := p.theme_edit_base for key in color_keys { if c := parse_hex_color(p.theme_edit_texts[key]) { theme.set_color(key, c) } } theme.overlay_opacity = p.theme_edit_opacity p.theme = theme p.palette = palette_of(theme) // The bar and its panels draw from ui2's globals every frame. apply_menu_colors(theme) p.apply_mpv_colors() } // save_theme_from_editor writes the edited colours to .toml, makes it // the current theme, and refreshes the theme menus. The name follows // save_queue's rules. fn (mut p Player) save_theme_from_editor() { mut clean := p.theme_edit_name.trim_space() if clean.len == 0 || clean.contains('/') || clean.contains('\\') || clean.contains('..') { p.notify('Theme name may not be empty or contain path separators.') return } p.theme_editor_preview() mut text := '[colors]\n' for key in color_keys { text += '${key} = 0x${theme_color_of(p.theme, key):06x}\n' } text += '\n[opacity]\noverlay = ${p.theme.overlay_opacity:.2f}\n' os.mkdir_all(theme_dir(), os.MkdirParams{}) or {} os.write_file(os.join_path(theme_dir(), '${clean}.toml'), text) or { p.notify('Could not save the theme: ${err}') return } p.theme_edit_name = clean p.set_theme(clean) p.notify('Theme saved as ${clean}.') ui2.set_menu_bar(p.menus()) }