vendetta / src / settings_view.v
580 lines · 559 sloc · 16.12 KB · 3e4163bcc5f246e02299f4c7fad2c482407f8e88
Raw
1module main
2
3import math
4import ui2
5import libvendetta
6
7// The settings dialog: ui2 owns one native window, so this is a ui2.settings
8// view in a modal over the player. It holds no copy of the options. Every build
9// reads the values from the player, and a change goes through the same event
10// the menus send, so the mpv flag rows follow the observed properties as they
11// do in the menus (and walk back if mpv refuses a write).
12
13const settings_id = 'app_settings'
14const settings_w = 760.0
15const settings_h = 540.0
16const settings_margin = 24.0
17
18// The number rows' width, enough for the speed's two decimals beside its steppers.
19const settings_number_w = 130.0
20
21// The number and slider rows have no menu event to reuse.
22// The theme editor's row sends the event its key and menu row used.
23const setting_theme_editor = 'view_theme_editor'
24const setting_slider_height = 'slider_height'
25const setting_max_volume = 'max_volume'
26const setting_speed = 'speed'
27
28// The pages with an icon in assets/icons, as page_<name>.png.
29const settings_icon_pages = ['interface', 'appearance', 'playback', 'video', 'audio', 'subtitles',
30 'advanced']
31
32// The menu's fixed scale steps; 0 is automatic.
33const setting_ui_scales = ['0', '1', '1.5', '2', '2.5', '3']
34
35// toggle_item is a switch row. Its key is the menu event that flips the option.
36fn toggle_item(key string, title string) ui2.SettingItem {
37 return ui2.SettingItem{
38 key: key
39 title: title
40 kind: .switch
41 }
42}
43
44// settings_scale_value is the option matching the current UI scale, '' for a
45// scale the steps do not offer.
46fn (p &Player) settings_scale_value() string {
47 for scale in setting_ui_scales {
48 if p.ui_scale == scale.f64() {
49 return scale
50 }
51 }
52 return ''
53}
54
55// settings_pages lays the options out. Dropdown keys are the prefixes of their
56// menu events.
57fn (p &Player) settings_pages() []ui2.SettingPage {
58 mut themes := []ui2.SettingOption{}
59 for name in available_themes() {
60 themes << ui2.SettingOption{
61 value: name
62 }
63 }
64 mut audio_servers := []ui2.SettingOption{}
65 for server in audio_server_names() {
66 audio_servers << ui2.SettingOption{
67 value: server
68 }
69 }
70 mut interface_items := [
71 toggle_item('overlay_mode', 'Overlay mode'),
72 toggle_item('view_smooth_bar', 'Smooth seek bar'),
73 toggle_item('view_preview', 'Seek bar hover preview'),
74 toggle_item('title_status', 'Show playback status in the window title'),
75 toggle_item('title_filename', 'Show filename in the window title'),
76 ]
77 $if mpris ? {
78 interface_items << toggle_item('media_controls', 'Desktop media controls (MPRIS)')
79 }
80 return [
81 ui2.SettingPage{
82 id: 'interface'
83 title: 'Interface'
84 icon: p.settings_icons['interface']
85 resettable: false
86 groups: [ui2.SettingGroup{
87 items: interface_items
88 }]
89 },
90 ui2.SettingPage{
91 id: 'appearance'
92 title: 'Appearance'
93 icon: p.settings_icons['appearance']
94 resettable: false
95 groups: [
96 ui2.SettingGroup{
97 items: [
98 ui2.SettingItem{
99 key: 'theme'
100 title: 'Theme'
101 kind: .dropdown
102 options: themes
103 },
104 ui2.SettingItem{
105 key: setting_theme_editor
106 title: 'Theme editor'
107 description: 'Opens in a sidebar, so the window previews your colours.'
108 kind: .button
109 button_text: 'Open'
110 },
111 ui2.SettingItem{
112 key: 'ui_scale'
113 title: 'UI scale'
114 kind: .dropdown
115 options: [
116 ui2.SettingOption{
117 value: '0'
118 label: 'Automatic'
119 },
120 ui2.SettingOption{
121 value: '1'
122 label: '1x'
123 },
124 ui2.SettingOption{
125 value: '1.5'
126 label: '1.5x'
127 },
128 ui2.SettingOption{
129 value: '2'
130 label: '2x'
131 },
132 ui2.SettingOption{
133 value: '2.5'
134 label: '2.5x'
135 },
136 ui2.SettingOption{
137 value: '3'
138 label: '3x'
139 },
140 ]
141 },
142 ui2.SettingItem{
143 key: setting_slider_height
144 title: 'Seek and volume bar height'
145 kind: .slider
146 min: 1.0
147 max: 30.0
148 unit: ' px'
149 },
150 ]
151 },
152 ]
153 },
154 ui2.SettingPage{
155 id: 'playback'
156 title: 'Playback'
157 icon: p.settings_icons['playback']
158 resettable: false
159 groups: [
160 ui2.SettingGroup{
161 items: [
162 ui2.SettingItem{
163 key: 'open_mode'
164 title: 'When opening files'
165 kind: .dropdown
166 options: [
167 ui2.SettingOption{
168 value: 'ask'
169 label: 'Ask'
170 },
171 ui2.SettingOption{
172 value: 'play'
173 label: 'Play now'
174 },
175 ui2.SettingOption{
176 value: 'queue'
177 label: 'Add to queue'
178 },
179 ]
180 },
181 toggle_item('playback_loop', 'Loop file'),
182 toggle_item('playback_loop_playlist', 'Loop playlist'),
183 ui2.SettingItem{
184 key: setting_max_volume
185 title: 'Max volume'
186 kind: .number
187 min: 1.0
188 max: 1000.0
189 precision: 0
190 field_width: settings_number_w
191 },
192 ui2.SettingItem{
193 key: setting_speed
194 title: 'Speed'
195 kind: .number
196 min: 0.01
197 max: 100.0
198 step: 0.25
199 precision: 2
200 field_width: settings_number_w
201 },
202 ]
203 },
204 ]
205 },
206 ui2.SettingPage{
207 id: 'video'
208 title: 'Video'
209 icon: p.settings_icons['video']
210 resettable: false
211 groups: [
212 ui2.SettingGroup{
213 items: [
214 toggle_item('video_hwdec', 'Hardware decoding'),
215 toggle_item('video_deinterlace', 'Deinterlace'),
216 toggle_item('video_fast_decode', 'Fast video decoding'),
217 toggle_item('video_interpolation', 'Interpolation'),
218 ]
219 },
220 ui2.SettingGroup{
221 title: 'Picture quality'
222 items: [
223 toggle_item('quality_deband', 'Deband'),
224 toggle_item('quality_dither', 'Dither'),
225 toggle_item('quality_correct_down', 'Correct downscaling'),
226 toggle_item('quality_linear_down', 'Linear light downscaling'),
227 toggle_item('quality_sigmoid_up', 'Sigmoid upscaling'),
228 ]
229 },
230 ]
231 },
232 ui2.SettingPage{
233 id: 'audio'
234 title: 'Audio'
235 icon: p.settings_icons['audio']
236 resettable: false
237 groups: [
238 ui2.SettingGroup{
239 items: [
240 ui2.SettingItem{
241 key: 'ao_server'
242 title: 'Audio server'
243 kind: .dropdown
244 options: audio_servers
245 },
246 toggle_item('audio_exclusive', 'Exclusive audio output'),
247 toggle_item('audio_pitch_correction', 'Correct audio pitch at speed changes'),
248 ]
249 },
250 ]
251 },
252 ui2.SettingPage{
253 id: 'subtitles'
254 title: 'Subtitles'
255 icon: p.settings_icons['subtitles']
256 resettable: false
257 groups: [
258 ui2.SettingGroup{
259 items: [
260 toggle_item('subs_visible', 'Show subtitles'),
261 toggle_item('quality_blend_subs', 'Blend subtitles into video'),
262 ]
263 },
264 ]
265 },
266 ui2.SettingPage{
267 id: 'advanced'
268 title: 'Advanced'
269 icon: p.settings_icons['advanced']
270 resettable: false
271 groups: [
272 ui2.SettingGroup{
273 items: [
274 ui2.SettingItem{
275 // Performance mode takes the continuous redraw away.
276 ...toggle_item('view_continuous', 'Continuous redraw')
277 disabled: p.low_quality
278 },
279 toggle_item('view_low_quality', 'Performance mode'),
280 toggle_item('render_wait_vsync', 'Wait for vsync timing'),
281 ui2.SettingItem{
282 key: 'video_sync'
283 title: 'Sync mode'
284 kind: .dropdown
285 options: [
286 ui2.SettingOption{
287 value: 'audio'
288 label: 'Audio sync (default)'
289 },
290 ui2.SettingOption{
291 value: 'display-resample'
292 label: 'Display resample'
293 },
294 ui2.SettingOption{
295 value: 'display-vdrop'
296 label: 'Display resample, drop late frames'
297 },
298 ]
299 },
300 ui2.SettingItem{
301 key: 'framedrop'
302 title: 'Frame dropping'
303 kind: .dropdown
304 options: [
305 ui2.SettingOption{
306 value: 'no'
307 label: 'Off'
308 },
309 ui2.SettingOption{
310 value: 'vo'
311 label: 'Display stage'
312 },
313 ui2.SettingOption{
314 value: 'decoder'
315 label: 'Decoder stage'
316 },
317 ui2.SettingOption{
318 value: 'decoder+vo'
319 label: 'Both stages'
320 },
321 ]
322 },
323 ]
324 },
325 ui2.SettingGroup{
326 title: 'Custom mpv options'
327 items: p.settings_mpv_items()
328 },
329 ]
330 },
331 ]
332}
333
334// Reuse the option dialog and reset events, including options saved by the CLI.
335fn (p &Player) settings_mpv_items() []ui2.SettingItem {
336 mut items := [ui2.SettingItem{
337 key: 'mpv_option'
338 title: 'Set custom mpv option'
339 description: 'Apply an mpv option by name and value and save it for future sessions.'
340 kind: .button
341 button_text: 'Add'
342 }]
343 mut names := p.mpv_opts.keys()
344 names.sort()
345 for name in names {
346 if name in mirrored_mpv_options {
347 continue
348 }
349 items << ui2.SettingItem{
350 key: 'mpv_option_edit:${name}'
351 title: name
352 description: p.mpv_opts[name]
353 kind: .button
354 button_text: 'Edit'
355 }
356 items << ui2.SettingItem{
357 key: 'mpv_option_reset:${name}'
358 title: 'Unset ${name}'
359 description: 'Remove the saved option. Some options reset after restarting.'
360 kind: .button
361 button_text: 'Unset'
362 }
363 }
364 return items
365}
366
367// settings_values reads every row's value from the player.
368fn (p &Player) settings_values() map[string]ui2.SettingValue {
369 mut values := map[string]ui2.SettingValue{}
370 toggles := {
371 'overlay_mode': if p.fullscreen {
372 p.fullscreen_overlay
373 } else {
374 p.windowed_overlay
375 }
376 'view_smooth_bar': p.smooth_bar
377 'title_status': p.show_playback_status
378 'title_filename': p.show_title_filename
379 'subs_visible': p.sub_visible
380 'view_preview': p.show_preview
381 'media_controls': p.media_controls
382 'playback_loop': p.loop
383 'playback_loop_playlist': p.loop_playlist
384 'video_hwdec': p.hwdec
385 'video_deinterlace': p.deinterlace
386 'video_fast_decode': p.fast_decode
387 'video_interpolation': p.interpolation
388 'quality_deband': p.deband
389 'quality_dither': p.dither
390 'quality_correct_down': p.correct_downscaling
391 'quality_linear_down': p.linear_downscaling
392 'quality_sigmoid_up': p.sigmoid_upscaling
393 'quality_blend_subs': p.blend_subtitles
394 'audio_exclusive': p.audio_exclusive
395 'audio_pitch_correction': p.pitch_correction
396 'view_continuous': p.continuous
397 'view_low_quality': p.low_quality
398 'render_wait_vsync': p.cfg.block_target_time
399 }
400 for key, on in toggles {
401 values[key] = ui2.SettingValue(on)
402 }
403 values['open_mode'] = ui2.SettingValue(p.open_mode)
404 values['theme'] = ui2.SettingValue(p.theme_name)
405 values['video_sync'] = ui2.SettingValue(p.video_sync)
406 values['framedrop'] = ui2.SettingValue(p.framedrop)
407 values['ao_server'] = ui2.SettingValue(p.ao)
408 values['ui_scale'] = ui2.SettingValue(p.settings_scale_value())
409 values[setting_slider_height] = ui2.SettingValue(p.cfg.slider_h)
410 values[setting_max_volume] = ui2.SettingValue(p.max_volume)
411 values[setting_speed] = ui2.SettingValue(p.speed)
412 return values
413}
414
415// settings_theme maps the app theme onto ui2's component colours, so the dialog
416// follows theme changes and the live editor.
417fn (p &Player) settings_theme() ui2.ComponentTheme {
418 return ui2.ComponentTheme{
419 ...ui2.component_theme(true)
420 background: p.theme.bg_dark
421 foreground: p.theme.text_main
422 // Buttons fill with muted and dropdowns with background: one colour for both.
423 muted: p.theme.bg_dark
424 muted_foreground: p.theme.text_muted
425 border: p.theme.menu_border
426 primary: p.theme.btn_primary
427 primary_foreground: p.theme.text_main
428 }
429}
430
431// settings_frame is the dialog's size in a window of the given bounds.
432fn settings_frame(b ui2.Rect) ui2.Rect {
433 return ui2.rect(0, 0, math.max(math.min(settings_w, b.width - settings_margin * 2), 0.0),
434 math.max(math.min(settings_h, b.height - settings_margin * 2), 0.0))
435}
436
437fn (p &Player) settings_config() ui2.SettingsConfig {
438 return ui2.SettingsConfig{
439 id: settings_id
440 frame: settings_frame(ui2.bounds())
441 pages: p.settings_pages()
442 values: p.settings_values()
443 state: p.settings_state
444 group_variant: .outline
445 nav_width: 200
446 // The smallest ui2 offers, the nearest to the app's own 11 to 13 pt text.
447 size: .small
448 transparent: true
449 theme: p.settings_theme()
450 }
451}
452
453// apply_setting hands one changed row to the handler its menu row uses.
454fn (mut p Player) apply_setting(change ui2.SettingChange) {
455 value := change.value
456 match value {
457 bool {
458 p.handle(change.key)
459 }
460 string {
461 p.handle('${change.key}:${value}')
462 }
463 f64 {
464 match change.key {
465 setting_slider_height {
466 p.cfg.slider_h = value
467 }
468 setting_max_volume {
469 p.set_max_volume(value)
470 }
471 setting_speed {
472 p.handle('playback_speed:${value}')
473 }
474 else {}
475 }
476 }
477 }
478}
479
480// take_settings_step stores what the dialog decoded, then rebuilds and does
481// what has to wait for the new tree.
482fn (mut p Player) take_settings_step(step ui2.SettingsStep) {
483 p.settings_state = step.state
484 for change in step.changes {
485 p.apply_setting(change)
486 }
487 // The editor is a sidebar, which the dialog's scrim would cover.
488 if step.action == setting_theme_editor {
489 p.set_settings_open(false)
490 if !p.theme_editor_open {
491 p.handle(setting_theme_editor)
492 }
493 return
494 }
495 if step.action == 'mpv_option' || step.action.starts_with('mpv_option_edit:') {
496 // The option dialog must be above the player, without the settings scrim.
497 p.set_settings_open(false)
498 p.handle(step.action)
499 return
500 }
501 if step.action.starts_with('mpv_option_reset:') {
502 p.handle(step.action)
503 }
504 p.settings_changed()
505 ui2.refresh()
506 if step.sync_id.len > 0 && ui2.text(step.sync_id) != step.sync_text {
507 ui2.set_text(step.sync_id, step.sync_text)
508 }
509 if step.focus.len > 0 {
510 ui2.focus(step.focus)
511 }
512 if step.scroll {
513 ui2.scroll_to_offset(step.scroll_id, step.scroll_to)
514 }
515}
516
517// handle_settings offers a window event to the open dialog and reports whether
518// it took it.
519fn (mut p Player) handle_settings(event string) bool {
520 step := ui2.settings_update(p.settings_config(), event, ui2.SettingsInput{
521 text: ui2.text
522 number: ui2.slider_value
523 }) or { return false }
524 p.take_settings_step(step)
525 return true
526}
527
528// settings_key_event offers a key to the open dialog: Ctrl+F goes to the search
529// field, and what the dialog does not use falls to handle_key's guard.
530fn (mut p Player) settings_key_event(event ui2.KeyEvent) {
531 if event.ctrl && event.code == .f && !event.alt && !event.shift {
532 ui2.consume_key()
533 ui2.focus(ui2.settings_search_field_id(settings_id))
534 return
535 }
536 step := ui2.settings_key(p.settings_config(), ui2.focused_id(), event) or { return }
537 ui2.consume_key()
538 p.take_settings_step(step)
539}
540
541// set_settings_open shows or hides the dialog. The selected page survives a
542// close; the search does not. The page icons resolve on the first open.
543fn (mut p Player) set_settings_open(open bool) {
544 if open && p.settings_icons.len == 0 {
545 for page in settings_icon_pages {
546 p.settings_icons[page] = libvendetta.bundled_icon('page_${page}')
547 }
548 }
549 p.settings_open = open
550 p.settings_state = ui2.SettingsState{
551 ...p.settings_state
552 query: ''
553 menu: ''
554 }
555 ui2.request_refresh()
556}
557
558// settings_dialog is the modal around the view. The view only builds while the
559// dialog is open, so it costs playback's rebuilds nothing.
560fn (p &Player) settings_dialog(b ui2.Rect) !ui2.Element {
561 frame := settings_frame(b)
562 return ui2.modal_view(ui2.ModalViewConfig{
563 id: 'settings_dialog'
564 frame: b
565 open: true
566 dismiss_action_id: 'settings_close'
567 overlay_box: ui2.BoxStyle{
568 bg: p.theme.overlay_bg
569 opacity: 0.45
570 }
571 content_width: frame.width
572 content_height: frame.height
573 content_box: ui2.BoxStyle{
574 bg: p.theme.bg_dark
575 opacity: p.theme.overlay_opacity
576 radius: 8
577 }
578 content: ui2.settings(p.settings_config())!
579 })
580}
581