tab indicator: Extract tab_rects()
Browse files at this commita21196ec54a0e2bb59e0f52308c38eabe841b923Parent: 0b83d991 changed file +56 -38
- src/layout/tab_indicator.rs +56 -38
src/layout/tab_indicator.rsmodified+56 -38
| @@ -51,50 +51,26 @@ impl TabIndicator { | ||
| 51 | 51 | } |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - #[allow(clippy::too_many_arguments)] | |
| 55 | - pub fn update_render_elements( | |
| 56 | - &mut self, | |
| 57 | - enabled: bool, | |
| 58 | - // Geometry of the tabs area. | |
| 54 | + fn tab_rects( | |
| 55 | + &self, | |
| 59 | 56 | area: Rectangle<f64, Logical>, |
| 60 | - // View rect relative to the tabs area. | |
| 61 | - area_view_rect: Rectangle<f64, Logical>, | |
| 62 | - // Tab count, should match the tabs iterator length. | |
| 63 | - tab_count: usize, | |
| 64 | - tabs: impl Iterator<Item = TabInfo>, | |
| 65 | - // TODO: do we indicate inactive-but-selected somehow? | |
| 66 | - _is_active: bool, | |
| 57 | + count: usize, | |
| 67 | 58 | scale: f64, |
| 68 | - ) { | |
| 69 | - if !enabled || self.config.off { | |
| 70 | - self.shader_locs.clear(); | |
| 71 | - self.shaders.clear(); | |
| 72 | - return; | |
| 73 | - } | |
| 74 | - | |
| 75 | - let count = tab_count; | |
| 76 | - if self.config.hide_when_single_tab && count == 1 { | |
| 77 | - self.shader_locs.clear(); | |
| 78 | - self.shaders.clear(); | |
| 79 | - return; | |
| 80 | - } | |
| 81 | - | |
| 59 | + ) -> impl Iterator<Item = Rectangle<f64, Logical>> { | |
| 82 | 60 | let round = |logical: f64| round_logical_in_physical(scale, logical); |
| 83 | 61 | |
| 84 | 62 | let width = round(self.config.width.0); |
| 85 | 63 | let gap = round(self.config.gap.0); |
| 86 | 64 | let gaps_between = round(self.config.gaps_between_tabs.0); |
| 87 | 65 | |
| 88 | - let side = match self.config.position { | |
| 66 | + let position = self.config.position; | |
| 67 | + let side = match position { | |
| 89 | 68 | TabIndicatorPosition::Left | TabIndicatorPosition::Right => area.size.h, |
| 90 | 69 | TabIndicatorPosition::Top | TabIndicatorPosition::Bottom => area.size.w, |
| 91 | 70 | }; |
| 92 | 71 | let total_prop = self.config.length.total_proportion.unwrap_or(0.5); |
| 93 | 72 | let min_length = round(side * total_prop.clamp(0., 2.)); |
| 94 | 73 | |
| 95 | - self.shaders.resize_with(count, Default::default); | |
| 96 | - self.shader_locs.resize_with(count, Default::default); | |
| 97 | - | |
| 98 | 74 | let pixel = 1. / scale; |
| 99 | 75 | let shortest_length = count as f64 * (pixel + gaps_between) - gaps_between; |
| 100 | 76 | let length = f64::max(min_length, shortest_length); |
| @@ -104,7 +80,7 @@ impl TabIndicator { | ||
| 104 | 80 | let mut ones_left = ((length - floored_length) / pixel).max(0.).round() as usize; |
| 105 | 81 | |
| 106 | 82 | let mut shader_loc = Point::from((-gap - width, round((side - length) / 2.))); |
| 107 | - match self.config.position { | |
| 83 | + match position { | |
| 108 | 84 | TabIndicatorPosition::Left => (), |
| 109 | 85 | TabIndicatorPosition::Right => shader_loc.x = area.size.w + gap, |
| 110 | 86 | TabIndicatorPosition::Top => mem::swap(&mut shader_loc.x, &mut shader_loc.y), |
| @@ -115,16 +91,16 @@ impl TabIndicator { | ||
| 115 | 91 | } |
| 116 | 92 | shader_loc += area.loc; |
| 117 | 93 | |
| 118 | - for ((shader, loc), tab) in zip(&mut self.shaders, &mut self.shader_locs).zip(tabs) { | |
| 119 | - *loc = shader_loc; | |
| 120 | - | |
| 94 | + (0..count).map(move |_| { | |
| 121 | 95 | let mut px_per_tab = px_per_tab; |
| 122 | 96 | if ones_left > 0 { |
| 123 | 97 | ones_left -= 1; |
| 124 | 98 | px_per_tab += pixel; |
| 125 | 99 | } |
| 126 | 100 | |
| 127 | - match self.config.position { | |
| 101 | + let loc = shader_loc; | |
| 102 | + | |
| 103 | + match position { | |
| 128 | 104 | TabIndicatorPosition::Left | TabIndicatorPosition::Right => { |
| 129 | 105 | shader_loc.y += px_per_tab + gaps_between |
| 130 | 106 | } |
| @@ -133,7 +109,7 @@ impl TabIndicator { | ||
| 133 | 109 | } |
| 134 | 110 | } |
| 135 | 111 | |
| 136 | - let shader_size = match self.config.position { | |
| 112 | + let size = match position { | |
| 137 | 113 | TabIndicatorPosition::Left | TabIndicatorPosition::Right => { |
| 138 | 114 | Size::from((width, px_per_tab)) |
| 139 | 115 | } |
| @@ -142,6 +118,48 @@ impl TabIndicator { | ||
| 142 | 118 | } |
| 143 | 119 | }; |
| 144 | 120 | |
| 121 | + Rectangle::new(loc, size) | |
| 122 | + }) | |
| 123 | + } | |
| 124 | + | |
| 125 | + #[allow(clippy::too_many_arguments)] | |
| 126 | + pub fn update_render_elements( | |
| 127 | + &mut self, | |
| 128 | + enabled: bool, | |
| 129 | + // Geometry of the tabs area. | |
| 130 | + area: Rectangle<f64, Logical>, | |
| 131 | + // View rect relative to the tabs area. | |
| 132 | + area_view_rect: Rectangle<f64, Logical>, | |
| 133 | + // Tab count, should match the tabs iterator length. | |
| 134 | + tab_count: usize, | |
| 135 | + tabs: impl Iterator<Item = TabInfo>, | |
| 136 | + // TODO: do we indicate inactive-but-selected somehow? | |
| 137 | + _is_active: bool, | |
| 138 | + scale: f64, | |
| 139 | + ) { | |
| 140 | + if !enabled || self.config.off { | |
| 141 | + self.shader_locs.clear(); | |
| 142 | + self.shaders.clear(); | |
| 143 | + return; | |
| 144 | + } | |
| 145 | + | |
| 146 | + let count = tab_count; | |
| 147 | + if self.config.hide_when_single_tab && count == 1 { | |
| 148 | + self.shader_locs.clear(); | |
| 149 | + self.shaders.clear(); | |
| 150 | + return; | |
| 151 | + } | |
| 152 | + | |
| 153 | + self.shaders.resize_with(count, Default::default); | |
| 154 | + self.shader_locs.resize_with(count, Default::default); | |
| 155 | + | |
| 156 | + let rects = self.tab_rects(area, count, scale); | |
| 157 | + for ((shader, loc), (tab, rect)) in zip( | |
| 158 | + zip(&mut self.shaders, &mut self.shader_locs), | |
| 159 | + zip(tabs, rects), | |
| 160 | + ) { | |
| 161 | + *loc = rect.loc; | |
| 162 | + | |
| 145 | 163 | let mut gradient_area = match tab.gradient.relative_to { |
| 146 | 164 | GradientRelativeTo::Window => tab.geometry, |
| 147 | 165 | GradientRelativeTo::WorkspaceView => area_view_rect, |
| @@ -149,13 +167,13 @@ impl TabIndicator { | ||
| 149 | 167 | gradient_area.loc -= *loc; |
| 150 | 168 | |
| 151 | 169 | shader.update( |
| 152 | - shader_size, | |
| 170 | + rect.size, | |
| 153 | 171 | gradient_area, |
| 154 | 172 | tab.gradient.in_, |
| 155 | 173 | tab.gradient.from, |
| 156 | 174 | tab.gradient.to, |
| 157 | 175 | ((tab.gradient.angle as f32) - 90.).to_radians(), |
| 158 | - Rectangle::from_size(shader_size), | |
| 176 | + Rectangle::from_size(rect.size), | |
| 159 | 177 | 0., |
| 160 | 178 | CornerRadius::default(), |
| 161 | 179 | scale as f32, |