1
//! The Options dialog: the theme, the keymap and the colour depth, picked from lists, and the
2
//! text of their overrides.
3

            
4
use forms::{Field as Descriptor, FormValues, Kind};
5
use tvui::{ColorDepth, Theme};
6

            
7
use super::{Form, FormKind};
8
use crate::event::Intent;
9
use crate::keymap::Keys;
10
use crate::modal::Spec;
11
use crate::widgets::{SelectOption, Widget};
12

            
13
/// The buttons of the Options dialog: OK saves and closes, Apply saves, Cancel puts back.
14
pub const OPTIONS_BUTTONS: [Spec; 3] = [
15
    ("~O~K", Intent::Submit),
16
    ("~C~ancel", Intent::CloseTopmost),
17
    ("A~p~ply", Intent::ApplyOptions),
18
];
19

            
20
const fn choice(key: &'static str, label: &'static str) -> Descriptor {
21
    Descriptor {
22
        key,
23
        label,
24
        kind: Kind::Choice {
25
            options: &[],
26
            default: None,
27
        },
28
        required: true,
29
    }
30
}
31

            
32
const fn text_area(key: &'static str, label: &'static str) -> Descriptor {
33
    Descriptor {
34
        key,
35
        label,
36
        kind: Kind::Text {
37
            multiline: true,
38
            suggest: None,
39
        },
40
        required: false,
41
    }
42
}
43

            
44
pub(super) const OPTIONS: forms::Form = forms::Form {
45
    name: "options",
46
    title: "Options",
47
    fields: &[
48
        choice("theme", "Theme"),
49
        choice("keymap", "Keymap"),
50
        choice("colors", "Colours"),
51
        text_area("theme-overrides", "Theme overrides"),
52
        text_area("keymap-overrides", "Keymap overrides"),
53
    ],
54
    action: forms::Action::Mutation,
55
};
56

            
57
32
fn colour_names() -> impl Iterator<Item = &'static str> {
58
32
    let depths = [ColorDepth::Rgb, ColorDepth::Indexed256, ColorDepth::Ansi16];
59
32
    std::iter::once("auto").chain(depths.map(ColorDepth::name))
60
32
}
61

            
62
96
fn options_of<'a>(names: impl Iterator<Item = &'a str>) -> Vec<SelectOption> {
63
96
    names
64
        .map(|name| SelectOption {
65
288
            id: name.to_string(),
66
288
            label: name.to_string(),
67
288
        })
68
        .collect()
69
96
}
70

            
71
impl Form {
72
    /// The Options dialog showing `shown`, the settings by their form keys.
73
    #[must_use]
74
32
    pub fn options(shown: &FormValues) -> Self {
75
32
        let mut form = Self::from_descriptor(&OPTIONS, FormKind::Options);
76
32
        let lists = [
77
32
            ("theme", options_of(Theme::preset_names())),
78
32
            ("keymap", options_of(Keys::preset_names())),
79
32
            ("colors", options_of(colour_names())),
80
32
        ];
81
32
        for (key, options) in lists {
82
96
            if let Some(select) = form.select_mut(key) {
83
96
                select.set_options(options);
84
            }
85
        }
86
32
        form.load(shown);
87
32
        form
88
32
    }
89

            
90
    /// Makes the select `key` offer only `id`, marked as set by `flag`.
91
5
    pub fn lock_choice(&mut self, key: &str, id: &str, flag: &str) {
92
5
        if let Some(select) = self.select_mut(key) {
93
5
            select.set_options(vec![SelectOption {
94
5
                id: id.to_string(),
95
5
                label: format!("{id} (set by {flag})"),
96
5
            }]);
97
        }
98
5
    }
99

            
100
101
    fn select_mut(&mut self, key: &str) -> Option<&mut crate::widgets::SelectWidget> {
101
101
        self.fields
102
101
            .iter_mut()
103
202
            .find(|field| field.key == Some(key))
104
101
            .and_then(|field| match &mut field.widget {
105
101
                Widget::Select(select) => Some(select),
106
                _ => None,
107
101
            })
108
101
    }
109

            
110
    /// The buttons the dialog has: the Options dialog's own three, else OK and Close.
111
    #[must_use]
112
5146
    pub fn buttons(&self) -> &'static [Spec] {
113
5146
        match self.kind {
114
616
            FormKind::Options => &OPTIONS_BUTTONS,
115
4530
            _ => &super::BUTTONS,
116
        }
117
5146
    }
118
}