vendetta / src / settings_view.v
534 lines · 514 sloc · 14.82 KB · 80f14d8fb92068552b258c65069f3fee405c23bc
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')
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 ]
326 },
327 ]
328}
329
330// settings_values reads every row's value from the player.
331fn (p &Player) settings_values() map[string]ui2.SettingValue {
332 mut values := map[string]ui2.SettingValue{}
333 toggles := {
334 'overlay_mode': if p.fullscreen {
335 p.fullscreen_overlay
336 } else {
337 p.windowed_overlay
338 }
339 'view_smooth_bar': p.smooth_bar
340 'title_status': p.show_playback_status
341 'title_filename': p.show_title_filename
342 'subs_visible': p.sub_visible
343 'view_preview': p.show_preview
344 'media_controls': p.media_controls
345 'playback_loop': p.loop
346 'playback_loop_playlist': p.loop_playlist
347 'video_hwdec': p.hwdec
348 'video_deinterlace': p.deinterlace
349 'video_fast_decode': p.fast_decode
350 'video_interpolation': p.interpolation
351 'quality_deband': p.deband
352 'quality_dither': p.dither
353 'quality_correct_down': p.correct_downscaling
354 'quality_linear_down': p.linear_downscaling
355 'quality_sigmoid_up': p.sigmoid_upscaling
356 'quality_blend_subs': p.blend_subtitles
357 'audio_exclusive': p.audio_exclusive
358 'audio_pitch_correction': p.pitch_correction
359 'view_continuous': p.continuous
360 'view_low_quality': p.low_quality
361 'render_wait_vsync': p.cfg.block_target_time
362 }
363 for key, on in toggles {
364 values[key] = ui2.SettingValue(on)
365 }
366 values['open_mode'] = ui2.SettingValue(p.open_mode)
367 values['theme'] = ui2.SettingValue(p.theme_name)
368 values['video_sync'] = ui2.SettingValue(p.video_sync)
369 values['framedrop'] = ui2.SettingValue(p.framedrop)
370 values['ao_server'] = ui2.SettingValue(p.ao)
371 values['ui_scale'] = ui2.SettingValue(p.settings_scale_value())
372 values[setting_slider_height] = ui2.SettingValue(p.cfg.slider_h)
373 values[setting_max_volume] = ui2.SettingValue(p.max_volume)
374 values[setting_speed] = ui2.SettingValue(p.speed)
375 return values
376}
377
378// settings_theme maps the app theme onto ui2's component colours, so the dialog
379// follows theme changes and the live editor.
380fn (p &Player) settings_theme() ui2.ComponentTheme {
381 return ui2.ComponentTheme{
382 ...ui2.component_theme(true)
383 background: p.theme.bg_dark
384 foreground: p.theme.text_main
385 // Buttons fill with muted and dropdowns with background: one colour for both.
386 muted: p.theme.bg_dark
387 muted_foreground: p.theme.text_muted
388 border: p.theme.menu_border
389 primary: p.theme.btn_primary
390 primary_foreground: p.theme.text_main
391 }
392}
393
394// settings_frame is the dialog's size in a window of the given bounds.
395fn settings_frame(b ui2.Rect) ui2.Rect {
396 return ui2.rect(0, 0, math.max(math.min(settings_w, b.width - settings_margin * 2), 0.0),
397 math.max(math.min(settings_h, b.height - settings_margin * 2), 0.0))
398}
399
400fn (p &Player) settings_config() ui2.SettingsConfig {
401 return ui2.SettingsConfig{
402 id: settings_id
403 frame: settings_frame(ui2.bounds())
404 pages: p.settings_pages()
405 values: p.settings_values()
406 state: p.settings_state
407 group_variant: .outline
408 nav_width: 200
409 // The smallest ui2 offers, the nearest to the app's own 11 to 13 pt text.
410 size: .small
411 transparent: true
412 theme: p.settings_theme()
413 }
414}
415
416// apply_setting hands one changed row to the handler its menu row uses.
417fn (mut p Player) apply_setting(change ui2.SettingChange) {
418 value := change.value
419 match value {
420 bool {
421 p.handle(change.key)
422 }
423 string {
424 p.handle('${change.key}:${value}')
425 }
426 f64 {
427 match change.key {
428 setting_slider_height {
429 p.cfg.slider_h = value
430 }
431 setting_max_volume {
432 p.set_max_volume(value)
433 }
434 setting_speed {
435 p.handle('playback_speed:${value}')
436 }
437 else {}
438 }
439 }
440 }
441}
442
443// take_settings_step stores what the dialog decoded, then rebuilds and does
444// what has to wait for the new tree.
445fn (mut p Player) take_settings_step(step ui2.SettingsStep) {
446 p.settings_state = step.state
447 for change in step.changes {
448 p.apply_setting(change)
449 }
450 // The editor is a sidebar, which the dialog's scrim would cover.
451 if step.action == setting_theme_editor {
452 p.set_settings_open(false)
453 if !p.theme_editor_open {
454 p.handle(setting_theme_editor)
455 }
456 return
457 }
458 p.settings_changed()
459 ui2.refresh()
460 if step.sync_id.len > 0 && ui2.text(step.sync_id) != step.sync_text {
461 ui2.set_text(step.sync_id, step.sync_text)
462 }
463 if step.focus.len > 0 {
464 ui2.focus(step.focus)
465 }
466 if step.scroll {
467 ui2.scroll_to_offset(step.scroll_id, step.scroll_to)
468 }
469}
470
471// handle_settings offers a window event to the open dialog and reports whether
472// it took it.
473fn (mut p Player) handle_settings(event string) bool {
474 step := ui2.settings_update(p.settings_config(), event, ui2.SettingsInput{
475 text: ui2.text
476 number: ui2.slider_value
477 }) or { return false }
478 p.take_settings_step(step)
479 return true
480}
481
482// settings_key_event offers a key to the open dialog: Ctrl+F goes to the search
483// field, and what the dialog does not use falls to handle_key's guard.
484fn (mut p Player) settings_key_event(event ui2.KeyEvent) {
485 if event.ctrl && event.code == .f && !event.alt && !event.shift {
486 ui2.consume_key()
487 ui2.focus(ui2.settings_search_field_id(settings_id))
488 return
489 }
490 step := ui2.settings_key(p.settings_config(), ui2.focused_id(), event) or { return }
491 ui2.consume_key()
492 p.take_settings_step(step)
493}
494
495// set_settings_open shows or hides the dialog. The selected page survives a
496// close; the search does not. The page icons resolve on the first open.
497fn (mut p Player) set_settings_open(open bool) {
498 if open && p.settings_icons.len == 0 {
499 for page in settings_icon_pages {
500 p.settings_icons[page] = libvendetta.bundled_icon('page_${page}')
501 }
502 }
503 p.settings_open = open
504 p.settings_state = ui2.SettingsState{
505 ...p.settings_state
506 query: ''
507 menu: ''
508 }
509 ui2.request_refresh()
510}
511
512// settings_dialog is the modal around the view. The view only builds while the
513// dialog is open, so it costs playback's rebuilds nothing.
514fn (p &Player) settings_dialog(b ui2.Rect) !ui2.Element {
515 frame := settings_frame(b)
516 return ui2.modal_view(ui2.ModalViewConfig{
517 id: 'settings_dialog'
518 frame: b
519 open: true
520 dismiss_action_id: 'settings_close'
521 overlay_box: ui2.BoxStyle{
522 bg: p.theme.overlay_bg
523 opacity: 0.45
524 }
525 content_width: frame.width
526 content_height: frame.height
527 content_box: ui2.BoxStyle{
528 bg: p.theme.bg_dark
529 opacity: p.theme.overlay_opacity
530 radius: 8
531 }
532 content: ui2.settings(p.settings_config())!
533 })
534}
535