From 6a7c8fcfd529ec31d274b347f4a591ad40366c09 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Sun, 15 Dec 2024 10:30:32 +0300 Subject: floating: Implement directional focus --- src/layout/floating.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'src/layout/floating.rs') diff --git a/src/layout/floating.rs b/src/layout/floating.rs index 417c7518..c0a56165 100644 --- a/src/layout/floating.rs +++ b/src/layout/floating.rs @@ -160,6 +160,10 @@ impl Data { self.recompute_logical_pos(); } + pub fn center(&self) -> Point { + self.logical_pos + self.size.downscale(2.) + } + #[cfg(test)] fn verify_invariants(&self) { let mut temp = *self; @@ -627,6 +631,66 @@ impl FloatingSpace { win.request_size(win_size, animate, None); } + fn focus_directional( + &mut self, + distance: impl Fn(Point, Point) -> f64, + ) -> bool { + let Some(active_id) = &self.active_window_id else { + return false; + }; + let active_idx = self.idx_of(active_id).unwrap(); + let center = self.data[active_idx].center(); + + let result = zip(&self.tiles, &self.data) + .filter(|(tile, _)| tile.window().id() != active_id) + .map(|(tile, data)| (tile, distance(center, data.center()))) + .filter(|(_, dist)| *dist > 0.) + .min_by(|(_, dist_a), (_, dist_b)| f64::total_cmp(dist_a, dist_b)); + if let Some((tile, _)) = result { + let id = tile.window().id().clone(); + self.activate_window(&id); + true + } else { + false + } + } + + pub fn focus_left(&mut self) -> bool { + self.focus_directional(|focus, other| focus.x - other.x) + } + + pub fn focus_right(&mut self) -> bool { + self.focus_directional(|focus, other| other.x - focus.x) + } + + pub fn focus_up(&mut self) -> bool { + self.focus_directional(|focus, other| focus.y - other.y) + } + + pub fn focus_down(&mut self) -> bool { + self.focus_directional(|focus, other| other.y - focus.y) + } + + pub fn focus_leftmost(&mut self) { + let result = self + .tiles_with_offsets() + .min_by(|(_, pos_a), (_, pos_b)| f64::total_cmp(&pos_a.x, &pos_b.x)); + if let Some((tile, _)) = result { + let id = tile.window().id().clone(); + self.activate_window(&id); + } + } + + pub fn focus_rightmost(&mut self) { + let result = self + .tiles_with_offsets() + .max_by(|(_, pos_a), (_, pos_b)| f64::total_cmp(&pos_a.x, &pos_b.x)); + if let Some((tile, _)) = result { + let id = tile.window().id().clone(); + self.activate_window(&id); + } + } + pub fn descendants_added(&mut self, id: &W::Id) -> bool { let Some(idx) = self.idx_of(id) else { return false; -- cgit