vendetta / src / presets.v
144 lines · 137 sloc · 5.39 KB · 77754ad9e6dc749afefba326ff89abc5ddd8c9c9
Raw
1import os
2import ui2
3import libvendetta
4import json2
5
6// presets_dir is where saved options presets live: <config base>/vendetta/presets.
7fn presets_dir() string {
8 return os.join_path(config_base(), 'vendetta', 'presets')
9}
10
11// saved_presets lists the saved preset names, sorted.
12fn saved_presets() []string {
13 mut names := []string{}
14 files := os.ls(presets_dir()) or { return names }
15 for file in files {
16 if file.ends_with('.json') {
17 names << file.all_before_last('.json')
18 }
19 }
20 names.sort()
21 return names
22}
23
24// load_preset_file reads a saved preset's options. A missing or malformed
25// file is not a preset.
26fn load_preset_file(name string) ?map[string]string {
27 text := os.read_file(os.join_path(presets_dir(), '${name}.json')) or { return none }
28 return json2.decode[map[string]string](text, json2.DecoderOptions{}) or { none }
29}
30
31// preset_snapshot is the Options bar as mpv options: every flag the menus
32// mirror from mpv's observed state, written in the same form the menu
33// handlers write it, overlaid by the options configured through the dialog
34// or --mpv-opt. Saving captures this map; applying writes it back option by
35// option. Presentation pacing is vendetta's render call, not an mpv
36// property, so it is the one Options row a preset cannot carry.
37fn (p &Player) preset_snapshot() map[string]string {
38 mut opts := map[string]string{
39 'interpolation': if p.interpolation { 'yes' } else { 'no' }
40 'audio-pitch-correction': if p.pitch_correction { 'yes' } else { 'no' }
41 'audio-exclusive': if p.audio_exclusive { 'yes' } else { 'no' }
42 'vd-lavc-fast': if p.fast_decode { 'yes' } else { 'no' }
43 'correct-downscaling': if p.correct_downscaling { 'yes' } else { 'no' }
44 'linear-downscaling': if p.linear_downscaling { 'yes' } else { 'no' }
45 'sigmoid-upscaling': if p.sigmoid_upscaling { 'yes' } else { 'no' }
46 'deband': if p.deband { 'yes' } else { 'no' }
47 'dither': if p.dither { 'fruit' } else { 'no' }
48 'blend-subtitles': if p.blend_subtitles { 'yes' } else { 'no' }
49 'deinterlace': if p.deinterlace { 'yes' } else { 'no' }
50 'hwdec': libvendetta.hwdec_mode(p.hwdec)
51 }
52 // The sync and dropping choices are strings whose observed value mpv
53 // reports once observed; before the first event arrives there is nothing
54 // to capture, and an empty write would unset the option instead.
55 if p.video_sync.len > 0 {
56 opts['video-sync'] = p.video_sync
57 }
58 if p.framedrop.len > 0 {
59 opts['framedrop'] = p.framedrop
60 }
61 // Options set by name through the dialog or --mpv-opt outrank the
62 // observed mirrors: they are what the user configured outright.
63 for name, value in p.mpv_opts {
64 opts[name] = value
65 }
66 return opts
67}
68
69// save_preset writes the current options under name. The name may not be
70// empty or escape the presets directory, like the queue and theme names.
71fn (mut p Player) save_preset(name string) bool {
72 mut clean := name.trim_space()
73 if clean.len == 0 || clean.contains('/') || clean.contains('\\') || clean.contains('..') {
74 p.notify('Preset name may not be empty or contain path separators.')
75 return false
76 }
77 os.mkdir_all(presets_dir(), os.MkdirParams{}) or {}
78 os.write_file(os.join_path(presets_dir(), '${clean}.json'), json2.encode(p.preset_snapshot(), json2.EncoderOptions{})) or {
79 p.notify('Could not save the preset: ${err}')
80 return false
81 }
82 p.preset_name = clean
83 p.notify('Preset saved as ${clean}.')
84 return true
85}
86
87// apply_preset loads a saved preset and writes its options into mpv one by
88// one. The preset names the whole configured set: options set earlier but
89// absent from it stop being carried, so switching presets replaces the set
90// instead of adding to it. Rejected writes are reported but do not stop the
91// rest, and an option mpv refuses leaves the configured set as it was.
92fn (mut p Player) apply_preset(name string) {
93 opts := load_preset_file(name) or {
94 p.notify('Could not load the preset ${name}.')
95 return
96 }
97 former := p.mpv_opts.keys()
98 for opt in former {
99 if opt !in opts {
100 p.unset_mpv_option(opt)
101 }
102 }
103 mut rejected := []string{}
104 for opt, value in opts {
105 // An empty value in the file unsets: the same rule as the dialog
106 // and the command line.
107 if value.len == 0 {
108 p.unset_mpv_option(opt)
109 continue
110 }
111 if complaint := p.set_mpv_option(opt, value) {
112 rejected << '${opt}=${value}: ${complaint}'
113 continue
114 }
115 p.mpv_opts[opt] = value
116 }
117 p.preset_name = name
118 if rejected.len > 0 {
119 p.notify('Preset ${name}: mpv rejected ${rejected.join('; ')}')
120 } else {
121 p.clear_notices()
122 }
123 p.settings_changed()
124}
125
126// preset_menu_items builds the Options > Presets entries: saving the current
127// options under a name, then the saved presets (checked for the one applied
128// last) and the delete submenu, shaped like the theme menu.
129fn (p &Player) preset_menu_items() []ui2.MenuItem {
130 mut items := []ui2.MenuItem{}
131 items << ui2.menu_item('preset_save', 'Save current options...')
132 mut delete_items := []ui2.MenuItem{}
133 for name in saved_presets() {
134 items << ui2.menu_check_item('preset:${name}', name, p.preset_name == name)
135 delete_items << ui2.menu_item('preset_delete:${name}', name)
136 }
137 if delete_items.len == 0 {
138 items << ui2.disabled(ui2.menu_item('presets_none', 'No saved presets'))
139 delete_items << ui2.disabled(ui2.menu_item('presets_none_delete', 'No saved presets'))
140 }
141 items << ui2.menu_separator()
142 items << ui2.submenu('Delete preset', delete_items)
143 return items
144}
145