vendetta / src / presets.v
141 lines · 134 sloc · 5.3 KB
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. Each write answers on its own (see option_written):
91// a rejected one is reported and kept out of the configured set, and the rest
92// go on.
93fn (mut p Player) apply_preset(name string) {
94 opts := load_preset_file(name) or {
95 p.notify('Could not load the preset ${name}.')
96 return
97 }
98 former := p.mpv_opts.keys()
99 for opt in former {
100 if opt !in opts {
101 p.unset_mpv_option(opt)
102 }
103 }
104 p.preset_name = name
105 // Cleared before the writes go out: a rejection can be reported at once.
106 p.clear_notices()
107 for opt, value in opts {
108 // An empty value in the file unsets: the same rule as the dialog
109 // and the command line.
110 if value.len == 0 {
111 p.unset_mpv_option(opt)
112 continue
113 }
114 p.send_mpv_option(OptionWrite{
115 name: opt
116 value: value
117 preset: name
118 })
119 }
120 p.settings_changed()
121}
122
123// preset_menu_items builds the Options > Presets entries: saving the current
124// options under a name, then the saved presets (checked for the one applied
125// last) and the delete submenu, shaped like the theme menu.
126fn (p &Player) preset_menu_items() []ui2.MenuItem {
127 mut items := []ui2.MenuItem{}
128 items << ui2.menu_item('preset_save', 'Save current options...')
129 mut delete_items := []ui2.MenuItem{}
130 for name in saved_presets() {
131 items << ui2.menu_check_item('preset:${name}', name, p.preset_name == name)
132 delete_items << ui2.menu_item('preset_delete:${name}', name)
133 }
134 if delete_items.len == 0 {
135 items << ui2.disabled(ui2.menu_item('presets_none', 'No saved presets'))
136 delete_items << ui2.disabled(ui2.menu_item('presets_none_delete', 'No saved presets'))
137 }
138 items << ui2.menu_separator()
139 items << ui2.submenu('Delete preset', delete_items)
140 return items
141}
142