Implement preset window heights
Browse files at this commitd0e624e6153e1a22ea0737e6830ed4edd7333304Parent: 087a50a6 changed files +236 -51
- niri-config/src/lib.rs +30 -12
- niri-ipc/src/lib.rs +2 -0
- src/input/mod.rs +3 -0
- src/layout/mod.rs +71 -13
- src/layout/monitor.rs +4 -0
- src/layout/workspace.rs +126 -26
niri-config/src/lib.rsmodified+30 -12
| @@ -392,9 +392,11 @@ pub struct Layout { | |||
| 392 | #[knuffel(child, default)] | 392 | #[knuffel(child, default)] |
| 393 | pub border: Border, | 393 | pub border: Border, |
| 394 | #[knuffel(child, unwrap(children), default)] | 394 | #[knuffel(child, unwrap(children), default)] |
| 395 | - pub preset_column_widths: Vec<PresetWidth>, | 395 | + pub preset_column_widths: Vec<PresetSize>, |
| 396 | #[knuffel(child)] | 396 | #[knuffel(child)] |
| 397 | - pub default_column_width: Option<DefaultColumnWidth>, | 397 | + pub default_column_width: Option<DefaultPresetSize>, |
| 398 | + #[knuffel(child, unwrap(children), default)] | ||
| 399 | + pub preset_window_heights: Vec<PresetSize>, | ||
| 398 | #[knuffel(child, unwrap(argument), default)] | 400 | #[knuffel(child, unwrap(argument), default)] |
| 399 | pub center_focused_column: CenterFocusedColumn, | 401 | pub center_focused_column: CenterFocusedColumn, |
| 400 | #[knuffel(child)] | 402 | #[knuffel(child)] |
| @@ -416,6 +418,7 @@ impl Default for Layout { | |||
| 416 | always_center_single_column: false, | 418 | always_center_single_column: false, |
| 417 | gaps: FloatOrInt(16.), | 419 | gaps: FloatOrInt(16.), |
| 418 | struts: Default::default(), | 420 | struts: Default::default(), |
| 421 | + preset_window_heights: Default::default(), | ||
| 419 | } | 422 | } |
| 420 | } | 423 | } |
| 421 | } | 424 | } |
| @@ -624,13 +627,13 @@ impl Default for Cursor { | |||
| 624 | } | 627 | } |
| 625 | 628 | ||
| 626 | #[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)] | 629 | #[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)] |
| 627 | -pub enum PresetWidth { | 630 | +pub enum PresetSize { |
| 628 | Proportion(#[knuffel(argument)] f64), | 631 | Proportion(#[knuffel(argument)] f64), |
| 629 | Fixed(#[knuffel(argument)] i32), | 632 | Fixed(#[knuffel(argument)] i32), |
| 630 | } | 633 | } |
| 631 | 634 | ||
| 632 | #[derive(Debug, Clone, PartialEq)] | 635 | #[derive(Debug, Clone, PartialEq)] |
| 633 | -pub struct DefaultColumnWidth(pub Option<PresetWidth>); | 636 | +pub struct DefaultPresetSize(pub Option<PresetSize>); |
| 634 | 637 | ||
| 635 | #[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)] | 638 | #[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)] |
| 636 | pub struct Struts { | 639 | pub struct Struts { |
| @@ -898,7 +901,7 @@ pub struct WindowRule { | |||
| 898 | 901 | ||
| 899 | // Rules applied at initial configure. | 902 | // Rules applied at initial configure. |
| 900 | #[knuffel(child)] | 903 | #[knuffel(child)] |
| 901 | - pub default_column_width: Option<DefaultColumnWidth>, | 904 | + pub default_column_width: Option<DefaultPresetSize>, |
| 902 | #[knuffel(child, unwrap(argument))] | 905 | #[knuffel(child, unwrap(argument))] |
| 903 | pub open_on_output: Option<String>, | 906 | pub open_on_output: Option<String>, |
| 904 | #[knuffel(child, unwrap(argument))] | 907 | #[knuffel(child, unwrap(argument))] |
| @@ -1155,6 +1158,7 @@ pub enum Action { | |||
| 1155 | #[knuffel(skip)] | 1158 | #[knuffel(skip)] |
| 1156 | ResetWindowHeightById(u64), | 1159 | ResetWindowHeightById(u64), |
| 1157 | SwitchPresetColumnWidth, | 1160 | SwitchPresetColumnWidth, |
| 1161 | + SwitchPresetWindowHeight, | ||
| 1158 | MaximizeColumn, | 1162 | MaximizeColumn, |
| 1159 | SetColumnWidth(#[knuffel(argument, str)] SizeChange), | 1163 | SetColumnWidth(#[knuffel(argument, str)] SizeChange), |
| 1160 | SwitchLayout(#[knuffel(argument, str)] LayoutSwitchTarget), | 1164 | SwitchLayout(#[knuffel(argument, str)] LayoutSwitchTarget), |
| @@ -1266,6 +1270,7 @@ impl From<niri_ipc::Action> for Action { | |||
| 1266 | niri_ipc::Action::ResetWindowHeight { id: None } => Self::ResetWindowHeight, | 1270 | niri_ipc::Action::ResetWindowHeight { id: None } => Self::ResetWindowHeight, |
| 1267 | niri_ipc::Action::ResetWindowHeight { id: Some(id) } => Self::ResetWindowHeightById(id), | 1271 | niri_ipc::Action::ResetWindowHeight { id: Some(id) } => Self::ResetWindowHeightById(id), |
| 1268 | niri_ipc::Action::SwitchPresetColumnWidth {} => Self::SwitchPresetColumnWidth, | 1272 | niri_ipc::Action::SwitchPresetColumnWidth {} => Self::SwitchPresetColumnWidth, |
| 1273 | + niri_ipc::Action::SwitchPresetWindowHeight {} => Self::SwitchPresetWindowHeight, | ||
| 1269 | niri_ipc::Action::MaximizeColumn {} => Self::MaximizeColumn, | 1274 | niri_ipc::Action::MaximizeColumn {} => Self::MaximizeColumn, |
| 1270 | niri_ipc::Action::SetColumnWidth { change } => Self::SetColumnWidth(change), | 1275 | niri_ipc::Action::SetColumnWidth { change } => Self::SetColumnWidth(change), |
| 1271 | niri_ipc::Action::SwitchLayout { layout } => Self::SwitchLayout(layout), | 1276 | niri_ipc::Action::SwitchLayout { layout } => Self::SwitchLayout(layout), |
| @@ -1882,7 +1887,7 @@ impl OutputName { | |||
| 1882 | } | 1887 | } |
| 1883 | } | 1888 | } |
| 1884 | 1889 | ||
| 1885 | -impl<S> knuffel::Decode<S> for DefaultColumnWidth | 1890 | +impl<S> knuffel::Decode<S> for DefaultPresetSize |
| 1886 | where | 1891 | where |
| 1887 | S: knuffel::traits::ErrorSpan, | 1892 | S: knuffel::traits::ErrorSpan, |
| 1888 | { | 1893 | { |
| @@ -1902,7 +1907,7 @@ where | |||
| 1902 | "expected no more than one child", | 1907 | "expected no more than one child", |
| 1903 | )); | 1908 | )); |
| 1904 | } | 1909 | } |
| 1905 | - PresetWidth::decode_node(child, ctx).map(Some).map(Self) | 1910 | + PresetSize::decode_node(child, ctx).map(Some).map(Self) |
| 1906 | } else { | 1911 | } else { |
| 1907 | Ok(Self(None)) | 1912 | Ok(Self(None)) |
| 1908 | } | 1913 | } |
| @@ -2914,6 +2919,13 @@ mod tests { | |||
| 2914 | fixed 1280 | 2919 | fixed 1280 |
| 2915 | } | 2920 | } |
| 2916 | 2921 | ||
| 2922 | + preset-window-heights { | ||
| 2923 | + proportion 0.25 | ||
| 2924 | + proportion 0.5 | ||
| 2925 | + fixed 960 | ||
| 2926 | + fixed 1280 | ||
| 2927 | + } | ||
| 2928 | + | ||
| 2917 | default-column-width { proportion 0.25; } | 2929 | default-column-width { proportion 0.25; } |
| 2918 | 2930 | ||
| 2919 | gaps 8 | 2931 | gaps 8 |
| @@ -3104,14 +3116,20 @@ mod tests { | |||
| 3104 | inactive_gradient: None, | 3116 | inactive_gradient: None, |
| 3105 | }, | 3117 | }, |
| 3106 | preset_column_widths: vec![ | 3118 | preset_column_widths: vec![ |
| 3107 | - PresetWidth::Proportion(0.25), | 3119 | + PresetSize::Proportion(0.25), |
| 3108 | - PresetWidth::Proportion(0.5), | 3120 | + PresetSize::Proportion(0.5), |
| 3109 | - PresetWidth::Fixed(960), | 3121 | + PresetSize::Fixed(960), |
| 3110 | - PresetWidth::Fixed(1280), | 3122 | + PresetSize::Fixed(1280), |
| 3111 | ], | 3123 | ], |
| 3112 | - default_column_width: Some(DefaultColumnWidth(Some(PresetWidth::Proportion( | 3124 | + default_column_width: Some(DefaultPresetSize(Some(PresetSize::Proportion( |
| 3113 | 0.25, | 3125 | 0.25, |
| 3114 | )))), | 3126 | )))), |
| 3127 | + preset_window_heights: vec![ | ||
| 3128 | + PresetSize::Proportion(0.25), | ||
| 3129 | + PresetSize::Proportion(0.5), | ||
| 3130 | + PresetSize::Fixed(960), | ||
| 3131 | + PresetSize::Fixed(1280), | ||
| 3132 | + ], | ||
| 3115 | gaps: FloatOrInt(8.), | 3133 | gaps: FloatOrInt(8.), |
| 3116 | struts: Struts { | 3134 | struts: Struts { |
| 3117 | left: FloatOrInt(1.), | 3135 | left: FloatOrInt(1.), |
niri-ipc/src/lib.rsmodified+2 -0
| @@ -350,6 +350,8 @@ pub enum Action { | |||
| 350 | }, | 350 | }, |
| 351 | /// Switch between preset column widths. | 351 | /// Switch between preset column widths. |
| 352 | SwitchPresetColumnWidth {}, | 352 | SwitchPresetColumnWidth {}, |
| 353 | + /// Switch between preset window heights. | ||
| 354 | + SwitchPresetWindowHeight {}, | ||
| 353 | /// Toggle the maximized state of the focused column. | 355 | /// Toggle the maximized state of the focused column. |
| 354 | MaximizeColumn {}, | 356 | MaximizeColumn {}, |
| 355 | /// Change the width of the focused column. | 357 | /// Change the width of the focused column. |
src/input/mod.rsmodified+3 -0
| @@ -1035,6 +1035,9 @@ impl State { | |||
| 1035 | Action::SwitchPresetColumnWidth => { | 1035 | Action::SwitchPresetColumnWidth => { |
| 1036 | self.niri.layout.toggle_width(); | 1036 | self.niri.layout.toggle_width(); |
| 1037 | } | 1037 | } |
| 1038 | + Action::SwitchPresetWindowHeight => { | ||
| 1039 | + self.niri.layout.toggle_window_height(); | ||
| 1040 | + } | ||
| 1038 | Action::CenterColumn => { | 1041 | Action::CenterColumn => { |
| 1039 | self.niri.layout.center_column(); | 1042 | self.niri.layout.center_column(); |
| 1040 | // FIXME: granular | 1043 | // FIXME: granular |
src/layout/mod.rsmodified+71 -13
| @@ -34,7 +34,9 @@ use std::mem; | |||
| 34 | use std::rc::Rc; | 34 | use std::rc::Rc; |
| 35 | use std::time::Duration; | 35 | use std::time::Duration; |
| 36 | 36 | ||
| 37 | -use niri_config::{CenterFocusedColumn, Config, FloatOrInt, Struts, Workspace as WorkspaceConfig}; | 37 | +use niri_config::{ |
| 38 | + CenterFocusedColumn, Config, FloatOrInt, PresetSize, Struts, Workspace as WorkspaceConfig, | ||
| 39 | +}; | ||
| 38 | use niri_ipc::SizeChange; | 40 | use niri_ipc::SizeChange; |
| 39 | use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; | 41 | use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; |
| 40 | use smithay::backend::renderer::element::Id; | 42 | use smithay::backend::renderer::element::Id; |
| @@ -238,11 +240,12 @@ pub struct Options { | |||
| 238 | pub center_focused_column: CenterFocusedColumn, | 240 | pub center_focused_column: CenterFocusedColumn, |
| 239 | pub always_center_single_column: bool, | 241 | pub always_center_single_column: bool, |
| 240 | /// Column widths that `toggle_width()` switches between. | 242 | /// Column widths that `toggle_width()` switches between. |
| 241 | - pub preset_widths: Vec<ColumnWidth>, | 243 | + pub preset_column_widths: Vec<ColumnWidth>, |
| 242 | /// Initial width for new columns. | 244 | /// Initial width for new columns. |
| 243 | - pub default_width: Option<ColumnWidth>, | 245 | + pub default_column_width: Option<ColumnWidth>, |
| 246 | + /// Window height that `toggle_window_height()` switches between. | ||
| 247 | + pub preset_window_heights: Vec<PresetSize>, | ||
| 244 | pub animations: niri_config::Animations, | 248 | pub animations: niri_config::Animations, |
| 245 | - | ||
| 246 | // Debug flags. | 249 | // Debug flags. |
| 247 | pub disable_resize_throttling: bool, | 250 | pub disable_resize_throttling: bool, |
| 248 | pub disable_transactions: bool, | 251 | pub disable_transactions: bool, |
| @@ -257,15 +260,20 @@ impl Default for Options { | |||
| 257 | border: Default::default(), | 260 | border: Default::default(), |
| 258 | center_focused_column: Default::default(), | 261 | center_focused_column: Default::default(), |
| 259 | always_center_single_column: false, | 262 | always_center_single_column: false, |
| 260 | - preset_widths: vec![ | 263 | + preset_column_widths: vec![ |
| 261 | ColumnWidth::Proportion(1. / 3.), | 264 | ColumnWidth::Proportion(1. / 3.), |
| 262 | ColumnWidth::Proportion(0.5), | 265 | ColumnWidth::Proportion(0.5), |
| 263 | ColumnWidth::Proportion(2. / 3.), | 266 | ColumnWidth::Proportion(2. / 3.), |
| 264 | ], | 267 | ], |
| 265 | - default_width: None, | 268 | + default_column_width: None, |
| 266 | animations: Default::default(), | 269 | animations: Default::default(), |
| 267 | disable_resize_throttling: false, | 270 | disable_resize_throttling: false, |
| 268 | disable_transactions: false, | 271 | disable_transactions: false, |
| 272 | + preset_window_heights: vec![ | ||
| 273 | + PresetSize::Proportion(1. / 3.), | ||
| 274 | + PresetSize::Proportion(0.5), | ||
| 275 | + PresetSize::Proportion(2. / 3.), | ||
| 276 | + ], | ||
| 269 | } | 277 | } |
| 270 | } | 278 | } |
| 271 | } | 279 | } |
| @@ -273,21 +281,26 @@ impl Default for Options { | |||
| 273 | impl Options { | 281 | impl Options { |
| 274 | fn from_config(config: &Config) -> Self { | 282 | fn from_config(config: &Config) -> Self { |
| 275 | let layout = &config.layout; | 283 | let layout = &config.layout; |
| 276 | - let preset_column_widths = &layout.preset_column_widths; | ||
| 277 | 284 | ||
| 278 | - let preset_widths = if preset_column_widths.is_empty() { | 285 | + let preset_column_widths = if layout.preset_column_widths.is_empty() { |
| 279 | - Options::default().preset_widths | 286 | + Options::default().preset_column_widths |
| 280 | } else { | 287 | } else { |
| 281 | - preset_column_widths | 288 | + layout |
| 289 | + .preset_column_widths | ||
| 282 | .iter() | 290 | .iter() |
| 283 | .copied() | 291 | .copied() |
| 284 | .map(ColumnWidth::from) | 292 | .map(ColumnWidth::from) |
| 285 | .collect() | 293 | .collect() |
| 286 | }; | 294 | }; |
| 295 | + let preset_window_heights = if layout.preset_window_heights.is_empty() { | ||
| 296 | + Options::default().preset_window_heights | ||
| 297 | + } else { | ||
| 298 | + layout.preset_window_heights.clone() | ||
| 299 | + }; | ||
| 287 | 300 | ||
| 288 | // Missing default_column_width maps to Some(ColumnWidth::Proportion(0.5)), | 301 | // Missing default_column_width maps to Some(ColumnWidth::Proportion(0.5)), |
| 289 | // while present, but empty, maps to None. | 302 | // while present, but empty, maps to None. |
| 290 | - let default_width = layout | 303 | + let default_column_width = layout |
| 291 | .default_column_width | 304 | .default_column_width |
| 292 | .as_ref() | 305 | .as_ref() |
| 293 | .map(|w| w.0.map(ColumnWidth::from)) | 306 | .map(|w| w.0.map(ColumnWidth::from)) |
| @@ -300,11 +313,12 @@ impl Options { | |||
| 300 | border: layout.border, | 313 | border: layout.border, |
| 301 | center_focused_column: layout.center_focused_column, | 314 | center_focused_column: layout.center_focused_column, |
| 302 | always_center_single_column: layout.always_center_single_column, | 315 | always_center_single_column: layout.always_center_single_column, |
| 303 | - preset_widths, | 316 | + preset_column_widths, |
| 304 | - default_width, | 317 | + default_column_width, |
| 305 | animations: config.animations.clone(), | 318 | animations: config.animations.clone(), |
| 306 | disable_resize_throttling: config.debug.disable_resize_throttling, | 319 | disable_resize_throttling: config.debug.disable_resize_throttling, |
| 307 | disable_transactions: config.debug.disable_transactions, | 320 | disable_transactions: config.debug.disable_transactions, |
| 321 | + preset_window_heights, | ||
| 308 | } | 322 | } |
| 309 | } | 323 | } |
| 310 | 324 | ||
| @@ -1937,6 +1951,13 @@ impl<W: LayoutElement> Layout<W> { | |||
| 1937 | monitor.toggle_width(); | 1951 | monitor.toggle_width(); |
| 1938 | } | 1952 | } |
| 1939 | 1953 | ||
| 1954 | + pub fn toggle_window_height(&mut self) { | ||
| 1955 | + let Some(monitor) = self.active_monitor() else { | ||
| 1956 | + return; | ||
| 1957 | + }; | ||
| 1958 | + monitor.toggle_window_height(); | ||
| 1959 | + } | ||
| 1960 | + | ||
| 1940 | pub fn toggle_full_width(&mut self) { | 1961 | pub fn toggle_full_width(&mut self) { |
| 1941 | let Some(monitor) = self.active_monitor() else { | 1962 | let Some(monitor) = self.active_monitor() else { |
| 1942 | return; | 1963 | return; |
| @@ -3027,6 +3048,7 @@ mod tests { | |||
| 3027 | }, | 3048 | }, |
| 3028 | MoveColumnToOutput(#[proptest(strategy = "1..=5u8")] u8), | 3049 | MoveColumnToOutput(#[proptest(strategy = "1..=5u8")] u8), |
| 3029 | SwitchPresetColumnWidth, | 3050 | SwitchPresetColumnWidth, |
| 3051 | + SwitchPresetWindowHeight, | ||
| 3030 | MaximizeColumn, | 3052 | MaximizeColumn, |
| 3031 | SetColumnWidth(#[proptest(strategy = "arbitrary_size_change()")] SizeChange), | 3053 | SetColumnWidth(#[proptest(strategy = "arbitrary_size_change()")] SizeChange), |
| 3032 | SetWindowHeight { | 3054 | SetWindowHeight { |
| @@ -3453,6 +3475,7 @@ mod tests { | |||
| 3453 | Op::MoveWorkspaceDown => layout.move_workspace_down(), | 3475 | Op::MoveWorkspaceDown => layout.move_workspace_down(), |
| 3454 | Op::MoveWorkspaceUp => layout.move_workspace_up(), | 3476 | Op::MoveWorkspaceUp => layout.move_workspace_up(), |
| 3455 | Op::SwitchPresetColumnWidth => layout.toggle_width(), | 3477 | Op::SwitchPresetColumnWidth => layout.toggle_width(), |
| 3478 | + Op::SwitchPresetWindowHeight => layout.toggle_window_height(), | ||
| 3456 | Op::MaximizeColumn => layout.toggle_full_width(), | 3479 | Op::MaximizeColumn => layout.toggle_full_width(), |
| 3457 | Op::SetColumnWidth(change) => layout.set_column_width(change), | 3480 | Op::SetColumnWidth(change) => layout.set_column_width(change), |
| 3458 | Op::SetWindowHeight { id, change } => { | 3481 | Op::SetWindowHeight { id, change } => { |
| @@ -4415,6 +4438,41 @@ mod tests { | |||
| 4415 | layout.verify_invariants(); | 4438 | layout.verify_invariants(); |
| 4416 | } | 4439 | } |
| 4417 | 4440 | ||
| 4441 | + #[test] | ||
| 4442 | + fn preset_height_change_removes_preset() { | ||
| 4443 | + let mut config = Config::default(); | ||
| 4444 | + config.layout.preset_window_heights = vec![PresetSize::Fixed(1), PresetSize::Fixed(2)]; | ||
| 4445 | + | ||
| 4446 | + let mut layout = Layout::new(&config); | ||
| 4447 | + | ||
| 4448 | + let ops = [ | ||
| 4449 | + Op::AddOutput(1), | ||
| 4450 | + Op::AddWindow { | ||
| 4451 | + id: 1, | ||
| 4452 | + bbox: Rectangle::from_loc_and_size((0, 0), (1280, 200)), | ||
| 4453 | + min_max_size: Default::default(), | ||
| 4454 | + }, | ||
| 4455 | + Op::AddWindow { | ||
| 4456 | + id: 2, | ||
| 4457 | + bbox: Rectangle::from_loc_and_size((0, 0), (1280, 200)), | ||
| 4458 | + min_max_size: Default::default(), | ||
| 4459 | + }, | ||
| 4460 | + Op::ConsumeOrExpelWindowLeft, | ||
| 4461 | + Op::SwitchPresetWindowHeight, | ||
| 4462 | + Op::SwitchPresetWindowHeight, | ||
| 4463 | + ]; | ||
| 4464 | + for op in ops { | ||
| 4465 | + op.apply(&mut layout); | ||
| 4466 | + } | ||
| 4467 | + | ||
| 4468 | + // Leave only one. | ||
| 4469 | + config.layout.preset_window_heights = vec![PresetSize::Fixed(1)]; | ||
| 4470 | + | ||
| 4471 | + layout.update_config(&config); | ||
| 4472 | + | ||
| 4473 | + layout.verify_invariants(); | ||
| 4474 | + } | ||
| 4475 | + | ||
| 4418 | #[test] | 4476 | #[test] |
| 4419 | fn working_area_starts_at_physical_pixel() { | 4477 | fn working_area_starts_at_physical_pixel() { |
| 4420 | let struts = Struts { | 4478 | let struts = Struts { |
src/layout/monitor.rsmodified+4 -0
| @@ -740,6 +740,10 @@ impl<W: LayoutElement> Monitor<W> { | |||
| 740 | self.active_workspace().toggle_width(); | 740 | self.active_workspace().toggle_width(); |
| 741 | } | 741 | } |
| 742 | 742 | ||
| 743 | + pub fn toggle_window_height(&mut self) { | ||
| 744 | + self.active_workspace().toggle_window_height(); | ||
| 745 | + } | ||
| 746 | + | ||
| 743 | pub fn toggle_full_width(&mut self) { | 747 | pub fn toggle_full_width(&mut self) { |
| 744 | self.active_workspace().toggle_full_width(); | 748 | self.active_workspace().toggle_full_width(); |
| 745 | } | 749 | } |
src/layout/workspace.rsmodified+126 -26
| @@ -4,7 +4,7 @@ use std::rc::Rc; | |||
| 4 | use std::time::Duration; | 4 | use std::time::Duration; |
| 5 | 5 | ||
| 6 | use niri_config::{ | 6 | use niri_config::{ |
| 7 | - CenterFocusedColumn, OutputName, PresetWidth, Struts, Workspace as WorkspaceConfig, | 7 | + CenterFocusedColumn, OutputName, PresetSize, Struts, Workspace as WorkspaceConfig, |
| 8 | }; | 8 | }; |
| 9 | use niri_ipc::SizeChange; | 9 | use niri_ipc::SizeChange; |
| 10 | use ordered_float::NotNan; | 10 | use ordered_float::NotNan; |
| @@ -202,16 +202,17 @@ pub enum ColumnWidth { | |||
| 202 | 202 | ||
| 203 | /// Height of a window in a column. | 203 | /// Height of a window in a column. |
| 204 | /// | 204 | /// |
| 205 | -/// Proportional height is intentionally omitted. With column widths you frequently want e.g. two | 205 | +/// Every window but one in a column must be `Auto`-sized so that the total height can add up to |
| 206 | -/// columns side-by-side with 50% width each, and you want them to remain this way when moving to a | 206 | +/// the workspace height. Resizing a window converts all other windows to `Auto`, weighted to |
| 207 | -/// differently sized monitor. Windows in a column, however, already auto-size to fill the available | 207 | +/// preserve their visual heights at the moment of the conversion. |
| 208 | -/// height, giving you this behavior. The only reason to set a different window height, then, is | ||
| 209 | -/// when you want something in the window to fit exactly, e.g. to fit 30 lines in a terminal, which | ||
| 210 | -/// corresponds to the `Fixed` variant. | ||
| 211 | /// | 208 | /// |
| 212 | -/// This does not preclude the usual set of binds to set or resize a window proportionally. Just, | 209 | +/// In contrast to column widths, proportional height changes are converted to, and stored as, |
| 213 | -/// they are converted to, and stored as fixed height right away, so that once you resize a window | 210 | +/// fixed height right away. With column widths you frequently want e.g. two columns side-by-side |
| 214 | -/// to fit the desired content, it can never become smaller than that when moving between monitors. | 211 | +/// with 50% width each, and you want them to remain this way when moving to a differently sized |
| 212 | +/// monitor. Windows in a column, however, already auto-size to fill the available height, giving | ||
| 213 | +/// you this behavior. The main reason to set a different window height, then, is when you want | ||
| 214 | +/// something in the window to fit exactly, e.g. to fit 30 lines in a terminal, which corresponds | ||
| 215 | +/// to the `Fixed` variant. | ||
| 215 | #[derive(Debug, Clone, Copy, PartialEq)] | 216 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 216 | pub enum WindowHeight { | 217 | pub enum WindowHeight { |
| 217 | /// Automatically computed *tile* height, distributed across the column according to weights. | 218 | /// Automatically computed *tile* height, distributed across the column according to weights. |
| @@ -221,6 +222,16 @@ pub enum WindowHeight { | |||
| 221 | Auto { weight: f64 }, | 222 | Auto { weight: f64 }, |
| 222 | /// Fixed *window* height in logical pixels. | 223 | /// Fixed *window* height in logical pixels. |
| 223 | Fixed(f64), | 224 | Fixed(f64), |
| 225 | + /// One of the *tile* height proportion presets. | ||
| 226 | + Preset(usize), | ||
| 227 | +} | ||
| 228 | + | ||
| 229 | +#[derive(Debug, Clone, Copy)] | ||
| 230 | +pub enum ResolvedSize { | ||
| 231 | + /// Size of the tile including borders. | ||
| 232 | + Tile(f64), | ||
| 233 | + /// Size of the window excluding borders. | ||
| 234 | + Window(f64), | ||
| 224 | } | 235 | } |
| 225 | 236 | ||
| 226 | #[derive(Debug)] | 237 | #[derive(Debug)] |
| @@ -319,18 +330,29 @@ impl ColumnWidth { | |||
| 319 | ColumnWidth::Proportion(proportion) => { | 330 | ColumnWidth::Proportion(proportion) => { |
| 320 | (view_width - options.gaps) * proportion - options.gaps | 331 | (view_width - options.gaps) * proportion - options.gaps |
| 321 | } | 332 | } |
| 322 | - ColumnWidth::Preset(idx) => options.preset_widths[idx].resolve(options, view_width), | 333 | + ColumnWidth::Preset(idx) => { |
| 334 | + options.preset_column_widths[idx].resolve(options, view_width) | ||
| 335 | + } | ||
| 323 | ColumnWidth::Fixed(width) => width, | 336 | ColumnWidth::Fixed(width) => width, |
| 324 | } | 337 | } |
| 325 | } | 338 | } |
| 326 | } | 339 | } |
| 327 | 340 | ||
| 328 | -impl From<PresetWidth> for ColumnWidth { | 341 | +impl From<PresetSize> for ColumnWidth { |
| 329 | - fn from(value: PresetWidth) -> Self { | 342 | + fn from(value: PresetSize) -> Self { |
| 330 | match value { | 343 | match value { |
| 331 | - PresetWidth::Proportion(p) => Self::Proportion(p.clamp(0., 10000.)), | 344 | + PresetSize::Proportion(p) => Self::Proportion(p.clamp(0., 10000.)), |
| 332 | - PresetWidth::Fixed(f) => Self::Fixed(f64::from(f.clamp(1, 100000))), | 345 | + PresetSize::Fixed(f) => Self::Fixed(f64::from(f.clamp(1, 100000))), |
| 346 | + } | ||
| 347 | + } | ||
| 348 | +} | ||
| 349 | + | ||
| 350 | +fn resolve_preset_size(preset: PresetSize, options: &Options, view_size: f64) -> ResolvedSize { | ||
| 351 | + match preset { | ||
| 352 | + PresetSize::Proportion(proportion) => { | ||
| 353 | + ResolvedSize::Tile((view_size - options.gaps) * proportion - options.gaps) | ||
| 333 | } | 354 | } |
| 355 | + PresetSize::Fixed(width) => ResolvedSize::Window(f64::from(width)), | ||
| 334 | } | 356 | } |
| 335 | } | 357 | } |
| 336 | 358 | ||
| @@ -650,7 +672,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 650 | match default_width { | 672 | match default_width { |
| 651 | Some(Some(width)) => Some(width), | 673 | Some(Some(width)) => Some(width), |
| 652 | Some(None) => None, | 674 | Some(None) => None, |
| 653 | - None => self.options.default_width, | 675 | + None => self.options.default_column_width, |
| 654 | } | 676 | } |
| 655 | } | 677 | } |
| 656 | 678 | ||
| @@ -2351,6 +2373,17 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 2351 | cancel_resize_for_column(&mut self.interactive_resize, col); | 2373 | cancel_resize_for_column(&mut self.interactive_resize, col); |
| 2352 | } | 2374 | } |
| 2353 | 2375 | ||
| 2376 | + pub fn toggle_window_height(&mut self) { | ||
| 2377 | + if self.columns.is_empty() { | ||
| 2378 | + return; | ||
| 2379 | + } | ||
| 2380 | + | ||
| 2381 | + let col = &mut self.columns[self.active_column_idx]; | ||
| 2382 | + col.toggle_window_height(None, true); | ||
| 2383 | + | ||
| 2384 | + cancel_resize_for_column(&mut self.interactive_resize, col); | ||
| 2385 | + } | ||
| 2386 | + | ||
| 2354 | pub fn set_fullscreen(&mut self, window: &W::Id, is_fullscreen: bool) { | 2387 | pub fn set_fullscreen(&mut self, window: &W::Id, is_fullscreen: bool) { |
| 2355 | let (mut col_idx, tile_idx) = self | 2388 | let (mut col_idx, tile_idx) = self |
| 2356 | .columns | 2389 | .columns |
| @@ -3016,12 +3049,18 @@ impl<W: LayoutElement> Column<W> { | |||
| 3016 | let mut update_sizes = false; | 3049 | let mut update_sizes = false; |
| 3017 | 3050 | ||
| 3018 | // If preset widths changed, make our width non-preset. | 3051 | // If preset widths changed, make our width non-preset. |
| 3019 | - if self.options.preset_widths != options.preset_widths { | 3052 | + if self.options.preset_column_widths != options.preset_column_widths { |
| 3020 | if let ColumnWidth::Preset(idx) = self.width { | 3053 | if let ColumnWidth::Preset(idx) = self.width { |
| 3021 | - self.width = self.options.preset_widths[idx]; | 3054 | + self.width = self.options.preset_column_widths[idx]; |
| 3022 | } | 3055 | } |
| 3023 | } | 3056 | } |
| 3024 | 3057 | ||
| 3058 | + // If preset heights changed, make our heights non-preset. | ||
| 3059 | + if self.options.preset_window_heights != options.preset_window_heights { | ||
| 3060 | + self.convert_heights_to_auto(); | ||
| 3061 | + update_sizes = true; | ||
| 3062 | + } | ||
| 3063 | + | ||
| 3025 | if self.options.gaps != options.gaps { | 3064 | if self.options.gaps != options.gaps { |
| 3026 | update_sizes = true; | 3065 | update_sizes = true; |
| 3027 | } | 3066 | } |
| @@ -3220,6 +3259,7 @@ impl<W: LayoutElement> Column<W> { | |||
| 3220 | 3259 | ||
| 3221 | let width = width.resolve(&self.options, self.working_area.size.w); | 3260 | let width = width.resolve(&self.options, self.working_area.size.w); |
| 3222 | let width = f64::max(f64::min(width, max_width), min_width); | 3261 | let width = f64::max(f64::min(width, max_width), min_width); |
| 3262 | + let height = self.working_area.size.h; | ||
| 3223 | 3263 | ||
| 3224 | // Compute the tile heights. Start by converting window heights to tile heights. | 3264 | // Compute the tile heights. Start by converting window heights to tile heights. |
| 3225 | let mut heights = zip(&self.tiles, &self.data) | 3265 | let mut heights = zip(&self.tiles, &self.data) |
| @@ -3228,8 +3268,19 @@ impl<W: LayoutElement> Column<W> { | |||
| 3228 | WindowHeight::Fixed(height) => { | 3268 | WindowHeight::Fixed(height) => { |
| 3229 | WindowHeight::Fixed(tile.tile_height_for_window_height(height.round().max(1.))) | 3269 | WindowHeight::Fixed(tile.tile_height_for_window_height(height.round().max(1.))) |
| 3230 | } | 3270 | } |
| 3271 | + WindowHeight::Preset(idx) => { | ||
| 3272 | + let preset = self.options.preset_window_heights[idx]; | ||
| 3273 | + let window_height = match resolve_preset_size(preset, &self.options, height) { | ||
| 3274 | + ResolvedSize::Tile(h) => tile.window_height_for_tile_height(h), | ||
| 3275 | + ResolvedSize::Window(h) => h, | ||
| 3276 | + }; | ||
| 3277 | + let tile_height = tile | ||
| 3278 | + .tile_height_for_window_height(window_height.round().clamp(1., 100000.)); | ||
| 3279 | + WindowHeight::Fixed(tile_height) | ||
| 3280 | + } | ||
| 3231 | }) | 3281 | }) |
| 3232 | .collect::<Vec<_>>(); | 3282 | .collect::<Vec<_>>(); |
| 3283 | + | ||
| 3233 | let gaps_left = self.options.gaps * (self.tiles.len() + 1) as f64; | 3284 | let gaps_left = self.options.gaps * (self.tiles.len() + 1) as f64; |
| 3234 | let mut height_left = self.working_area.size.h - gaps_left; | 3285 | let mut height_left = self.working_area.size.h - gaps_left; |
| 3235 | let mut auto_tiles_left = self.tiles.len(); | 3286 | let mut auto_tiles_left = self.tiles.len(); |
| @@ -3283,6 +3334,7 @@ impl<W: LayoutElement> Column<W> { | |||
| 3283 | let weight = match *h { | 3334 | let weight = match *h { |
| 3284 | WindowHeight::Auto { weight } => weight, | 3335 | WindowHeight::Auto { weight } => weight, |
| 3285 | WindowHeight::Fixed(_) => continue, | 3336 | WindowHeight::Fixed(_) => continue, |
| 3337 | + WindowHeight::Preset(_) => unreachable!(), | ||
| 3286 | }; | 3338 | }; |
| 3287 | let factor = weight / total_weight_2; | 3339 | let factor = weight / total_weight_2; |
| 3288 | 3340 | ||
| @@ -3321,6 +3373,7 @@ impl<W: LayoutElement> Column<W> { | |||
| 3321 | let weight = match *h { | 3373 | let weight = match *h { |
| 3322 | WindowHeight::Auto { weight } => weight, | 3374 | WindowHeight::Auto { weight } => weight, |
| 3323 | WindowHeight::Fixed(_) => continue, | 3375 | WindowHeight::Fixed(_) => continue, |
| 3376 | + WindowHeight::Preset(_) => unreachable!(), | ||
| 3324 | }; | 3377 | }; |
| 3325 | let factor = weight / total_weight; | 3378 | let factor = weight / total_weight; |
| 3326 | 3379 | ||
| @@ -3456,6 +3509,10 @@ impl<W: LayoutElement> Column<W> { | |||
| 3456 | ); | 3509 | ); |
| 3457 | found_fixed = true; | 3510 | found_fixed = true; |
| 3458 | } | 3511 | } |
| 3512 | + | ||
| 3513 | + if let WindowHeight::Preset(idx) = data.height { | ||
| 3514 | + assert!(self.options.preset_window_heights.len() > idx); | ||
| 3515 | + } | ||
| 3459 | } | 3516 | } |
| 3460 | } | 3517 | } |
| 3461 | 3518 | ||
| @@ -3467,11 +3524,11 @@ impl<W: LayoutElement> Column<W> { | |||
| 3467 | }; | 3524 | }; |
| 3468 | 3525 | ||
| 3469 | let idx = match width { | 3526 | let idx = match width { |
| 3470 | - ColumnWidth::Preset(idx) => (idx + 1) % self.options.preset_widths.len(), | 3527 | + ColumnWidth::Preset(idx) => (idx + 1) % self.options.preset_column_widths.len(), |
| 3471 | _ => { | 3528 | _ => { |
| 3472 | let current = self.width(); | 3529 | let current = self.width(); |
| 3473 | self.options | 3530 | self.options |
| 3474 | - .preset_widths | 3531 | + .preset_column_widths |
| 3475 | .iter() | 3532 | .iter() |
| 3476 | .position(|prop| { | 3533 | .position(|prop| { |
| 3477 | let resolved = prop.resolve(&self.options, self.working_area.size.w); | 3534 | let resolved = prop.resolve(&self.options, self.working_area.size.w); |
| @@ -3500,7 +3557,7 @@ impl<W: LayoutElement> Column<W> { | |||
| 3500 | let current_px = width.resolve(&self.options, self.working_area.size.w); | 3557 | let current_px = width.resolve(&self.options, self.working_area.size.w); |
| 3501 | 3558 | ||
| 3502 | let current = match width { | 3559 | let current = match width { |
| 3503 | - ColumnWidth::Preset(idx) => self.options.preset_widths[idx], | 3560 | + ColumnWidth::Preset(idx) => self.options.preset_column_widths[idx], |
| 3504 | current => current, | 3561 | current => current, |
| 3505 | }; | 3562 | }; |
| 3506 | 3563 | ||
| @@ -3551,17 +3608,18 @@ impl<W: LayoutElement> Column<W> { | |||
| 3551 | let tile_idx = tile_idx.unwrap_or(self.active_tile_idx); | 3608 | let tile_idx = tile_idx.unwrap_or(self.active_tile_idx); |
| 3552 | 3609 | ||
| 3553 | // Start by converting all heights to automatic, since only one window in the column can be | 3610 | // Start by converting all heights to automatic, since only one window in the column can be |
| 3554 | - // fixed-height. If the current tile is already fixed, however, we can skip that step. | 3611 | + // non-auto-height. If the current tile is already non-auto, however, we can skip that |
| 3555 | - // Which is not only for optimization, but also preserves automatic weights in case one | 3612 | + // step. Which is not only for optimization, but also preserves automatic weights in case |
| 3556 | - // window is resized in such a way that other windows hit their min size, and then back. | 3613 | + // one window is resized in such a way that other windows hit their min size, and then |
| 3557 | - if !matches!(self.data[tile_idx].height, WindowHeight::Fixed(_)) { | 3614 | + // back. |
| 3615 | + if matches!(self.data[tile_idx].height, WindowHeight::Auto { .. }) { | ||
| 3558 | self.convert_heights_to_auto(); | 3616 | self.convert_heights_to_auto(); |
| 3559 | } | 3617 | } |
| 3560 | 3618 | ||
| 3561 | let current = self.data[tile_idx].height; | 3619 | let current = self.data[tile_idx].height; |
| 3562 | let tile = &self.tiles[tile_idx]; | 3620 | let tile = &self.tiles[tile_idx]; |
| 3563 | let current_window_px = match current { | 3621 | let current_window_px = match current { |
| 3564 | - WindowHeight::Auto { .. } => tile.window_size().h, | 3622 | + WindowHeight::Auto { .. } | WindowHeight::Preset(_) => tile.window_size().h, |
| 3565 | WindowHeight::Fixed(height) => height, | 3623 | WindowHeight::Fixed(height) => height, |
| 3566 | }; | 3624 | }; |
| 3567 | let current_tile_px = tile.tile_height_for_window_height(current_window_px); | 3625 | let current_tile_px = tile.tile_height_for_window_height(current_window_px); |
| @@ -3631,6 +3689,48 @@ impl<W: LayoutElement> Column<W> { | |||
| 3631 | self.update_tile_sizes(animate); | 3689 | self.update_tile_sizes(animate); |
| 3632 | } | 3690 | } |
| 3633 | 3691 | ||
| 3692 | + fn toggle_window_height(&mut self, tile_idx: Option<usize>, animate: bool) { | ||
| 3693 | + let tile_idx = tile_idx.unwrap_or(self.active_tile_idx); | ||
| 3694 | + | ||
| 3695 | + // Start by converting all heights to automatic, since only one window in the column can be | ||
| 3696 | + // non-auto-height. If the current tile is already non-auto, however, we can skip that | ||
| 3697 | + // step. Which is not only for optimization, but also preserves automatic weights in case | ||
| 3698 | + // one window is resized in such a way that other windows hit their min size, and then | ||
| 3699 | + // back. | ||
| 3700 | + if matches!(self.data[tile_idx].height, WindowHeight::Auto { .. }) { | ||
| 3701 | + self.convert_heights_to_auto(); | ||
| 3702 | + } | ||
| 3703 | + | ||
| 3704 | + let preset_idx = match self.data[tile_idx].height { | ||
| 3705 | + WindowHeight::Preset(idx) => (idx + 1) % self.options.preset_window_heights.len(), | ||
| 3706 | + _ => { | ||
| 3707 | + let current = self.data[tile_idx].size.h; | ||
| 3708 | + let tile = &self.tiles[tile_idx]; | ||
| 3709 | + self.options | ||
| 3710 | + .preset_window_heights | ||
| 3711 | + .iter() | ||
| 3712 | + .copied() | ||
| 3713 | + .position(|preset| { | ||
| 3714 | + let resolved = | ||
| 3715 | + resolve_preset_size(preset, &self.options, self.working_area.size.h); | ||
| 3716 | + let window_height = match resolved { | ||
| 3717 | + ResolvedSize::Tile(h) => tile.window_height_for_tile_height(h), | ||
| 3718 | + ResolvedSize::Window(h) => h, | ||
| 3719 | + }; | ||
| 3720 | + let resolved = tile.tile_height_for_window_height( | ||
| 3721 | + window_height.round().clamp(1., 100000.), | ||
| 3722 | + ); | ||
| 3723 | + | ||
| 3724 | + // Some allowance for fractional scaling purposes. | ||
| 3725 | + current + 1. < resolved | ||
| 3726 | + }) | ||
| 3727 | + .unwrap_or(0) | ||
| 3728 | + } | ||
| 3729 | + }; | ||
| 3730 | + self.data[tile_idx].height = WindowHeight::Preset(preset_idx); | ||
| 3731 | + self.update_tile_sizes(animate); | ||
| 3732 | + } | ||
| 3733 | + | ||
| 3634 | /// Converts all heights in the column to automatic, preserving the apparent heights. | 3734 | /// Converts all heights in the column to automatic, preserving the apparent heights. |
| 3635 | /// | 3735 | /// |
| 3636 | /// All weights are recomputed to preserve the current tile heights while "centering" the | 3736 | /// All weights are recomputed to preserve the current tile heights while "centering" the |