aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/input/handler.rs
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2021-02-09 17:36:37 +0100
committerGitHub <noreply@github.com>2021-02-09 17:36:37 +0100
commit14783c2b2c3bcd3b4b01e72c46d6af3ecbdd203c (patch)
tree7e25be8c93e85ec9fc03335f1993f319f713c614 /src/common/input/handler.rs
parentMerge pull request #158 from mosaic-org/add-files (diff)
downloadsl0ppij-14783c2b2c3bcd3b4b01e72c46d6af3ecbdd203c.tar.gz
sl0ppij-14783c2b2c3bcd3b4b01e72c46d6af3ecbdd203c.tar.xz
sl0ppij-14783c2b2c3bcd3b4b01e72c46d6af3ecbdd203c.zip
feat(ui): new keybindings and status bar (#160)
* work * feat(statusbar): new design and keybindings * fix(ipc): add missing const * style(fmt): rustfmt * style(input): remove accidental comment
Diffstat (limited to 'src/common/input/handler.rs')
-rw-r--r--src/common/input/handler.rs124
1 files changed, 91 insertions, 33 deletions
diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs
index b10e9df3..2a1c2c72 100644
--- a/src/common/input/handler.rs
+++ b/src/common/input/handler.rs
@@ -9,6 +9,7 @@ use crate::screen::ScreenInstruction;
use crate::wasm_vm::PluginInstruction;
use crate::CommandIsExecuting;
+use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;
use termion::input::TermReadEventsAndRaw;
@@ -16,6 +17,7 @@ use super::keybinds::key_to_action;
struct InputHandler {
mode: InputMode,
+ mode_is_persistent: bool,
os_input: Box<dyn OsApi>,
command_is_executing: CommandIsExecuting,
send_screen_instructions: SenderWithContext<ScreenInstruction>,
@@ -35,6 +37,7 @@ impl InputHandler {
) -> Self {
InputHandler {
mode: InputMode::Normal,
+ mode_is_persistent: false,
os_input,
command_is_executing,
send_screen_instructions,
@@ -68,12 +71,13 @@ impl InputHandler {
&key, raw_bytes, &self.mode, &keybinds,
));
//@@@ This is a hack until we dispatch more than one action per key stroke
- if entry_mode == InputMode::Command
- && self.mode == InputMode::Command
- {
+ if entry_mode == self.mode && !self.mode_is_persistent {
self.mode = InputMode::Normal;
update_state(&self.send_app_instructions, |_| AppState {
- input_mode: self.mode,
+ input_state: InputState {
+ mode: self.mode,
+ persistent: self.mode_is_persistent,
+ },
});
}
if should_break {
@@ -113,13 +117,28 @@ impl InputHandler {
}
Action::SwitchToMode(mode) => {
self.mode = mode;
+ if mode == InputMode::Normal {
+ self.mode_is_persistent = false;
+ }
update_state(&self.send_app_instructions, |_| AppState {
- input_mode: self.mode,
+ input_state: InputState {
+ mode: self.mode,
+ persistent: self.mode_is_persistent,
+ },
});
self.send_screen_instructions
.send(ScreenInstruction::Render)
.unwrap();
}
+ Action::TogglePersistentMode => {
+ self.mode_is_persistent = !self.mode_is_persistent;
+ update_state(&self.send_app_instructions, |_| AppState {
+ input_state: InputState {
+ mode: self.mode,
+ persistent: self.mode_is_persistent,
+ },
+ });
+ }
Action::Resize(direction) => {
let screen_instr = match direction {
super::actions::Direction::Left => ScreenInstruction::ResizeLeft,
@@ -224,6 +243,21 @@ impl InputHandler {
}
}
+#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)]
+pub struct InputState {
+ mode: InputMode,
+ persistent: bool,
+}
+
+impl Default for InputState {
+ fn default() -> InputState {
+ InputState {
+ mode: InputMode::Normal,
+ persistent: false,
+ }
+ }
+}
+
/// Dictates whether we're in command mode, persistent command mode, normal mode or exiting:
/// - Normal mode either writes characters to the terminal, or switches to command mode
/// using a particular key control
@@ -231,41 +265,65 @@ impl InputHandler {
/// back to normal mode
/// - Persistent command mode is the same as command mode, but doesn't return automatically to
/// normal mode
-#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, EnumIter)]
+#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, EnumIter, Serialize, Deserialize)]
pub enum InputMode {
Normal,
Command,
- CommandPersistent,
+ Resize,
+ Pane,
+ Tab,
+ Scroll,
Exiting,
}
-// FIXME: This should be auto-generated from the soon-to-be-added `get_default_keybinds`
-pub fn get_help(mode: &InputMode) -> Vec<String> {
- let command_help = vec![
- "<n/b/z> Split".into(),
- "<j/k/h/l> Resize".into(),
- "<p> Focus Next".into(),
- "<x> Close Pane".into(),
- "<q> Quit".into(),
- "<PgUp/PgDown> Scroll".into(),
- "<1> New Tab".into(),
- "<2/3> Move Tab".into(),
- "<4> Close Tab".into(),
- ];
- match mode {
- InputMode::Normal => vec!["<Ctrl-g> Command Mode".into()],
- InputMode::Command => [
- vec![
- "<Ctrl-g> Persistent Mode".into(),
- "<ESC> Normal Mode".into(),
- ],
- command_help,
- ]
- .concat(),
- InputMode::CommandPersistent => {
- [vec!["<ESC/Ctrl-g> Normal Mode".into()], command_help].concat()
+#[derive(Default, Debug, Clone, Serialize, Deserialize)]
+pub struct Help {
+ pub mode: InputMode,
+ pub mode_is_persistent: bool,
+ pub keybinds: Vec<(String, String)>, // <shortcut> => <shortcut description>
+}
+
+impl Default for InputMode {
+ fn default() -> InputMode {
+ InputMode::Normal
+ }
+}
+
+pub fn get_help(input_state: &InputState) -> Help {
+ let mut keybinds: Vec<(String, String)> = vec![];
+ match input_state.mode {
+ InputMode::Normal | InputMode::Command | InputMode::Exiting => {
+ keybinds.push((format!("p"), format!("Pane mode")));
+ keybinds.push((format!("t"), format!("Tab mode")));
+ keybinds.push((format!("r"), format!("Resize mode")));
}
- InputMode::Exiting => vec!["Bye from Mosaic!".into()],
+ InputMode::Resize => {
+ keybinds.push((format!("←↓↑→"), format!("resize pane")));
+ }
+ InputMode::Pane => {
+ keybinds.push((format!("←↓↑→"), format!("move focus")));
+ keybinds.push((format!("p"), format!("next pane")));
+ keybinds.push((format!("n"), format!("new pane")));
+ keybinds.push((format!("d"), format!("down split")));
+ keybinds.push((format!("r"), format!("right split")));
+ keybinds.push((format!("x"), format!("exit pane")));
+ keybinds.push((format!("f"), format!("fullscreen pane")));
+ }
+ InputMode::Tab => {
+ keybinds.push((format!("←↓↑→"), format!("move tab focus")));
+ keybinds.push((format!("n"), format!("new tab")));
+ keybinds.push((format!("x"), format!("exit tab")));
+ }
+ InputMode::Scroll => {
+ keybinds.push((format!("↓↑"), format!("scroll up/down")));
+ }
+ }
+ keybinds.push((format!("ESC"), format!("Back")));
+ keybinds.push((format!("q"), format!("Quit")));
+ Help {
+ mode: input_state.mode,
+ mode_is_persistent: input_state.persistent,
+ keybinds,
}
}