aboutsummaryrefslogtreecommitdiffstats
path: root/default-plugins/configuration/src/main.rs
blob: 6a9c6fc97efe8585d4eb6ef877b5944627b305c0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
mod presets;
mod presets_screen;
mod rebind_leaders_screen;
mod ui_components;

use zellij_tile::prelude::*;

use presets_screen::PresetsScreen;
use rebind_leaders_screen::RebindLeadersScreen;
use ui_components::top_tab_menu;

use std::collections::BTreeMap;

pub static UI_SIZE: usize = 15;
pub static WIDTH_BREAKPOINTS: (usize, usize) = (62, 35);
pub static POSSIBLE_MODIFIERS: [KeyModifier; 4] = [
    KeyModifier::Ctrl,
    KeyModifier::Alt,
    KeyModifier::Super,
    KeyModifier::Shift,
];

#[derive(Debug)]
enum Screen {
    RebindLeaders(RebindLeadersScreen),
    Presets(PresetsScreen),
}

impl Screen {
    pub fn reset_state(&mut self, is_setup_wizard: bool) {
        if is_setup_wizard {
            Screen::new_reset_keybindings_screen(Some(0));
        } else {
            match self {
                Screen::RebindLeaders(r) => {
                    let notification = r.drain_notification();
                    *r = Default::default();
                    r.set_notification(notification);
                },
                Screen::Presets(r) => {
                    let notification = r.drain_notification();
                    *r = Default::default();
                    r.set_notification(notification);
                },
            }
        }
    }
    pub fn update_mode_info(&mut self, latest_mode_info: ModeInfo) {
        match self {
            Screen::RebindLeaders(r) => r.update_mode_info(latest_mode_info),
            Screen::Presets(r) => r.update_mode_info(latest_mode_info),
        }
    }
}

impl Default for Screen {
    fn default() -> Self {
        Screen::RebindLeaders(Default::default())
    }
}

impl Screen {
    pub fn new_reset_keybindings_screen(selected_index: Option<usize>) -> Self {
        Screen::Presets(PresetsScreen::new(selected_index))
    }
}

struct State {
    notification: Option<String>,
    is_setup_wizard: bool,
    ui_size: usize,
    current_screen: Screen,
    latest_mode_info: Option<ModeInfo>,
    colors: Styling,
}

impl Default for State {
    fn default() -> Self {
        State {
            notification: None,
            is_setup_wizard: false,
            ui_size: UI_SIZE,
            current_screen: Screen::default(),
            latest_mode_info: None,
            colors: Palette::default().into(),
        }
    }
}

register_plugin!(State);

impl ZellijPlugin for State {
    fn load(&mut self, configuration: BTreeMap<String, String>) {
        self.is_setup_wizard = configuration
            .get("is_setup_wizard")
            .map(|v| v == "true")
            .unwrap_or(false);
        subscribe(&[
            EventType::Key,
            EventType::FailedToWriteConfigToDisk,
            EventType::ModeUpdate,
        ]);
        let own_plugin_id = get_plugin_ids().plugin_id;
        if self.is_setup_wizard {
            self.ui_size = 18;
            self.current_screen = Screen::new_reset_keybindings_screen(Some(0));
            rename_plugin_pane(own_plugin_id, "First Run Setup Wizard (Step 1/1)");
            resize_focused_pane(Resize::Increase);
            resize_focused_pane(Resize::Increase);
            resize_focused_pane(Resize::Increase);
        } else {
            rename_plugin_pane(own_plugin_id, "Configuration");
        }
    }
    fn update(&mut self, event: Event) -> bool {
        let mut should_render = false;
        match event {
            Event::ModeUpdate(mode_info) => {
                self.colors = mode_info.style.colors;
                if self.latest_mode_info.as_ref().and_then(|l| l.base_mode) != mode_info.base_mode {
                    // reset ui state
                    self.current_screen.reset_state(self.is_setup_wizard);
                }
                self.latest_mode_info = Some(mode_info.clone());
                self.current_screen.update_mode_info(mode_info.clone());
                should_render = true;
            },
            Event::Key(key) => {
                if self.notification.is_some() {
                    self.notification = None;
                    should_render = true;
                } else if key.bare_key == BareKey::Tab
                    && key.has_no_modifiers()
                    && !self.is_setup_wizard
                {
                    self.switch_screen();
                    should_render = true;
                } else {
                    should_render = match &mut self.current_screen {
                        Screen::RebindLeaders(rebind_leaders_screen) => {
                            rebind_leaders_screen.handle_key(key)
                        },
                        Screen::Presets(presets_screen) => {
                            if self.is_setup_wizard {
                                presets_screen.handle_setup_wizard_key(key)
                            } else {
                                presets_screen.handle_presets_key(key)
                            }
                        },
                    };
                }
            },
            Event::FailedToWriteConfigToDisk(config_file_path) => {
                match config_file_path {
                    Some(failed_path) => {
                        self.notification = Some(format!(
                            "Failed to write configuration file: {}",
                            failed_path
                        ));
                    },
                    None => {
                        self.notification = Some(format!("Failed to write configuration file."));
                    },
                }
                should_render = true;
            },
            _ => (),
        };
        should_render
    }
    fn render(&mut self, rows: usize, cols: usize) {
        let notification = self.notification.clone();
        if self.is_in_main_screen() {
            top_tab_menu(cols, &self.current_screen, &self.colors);
        }
        match &mut self.current_screen {
            Screen::RebindLeaders(rebind_leaders_screen) => {
                rebind_leaders_screen.render(rows, cols, self.ui_size, &notification);
            },
            Screen::Presets(presets_screen) => {
                if self.is_setup_wizard {
                    presets_screen.render_setup_wizard_screen(
                        rows,
                        cols,
                        self.ui_size,
                        &notification,
                    )
                } else {
                    presets_screen.render_reset_keybindings_screen(
                        rows,
                        cols,
                        self.ui_size,
                        &notification,
                    )
                }
            },
        };
    }
}

impl State {
    fn is_in_main_screen(&self) -> bool {
        match &self.current_screen {
            Screen::RebindLeaders(_) => true,
            Screen::Presets(presets_screen) => {
                if self.is_setup_wizard || presets_screen.rebinding_leaders() {
                    false
                } else {
                    true
                }
            },
        }
    }
    fn switch_screen(&mut self) {
        match &self.current_screen {
            Screen::RebindLeaders(_) => {
                self.current_screen = Screen::Presets(Default::default());
            },
            Screen::Presets(_) => {
                self.current_screen = Screen::RebindLeaders(
                    RebindLeadersScreen::default().with_mode_info(self.latest_mode_info.clone()),
                );
            },
        }
        if let Some(mode_info) = &self.latest_mode_info {
            self.current_screen.update_mode_info(mode_info.clone());
        }
    }
}