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