From ef072255dde5762f290e589d9a1c827b0da0d709 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 3 Jan 2020 09:10:11 -0800 Subject: [PATCH] Process shift+pageup/down at the gui layer Refs: https://github.com/wez/wezterm/issues/106 --- docs/config/keys.markdown | 3 +++ src/config/keys.rs | 7 +++++++ src/frontend/gui/termwindow.rs | 21 +++++++++++++++++++++ src/keyassignment.rs | 3 +++ 4 files changed, 34 insertions(+) diff --git a/docs/config/keys.markdown b/docs/config/keys.markdown index 98d202ce2..40ed32b2f 100644 --- a/docs/config/keys.markdown +++ b/docs/config/keys.markdown @@ -46,6 +46,8 @@ The default key bindings are: | `SUPER+SHIFT` | `]` | `ActivateTabRelative(1)` | | `CTRL+SHIFT` | `PAGEUP` | `MoveTabRelative(-1)` | | `CTRL+SHIFT` | `PAGEDOWN` | `MoveTabRelative(1)` | +| `SHIFT` | `PAGEUP` | `ScrollByPage(-1)` | +| `SHIFT` | `PAGEDOWN` | `ScrollByPage(1)` | These can be overridden using the `keys` section in your `~/.wezterm.toml` config file. For example, you can disable a default assignment like this: @@ -113,6 +115,7 @@ specified via the `arg` key; see examples below. | `CloseCurrentTab` | Equivalent to clicking the `x` on the window title bar to close it: Closes the current tab. If that was the last tab, closes that window. If that was the last window, wezterm terminates. | | `MoveTabRelative` | Move the current tab relative to its peers. The `arg` value specifies an offset. eg: `-1` moves the tab to the left of the current tab, while `1` moves the tab to the right. | | `MoveTab` | Move the tab so that it has the index specified by the `arg` value. eg: `0` moves the tab to be leftmost, while `1` moves the tab so that it is second tab from the left, and so on. | +| `ScrollByPage` | Adjusts the scroll position by the number of pages specified by the `arg` value. Negative values scroll upwards, while positive values scroll downwards. | Example: diff --git a/src/config/keys.rs b/src/config/keys.rs index 10301e2da..0aa8fc6e3 100644 --- a/src/config/keys.rs +++ b/src/config/keys.rs @@ -76,6 +76,12 @@ impl std::convert::TryInto for &Key { .ok_or_else(|| anyhow!("missing arg for {:?}", self))? .parse()?, ), + KeyAction::ScrollByPage => KeyAssignment::ScrollByPage( + self.arg + .as_ref() + .ok_or_else(|| anyhow!("missing arg for {:?}", self))? + .parse()?, + ), }) } } @@ -102,6 +108,7 @@ pub enum KeyAction { ReloadConfiguration, MoveTab, MoveTabRelative, + ScrollByPage, } fn de_keycode<'de, D>(deserializer: D) -> Result diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index bbd7f0ef7..f4513e56a 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -1087,6 +1087,26 @@ impl TermWindow { Ok(()) } + fn scroll_by_page(&mut self, amount: isize) -> anyhow::Result<()> { + let mux = Mux::get().unwrap(); + let tab = match mux.get_active_tab_for_window(self.mux_window_id) { + Some(tab) => tab, + None => return Ok(()), + }; + let mut render = tab.renderer(); + let dims = render.get_dimensions(); + let position = self + .get_viewport(tab.tab_id()) + .unwrap_or(dims.physical_top) + .saturating_add(amount * dims.viewport_rows as isize); + self.set_viewport(tab.tab_id(), Some(position), dims); + render.make_all_lines_dirty(); + if let Some(win) = self.window.as_ref() { + win.invalidate(); + } + Ok(()) + } + fn move_tab_relative(&mut self, delta: isize) -> anyhow::Result<()> { let mux = Mux::get().unwrap(); let window = mux @@ -1211,6 +1231,7 @@ impl TermWindow { ReloadConfiguration => crate::config::reload(), MoveTab(n) => self.move_tab(*n)?, MoveTabRelative(n) => self.move_tab_relative(*n)?, + ScrollByPage(n) => self.scroll_by_page(*n)?, }; Ok(()) } diff --git a/src/keyassignment.rs b/src/keyassignment.rs index 82fa13c8e..e592a6cce 100644 --- a/src/keyassignment.rs +++ b/src/keyassignment.rs @@ -37,6 +37,7 @@ pub enum KeyAssignment { ReloadConfiguration, MoveTabRelative(isize), MoveTab(usize), + ScrollByPage(isize), } pub struct KeyMap(HashMap<(KeyCode, KeyModifiers), KeyAssignment>); @@ -143,6 +144,8 @@ impl KeyMap { [ctrl_shift, KeyCode::Char('R'), ReloadConfiguration], [ctrl_shift, KeyCode::PageUp, MoveTabRelative(-1)], [ctrl_shift, KeyCode::PageDown, MoveTabRelative(1)], + [KeyModifiers::SHIFT, KeyCode::PageUp, ScrollByPage(-1)], + [KeyModifiers::SHIFT, KeyCode::PageDown, ScrollByPage(1)], ); Self(map)