forge
Explore
Log in

Don't you dare go Hollow

layout: Implement auto height distribution

Browse files at this commit

dc9cee4589495204f56c4cc53da1d18ea1f8764cIvan MolodetskikhParent: 8056dd0
Takes into account min and, partially, max window heights, and adds 1 px
when necessary to account for uneven division.
src/layout.rsmodified+123 -16
@@ -282,6 +282,17 @@ impl From<PresetWidth> for ColumnWidth {
282282 }
283283
284284 /// Height of a window in a column.
285+///
286+/// Proportional height is intentionally omitted. With column widths you frequently want e.g. two
287+/// columns side-by-side with 50% width each, and you want them to remain this way when moving to a
288+/// differently sized monitor. Windows in a column, however, already auto-size to fill the available
289+/// height, giving you this behavior. The only reason to set a different window height, then, is
290+/// when you want something in the window to fit exactly, e.g. to fit 30 lines in a terminal, which
291+/// corresponds to the `Fixed` variant.
292+///
293+/// This does not preclude the usual set of binds to set or resize a window proportionally. Just,
294+/// they are converted to, and stored as fixed height right away, so that once you resize a window
295+/// to fit the desired content, it can never become smaller than that when moving between monitors.
285296 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
286297 pub enum WindowHeight {
287298 /// Automatically computed height, evenly distributed across the column.
@@ -2651,10 +2662,6 @@ impl<W: LayoutElement> Column<W> {
26512662 }
26522663 }
26532664
2654- fn window_count(&self) -> usize {
2655- self.windows.len()
2656- }
2657-
26582665 fn set_width(&mut self, width: ColumnWidth) {
26592666 self.width = width;
26602667 self.update_window_sizes();
@@ -2682,11 +2689,14 @@ impl<W: LayoutElement> Column<W> {
26822689 return;
26832690 }
26842691
2685- let min_width = self
2686- .windows
2692+ let min_size: Vec<_> = self.windows.iter().map(LayoutElement::min_size).collect();
2693+ let max_size: Vec<_> = self.windows.iter().map(LayoutElement::max_size).collect();
2694+
2695+ // Compute the column width.
2696+ let min_width = min_size
26872697 .iter()
2688- .filter_map(|win| {
2689- let w = win.min_size().w;
2698+ .filter_map(|size| {
2699+ let w = size.w;
26902700 if w == 0 {
26912701 None
26922702 } else {
@@ -2695,11 +2705,10 @@ impl<W: LayoutElement> Column<W> {
26952705 })
26962706 .max()
26972707 .unwrap_or(1);
2698- let max_width = self
2699- .windows
2708+ let max_width = max_size
27002709 .iter()
2701- .filter_map(|win| {
2702- let w = win.max_size().w;
2710+ .filter_map(|size| {
2711+ let w = size.w;
27032712 if w == 0 {
27042713 None
27052714 } else {
@@ -2711,11 +2720,109 @@ impl<W: LayoutElement> Column<W> {
27112720 let max_width = max(max_width, min_width);
27122721
27132722 let width = self.width.resolve(&self.options, self.working_area.size.w);
2714- let height = (self.working_area.size.h - self.options.gaps) / self.window_count() as i32
2715- - self.options.gaps;
2716- let size = Size::from((max(min(width, max_width), min_width), max(height, 1)));
2723+ let width = max(min(width, max_width), min_width);
2724+
2725+ // Compute the window heights.
2726+ let mut heights = self.heights.clone();
2727+ let mut height_left = self.working_area.size.h - self.options.gaps;
2728+ let mut auto_windows_left = self.windows.len();
2729+
2730+ // Subtract all fixed-height windows.
2731+ for (h, (min_size, max_size)) in zip(&mut heights, zip(&min_size, &max_size)) {
2732+ // Check if the window has an exact height constraint.
2733+ if min_size.h > 0 && min_size.h == max_size.h {
2734+ *h = WindowHeight::Fixed(min_size.h);
2735+ }
2736+
2737+ if let WindowHeight::Fixed(h) = h {
2738+ if max_size.h > 0 {
2739+ *h = min(*h, max_size.h);
2740+ }
2741+ if min_size.h > 0 {
2742+ *h = max(*h, min_size.h);
2743+ }
2744+ *h = max(*h, 1);
2745+
2746+ height_left -= *h + self.options.gaps;
2747+ auto_windows_left -= 1;
2748+ }
2749+ }
2750+
2751+ // Iteratively try to distribute the remaining height, checking against window min heights.
2752+ // Pick an auto height according to the current sizes, then check if it satisfies all
2753+ // remaining min heights. If not, allocate fixed height to those windows and repeat the
2754+ // loop. On each iteration the auto height will get smaller.
2755+ //
2756+ // NOTE: we do not respect max height here. Doing so would complicate things: if the current
2757+ // auto height is above some window's max height, then the auto height can become larger.
2758+ // Combining this with the min height loop is where the complexity appears.
2759+ //
2760+ // However, most max height uses are for fixed-size dialogs, where min height == max_height.
2761+ // This case is separately handled above.
2762+ while auto_windows_left > 0 {
2763+ // Compute the current auto height.
2764+ let auto_height = height_left / auto_windows_left as i32 - self.options.gaps;
2765+ let auto_height = max(auto_height, 1);
2766+
2767+ // Integer division above can result in imperfect height distribution. We will make some
2768+ // windows 1 px taller to account for this.
2769+ let mut ones_left = height_left
2770+ .saturating_sub((auto_height + self.options.gaps) * auto_windows_left as i32);
2771+
2772+ let mut unsatisfied_min = false;
2773+ let mut ones_left_2 = ones_left;
2774+ for (h, min_size) in zip(&mut heights, &min_size) {
2775+ if matches!(h, WindowHeight::Fixed(_)) {
2776+ continue;
2777+ }
2778+
2779+ let mut auto = auto_height;
2780+ if ones_left_2 > 0 {
2781+ auto += 1;
2782+ ones_left_2 -= 1;
2783+ }
2784+
2785+ // Check if the auto height satisfies the min height.
2786+ if min_size.h > 0 && min_size.h > auto {
2787+ *h = WindowHeight::Fixed(min_size.h);
2788+ height_left -= min_size.h + self.options.gaps;
2789+ auto_windows_left -= 1;
2790+ unsatisfied_min = true;
2791+ }
2792+ }
2793+
2794+ // If some min height was unsatisfied, then we allocated the window more than the auto
2795+ // height, which means that the remaining auto windows now have less height to work
2796+ // with, and the loop must run again.
2797+ if unsatisfied_min {
2798+ continue;
2799+ }
2800+
2801+ // All min heights were satisfied, fill them in.
2802+ for h in &mut heights {
2803+ if matches!(h, WindowHeight::Fixed(_)) {
2804+ continue;
2805+ }
2806+
2807+ let mut auto = auto_height;
2808+ if ones_left > 0 {
2809+ auto += 1;
2810+ ones_left -= 1;
2811+ }
2812+
2813+ *h = WindowHeight::Fixed(auto);
2814+ auto_windows_left -= 1;
2815+ }
2816+
2817+ assert_eq!(auto_windows_left, 0);
2818+ }
2819+
2820+ for (win, h) in zip(&self.windows, heights) {
2821+ let WindowHeight::Fixed(height) = h else {
2822+ unreachable!()
2823+ };
27172824
2718- for win in &self.windows {
2825+ let size = Size::from((width, height));
27192826 win.request_size(size);
27202827 }
27212828 }