module main import math import ui2 import libvendetta // The settings dialog: ui2 owns one native window, so this is a ui2.settings // view in a modal over the player. It holds no copy of the options. Every build // reads the values from the player, and a change goes through the same event // the menus send, so the mpv flag rows follow the observed properties as they // do in the menus (and walk back if mpv refuses a write). const settings_id = 'app_settings' const settings_w = 760.0 const settings_h = 540.0 const settings_margin = 24.0 // The number rows' width, enough for the speed's two decimals beside its steppers. const settings_number_w = 130.0 // The number and slider rows have no menu event to reuse. // The theme editor's row sends the event its key and menu row used. const setting_theme_editor = 'view_theme_editor' const setting_slider_height = 'slider_height' const setting_max_volume = 'max_volume' const setting_speed = 'speed' // The pages with an icon in assets/icons, as page_.png. const settings_icon_pages = ['interface', 'appearance', 'playback', 'video', 'audio', 'subtitles', 'advanced'] // The menu's fixed scale steps; 0 is automatic. const setting_ui_scales = ['0', '1', '1.5', '2', '2.5', '3'] // toggle_item is a switch row. Its key is the menu event that flips the option. fn toggle_item(key string, title string) ui2.SettingItem { return ui2.SettingItem{ key: key title: title kind: .switch } } // settings_scale_value is the option matching the current UI scale, '' for a // scale the steps do not offer. fn (p &Player) settings_scale_value() string { for scale in setting_ui_scales { if p.ui_scale == scale.f64() { return scale } } return '' } // settings_pages lays the options out. Dropdown keys are the prefixes of their // menu events. fn (p &Player) settings_pages() []ui2.SettingPage { mut themes := []ui2.SettingOption{} for name in available_themes() { themes << ui2.SettingOption{ value: name } } mut audio_servers := []ui2.SettingOption{} for server in audio_server_names() { audio_servers << ui2.SettingOption{ value: server } } mut interface_items := [ toggle_item('overlay_mode', 'Overlay mode'), toggle_item('view_smooth_bar', 'Smooth seek bar'), toggle_item('view_preview', 'Seek bar hover preview'), toggle_item('title_status', 'Show playback status in the window title'), toggle_item('title_filename', 'Show filename in the window title'), ] $if mpris ? { interface_items << toggle_item('media_controls', 'Desktop media controls (MPRIS)') } return [ ui2.SettingPage{ id: 'interface' title: 'Interface' icon: p.settings_icons['interface'] resettable: false groups: [ui2.SettingGroup{ items: interface_items }] }, ui2.SettingPage{ id: 'appearance' title: 'Appearance' icon: p.settings_icons['appearance'] resettable: false groups: [ ui2.SettingGroup{ items: [ ui2.SettingItem{ key: 'theme' title: 'Theme' kind: .dropdown options: themes }, ui2.SettingItem{ key: setting_theme_editor title: 'Theme editor' description: 'Opens in a sidebar, so the window previews your colours.' kind: .button button_text: 'Open' }, ui2.SettingItem{ key: 'ui_scale' title: 'UI scale' kind: .dropdown options: [ ui2.SettingOption{ value: '0' label: 'Automatic' }, ui2.SettingOption{ value: '1' label: '1x' }, ui2.SettingOption{ value: '1.5' label: '1.5x' }, ui2.SettingOption{ value: '2' label: '2x' }, ui2.SettingOption{ value: '2.5' label: '2.5x' }, ui2.SettingOption{ value: '3' label: '3x' }, ] }, ui2.SettingItem{ key: setting_slider_height title: 'Seek and volume bar height' kind: .slider min: 1.0 max: 30.0 unit: ' px' }, ] }, ] }, ui2.SettingPage{ id: 'playback' title: 'Playback' icon: p.settings_icons['playback'] resettable: false groups: [ ui2.SettingGroup{ items: [ ui2.SettingItem{ key: 'open_mode' title: 'When opening files' kind: .dropdown options: [ ui2.SettingOption{ value: 'ask' label: 'Ask' }, ui2.SettingOption{ value: 'play' label: 'Play now' }, ui2.SettingOption{ value: 'queue' label: 'Add to queue' }, ] }, toggle_item('playback_loop', 'Loop file'), toggle_item('playback_loop_playlist', 'Loop playlist'), ui2.SettingItem{ key: setting_max_volume title: 'Max volume' kind: .number min: 1.0 max: 1000.0 precision: 0 field_width: settings_number_w }, ui2.SettingItem{ key: setting_speed title: 'Speed' kind: .number min: 0.01 max: 100.0 step: 0.25 precision: 2 field_width: settings_number_w }, ] }, ] }, ui2.SettingPage{ id: 'video' title: 'Video' icon: p.settings_icons['video'] resettable: false groups: [ ui2.SettingGroup{ items: [ toggle_item('video_hwdec', 'Hardware decoding'), toggle_item('video_deinterlace', 'Deinterlace'), toggle_item('video_fast_decode', 'Fast video decoding'), toggle_item('video_interpolation', 'Interpolation'), ] }, ui2.SettingGroup{ title: 'Picture quality' items: [ toggle_item('quality_deband', 'Deband'), toggle_item('quality_dither', 'Dither'), toggle_item('quality_correct_down', 'Correct downscaling'), toggle_item('quality_linear_down', 'Linear light downscaling'), toggle_item('quality_sigmoid_up', 'Sigmoid upscaling'), ] }, ] }, ui2.SettingPage{ id: 'audio' title: 'Audio' icon: p.settings_icons['audio'] resettable: false groups: [ ui2.SettingGroup{ items: [ ui2.SettingItem{ key: 'ao_server' title: 'Audio server' kind: .dropdown options: audio_servers }, toggle_item('audio_exclusive', 'Exclusive audio output'), toggle_item('audio_pitch_correction', 'Correct audio pitch at speed changes'), ] }, ] }, ui2.SettingPage{ id: 'subtitles' title: 'Subtitles' icon: p.settings_icons['subtitles'] resettable: false groups: [ ui2.SettingGroup{ items: [ toggle_item('subs_visible', 'Show subtitles'), toggle_item('quality_blend_subs', 'Blend subtitles into video'), ] }, ] }, ui2.SettingPage{ id: 'advanced' title: 'Advanced' icon: p.settings_icons['advanced'] resettable: false groups: [ ui2.SettingGroup{ items: [ ui2.SettingItem{ // Performance mode takes the continuous redraw away. ...toggle_item('view_continuous', 'Continuous redraw') disabled: p.low_quality }, toggle_item('view_low_quality', 'Performance mode'), toggle_item('render_wait_vsync', 'Wait for vsync timing'), ui2.SettingItem{ key: 'video_sync' title: 'Sync mode' kind: .dropdown options: [ ui2.SettingOption{ value: 'audio' label: 'Audio sync (default)' }, ui2.SettingOption{ value: 'display-resample' label: 'Display resample' }, ui2.SettingOption{ value: 'display-vdrop' label: 'Display resample, drop late frames' }, ] }, ui2.SettingItem{ key: 'framedrop' title: 'Frame dropping' kind: .dropdown options: [ ui2.SettingOption{ value: 'no' label: 'Off' }, ui2.SettingOption{ value: 'vo' label: 'Display stage' }, ui2.SettingOption{ value: 'decoder' label: 'Decoder stage' }, ui2.SettingOption{ value: 'decoder+vo' label: 'Both stages' }, ] }, ] }, ui2.SettingGroup{ title: 'Custom mpv options' items: p.settings_mpv_items() }, ] }, ] } // Reuse the option dialog and reset events, including options saved by the CLI. fn (p &Player) settings_mpv_items() []ui2.SettingItem { mut items := [ui2.SettingItem{ key: 'mpv_option' title: 'Set custom mpv option' description: 'Apply an mpv option by name and value and save it for future sessions.' kind: .button button_text: 'Add' }] mut names := p.mpv_opts.keys() names.sort() for name in names { if name in mirrored_mpv_options { continue } items << ui2.SettingItem{ key: 'mpv_option_edit:${name}' title: name description: p.mpv_opts[name] kind: .button button_text: 'Edit' } items << ui2.SettingItem{ key: 'mpv_option_reset:${name}' title: 'Unset ${name}' description: 'Remove the saved option. Some options reset after restarting.' kind: .button button_text: 'Unset' } } return items } // settings_values reads every row's value from the player. fn (p &Player) settings_values() map[string]ui2.SettingValue { mut values := map[string]ui2.SettingValue{} toggles := { 'overlay_mode': if p.fullscreen { p.fullscreen_overlay } else { p.windowed_overlay } 'view_smooth_bar': p.smooth_bar 'title_status': p.show_playback_status 'title_filename': p.show_title_filename 'subs_visible': p.sub_visible 'view_preview': p.show_preview 'media_controls': p.media_controls 'playback_loop': p.loop 'playback_loop_playlist': p.loop_playlist 'video_hwdec': p.hwdec 'video_deinterlace': p.deinterlace 'video_fast_decode': p.fast_decode 'video_interpolation': p.interpolation 'quality_deband': p.deband 'quality_dither': p.dither 'quality_correct_down': p.correct_downscaling 'quality_linear_down': p.linear_downscaling 'quality_sigmoid_up': p.sigmoid_upscaling 'quality_blend_subs': p.blend_subtitles 'audio_exclusive': p.audio_exclusive 'audio_pitch_correction': p.pitch_correction 'view_continuous': p.continuous 'view_low_quality': p.low_quality 'render_wait_vsync': p.cfg.block_target_time } for key, on in toggles { values[key] = ui2.SettingValue(on) } values['open_mode'] = ui2.SettingValue(p.open_mode) values['theme'] = ui2.SettingValue(p.theme_name) values['video_sync'] = ui2.SettingValue(p.video_sync) values['framedrop'] = ui2.SettingValue(p.framedrop) values['ao_server'] = ui2.SettingValue(p.ao) values['ui_scale'] = ui2.SettingValue(p.settings_scale_value()) values[setting_slider_height] = ui2.SettingValue(p.cfg.slider_h) values[setting_max_volume] = ui2.SettingValue(p.max_volume) values[setting_speed] = ui2.SettingValue(p.speed) return values } // settings_theme maps the app theme onto ui2's component colours, so the dialog // follows theme changes and the live editor. fn (p &Player) settings_theme() ui2.ComponentTheme { return ui2.ComponentTheme{ ...ui2.component_theme(true) background: p.theme.bg_dark foreground: p.theme.text_main // Buttons fill with muted and dropdowns with background: one colour for both. muted: p.theme.bg_dark muted_foreground: p.theme.text_muted border: p.theme.menu_border primary: p.theme.btn_primary primary_foreground: p.theme.text_main } } // settings_frame is the dialog's size in a window of the given bounds. fn settings_frame(b ui2.Rect) ui2.Rect { return ui2.rect(0, 0, math.max(math.min(settings_w, b.width - settings_margin * 2), 0.0), math.max(math.min(settings_h, b.height - settings_margin * 2), 0.0)) } fn (p &Player) settings_config() ui2.SettingsConfig { return ui2.SettingsConfig{ id: settings_id frame: settings_frame(ui2.bounds()) pages: p.settings_pages() values: p.settings_values() state: p.settings_state group_variant: .outline nav_width: 200 // The smallest ui2 offers, the nearest to the app's own 11 to 13 pt text. size: .small transparent: true theme: p.settings_theme() } } // apply_setting hands one changed row to the handler its menu row uses. fn (mut p Player) apply_setting(change ui2.SettingChange) { value := change.value match value { bool { p.handle(change.key) } string { p.handle('${change.key}:${value}') } f64 { match change.key { setting_slider_height { p.cfg.slider_h = value } setting_max_volume { p.set_max_volume(value) } setting_speed { p.handle('playback_speed:${value}') } else {} } } } } // take_settings_step stores what the dialog decoded, then rebuilds and does // what has to wait for the new tree. fn (mut p Player) take_settings_step(step ui2.SettingsStep) { p.settings_state = step.state for change in step.changes { p.apply_setting(change) } // The editor is a sidebar, which the dialog's scrim would cover. if step.action == setting_theme_editor { p.set_settings_open(false) if !p.theme_editor_open { p.handle(setting_theme_editor) } return } if step.action == 'mpv_option' || step.action.starts_with('mpv_option_edit:') { // The option dialog must be above the player, without the settings scrim. p.set_settings_open(false) p.handle(step.action) return } if step.action.starts_with('mpv_option_reset:') { p.handle(step.action) } p.settings_changed() ui2.refresh() if step.sync_id.len > 0 && ui2.text(step.sync_id) != step.sync_text { ui2.set_text(step.sync_id, step.sync_text) } if step.focus.len > 0 { ui2.focus(step.focus) } if step.scroll { ui2.scroll_to_offset(step.scroll_id, step.scroll_to) } } // handle_settings offers a window event to the open dialog and reports whether // it took it. fn (mut p Player) handle_settings(event string) bool { step := ui2.settings_update(p.settings_config(), event, ui2.SettingsInput{ text: ui2.text number: ui2.slider_value }) or { return false } p.take_settings_step(step) return true } // settings_key_event offers a key to the open dialog: Ctrl+F goes to the search // field, and what the dialog does not use falls to handle_key's guard. fn (mut p Player) settings_key_event(event ui2.KeyEvent) { if event.ctrl && event.code == .f && !event.alt && !event.shift { ui2.consume_key() ui2.focus(ui2.settings_search_field_id(settings_id)) return } step := ui2.settings_key(p.settings_config(), ui2.focused_id(), event) or { return } ui2.consume_key() p.take_settings_step(step) } // set_settings_open shows or hides the dialog. The selected page survives a // close; the search does not. The page icons resolve on the first open. fn (mut p Player) set_settings_open(open bool) { if open && p.settings_icons.len == 0 { for page in settings_icon_pages { p.settings_icons[page] = libvendetta.bundled_icon('page_${page}') } } p.settings_open = open p.settings_state = ui2.SettingsState{ ...p.settings_state query: '' menu: '' } ui2.request_refresh() } // settings_dialog is the modal around the view. The view only builds while the // dialog is open, so it costs playback's rebuilds nothing. fn (p &Player) settings_dialog(b ui2.Rect) !ui2.Element { frame := settings_frame(b) return ui2.modal_view(ui2.ModalViewConfig{ id: 'settings_dialog' frame: b open: true dismiss_action_id: 'settings_close' overlay_box: ui2.BoxStyle{ bg: p.theme.overlay_bg opacity: 0.45 } content_width: frame.width content_height: frame.height content_box: ui2.BoxStyle{ bg: p.theme.bg_dark opacity: p.theme.overlay_opacity radius: 8 } content: ui2.settings(p.settings_config())! }) }