layout: Extract Monitor::workspaces_with_render_positions()
Browse files at this commit29602ca995e349b47e65b6ef07f5cf47d6da8e5aParent: d7156df3 changed files +85 -156
- niri-visual-tests/src/cases/layout.rs +0 -1
- src/layout/monitor.rs +84 -154
- src/niri.rs +1 -1
niri-visual-tests/src/cases/layout.rsmodified+0 -1
| @@ -233,7 +233,6 @@ impl TestCase for Layout { | |||
| 233 | .monitor_for_output(&self.output) | 233 | .monitor_for_output(&self.output) |
| 234 | .unwrap() | 234 | .unwrap() |
| 235 | .render_elements(renderer, RenderTarget::Output) | 235 | .render_elements(renderer, RenderTarget::Output) |
| 236 | - .into_iter() | ||
| 237 | .map(|elem| Box::new(elem) as _) | 236 | .map(|elem| Box::new(elem) as _) |
| 238 | .collect() | 237 | .collect() |
| 239 | } | 238 | } |
src/layout/monitor.rsmodified+84 -154
| @@ -20,7 +20,7 @@ use crate::render_helpers::renderer::NiriRenderer; | |||
| 20 | use crate::render_helpers::RenderTarget; | 20 | use crate::render_helpers::RenderTarget; |
| 21 | use crate::rubber_band::RubberBand; | 21 | use crate::rubber_band::RubberBand; |
| 22 | use crate::utils::transaction::Transaction; | 22 | use crate::utils::transaction::Transaction; |
| 23 | -use crate::utils::{output_size, to_physical_precise_round, ResizeEdge}; | 23 | +use crate::utils::{output_size, round_logical_in_physical, ResizeEdge}; |
| 24 | 24 | ||
| 25 | /// Amount of touchpad movement to scroll the height of one workspace. | 25 | /// Amount of touchpad movement to scroll the height of one workspace. |
| 26 | const WORKSPACE_GESTURE_MOVEMENT: f64 = 300.; | 26 | const WORKSPACE_GESTURE_MOVEMENT: f64 = 300.; |
| @@ -814,90 +814,71 @@ impl<W: LayoutElement> Monitor<W> { | |||
| 814 | Some(rect) | 814 | Some(rect) |
| 815 | } | 815 | } |
| 816 | 816 | ||
| 817 | - pub fn window_under( | 817 | + pub fn workspaces_with_render_positions( |
| 818 | &self, | 818 | &self, |
| 819 | - pos_within_output: Point<f64, Logical>, | 819 | + ) -> impl Iterator<Item = (&Workspace<W>, Point<f64, Logical>)> { |
| 820 | - ) -> Option<(&W, Option<Point<f64, Logical>>)> { | 820 | + let mut first = None; |
| 821 | + let mut second = None; | ||
| 822 | + | ||
| 821 | match &self.workspace_switch { | 823 | match &self.workspace_switch { |
| 822 | Some(switch) => { | 824 | Some(switch) => { |
| 823 | - let size = output_size(&self.output).to_f64(); | ||
| 824 | - | ||
| 825 | let render_idx = switch.current_idx(); | 825 | let render_idx = switch.current_idx(); |
| 826 | let before_idx = render_idx.floor(); | 826 | let before_idx = render_idx.floor(); |
| 827 | let after_idx = render_idx.ceil(); | 827 | let after_idx = render_idx.ceil(); |
| 828 | 828 | ||
| 829 | - let offset = (render_idx - before_idx) * size.h; | 829 | + if after_idx >= 0. && before_idx < self.workspaces.len() as f64 { |
| 830 | + let scale = self.output.current_scale().fractional_scale(); | ||
| 831 | + let size = output_size(&self.output); | ||
| 832 | + let offset = | ||
| 833 | + round_logical_in_physical(scale, (render_idx - before_idx) * size.h); | ||
| 830 | 834 | ||
| 831 | - if after_idx < 0. || before_idx as usize >= self.workspaces.len() { | 835 | + // Ceil the height in physical pixels. |
| 832 | - return None; | 836 | + let height = (size.h * scale).ceil() / scale; |
| 833 | - } | ||
| 834 | 837 | ||
| 835 | - let after_idx = after_idx as usize; | 838 | + if before_idx >= 0. { |
| 836 | - | 839 | + let before_idx = before_idx as usize; |
| 837 | - let (idx, ws_offset) = if pos_within_output.y < size.h - offset { | 840 | + let before_offset = Point::from((0., -offset)); |
| 838 | - if before_idx < 0. { | 841 | + first = Some((&self.workspaces[before_idx], before_offset)); |
| 839 | - return None; | ||
| 840 | } | 842 | } |
| 841 | 843 | ||
| 842 | - (before_idx as usize, Point::from((0., offset))) | 844 | + let after_idx = after_idx as usize; |
| 843 | - } else { | 845 | + if after_idx < self.workspaces.len() { |
| 844 | - if after_idx >= self.workspaces.len() { | 846 | + let after_offset = Point::from((0., -offset + height)); |
| 845 | - return None; | 847 | + second = Some((&self.workspaces[after_idx], after_offset)); |
| 846 | } | 848 | } |
| 847 | - | 849 | + } |
| 848 | - (after_idx, Point::from((0., -size.h + offset))) | ||
| 849 | - }; | ||
| 850 | - | ||
| 851 | - let ws = &self.workspaces[idx]; | ||
| 852 | - let (win, win_pos) = ws.window_under(pos_within_output + ws_offset)?; | ||
| 853 | - Some((win, win_pos.map(|p| p - ws_offset))) | ||
| 854 | } | 850 | } |
| 855 | None => { | 851 | None => { |
| 856 | - let ws = &self.workspaces[self.active_workspace_idx]; | 852 | + first = Some(( |
| 857 | - ws.window_under(pos_within_output) | 853 | + &self.workspaces[self.active_workspace_idx], |
| 854 | + Point::from((0., 0.)), | ||
| 855 | + )); | ||
| 858 | } | 856 | } |
| 859 | } | 857 | } |
| 860 | - } | ||
| 861 | 858 | ||
| 862 | - pub fn resize_edges_under(&self, pos_within_output: Point<f64, Logical>) -> Option<ResizeEdge> { | 859 | + first.into_iter().chain(second) |
| 863 | - match &self.workspace_switch { | 860 | + } |
| 864 | - Some(switch) => { | ||
| 865 | - let size = output_size(&self.output); | ||
| 866 | - | ||
| 867 | - let render_idx = switch.current_idx(); | ||
| 868 | - let before_idx = render_idx.floor(); | ||
| 869 | - let after_idx = render_idx.ceil(); | ||
| 870 | - | ||
| 871 | - let offset = (render_idx - before_idx) * size.h; | ||
| 872 | - | ||
| 873 | - if after_idx < 0. || before_idx as usize >= self.workspaces.len() { | ||
| 874 | - return None; | ||
| 875 | - } | ||
| 876 | - | ||
| 877 | - let after_idx = after_idx as usize; | ||
| 878 | - | ||
| 879 | - let (idx, ws_offset) = if pos_within_output.y < size.h - offset { | ||
| 880 | - if before_idx < 0. { | ||
| 881 | - return None; | ||
| 882 | - } | ||
| 883 | - | ||
| 884 | - (before_idx as usize, Point::from((0., offset))) | ||
| 885 | - } else { | ||
| 886 | - if after_idx >= self.workspaces.len() { | ||
| 887 | - return None; | ||
| 888 | - } | ||
| 889 | 861 | ||
| 890 | - (after_idx, Point::from((0., -size.h + offset))) | 862 | + pub fn window_under( |
| 891 | - }; | 863 | + &self, |
| 864 | + pos_within_output: Point<f64, Logical>, | ||
| 865 | + ) -> Option<(&W, Option<Point<f64, Logical>>)> { | ||
| 866 | + let size = output_size(&self.output); | ||
| 867 | + let (ws, bounds) = self | ||
| 868 | + .workspaces_with_render_positions() | ||
| 869 | + .map(|(ws, offset)| (ws, Rectangle::from_loc_and_size(offset, size))) | ||
| 870 | + .find(|(_, bounds)| bounds.contains(pos_within_output))?; | ||
| 871 | + let (win, win_pos) = ws.window_under(pos_within_output - bounds.loc)?; | ||
| 872 | + Some((win, win_pos.map(|p| p + bounds.loc))) | ||
| 873 | + } | ||
| 892 | 874 | ||
| 893 | - let ws = &self.workspaces[idx]; | 875 | + pub fn resize_edges_under(&self, pos_within_output: Point<f64, Logical>) -> Option<ResizeEdge> { |
| 894 | - ws.resize_edges_under(pos_within_output + ws_offset) | 876 | + let size = output_size(&self.output); |
| 895 | - } | 877 | + let (ws, bounds) = self |
| 896 | - None => { | 878 | + .workspaces_with_render_positions() |
| 897 | - let ws = &self.workspaces[self.active_workspace_idx]; | 879 | + .map(|(ws, offset)| (ws, Rectangle::from_loc_and_size(offset, size))) |
| 898 | - ws.resize_edges_under(pos_within_output) | 880 | + .find(|(_, bounds)| bounds.contains(pos_within_output))?; |
| 899 | - } | 881 | + ws.resize_edges_under(pos_within_output - bounds.loc) |
| 900 | - } | ||
| 901 | } | 882 | } |
| 902 | 883 | ||
| 903 | pub fn render_above_top_layer(&self) -> bool { | 884 | pub fn render_above_top_layer(&self) -> bool { |
| @@ -910,103 +891,52 @@ impl<W: LayoutElement> Monitor<W> { | |||
| 910 | ws.render_above_top_layer() | 891 | ws.render_above_top_layer() |
| 911 | } | 892 | } |
| 912 | 893 | ||
| 913 | - pub fn render_elements<R: NiriRenderer>( | 894 | + pub fn render_elements<'a, R: NiriRenderer>( |
| 914 | - &self, | 895 | + &'a self, |
| 915 | - renderer: &mut R, | 896 | + renderer: &'a mut R, |
| 916 | target: RenderTarget, | 897 | target: RenderTarget, |
| 917 | - ) -> Vec<MonitorRenderElement<R>> { | 898 | + ) -> impl Iterator<Item = MonitorRenderElement<R>> + '_ { |
| 918 | let _span = tracy_client::span!("Monitor::render_elements"); | 899 | let _span = tracy_client::span!("Monitor::render_elements"); |
| 919 | 900 | ||
| 920 | let scale = self.output.current_scale().fractional_scale(); | 901 | let scale = self.output.current_scale().fractional_scale(); |
| 921 | let size = output_size(&self.output); | 902 | let size = output_size(&self.output); |
| 903 | + // Ceil the height in physical pixels. | ||
| 904 | + let height = (size.h * scale).ceil() as i32; | ||
| 905 | + | ||
| 906 | + // Crop the elements to prevent them overflowing, currently visible during a workspace | ||
| 907 | + // switch. | ||
| 908 | + // | ||
| 909 | + // HACK: crop to infinite bounds at least horizontally where we | ||
| 910 | + // know there's no workspace joining or monitor bounds, otherwise | ||
| 911 | + // it will cut pixel shaders and mess up the coordinate space. | ||
| 912 | + // There's also a damage tracking bug which causes glitched | ||
| 913 | + // rendering for maximized GTK windows. | ||
| 914 | + // | ||
| 915 | + // FIXME: use proper bounds after fixing the Crop element. | ||
| 916 | + let crop_bounds = if self.workspace_switch.is_some() { | ||
| 917 | + Rectangle::from_loc_and_size((-i32::MAX / 2, 0), (i32::MAX, height)) | ||
| 918 | + } else { | ||
| 919 | + Rectangle::from_loc_and_size((-i32::MAX / 2, -i32::MAX / 2), (i32::MAX, i32::MAX)) | ||
| 920 | + }; | ||
| 922 | 921 | ||
| 923 | - match &self.workspace_switch { | 922 | + self.workspaces_with_render_positions() |
| 924 | - Some(switch) => { | 923 | + .flat_map(move |(ws, offset)| { |
| 925 | - let render_idx = switch.current_idx(); | 924 | + ws.render_elements(renderer, target) |
| 926 | - let before_idx = render_idx.floor(); | ||
| 927 | - let after_idx = render_idx.ceil(); | ||
| 928 | - | ||
| 929 | - let offset = (render_idx - before_idx) * size.h; | ||
| 930 | - | ||
| 931 | - if after_idx < 0. || before_idx as usize >= self.workspaces.len() { | ||
| 932 | - return vec![]; | ||
| 933 | - } | ||
| 934 | - | ||
| 935 | - let after_idx = after_idx as usize; | ||
| 936 | - let after = if after_idx < self.workspaces.len() { | ||
| 937 | - let after = self.workspaces[after_idx].render_elements(renderer, target); | ||
| 938 | - let after = after.into_iter().filter_map(|elem| { | ||
| 939 | - Some(RelocateRenderElement::from_element( | ||
| 940 | - CropRenderElement::from_element( | ||
| 941 | - elem, | ||
| 942 | - scale, | ||
| 943 | - // HACK: crop to infinite bounds for all sides except the side | ||
| 944 | - // where the workspaces join, | ||
| 945 | - // otherwise it will cut pixel shaders and mess up | ||
| 946 | - // the coordinate space. | ||
| 947 | - Rectangle::from_extemities( | ||
| 948 | - (-i32::MAX / 2, 0), | ||
| 949 | - (i32::MAX / 2, i32::MAX / 2), | ||
| 950 | - ), | ||
| 951 | - )?, | ||
| 952 | - Point::from((0., -offset + size.h)).to_physical_precise_round(scale), | ||
| 953 | - Relocate::Relative, | ||
| 954 | - )) | ||
| 955 | - }); | ||
| 956 | - | ||
| 957 | - if before_idx < 0. { | ||
| 958 | - return after.collect(); | ||
| 959 | - } | ||
| 960 | - | ||
| 961 | - Some(after) | ||
| 962 | - } else { | ||
| 963 | - None | ||
| 964 | - }; | ||
| 965 | - | ||
| 966 | - let before_idx = before_idx as usize; | ||
| 967 | - let before = self.workspaces[before_idx].render_elements(renderer, target); | ||
| 968 | - let before = before.into_iter().filter_map(|elem| { | ||
| 969 | - Some(RelocateRenderElement::from_element( | ||
| 970 | - CropRenderElement::from_element( | ||
| 971 | - elem, | ||
| 972 | - scale, | ||
| 973 | - Rectangle::from_extemities( | ||
| 974 | - (-i32::MAX / 2, -i32::MAX / 2), | ||
| 975 | - (i32::MAX / 2, to_physical_precise_round(scale, size.h)), | ||
| 976 | - ), | ||
| 977 | - )?, | ||
| 978 | - Point::from((0., -offset)).to_physical_precise_round(scale), | ||
| 979 | - Relocate::Relative, | ||
| 980 | - )) | ||
| 981 | - }); | ||
| 982 | - before.chain(after.into_iter().flatten()).collect() | ||
| 983 | - } | ||
| 984 | - None => { | ||
| 985 | - let elements = | ||
| 986 | - self.workspaces[self.active_workspace_idx].render_elements(renderer, target); | ||
| 987 | - elements | ||
| 988 | .into_iter() | 925 | .into_iter() |
| 989 | - .filter_map(|elem| { | 926 | + .filter_map(move |elem| { |
| 990 | - Some(RelocateRenderElement::from_element( | 927 | + CropRenderElement::from_element(elem, scale, crop_bounds) |
| 991 | - CropRenderElement::from_element( | 928 | + }) |
| 992 | - elem, | 929 | + .map(move |elem| { |
| 993 | - scale, | 930 | + RelocateRenderElement::from_element( |
| 994 | - // HACK: set infinite crop bounds due to a damage tracking bug | 931 | + elem, |
| 995 | - // which causes glitched rendering for maximized GTK windows. | 932 | + // The offset we get from workspaces_with_render_positions() is already |
| 996 | - // FIXME: use proper bounds after fixing the Crop element. | 933 | + // rounded to physical pixels, but it's in the logical coordinate |
| 997 | - Rectangle::from_loc_and_size( | 934 | + // space, so we need to convert it to physical. |
| 998 | - (-i32::MAX / 2, -i32::MAX / 2), | 935 | + offset.to_physical_precise_round(scale), |
| 999 | - (i32::MAX, i32::MAX), | ||
| 1000 | - ), | ||
| 1001 | - // Rectangle::from_loc_and_size((0, 0), size), | ||
| 1002 | - )?, | ||
| 1003 | - (0, 0), | ||
| 1004 | Relocate::Relative, | 936 | Relocate::Relative, |
| 1005 | - )) | 937 | + ) |
| 1006 | }) | 938 | }) |
| 1007 | - .collect() | 939 | + }) |
| 1008 | - } | ||
| 1009 | - } | ||
| 1010 | } | 940 | } |
| 1011 | 941 | ||
| 1012 | pub fn workspace_switch_gesture_begin(&mut self, is_touchpad: bool) { | 942 | pub fn workspace_switch_gesture_begin(&mut self, is_touchpad: bool) { |
src/niri.rsmodified+1 -1
| @@ -3044,7 +3044,7 @@ impl Niri { | |||
| 3044 | 3044 | ||
| 3045 | // Get monitor elements. | 3045 | // Get monitor elements. |
| 3046 | let mon = self.layout.monitor_for_output(output).unwrap(); | 3046 | let mon = self.layout.monitor_for_output(output).unwrap(); |
| 3047 | - let monitor_elements = mon.render_elements(renderer, target); | 3047 | + let monitor_elements: Vec<_> = mon.render_elements(renderer, target).collect(); |
| 3048 | 3048 | ||
| 3049 | // Get layer-shell elements. | 3049 | // Get layer-shell elements. |
| 3050 | let layer_map = layer_map_for_output(output); | 3050 | let layer_map = layer_map_for_output(output); |