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 +++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'zellij-server/src/panes/floating_panes/floating_pane_grid.rs') 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)?; -- cgit v1.2.3