floating: Implement directional focus
Browse files at this commit6a7c8fcfd529ec31d274b347f4a591ad40366c09Parent: 14b10032 changed files +94 -30
- src/layout/floating.rs +64 -0
- src/layout/workspace.rs +30 -30
src/layout/floating.rsmodified+64 -0
| @@ -160,6 +160,10 @@ impl Data { | |||
| 160 | self.recompute_logical_pos(); | 160 | self.recompute_logical_pos(); |
| 161 | } | 161 | } |
| 162 | 162 | ||
| 163 | + pub fn center(&self) -> Point<f64, Logical> { | ||
| 164 | + self.logical_pos + self.size.downscale(2.) | ||
| 165 | + } | ||
| 166 | + | ||
| 163 | #[cfg(test)] | 167 | #[cfg(test)] |
| 164 | fn verify_invariants(&self) { | 168 | fn verify_invariants(&self) { |
| 165 | let mut temp = *self; | 169 | let mut temp = *self; |
| @@ -627,6 +631,66 @@ impl<W: LayoutElement> FloatingSpace<W> { | |||
| 627 | win.request_size(win_size, animate, None); | 631 | win.request_size(win_size, animate, None); |
| 628 | } | 632 | } |
| 629 | 633 | ||
| 634 | + fn focus_directional( | ||
| 635 | + &mut self, | ||
| 636 | + distance: impl Fn(Point<f64, Logical>, Point<f64, Logical>) -> f64, | ||
| 637 | + ) -> bool { | ||
| 638 | + let Some(active_id) = &self.active_window_id else { | ||
| 639 | + return false; | ||
| 640 | + }; | ||
| 641 | + let active_idx = self.idx_of(active_id).unwrap(); | ||
| 642 | + let center = self.data[active_idx].center(); | ||
| 643 | + | ||
| 644 | + let result = zip(&self.tiles, &self.data) | ||
| 645 | + .filter(|(tile, _)| tile.window().id() != active_id) | ||
| 646 | + .map(|(tile, data)| (tile, distance(center, data.center()))) | ||
| 647 | + .filter(|(_, dist)| *dist > 0.) | ||
| 648 | + .min_by(|(_, dist_a), (_, dist_b)| f64::total_cmp(dist_a, dist_b)); | ||
| 649 | + if let Some((tile, _)) = result { | ||
| 650 | + let id = tile.window().id().clone(); | ||
| 651 | + self.activate_window(&id); | ||
| 652 | + true | ||
| 653 | + } else { | ||
| 654 | + false | ||
| 655 | + } | ||
| 656 | + } | ||
| 657 | + | ||
| 658 | + pub fn focus_left(&mut self) -> bool { | ||
| 659 | + self.focus_directional(|focus, other| focus.x - other.x) | ||
| 660 | + } | ||
| 661 | + | ||
| 662 | + pub fn focus_right(&mut self) -> bool { | ||
| 663 | + self.focus_directional(|focus, other| other.x - focus.x) | ||
| 664 | + } | ||
| 665 | + | ||
| 666 | + pub fn focus_up(&mut self) -> bool { | ||
| 667 | + self.focus_directional(|focus, other| focus.y - other.y) | ||
| 668 | + } | ||
| 669 | + | ||
| 670 | + pub fn focus_down(&mut self) -> bool { | ||
| 671 | + self.focus_directional(|focus, other| other.y - focus.y) | ||
| 672 | + } | ||
| 673 | + | ||
| 674 | + pub fn focus_leftmost(&mut self) { | ||
| 675 | + let result = self | ||
| 676 | + .tiles_with_offsets() | ||
| 677 | + .min_by(|(_, pos_a), (_, pos_b)| f64::total_cmp(&pos_a.x, &pos_b.x)); | ||
| 678 | + if let Some((tile, _)) = result { | ||
| 679 | + let id = tile.window().id().clone(); | ||
| 680 | + self.activate_window(&id); | ||
| 681 | + } | ||
| 682 | + } | ||
| 683 | + | ||
| 684 | + pub fn focus_rightmost(&mut self) { | ||
| 685 | + let result = self | ||
| 686 | + .tiles_with_offsets() | ||
| 687 | + .max_by(|(_, pos_a), (_, pos_b)| f64::total_cmp(&pos_a.x, &pos_b.x)); | ||
| 688 | + if let Some((tile, _)) = result { | ||
| 689 | + let id = tile.window().id().clone(); | ||
| 690 | + self.activate_window(&id); | ||
| 691 | + } | ||
| 692 | + } | ||
| 693 | + | ||
| 630 | pub fn descendants_added(&mut self, id: &W::Id) -> bool { | 694 | pub fn descendants_added(&mut self, id: &W::Id) -> bool { |
| 631 | let Some(idx) = self.idx_of(id) else { | 695 | let Some(idx) = self.idx_of(id) else { |
| 632 | return false; | 696 | return false; |
src/layout/workspace.rsmodified+30 -30
| @@ -747,35 +747,35 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 747 | } | 747 | } |
| 748 | 748 | ||
| 749 | pub fn focus_left(&mut self) -> bool { | 749 | pub fn focus_left(&mut self) -> bool { |
| 750 | - // TODO | ||
| 751 | if self.floating_is_active { | 750 | if self.floating_is_active { |
| 752 | - return true; | 751 | + self.floating.focus_left() |
| 752 | + } else { | ||
| 753 | + self.scrolling.focus_left() | ||
| 753 | } | 754 | } |
| 754 | - self.scrolling.focus_left() | ||
| 755 | } | 755 | } |
| 756 | 756 | ||
| 757 | pub fn focus_right(&mut self) -> bool { | 757 | pub fn focus_right(&mut self) -> bool { |
| 758 | - // TODO | ||
| 759 | if self.floating_is_active { | 758 | if self.floating_is_active { |
| 760 | - return true; | 759 | + self.floating.focus_right() |
| 760 | + } else { | ||
| 761 | + self.scrolling.focus_right() | ||
| 761 | } | 762 | } |
| 762 | - self.scrolling.focus_right() | ||
| 763 | } | 763 | } |
| 764 | 764 | ||
| 765 | pub fn focus_column_first(&mut self) { | 765 | pub fn focus_column_first(&mut self) { |
| 766 | - // TODO | ||
| 767 | if self.floating_is_active { | 766 | if self.floating_is_active { |
| 768 | - return; | 767 | + self.floating.focus_leftmost(); |
| 768 | + } else { | ||
| 769 | + self.scrolling.focus_column_first(); | ||
| 769 | } | 770 | } |
| 770 | - self.scrolling.focus_column_first(); | ||
| 771 | } | 771 | } |
| 772 | 772 | ||
| 773 | pub fn focus_column_last(&mut self) { | 773 | pub fn focus_column_last(&mut self) { |
| 774 | - // TODO | ||
| 775 | if self.floating_is_active { | 774 | if self.floating_is_active { |
| 776 | - return; | 775 | + self.floating.focus_rightmost(); |
| 776 | + } else { | ||
| 777 | + self.scrolling.focus_column_last(); | ||
| 777 | } | 778 | } |
| 778 | - self.scrolling.focus_column_last(); | ||
| 779 | } | 779 | } |
| 780 | 780 | ||
| 781 | pub fn focus_column_right_or_first(&mut self) { | 781 | pub fn focus_column_right_or_first(&mut self) { |
| @@ -791,51 +791,51 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 791 | } | 791 | } |
| 792 | 792 | ||
| 793 | pub fn focus_down(&mut self) -> bool { | 793 | pub fn focus_down(&mut self) -> bool { |
| 794 | - // TODO | ||
| 795 | if self.floating_is_active { | 794 | if self.floating_is_active { |
| 796 | - return true; | 795 | + self.floating.focus_down() |
| 796 | + } else { | ||
| 797 | + self.scrolling.focus_down() | ||
| 797 | } | 798 | } |
| 798 | - self.scrolling.focus_down() | ||
| 799 | } | 799 | } |
| 800 | 800 | ||
| 801 | pub fn focus_up(&mut self) -> bool { | 801 | pub fn focus_up(&mut self) -> bool { |
| 802 | - // TODO | ||
| 803 | if self.floating_is_active { | 802 | if self.floating_is_active { |
| 804 | - return true; | 803 | + self.floating.focus_up() |
| 804 | + } else { | ||
| 805 | + self.scrolling.focus_up() | ||
| 805 | } | 806 | } |
| 806 | - self.scrolling.focus_up() | ||
| 807 | } | 807 | } |
| 808 | 808 | ||
| 809 | pub fn focus_down_or_left(&mut self) { | 809 | pub fn focus_down_or_left(&mut self) { |
| 810 | - // TODO | ||
| 811 | if self.floating_is_active { | 810 | if self.floating_is_active { |
| 812 | - return; | 811 | + self.floating.focus_down(); |
| 812 | + } else { | ||
| 813 | + self.scrolling.focus_down_or_left(); | ||
| 813 | } | 814 | } |
| 814 | - self.scrolling.focus_down_or_left(); | ||
| 815 | } | 815 | } |
| 816 | 816 | ||
| 817 | pub fn focus_down_or_right(&mut self) { | 817 | pub fn focus_down_or_right(&mut self) { |
| 818 | - // TODO | ||
| 819 | if self.floating_is_active { | 818 | if self.floating_is_active { |
| 820 | - return; | 819 | + self.floating.focus_down(); |
| 820 | + } else { | ||
| 821 | + self.scrolling.focus_down_or_right(); | ||
| 821 | } | 822 | } |
| 822 | - self.scrolling.focus_down_or_right(); | ||
| 823 | } | 823 | } |
| 824 | 824 | ||
| 825 | pub fn focus_up_or_left(&mut self) { | 825 | pub fn focus_up_or_left(&mut self) { |
| 826 | - // TODO | ||
| 827 | if self.floating_is_active { | 826 | if self.floating_is_active { |
| 828 | - return; | 827 | + self.floating.focus_up(); |
| 828 | + } else { | ||
| 829 | + self.scrolling.focus_up_or_left(); | ||
| 829 | } | 830 | } |
| 830 | - self.scrolling.focus_up_or_left(); | ||
| 831 | } | 831 | } |
| 832 | 832 | ||
| 833 | pub fn focus_up_or_right(&mut self) { | 833 | pub fn focus_up_or_right(&mut self) { |
| 834 | - // TODO | ||
| 835 | if self.floating_is_active { | 834 | if self.floating_is_active { |
| 836 | - return; | 835 | + self.floating.focus_up(); |
| 836 | + } else { | ||
| 837 | + self.scrolling.focus_up_or_right(); | ||
| 837 | } | 838 | } |
| 838 | - self.scrolling.focus_up_or_right(); | ||
| 839 | } | 839 | } |
| 840 | 840 | ||
| 841 | pub fn move_left(&mut self) -> bool { | 841 | pub fn move_left(&mut self) -> bool { |