forge
Explore
Log in

Don't you dare go Hollow

layout: Implement weighted height distribution

Browse files at this commit

66f23c39809eef827ef7f47fab7bdbf68dd69a26Ivan MolodetskikhParent: 7a6ab31
The intention is to make columns add up to the working area height most
of the time, while still preserving the ability to have one fixed-height
window.

Automatic heights are now distributed according to their weight, rather
than evenly. This is similar to flex-grow in CSS or fraction in Typst.

Resizing one window in a column still makes that window fixed, however
it changes all other windows to automatic height, computing their
weights in such a way as to preserve their apparent heights.
src/layout/mod.rsmodified+95 -0
@@ -4263,6 +4263,101 @@ mod tests {
4263 compute_working_area(&output, struts);4263 compute_working_area(&output, struts);
4264 }4264 }
4265 4265
4266+ #[test]
4267+ fn set_window_height_recomputes_to_auto() {
4268+ let ops = [
4269+ Op::AddOutput(1),
4270+ Op::AddWindow {
4271+ id: 0,
4272+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
4273+ min_max_size: Default::default(),
4274+ },
4275+ Op::AddWindow {
4276+ id: 1,
4277+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
4278+ min_max_size: Default::default(),
4279+ },
4280+ Op::ConsumeOrExpelWindowLeft,
4281+ Op::AddWindow {
4282+ id: 2,
4283+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
4284+ min_max_size: Default::default(),
4285+ },
4286+ Op::ConsumeOrExpelWindowLeft,
4287+ Op::SetWindowHeight(SizeChange::SetFixed(100)),
4288+ Op::FocusWindowUp,
4289+ Op::SetWindowHeight(SizeChange::SetFixed(200)),
4290+ ];
4291+
4292+ check_ops(&ops);
4293+ }
4294+
4295+ #[test]
4296+ fn one_window_in_column_becomes_weight_1() {
4297+ let ops = [
4298+ Op::AddOutput(1),
4299+ Op::AddWindow {
4300+ id: 0,
4301+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
4302+ min_max_size: Default::default(),
4303+ },
4304+ Op::AddWindow {
4305+ id: 1,
4306+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
4307+ min_max_size: Default::default(),
4308+ },
4309+ Op::ConsumeOrExpelWindowLeft,
4310+ Op::AddWindow {
4311+ id: 2,
4312+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
4313+ min_max_size: Default::default(),
4314+ },
4315+ Op::ConsumeOrExpelWindowLeft,
4316+ Op::SetWindowHeight(SizeChange::SetFixed(100)),
4317+ Op::Communicate(2),
4318+ Op::FocusWindowUp,
4319+ Op::SetWindowHeight(SizeChange::SetFixed(200)),
4320+ Op::Communicate(1),
4321+ Op::CloseWindow(0),
4322+ Op::CloseWindow(1),
4323+ ];
4324+
4325+ check_ops(&ops);
4326+ }
4327+
4328+ #[test]
4329+ fn one_window_in_column_becomes_weight_1_after_fullscreen() {
4330+ let ops = [
4331+ Op::AddOutput(1),
4332+ Op::AddWindow {
4333+ id: 0,
4334+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
4335+ min_max_size: Default::default(),
4336+ },
4337+ Op::AddWindow {
4338+ id: 1,
4339+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
4340+ min_max_size: Default::default(),
4341+ },
4342+ Op::ConsumeOrExpelWindowLeft,
4343+ Op::AddWindow {
4344+ id: 2,
4345+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
4346+ min_max_size: Default::default(),
4347+ },
4348+ Op::ConsumeOrExpelWindowLeft,
4349+ Op::SetWindowHeight(SizeChange::SetFixed(100)),
4350+ Op::Communicate(2),
4351+ Op::FocusWindowUp,
4352+ Op::SetWindowHeight(SizeChange::SetFixed(200)),
4353+ Op::Communicate(1),
4354+ Op::CloseWindow(0),
4355+ Op::FullscreenWindow(1),
4356+ ];
4357+
4358+ check_ops(&ops);
4359+ }
4360+
4266 fn arbitrary_spacing() -> impl Strategy<Value = f64> {4361 fn arbitrary_spacing() -> impl Strategy<Value = f64> {
4267 // Give equal weight to:4362 // Give equal weight to:
4268 // - 0: the element is disabled4363 // - 0: the element is disabled
src/layout/workspace.rsmodified+107 -18
@@ -196,9 +196,12 @@ pub enum ColumnWidth {
196 /// to fit the desired content, it can never become smaller than that when moving between monitors.196 /// to fit the desired content, it can never become smaller than that when moving between monitors.
197 #[derive(Debug, Clone, Copy, PartialEq)]197 #[derive(Debug, Clone, Copy, PartialEq)]
198 pub enum WindowHeight {198 pub enum WindowHeight {
199- /// Automatically computed height, evenly distributed across the column.199+ /// Automatically computed *tile* height, distributed across the column according to weights.
200- Auto,200+ ///
201- /// Fixed height in logical pixels.201+ /// This controls the tile height rather than the window height because it's easier in the auto
202+ /// height distribution algorithm.
203+ Auto { weight: f64 },
204+ /// Fixed *window* height in logical pixels.
202 Fixed(f64),205 Fixed(f64),
203 }206 }
204 207
@@ -312,6 +315,12 @@ impl From<PresetWidth> for ColumnWidth {
312 }315 }
313 }316 }
314 317
318+impl WindowHeight {
319+ const fn auto_1() -> Self {
320+ Self::Auto { weight: 1. }
321+ }
322+}
323+
315 impl TileData {324 impl TileData {
316 pub fn new<W: LayoutElement>(tile: &Tile<W>, height: WindowHeight) -> Self {325 pub fn new<W: LayoutElement>(tile: &Tile<W>, height: WindowHeight) -> Self {
317 let mut rv = Self {326 let mut rv = Self {
@@ -1106,6 +1115,13 @@ impl<W: LayoutElement> Workspace<W> {
1106 let tile = column.tiles.remove(window_idx);1115 let tile = column.tiles.remove(window_idx);
1107 column.data.remove(window_idx);1116 column.data.remove(window_idx);
1108 1117
1118+ // If one window is left, reset its weight to 1.
1119+ if column.data.len() == 1 {
1120+ if let WindowHeight::Auto { weight } = &mut column.data[0].height {
1121+ *weight = 1.;
1122+ }
1123+ }
1124+
1109 if let Some(output) = &self.output {1125 if let Some(output) = &self.output {
1110 tile.window().output_leave(output);1126 tile.window().output_leave(output);
1111 }1127 }
@@ -2265,6 +2281,14 @@ impl<W: LayoutElement> Workspace<W> {
2265 let window = col.tiles.remove(tile_idx).into_window();2281 let window = col.tiles.remove(tile_idx).into_window();
2266 col.data.remove(tile_idx);2282 col.data.remove(tile_idx);
2267 col.active_tile_idx = min(col.active_tile_idx, col.tiles.len() - 1);2283 col.active_tile_idx = min(col.active_tile_idx, col.tiles.len() - 1);
2284+
2285+ // If one window is left, reset its weight to 1.
2286+ if col.data.len() == 1 {
2287+ if let WindowHeight::Auto { weight } = &mut col.data[0].height {
2288+ *weight = 1.;
2289+ }
2290+ }
2291+
2268 col.update_tile_sizes(false);2292 col.update_tile_sizes(false);
2269 self.data[col_idx].update(col);2293 self.data[col_idx].update(col);
2270 let width = col.width;2294 let width = col.width;
@@ -2937,7 +2961,7 @@ impl<W: LayoutElement> Column<W> {
2937 2961
2938 fn add_tile(&mut self, tile: Tile<W>, animate: bool) {2962 fn add_tile(&mut self, tile: Tile<W>, animate: bool) {
2939 self.is_fullscreen = false;2963 self.is_fullscreen = false;
2940- self.data.push(TileData::new(&tile, WindowHeight::Auto));2964+ self.data.push(TileData::new(&tile, WindowHeight::auto_1()));
2941 self.tiles.push(tile);2965 self.tiles.push(tile);
2942 self.update_tile_sizes(animate);2966 self.update_tile_sizes(animate);
2943 }2967 }
@@ -3020,7 +3044,7 @@ impl<W: LayoutElement> Column<W> {
3020 // Compute the tile heights. Start by converting window heights to tile heights.3044 // Compute the tile heights. Start by converting window heights to tile heights.
3021 let mut heights = zip(&self.tiles, &self.data)3045 let mut heights = zip(&self.tiles, &self.data)
3022 .map(|(tile, data)| match data.height {3046 .map(|(tile, data)| match data.height {
3023- WindowHeight::Auto => WindowHeight::Auto,3047+ auto @ WindowHeight::Auto { .. } => auto,
3024 WindowHeight::Fixed(height) => {3048 WindowHeight::Fixed(height) => {
3025 WindowHeight::Fixed(tile.tile_height_for_window_height(height.round().max(1.)))3049 WindowHeight::Fixed(tile.tile_height_for_window_height(height.round().max(1.)))
3026 }3050 }
@@ -3062,18 +3086,31 @@ impl<W: LayoutElement> Column<W> {
3062 // However, most max height uses are for fixed-size dialogs, where min height == max_height.3086 // However, most max height uses are for fixed-size dialogs, where min height == max_height.
3063 // This case is separately handled above.3087 // This case is separately handled above.
3064 while auto_tiles_left > 0 {3088 while auto_tiles_left > 0 {
3089+ let mut total_weight: f64 = heights
3090+ .iter()
3091+ .filter_map(|h| {
3092+ if let WindowHeight::Auto { weight } = *h {
3093+ Some(weight)
3094+ } else {
3095+ None
3096+ }
3097+ })
3098+ .sum();
3099+
3065 // Wayland requires us to round the requested size for a window to integer logical3100 // Wayland requires us to round the requested size for a window to integer logical
3066 // pixels, therefore we compute the remaining auto height dynamically.3101 // pixels, therefore we compute the remaining auto height dynamically.
3067 let mut height_left_2 = height_left;3102 let mut height_left_2 = height_left;
3068- let mut auto_tiles_left_2 = auto_tiles_left;3103+ let mut total_weight_2 = total_weight;
3069 let mut unsatisfied_min = false;3104 let mut unsatisfied_min = false;
3070 for ((h, tile), min_size) in zip(zip(&mut heights, &self.tiles), &min_size) {3105 for ((h, tile), min_size) in zip(zip(&mut heights, &self.tiles), &min_size) {
3071- if matches!(h, WindowHeight::Fixed(_)) {3106+ let weight = match *h {
3072- continue;3107+ WindowHeight::Auto { weight } => weight,
3073- }3108+ WindowHeight::Fixed(_) => continue,
3109+ };
3110+ let factor = weight / total_weight_2;
3074 3111
3075 // Compute the current auto height.3112 // Compute the current auto height.
3076- let auto = height_left_2 / auto_tiles_left_2 as f64;3113+ let auto = height_left_2 * factor;
3077 let mut auto = tile.tile_height_for_window_height(3114 let mut auto = tile.tile_height_for_window_height(
3078 tile.window_height_for_tile_height(auto).round().max(1.),3115 tile.window_height_for_tile_height(auto).round().max(1.),
3079 );3116 );
@@ -3087,8 +3124,8 @@ impl<W: LayoutElement> Column<W> {
3087 unsatisfied_min = true;3124 unsatisfied_min = true;
3088 }3125 }
3089 3126
3090- auto_tiles_left_2 -= 1;
3091 height_left_2 -= auto;3127 height_left_2 -= auto;
3128+ total_weight_2 -= weight;
3092 }3129 }
3093 3130
3094 // If some min height was unsatisfied, then we allocated the tile more than the auto3131 // If some min height was unsatisfied, then we allocated the tile more than the auto
@@ -3100,18 +3137,21 @@ impl<W: LayoutElement> Column<W> {
3100 3137
3101 // All min heights were satisfied, fill them in.3138 // All min heights were satisfied, fill them in.
3102 for (h, tile) in zip(&mut heights, &self.tiles) {3139 for (h, tile) in zip(&mut heights, &self.tiles) {
3103- if matches!(h, WindowHeight::Fixed(_)) {3140+ let weight = match *h {
3104- continue;3141+ WindowHeight::Auto { weight } => weight,
3105- }3142+ WindowHeight::Fixed(_) => continue,
3143+ };
3144+ let factor = weight / total_weight;
3106 3145
3107 // Compute the current auto height.3146 // Compute the current auto height.
3108- let auto = height_left / auto_tiles_left as f64;3147+ let auto = height_left * factor;
3109 let auto = tile.tile_height_for_window_height(3148 let auto = tile.tile_height_for_window_height(
3110 tile.window_height_for_tile_height(auto).round().max(1.),3149 tile.window_height_for_tile_height(auto).round().max(1.),
3111 );3150 );
3112 3151
3113 *h = WindowHeight::Fixed(auto);3152 *h = WindowHeight::Fixed(auto);
3114 height_left -= auto;3153 height_left -= auto;
3154+ total_weight -= weight;
3115 auto_tiles_left -= 1;3155 auto_tiles_left -= 1;
3116 }3156 }
3117 3157
@@ -3203,6 +3243,16 @@ impl<W: LayoutElement> Column<W> {
3203 assert_eq!(self.tiles.len(), 1);3243 assert_eq!(self.tiles.len(), 1);
3204 }3244 }
3205 3245
3246+ if self.tiles.len() == 1 {
3247+ if let WindowHeight::Auto { weight } = self.data[0].height {
3248+ assert_eq!(
3249+ weight, 1.,
3250+ "auto height weight must reset to 1 for a single window"
3251+ );
3252+ }
3253+ }
3254+
3255+ let mut found_fixed = false;
3206 for (tile, data) in zip(&self.tiles, &self.data) {3256 for (tile, data) in zip(&self.tiles, &self.data) {
3207 assert!(Rc::ptr_eq(&self.options, &tile.options));3257 assert!(Rc::ptr_eq(&self.options, &tile.options));
3208 assert_eq!(self.scale, tile.scale());3258 assert_eq!(self.scale, tile.scale());
@@ -3217,6 +3267,14 @@ impl<W: LayoutElement> Column<W> {
3217 let rounded = size.to_physical_precise_round(scale).to_logical(scale);3267 let rounded = size.to_physical_precise_round(scale).to_logical(scale);
3218 assert_abs_diff_eq!(size.w, rounded.w, epsilon = 1e-5);3268 assert_abs_diff_eq!(size.w, rounded.w, epsilon = 1e-5);
3219 assert_abs_diff_eq!(size.h, rounded.h, epsilon = 1e-5);3269 assert_abs_diff_eq!(size.h, rounded.h, epsilon = 1e-5);
3270+
3271+ if matches!(data.height, WindowHeight::Fixed(_)) {
3272+ assert!(
3273+ !found_fixed,
3274+ "there can only be one fixed-height window in a column"
3275+ );
3276+ found_fixed = true;
3277+ }
3220 }3278 }
3221 }3279 }
3222 3280
@@ -3310,10 +3368,19 @@ impl<W: LayoutElement> Column<W> {
3310 3368
3311 fn set_window_height(&mut self, change: SizeChange, tile_idx: Option<usize>, animate: bool) {3369 fn set_window_height(&mut self, change: SizeChange, tile_idx: Option<usize>, animate: bool) {
3312 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);3370 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);
3371+
3372+ // Start by converting all heights to automatic, since only one window in the column can be
3373+ // fixed-height. If the current tile is already fixed, however, we can skip that step.
3374+ // Which is not only for optimization, but also preserves automatic weights in case one
3375+ // window is resized in such a way that other windows hit their min size, and then back.
3376+ if !matches!(self.data[tile_idx].height, WindowHeight::Fixed(_)) {
3377+ self.convert_heights_to_auto();
3378+ }
3379+
3313 let current = self.data[tile_idx].height;3380 let current = self.data[tile_idx].height;
3314 let tile = &self.tiles[tile_idx];3381 let tile = &self.tiles[tile_idx];
3315 let current_window_px = match current {3382 let current_window_px = match current {
3316- WindowHeight::Auto => tile.window_size().h,3383+ WindowHeight::Auto { .. } => tile.window_size().h,
3317 WindowHeight::Fixed(height) => height,3384 WindowHeight::Fixed(height) => height,
3318 };3385 };
3319 let current_tile_px = tile.tile_height_for_window_height(current_window_px);3386 let current_tile_px = tile.tile_height_for_window_height(current_window_px);
@@ -3362,10 +3429,32 @@ impl<W: LayoutElement> Column<W> {
3362 3429
3363 fn reset_window_height(&mut self, tile_idx: Option<usize>, animate: bool) {3430 fn reset_window_height(&mut self, tile_idx: Option<usize>, animate: bool) {
3364 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);3431 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);
3365- self.data[tile_idx].height = WindowHeight::Auto;3432+ self.data[tile_idx].height = WindowHeight::auto_1();
3366 self.update_tile_sizes(animate);3433 self.update_tile_sizes(animate);
3367 }3434 }
3368 3435
3436+ /// Converts all heights in the column to automatic, preserving the apparent heights.
3437+ ///
3438+ /// All weights are recomputed to preserve the current tile heights while "centering" the
3439+ /// weights at the median window height (it gets weight = 1).
3440+ ///
3441+ /// One case where apparent heights will not be preserved is when the column is taller than the
3442+ /// working area.
3443+ fn convert_heights_to_auto(&mut self) {
3444+ let heights: Vec<_> = self.tiles.iter().map(|tile| tile.tile_size().h).collect();
3445+
3446+ // Weights are invariant to multiplication: a column with weights 2, 2, 1 is equivalent to
3447+ // a column with weights 4, 4, 2. So we find the median window height and use that as 1.
3448+ let mut sorted = heights.clone();
3449+ sorted.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
3450+ let median = sorted[sorted.len() / 2];
3451+
3452+ for (data, height) in zip(&mut self.data, heights) {
3453+ let weight = height / median;
3454+ data.height = WindowHeight::Auto { weight };
3455+ }
3456+ }
3457+
3369 fn set_fullscreen(&mut self, is_fullscreen: bool) {3458 fn set_fullscreen(&mut self, is_fullscreen: bool) {
3370 if self.is_fullscreen == is_fullscreen {3459 if self.is_fullscreen == is_fullscreen {
3371 return;3460 return;
@@ -3399,7 +3488,7 @@ impl<W: LayoutElement> Column<W> {
3399 3488
3400 // Chain with a dummy value to be able to get one past all tiles' Y.3489 // Chain with a dummy value to be able to get one past all tiles' Y.
3401 let dummy = TileData {3490 let dummy = TileData {
3402- height: WindowHeight::Auto,3491+ height: WindowHeight::auto_1(),
3403 size: Size::default(),3492 size: Size::default(),
3404 interactively_resizing_by_left_edge: false,3493 interactively_resizing_by_left_edge: false,
3405 };3494 };