From c0dd1ba1abb5796508d805129092ca7350d143e0 Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Wed, 15 Mar 2023 17:00:16 +0100 Subject: fix(screen): focus pane on screen edge when moving pane focus offtab (#2293) * fix(screen): focus pane on proper edge when switching tabs through pane switch * style(fmt): rustfmt --- .../src/panes/floating_panes/floating_pane_grid.rs | 46 +++++++++++++++++++++- zellij-server/src/panes/floating_panes/mod.rs | 13 ++++++ 2 files changed, 58 insertions(+), 1 deletion(-) (limited to 'zellij-server/src/panes/floating_panes') diff --git a/zellij-server/src/panes/floating_panes/floating_pane_grid.rs b/zellij-server/src/panes/floating_panes/floating_pane_grid.rs index afeecc8d2..b8472dfc5 100644 --- a/zellij-server/src/panes/floating_panes/floating_pane_grid.rs +++ b/zellij-server/src/panes/floating_panes/floating_pane_grid.rs @@ -2,7 +2,7 @@ use crate::tab::{MIN_TERMINAL_HEIGHT, MIN_TERMINAL_WIDTH}; use crate::{panes::PaneId, tab::Pane}; use std::cmp::Ordering; use std::collections::HashMap; -use zellij_utils::data::ResizeStrategy; +use zellij_utils::data::{Direction, ResizeStrategy}; use zellij_utils::errors::prelude::*; use zellij_utils::pane_size::{Dimension, PaneGeom, Size, Viewport}; @@ -625,6 +625,50 @@ impl<'a> FloatingPaneGrid<'a> { .copied(); next_index } + pub fn pane_id_on_edge(&self, direction: Direction) -> Option { + let panes = self.panes.borrow(); + let panes: Vec<(PaneId, &&mut Box)> = panes + .iter() + .filter(|(_, p)| p.selectable()) + .map(|(p_id, p)| (*p_id, p)) + .collect(); + let next_index = panes + .iter() + .enumerate() + .max_by(|(_, (_, a)), (_, (_, b))| match direction { + Direction::Left => { + let x_comparison = a.x().cmp(&b.x()); + match x_comparison { + Ordering::Equal => a.y().cmp(&b.y()), + _ => x_comparison, + } + }, + Direction::Right => { + let x_comparison = b.x().cmp(&a.x()); + match x_comparison { + Ordering::Equal => a.y().cmp(&b.y()), + _ => x_comparison, + } + }, + Direction::Up => { + let y_comparison = a.y().cmp(&b.y()); + match y_comparison { + Ordering::Equal => a.x().cmp(&b.x()), + _ => y_comparison, + } + }, + Direction::Down => { + let y_comparison = b.y().cmp(&a.y()); + match y_comparison { + Ordering::Equal => b.x().cmp(&a.x()), + _ => y_comparison, + } + }, + }) + .map(|(_, (pid, _))| pid) + .copied(); + next_index + } pub fn next_selectable_pane_id_below(&self, current_pane_id: &PaneId) -> Option { let panes = self.panes.borrow(); let current_pane = panes.get(current_pane_id)?; diff --git a/zellij-server/src/panes/floating_panes/mod.rs b/zellij-server/src/panes/floating_panes/mod.rs index 765f0196d..84e85431f 100644 --- a/zellij-server/src/panes/floating_panes/mod.rs +++ b/zellij-server/src/panes/floating_panes/mod.rs @@ -529,6 +529,19 @@ impl FloatingPanes { } Ok(false) } + pub fn focus_pane_on_edge(&mut self, direction: Direction, client_id: ClientId) { + let display_area = *self.display_area.borrow(); + let viewport = *self.viewport.borrow(); + let mut floating_pane_grid = FloatingPaneGrid::new( + &mut self.panes, + &mut self.desired_pane_positions, + display_area, + viewport, + ); + let pane_id = floating_pane_grid.pane_id_on_edge(direction).unwrap(); + self.focus_pane(pane_id, client_id); + self.set_force_render(); + } pub fn move_active_pane_down(&mut self, client_id: ClientId) { let display_area = *self.display_area.borrow(); -- cgit v1.2.3