Refactor and simplify new view offset calculation
Browse files at this commitfb636ef98d0f7011724d5ddeb7ad3bc1686bf422Parent: 6147a311 changed file +93 -109
- src/layout/workspace.rs +93 -109
src/layout/workspace.rsmodified+93 -109
| @@ -660,8 +660,9 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 660 | }); | 660 | }); |
| 661 | } | 661 | } |
| 662 | 662 | ||
| 663 | - fn compute_new_view_offset_for_column(&self, current_x: f64, idx: usize) -> f64 { | 663 | + fn compute_new_view_offset_for_column_fit(&self, current_x: f64, idx: usize) -> f64 { |
| 664 | - if self.columns[idx].is_fullscreen { | 664 | + let col = &self.columns[idx]; |
| 665 | + if col.is_fullscreen { | ||
| 665 | return 0.; | 666 | return 0.; |
| 666 | } | 667 | } |
| 667 | 668 | ||
| @@ -677,7 +678,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 677 | final_x + self.working_area.loc.x, | 678 | final_x + self.working_area.loc.x, |
| 678 | self.working_area.size.w, | 679 | self.working_area.size.w, |
| 679 | new_col_x, | 680 | new_col_x, |
| 680 | - self.columns[idx].width(), | 681 | + col.width(), |
| 681 | self.options.gaps, | 682 | self.options.gaps, |
| 682 | ); | 683 | ); |
| 683 | 684 | ||
| @@ -685,6 +686,71 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 685 | new_offset - self.working_area.loc.x | 686 | new_offset - self.working_area.loc.x |
| 686 | } | 687 | } |
| 687 | 688 | ||
| 689 | + fn compute_new_view_offset_for_column_centered(&self, current_x: f64, idx: usize) -> f64 { | ||
| 690 | + let col = &self.columns[idx]; | ||
| 691 | + if col.is_fullscreen { | ||
| 692 | + return self.compute_new_view_offset_for_column_fit(current_x, idx); | ||
| 693 | + } | ||
| 694 | + | ||
| 695 | + let width = col.width(); | ||
| 696 | + | ||
| 697 | + // Columns wider than the view are left-aligned (the fit code can deal with that). | ||
| 698 | + if self.working_area.size.w <= width { | ||
| 699 | + return self.compute_new_view_offset_for_column_fit(current_x, idx); | ||
| 700 | + } | ||
| 701 | + | ||
| 702 | + -(self.working_area.size.w - width) / 2. - self.working_area.loc.x | ||
| 703 | + } | ||
| 704 | + | ||
| 705 | + fn compute_new_view_offset_for_column( | ||
| 706 | + &self, | ||
| 707 | + current_x: f64, | ||
| 708 | + idx: usize, | ||
| 709 | + prev_idx: Option<usize>, | ||
| 710 | + ) -> f64 { | ||
| 711 | + match self.options.center_focused_column { | ||
| 712 | + CenterFocusedColumn::Always => { | ||
| 713 | + self.compute_new_view_offset_for_column_centered(current_x, idx) | ||
| 714 | + } | ||
| 715 | + CenterFocusedColumn::OnOverflow => { | ||
| 716 | + let Some(prev_idx) = prev_idx else { | ||
| 717 | + return self.compute_new_view_offset_for_column_fit(current_x, idx); | ||
| 718 | + }; | ||
| 719 | + | ||
| 720 | + // Always take the left or right neighbor of the target as the source. | ||
| 721 | + let source_idx = if prev_idx > idx { | ||
| 722 | + min(idx + 1, self.columns.len() - 1) | ||
| 723 | + } else { | ||
| 724 | + idx.saturating_sub(1) | ||
| 725 | + }; | ||
| 726 | + | ||
| 727 | + let source_x = self.column_x(source_idx); | ||
| 728 | + let source_width = self.columns[source_idx].width(); | ||
| 729 | + | ||
| 730 | + let target_x = self.column_x(idx); | ||
| 731 | + let target_width = self.columns[idx].width(); | ||
| 732 | + | ||
| 733 | + let total_width = if source_x < target_x { | ||
| 734 | + // Source is left from target. | ||
| 735 | + target_x - source_x + target_width | ||
| 736 | + } else { | ||
| 737 | + // Source is right from target. | ||
| 738 | + source_x - target_x + source_width | ||
| 739 | + } + self.options.gaps * 2.; | ||
| 740 | + | ||
| 741 | + // If it fits together, do a normal animation, otherwise center the new column. | ||
| 742 | + if total_width <= self.working_area.size.w { | ||
| 743 | + self.compute_new_view_offset_for_column_fit(current_x, idx) | ||
| 744 | + } else { | ||
| 745 | + self.compute_new_view_offset_for_column_centered(current_x, idx) | ||
| 746 | + } | ||
| 747 | + } | ||
| 748 | + CenterFocusedColumn::Never => { | ||
| 749 | + self.compute_new_view_offset_for_column_fit(current_x, idx) | ||
| 750 | + } | ||
| 751 | + } | ||
| 752 | + } | ||
| 753 | + | ||
| 688 | fn animate_view_offset(&mut self, current_x: f64, idx: usize, new_view_offset: f64) { | 754 | fn animate_view_offset(&mut self, current_x: f64, idx: usize, new_view_offset: f64) { |
| 689 | self.animate_view_offset_with_config( | 755 | self.animate_view_offset_with_config( |
| 690 | current_x, | 756 | current_x, |
| @@ -728,44 +794,24 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 728 | ))); | 794 | ))); |
| 729 | } | 795 | } |
| 730 | 796 | ||
| 731 | - fn animate_view_offset_to_column_fit( | 797 | + fn animate_view_offset_to_column_centered( |
| 732 | &mut self, | 798 | &mut self, |
| 733 | current_x: f64, | 799 | current_x: f64, |
| 734 | idx: usize, | 800 | idx: usize, |
| 735 | config: niri_config::Animation, | 801 | config: niri_config::Animation, |
| 736 | ) { | 802 | ) { |
| 737 | - let new_view_offset = self.compute_new_view_offset_for_column(current_x, idx); | 803 | + let new_view_offset = self.compute_new_view_offset_for_column_centered(current_x, idx); |
| 738 | self.animate_view_offset_with_config(current_x, idx, new_view_offset, config); | 804 | self.animate_view_offset_with_config(current_x, idx, new_view_offset, config); |
| 739 | } | 805 | } |
| 740 | 806 | ||
| 741 | - fn animate_view_offset_to_column_centered( | 807 | + fn animate_view_offset_to_column_with_config( |
| 742 | &mut self, | 808 | &mut self, |
| 743 | current_x: f64, | 809 | current_x: f64, |
| 744 | idx: usize, | 810 | idx: usize, |
| 811 | + prev_idx: Option<usize>, | ||
| 745 | config: niri_config::Animation, | 812 | config: niri_config::Animation, |
| 746 | ) { | 813 | ) { |
| 747 | - if self.columns.is_empty() { | 814 | + let new_view_offset = self.compute_new_view_offset_for_column(current_x, idx, prev_idx); |
| 748 | - return; | ||
| 749 | - } | ||
| 750 | - | ||
| 751 | - let col = &self.columns[idx]; | ||
| 752 | - if col.is_fullscreen { | ||
| 753 | - self.animate_view_offset_to_column_fit(current_x, idx, config); | ||
| 754 | - return; | ||
| 755 | - } | ||
| 756 | - | ||
| 757 | - let width = col.width(); | ||
| 758 | - | ||
| 759 | - // If the column is wider than the working area, then on commit it will be shifted to left | ||
| 760 | - // edge alignment by the usual positioning code, so there's no use in trying to center it | ||
| 761 | - // here. | ||
| 762 | - if self.working_area.size.w <= width { | ||
| 763 | - self.animate_view_offset_to_column_fit(current_x, idx, config); | ||
| 764 | - return; | ||
| 765 | - } | ||
| 766 | - | ||
| 767 | - let new_view_offset = -(self.working_area.size.w - width) / 2. - self.working_area.loc.x; | ||
| 768 | - | ||
| 769 | self.animate_view_offset_with_config(current_x, idx, new_view_offset, config); | 815 | self.animate_view_offset_with_config(current_x, idx, new_view_offset, config); |
| 770 | } | 816 | } |
| 771 | 817 | ||
| @@ -783,57 +829,6 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 783 | ) | 829 | ) |
| 784 | } | 830 | } |
| 785 | 831 | ||
| 786 | - fn animate_view_offset_to_column_with_config( | ||
| 787 | - &mut self, | ||
| 788 | - current_x: f64, | ||
| 789 | - idx: usize, | ||
| 790 | - prev_idx: Option<usize>, | ||
| 791 | - config: niri_config::Animation, | ||
| 792 | - ) { | ||
| 793 | - match self.options.center_focused_column { | ||
| 794 | - CenterFocusedColumn::Always => { | ||
| 795 | - self.animate_view_offset_to_column_centered(current_x, idx, config) | ||
| 796 | - } | ||
| 797 | - CenterFocusedColumn::OnOverflow => { | ||
| 798 | - let Some(prev_idx) = prev_idx else { | ||
| 799 | - self.animate_view_offset_to_column_fit(current_x, idx, config); | ||
| 800 | - return; | ||
| 801 | - }; | ||
| 802 | - | ||
| 803 | - // Always take the left or right neighbor of the target as the source. | ||
| 804 | - let source_idx = if prev_idx > idx { | ||
| 805 | - min(idx + 1, self.columns.len() - 1) | ||
| 806 | - } else { | ||
| 807 | - idx.saturating_sub(1) | ||
| 808 | - }; | ||
| 809 | - | ||
| 810 | - let source_x = self.column_x(source_idx); | ||
| 811 | - let source_width = self.columns[source_idx].width(); | ||
| 812 | - | ||
| 813 | - let target_x = self.column_x(idx); | ||
| 814 | - let target_width = self.columns[idx].width(); | ||
| 815 | - | ||
| 816 | - let total_width = if source_x < target_x { | ||
| 817 | - // Source is left from target. | ||
| 818 | - target_x - source_x + target_width | ||
| 819 | - } else { | ||
| 820 | - // Source is right from target. | ||
| 821 | - source_x - target_x + source_width | ||
| 822 | - } + self.options.gaps * 2.; | ||
| 823 | - | ||
| 824 | - // If it fits together, do a normal animation, otherwise center the new column. | ||
| 825 | - if total_width <= self.working_area.size.w { | ||
| 826 | - self.animate_view_offset_to_column_fit(current_x, idx, config); | ||
| 827 | - } else { | ||
| 828 | - self.animate_view_offset_to_column_centered(current_x, idx, config); | ||
| 829 | - } | ||
| 830 | - } | ||
| 831 | - CenterFocusedColumn::Never => { | ||
| 832 | - self.animate_view_offset_to_column_fit(current_x, idx, config) | ||
| 833 | - } | ||
| 834 | - }; | ||
| 835 | - } | ||
| 836 | - | ||
| 837 | fn activate_column(&mut self, idx: usize) { | 832 | fn activate_column(&mut self, idx: usize) { |
| 838 | self.activate_column_with_anim_config( | 833 | self.activate_column_with_anim_config( |
| 839 | idx, | 834 | idx, |
| @@ -913,31 +908,24 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 913 | is_full_width, | 908 | is_full_width, |
| 914 | true, | 909 | true, |
| 915 | ); | 910 | ); |
| 916 | - let width = column.width(); | ||
| 917 | self.data.insert(col_idx, ColumnData::new(&column)); | 911 | self.data.insert(col_idx, ColumnData::new(&column)); |
| 918 | self.columns.insert(col_idx, column); | 912 | self.columns.insert(col_idx, column); |
| 919 | 913 | ||
| 920 | if activate { | 914 | if activate { |
| 921 | - // If this is the first window on an empty workspace, skip the animation from whatever | 915 | + // If this is the first window on an empty workspace, remove the effect of whatever |
| 922 | - // view_offset was left over. | 916 | + // view_offset was left over and skip the animation. |
| 923 | if was_empty { | 917 | if was_empty { |
| 924 | - if self.options.center_focused_column == CenterFocusedColumn::Always { | 918 | + self.view_offset = 0.; |
| 925 | - self.view_offset = | ||
| 926 | - -(self.working_area.size.w - width) / 2. - self.working_area.loc.x; | ||
| 927 | - } else { | ||
| 928 | - // Try to make the code produce a left-aligned offset, even in presence of left | ||
| 929 | - // exclusive zones. | ||
| 930 | - self.view_offset = self.compute_new_view_offset_for_column(self.column_x(0), 0); | ||
| 931 | - } | ||
| 932 | self.view_offset_adj = None; | 919 | self.view_offset_adj = None; |
| 920 | + self.view_offset = | ||
| 921 | + self.compute_new_view_offset_for_column(self.view_pos(), col_idx, None); | ||
| 933 | } | 922 | } |
| 934 | 923 | ||
| 935 | let prev_offset = (!was_empty).then(|| self.static_view_offset()); | 924 | let prev_offset = (!was_empty).then(|| self.static_view_offset()); |
| 936 | 925 | ||
| 937 | - self.activate_column_with_anim_config( | 926 | + let anim_config = |
| 938 | - col_idx, | 927 | + anim_config.unwrap_or(self.options.animations.horizontal_view_movement.0); |
| 939 | - anim_config.unwrap_or(self.options.animations.horizontal_view_movement.0), | 928 | + self.activate_column_with_anim_config(col_idx, anim_config); |
| 940 | - ); | ||
| 941 | self.activate_prev_column_on_removal = prev_offset; | 929 | self.activate_prev_column_on_removal = prev_offset; |
| 942 | } | 930 | } |
| 943 | 931 | ||
| @@ -1054,23 +1042,17 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1054 | 1042 | ||
| 1055 | column.update_config(self.scale.fractional_scale(), self.options.clone()); | 1043 | column.update_config(self.scale.fractional_scale(), self.options.clone()); |
| 1056 | column.set_view_size(self.view_size, self.working_area); | 1044 | column.set_view_size(self.view_size, self.working_area); |
| 1057 | - let width = column.width(); | ||
| 1058 | self.data.insert(idx, ColumnData::new(&column)); | 1045 | self.data.insert(idx, ColumnData::new(&column)); |
| 1059 | self.columns.insert(idx, column); | 1046 | self.columns.insert(idx, column); |
| 1060 | 1047 | ||
| 1061 | if activate { | 1048 | if activate { |
| 1062 | - // If this is the first window on an empty workspace, skip the animation from whatever | 1049 | + // If this is the first window on an empty workspace, remove the effect of whatever |
| 1063 | - // view_offset was left over. | 1050 | + // view_offset was left over and skip the animation. |
| 1064 | if was_empty { | 1051 | if was_empty { |
| 1065 | - if self.options.center_focused_column == CenterFocusedColumn::Always { | 1052 | + self.view_offset = 0.; |
| 1066 | - self.view_offset = | ||
| 1067 | - -(self.working_area.size.w - width) / 2. - self.working_area.loc.x; | ||
| 1068 | - } else { | ||
| 1069 | - // Try to make the code produce a left-aligned offset, even in presence of left | ||
| 1070 | - // exclusive zones. | ||
| 1071 | - self.view_offset = self.compute_new_view_offset_for_column(self.column_x(0), 0); | ||
| 1072 | - } | ||
| 1073 | self.view_offset_adj = None; | 1053 | self.view_offset_adj = None; |
| 1054 | + self.view_offset = | ||
| 1055 | + self.compute_new_view_offset_for_column(self.view_pos(), idx, None); | ||
| 1074 | } | 1056 | } |
| 1075 | 1057 | ||
| 1076 | let prev_offset = (!was_empty).then(|| self.static_view_offset()); | 1058 | let prev_offset = (!was_empty).then(|| self.static_view_offset()); |
| @@ -1947,6 +1929,10 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1947 | } | 1929 | } |
| 1948 | 1930 | ||
| 1949 | pub fn center_column(&mut self) { | 1931 | pub fn center_column(&mut self) { |
| 1932 | + if self.columns.is_empty() { | ||
| 1933 | + return; | ||
| 1934 | + } | ||
| 1935 | + | ||
| 1950 | let center_x = self.view_pos(); | 1936 | let center_x = self.view_pos(); |
| 1951 | self.animate_view_offset_to_column_centered( | 1937 | self.animate_view_offset_to_column_centered( |
| 1952 | center_x, | 1938 | center_x, |
| @@ -1954,10 +1940,8 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1954 | self.options.animations.horizontal_view_movement.0, | 1940 | self.options.animations.horizontal_view_movement.0, |
| 1955 | ); | 1941 | ); |
| 1956 | 1942 | ||
| 1957 | - if !self.columns.is_empty() { | 1943 | + let col = &mut self.columns[self.active_column_idx]; |
| 1958 | - let col = &mut self.columns[self.active_column_idx]; | 1944 | + cancel_resize_for_column(&mut self.interactive_resize, col); |
| 1959 | - cancel_resize_for_column(&mut self.interactive_resize, col); | ||
| 1960 | - } | ||
| 1961 | } | 1945 | } |
| 1962 | 1946 | ||
| 1963 | fn view_pos(&self) -> f64 { | 1947 | fn view_pos(&self) -> f64 { |