1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-27 12:23:46 +03:00

wezterm: add AdjustPaneSize keyassignment

This allows resizing a pane directionally.
This commit is contained in:
Wez Furlong 2020-09-23 20:26:25 -07:00
parent 86cc214344
commit 97ff359a18
3 changed files with 118 additions and 0 deletions

View File

@ -1850,6 +1850,19 @@ impl TermWindow {
self.assign_overlay_for_pane(pane.pane_id(), copy);
}
}
AdjustPaneSize(direction, amount) => {
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 tab_id = tab.tab_id();
if self.tab_state(tab_id).overlay.is_none() {
tab.adjust_pane_size(*direction, *amount);
}
}
};
Ok(())
}

View File

@ -70,6 +70,14 @@ pub struct SpawnCommand {
pub domain: SpawnTabDomain,
}
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
pub enum PaneDirection {
Up,
Down,
Left,
Right,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum KeyAssignment {
SpawnTab(SpawnTabDomain),
@ -110,6 +118,8 @@ pub enum KeyAssignment {
OpenLinkAtMouseCursor,
CompleteSelection,
CompleteSelectionOrOpenLinkAtMouseCursor,
AdjustPaneSize(PaneDirection, usize),
}
impl_lua_conversion!(KeyAssignment);
@ -265,6 +275,26 @@ impl InputMap {
..Default::default()
})
],
[
KeyModifiers::CTRL | KeyModifiers::ALT | KeyModifiers::SHIFT,
KeyCode::LeftArrow,
AdjustPaneSize(PaneDirection::Left, 1)
],
[
KeyModifiers::CTRL | KeyModifiers::ALT | KeyModifiers::SHIFT,
KeyCode::RightArrow,
AdjustPaneSize(PaneDirection::Right, 1)
],
[
KeyModifiers::CTRL | KeyModifiers::ALT | KeyModifiers::SHIFT,
KeyCode::UpArrow,
AdjustPaneSize(PaneDirection::Up, 1)
],
[
KeyModifiers::CTRL | KeyModifiers::ALT | KeyModifiers::SHIFT,
KeyCode::DownArrow,
AdjustPaneSize(PaneDirection::Down, 1)
],
);
#[cfg(target_os = "macos")]

View File

@ -1,3 +1,4 @@
use crate::keyassignment::PaneDirection;
use crate::mux::domain::DomainId;
use crate::mux::renderable::Renderable;
use crate::mux::Mux;
@ -423,6 +424,11 @@ impl Tab {
}
// Now cursor is looking at the split
self.adjust_node_at_cursor(&mut cursor, delta);
self.cascade_size_from_cursor(root, cursor);
}
fn adjust_node_at_cursor(&self, cursor: &mut Cursor, delta: isize) {
if let Ok(Some(node)) = cursor.node_mut() {
match node.direction {
SplitDirection::Horizontal => {
@ -451,7 +457,9 @@ impl Tab {
}
}
}
}
fn cascade_size_from_cursor(&self, mut root: RefMut<Option<Tree>>, mut cursor: Cursor) {
// Now we need to cascade this down to children
match cursor.preorder_next() {
Ok(c) => cursor = c,
@ -491,6 +499,73 @@ impl Tab {
}
}
/// Adjusts the size of the active pane in the specified direction
/// by the specified amount.
pub fn adjust_pane_size(&self, direction: PaneDirection, amount: usize) {
let active_index = *self.active.borrow();
let mut root = self.pane.borrow_mut();
let mut cursor = root.take().unwrap().cursor();
let mut index = 0;
// Position cursor on the active leaf
loop {
if cursor.is_leaf() {
if index == active_index {
// Found it
break;
}
index += 1;
}
match cursor.preorder_next() {
Ok(c) => cursor = c,
Err(c) => {
// Didn't find it
root.replace(c.tree());
return;
}
}
}
// We are on the active leaf.
// Now we go up until we find the parent node that is
// aligned with the desired direction.
let split_direction = match direction {
PaneDirection::Left | PaneDirection::Right => SplitDirection::Horizontal,
PaneDirection::Up | PaneDirection::Down => SplitDirection::Vertical,
};
loop {
let is_second = cursor.is_right();
match cursor.go_up() {
Ok(mut c) => {
if let Ok(Some(node)) = c.node_mut() {
if node.direction == split_direction {
let delta = match (is_second, direction) {
(false, PaneDirection::Up)
| (false, PaneDirection::Left)
| (true, PaneDirection::Down)
| (true, PaneDirection::Right) => amount as isize,
(false, PaneDirection::Down)
| (false, PaneDirection::Right)
| (true, PaneDirection::Up)
| (true, PaneDirection::Left) => -(amount as isize),
};
self.adjust_node_at_cursor(&mut c, delta);
self.cascade_size_from_cursor(root, c);
return;
}
}
cursor = c;
}
Err(c) => {
root.replace(c.tree());
return;
}
}
}
}
pub fn prune_dead_panes(&self) -> bool {
let mut dead_panes = vec![];