import os import ui2 import libvendetta import json2 // presets_dir is where saved options presets live: /vendetta/presets. fn presets_dir() string { return os.join_path(config_base(), 'vendetta', 'presets') } // saved_presets lists the saved preset names, sorted. fn saved_presets() []string { mut names := []string{} files := os.ls(presets_dir()) or { return names } for file in files { if file.ends_with('.json') { names << file.all_before_last('.json') } } names.sort() return names } // load_preset_file reads a saved preset's options. A missing or malformed // file is not a preset. fn load_preset_file(name string) ?map[string]string { text := os.read_file(os.join_path(presets_dir(), '${name}.json')) or { return none } return json2.decode[map[string]string](text, json2.DecoderOptions{}) or { none } } // preset_snapshot is the Options bar as mpv options: every flag the menus // mirror from mpv's observed state, written in the same form the menu // handlers write it, overlaid by the options configured through the dialog // or --mpv-opt. Saving captures this map; applying writes it back option by // option. Presentation pacing is vendetta's render call, not an mpv // property, so it is the one Options row a preset cannot carry. fn (p &Player) preset_snapshot() map[string]string { mut opts := map[string]string{ 'interpolation': if p.interpolation { 'yes' } else { 'no' } 'audio-pitch-correction': if p.pitch_correction { 'yes' } else { 'no' } 'audio-exclusive': if p.audio_exclusive { 'yes' } else { 'no' } 'vd-lavc-fast': if p.fast_decode { 'yes' } else { 'no' } 'correct-downscaling': if p.correct_downscaling { 'yes' } else { 'no' } 'linear-downscaling': if p.linear_downscaling { 'yes' } else { 'no' } 'sigmoid-upscaling': if p.sigmoid_upscaling { 'yes' } else { 'no' } 'deband': if p.deband { 'yes' } else { 'no' } 'dither': if p.dither { 'fruit' } else { 'no' } 'blend-subtitles': if p.blend_subtitles { 'yes' } else { 'no' } 'deinterlace': if p.deinterlace { 'yes' } else { 'no' } 'hwdec': libvendetta.hwdec_mode(p.hwdec) } // The sync and dropping choices are strings whose observed value mpv // reports once observed; before the first event arrives there is nothing // to capture, and an empty write would unset the option instead. if p.video_sync.len > 0 { opts['video-sync'] = p.video_sync } if p.framedrop.len > 0 { opts['framedrop'] = p.framedrop } // Options set by name through the dialog or --mpv-opt outrank the // observed mirrors: they are what the user configured outright. for name, value in p.mpv_opts { opts[name] = value } return opts } // save_preset writes the current options under name. The name may not be // empty or escape the presets directory, like the queue and theme names. fn (mut p Player) save_preset(name string) bool { mut clean := name.trim_space() if clean.len == 0 || clean.contains('/') || clean.contains('\\') || clean.contains('..') { p.notify('Preset name may not be empty or contain path separators.') return false } os.mkdir_all(presets_dir(), os.MkdirParams{}) or {} os.write_file(os.join_path(presets_dir(), '${clean}.json'), json2.encode(p.preset_snapshot(), json2.EncoderOptions{})) or { p.notify('Could not save the preset: ${err}') return false } p.preset_name = clean p.notify('Preset saved as ${clean}.') return true } // apply_preset loads a saved preset and writes its options into mpv one by // one. The preset names the whole configured set: options set earlier but // absent from it stop being carried, so switching presets replaces the set // instead of adding to it. Each write answers on its own (see option_written): // a rejected one is reported and kept out of the configured set, and the rest // go on. fn (mut p Player) apply_preset(name string) { opts := load_preset_file(name) or { p.notify('Could not load the preset ${name}.') return } former := p.mpv_opts.keys() for opt in former { if opt !in opts { p.unset_mpv_option(opt) } } p.preset_name = name // Cleared before the writes go out: a rejection can be reported at once. p.clear_notices() for opt, value in opts { // An empty value in the file unsets: the same rule as the dialog // and the command line. if value.len == 0 { p.unset_mpv_option(opt) continue } p.send_mpv_option(OptionWrite{ name: opt value: value preset: name }) } p.settings_changed() } // preset_menu_items builds the Options > Presets entries: saving the current // options under a name, then the saved presets (checked for the one applied // last) and the delete submenu, shaped like the theme menu. fn (p &Player) preset_menu_items() []ui2.MenuItem { mut items := []ui2.MenuItem{} items << ui2.menu_item('preset_save', 'Save current options...') mut delete_items := []ui2.MenuItem{} for name in saved_presets() { items << ui2.menu_check_item('preset:${name}', name, p.preset_name == name) delete_items << ui2.menu_item('preset_delete:${name}', name) } if delete_items.len == 0 { items << ui2.disabled(ui2.menu_item('presets_none', 'No saved presets')) delete_items << ui2.disabled(ui2.menu_item('presets_none_delete', 'No saved presets')) } items << ui2.menu_separator() items << ui2.submenu('Delete preset', delete_items) return items }