Add center-focused-column setting
Browse files at this commitfb93038bd88e4443015fb482de3450c27f009219Parent: 71fef2a4 changed files +116 -45
- niri-config/src/lib.rs +17 -0
- resources/default-config.kdl +8 -0
- src/layout/mod.rs +3 -0
- src/layout/workspace.rs +88 -45
niri-config/src/lib.rsmodified+17 -0
| @@ -94,6 +94,18 @@ impl Xkb { | |||
| 94 | } | 94 | } |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | +#[derive(knuffel::DecodeScalar, Debug, Default, PartialEq, Eq, Clone, Copy)] | ||
| 98 | +pub enum CenterFocusedColumn { | ||
| 99 | + /// Focusing a column will not center the column. | ||
| 100 | + #[default] | ||
| 101 | + Never, | ||
| 102 | + /// The focused column will always be centered. | ||
| 103 | + Always, | ||
| 104 | + /// Focusing a column will center it if it doesn't fit on the screen together with the | ||
| 105 | + /// previously focused column. | ||
| 106 | + OnOverflow, | ||
| 107 | +} | ||
| 108 | + | ||
| 97 | #[derive(knuffel::DecodeScalar, Debug, Default, PartialEq, Eq)] | 109 | #[derive(knuffel::DecodeScalar, Debug, Default, PartialEq, Eq)] |
| 98 | pub enum TrackLayout { | 110 | pub enum TrackLayout { |
| 99 | /// The layout change is global. | 111 | /// The layout change is global. |
| @@ -217,6 +229,8 @@ pub struct Layout { | |||
| 217 | pub preset_column_widths: Vec<PresetWidth>, | 229 | pub preset_column_widths: Vec<PresetWidth>, |
| 218 | #[knuffel(child)] | 230 | #[knuffel(child)] |
| 219 | pub default_column_width: Option<DefaultColumnWidth>, | 231 | pub default_column_width: Option<DefaultColumnWidth>, |
| 232 | + #[knuffel(child, unwrap(argument), default)] | ||
| 233 | + pub center_focused_column: CenterFocusedColumn, | ||
| 220 | #[knuffel(child, unwrap(argument), default = 16)] | 234 | #[knuffel(child, unwrap(argument), default = 16)] |
| 221 | pub gaps: u16, | 235 | pub gaps: u16, |
| 222 | #[knuffel(child, default)] | 236 | #[knuffel(child, default)] |
| @@ -739,6 +753,8 @@ mod tests { | |||
| 739 | right 2 | 753 | right 2 |
| 740 | top 3 | 754 | top 3 |
| 741 | } | 755 | } |
| 756 | + | ||
| 757 | + center-focused-column "on-overflow" | ||
| 742 | } | 758 | } |
| 743 | 759 | ||
| 744 | spawn-at-startup "alacritty" "-e" "fish" | 760 | spawn-at-startup "alacritty" "-e" "fish" |
| @@ -856,6 +872,7 @@ mod tests { | |||
| 856 | top: 3, | 872 | top: 3, |
| 857 | bottom: 0, | 873 | bottom: 0, |
| 858 | }, | 874 | }, |
| 875 | + center_focused_column: CenterFocusedColumn::OnOverflow, | ||
| 859 | }, | 876 | }, |
| 860 | spawn_at_startup: vec![SpawnAtStartup { | 877 | spawn_at_startup: vec![SpawnAtStartup { |
| 861 | command: vec!["alacritty".to_owned(), "-e".to_owned(), "fish".to_owned()], | 878 | command: vec!["alacritty".to_owned(), "-e".to_owned(), "fish".to_owned()], |
resources/default-config.kdlmodified+8 -0
| @@ -143,6 +143,14 @@ layout { | |||
| 143 | // top 64 | 143 | // top 64 |
| 144 | // bottom 64 | 144 | // bottom 64 |
| 145 | } | 145 | } |
| 146 | + | ||
| 147 | + // When to center a column when changing focus, options are: | ||
| 148 | + // - "never", default behavior, focusing an off-screen column will keep at the left | ||
| 149 | + // or right edge of the screen. | ||
| 150 | + // - "on-overflow", focusing a column will center it if it doesn't fit | ||
| 151 | + // together with the previously focused column. | ||
| 152 | + // - "always", the focused column will always be centered. | ||
| 153 | + center-focused-column "never" | ||
| 146 | } | 154 | } |
| 147 | 155 | ||
| 148 | // Add lines like this to spawn processes at startup. | 156 | // Add lines like this to spawn processes at startup. |
src/layout/mod.rsmodified+3 -0
| @@ -139,6 +139,7 @@ pub struct Options { | |||
| 139 | struts: Struts, | 139 | struts: Struts, |
| 140 | focus_ring: niri_config::FocusRing, | 140 | focus_ring: niri_config::FocusRing, |
| 141 | border: niri_config::FocusRing, | 141 | border: niri_config::FocusRing, |
| 142 | + center_focused_column: niri_config::CenterFocusedColumn, | ||
| 142 | /// Column widths that `toggle_width()` switches between. | 143 | /// Column widths that `toggle_width()` switches between. |
| 143 | preset_widths: Vec<ColumnWidth>, | 144 | preset_widths: Vec<ColumnWidth>, |
| 144 | /// Initial width for new columns. | 145 | /// Initial width for new columns. |
| @@ -152,6 +153,7 @@ impl Default for Options { | |||
| 152 | struts: Default::default(), | 153 | struts: Default::default(), |
| 153 | focus_ring: Default::default(), | 154 | focus_ring: Default::default(), |
| 154 | border: niri_config::default_border(), | 155 | border: niri_config::default_border(), |
| 156 | + center_focused_column: Default::default(), | ||
| 155 | preset_widths: vec![ | 157 | preset_widths: vec![ |
| 156 | ColumnWidth::Proportion(1. / 3.), | 158 | ColumnWidth::Proportion(1. / 3.), |
| 157 | ColumnWidth::Proportion(0.5), | 159 | ColumnWidth::Proportion(0.5), |
| @@ -190,6 +192,7 @@ impl Options { | |||
| 190 | struts: layout.struts, | 192 | struts: layout.struts, |
| 191 | focus_ring: layout.focus_ring, | 193 | focus_ring: layout.focus_ring, |
| 192 | border: layout.border, | 194 | border: layout.border, |
| 195 | + center_focused_column: layout.center_focused_column, | ||
| 193 | preset_widths, | 196 | preset_widths, |
| 194 | default_width, | 197 | default_width, |
| 195 | } | 198 | } |
src/layout/workspace.rsmodified+88 -45
| @@ -3,7 +3,7 @@ use std::iter::zip; | |||
| 3 | use std::rc::Rc; | 3 | use std::rc::Rc; |
| 4 | use std::time::Duration; | 4 | use std::time::Duration; |
| 5 | 5 | ||
| 6 | -use niri_config::{PresetWidth, SizeChange, Struts}; | 6 | +use niri_config::{CenterFocusedColumn, PresetWidth, SizeChange, Struts}; |
| 7 | use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; | 7 | use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; |
| 8 | use smithay::backend::renderer::element::utils::RelocateRenderElement; | 8 | use smithay::backend::renderer::element::utils::RelocateRenderElement; |
| 9 | use smithay::backend::renderer::{ImportAll, Renderer}; | 9 | use smithay::backend::renderer::{ImportAll, Renderer}; |
| @@ -397,9 +397,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 397 | new_offset - self.working_area.loc.x | 397 | new_offset - self.working_area.loc.x |
| 398 | } | 398 | } |
| 399 | 399 | ||
| 400 | - fn animate_view_offset_to_column(&mut self, current_x: i32, idx: usize) { | 400 | + fn animate_view_offset(&mut self, current_x: i32, idx: usize, new_view_offset: i32) { |
| 401 | - let new_view_offset = self.compute_new_view_offset_for_column(current_x, idx); | ||
| 402 | - | ||
| 403 | let new_col_x = self.column_x(idx); | 401 | let new_col_x = self.column_x(idx); |
| 404 | let from_view_offset = current_x - new_col_x; | 402 | let from_view_offset = current_x - new_col_x; |
| 405 | self.view_offset = from_view_offset; | 403 | self.view_offset = from_view_offset; |
| @@ -426,13 +424,78 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 426 | )); | 424 | )); |
| 427 | } | 425 | } |
| 428 | 426 | ||
| 427 | + fn animate_view_offset_to_column(&mut self, current_x: i32, idx: usize) { | ||
| 428 | + let new_view_offset = self.compute_new_view_offset_for_column(current_x, idx); | ||
| 429 | + self.animate_view_offset(current_x, idx, new_view_offset); | ||
| 430 | + } | ||
| 431 | + | ||
| 432 | + fn animate_view_offset_to_column_centered(&mut self, current_x: i32, idx: usize) { | ||
| 433 | + if self.columns.is_empty() { | ||
| 434 | + return; | ||
| 435 | + } | ||
| 436 | + | ||
| 437 | + let col = &self.columns[idx]; | ||
| 438 | + if col.is_fullscreen { | ||
| 439 | + self.animate_view_offset_to_column(current_x, idx); | ||
| 440 | + return; | ||
| 441 | + } | ||
| 442 | + | ||
| 443 | + let width = col.width(); | ||
| 444 | + | ||
| 445 | + // If the column is wider than the working area, then on commit it will be shifted to left | ||
| 446 | + // edge alignment by the usual positioning code, so there's no use in trying to center it | ||
| 447 | + // here. | ||
| 448 | + if self.working_area.size.w <= width { | ||
| 449 | + self.animate_view_offset_to_column(current_x, idx); | ||
| 450 | + return; | ||
| 451 | + } | ||
| 452 | + | ||
| 453 | + let new_view_offset = -(self.working_area.size.w - width) / 2 - self.working_area.loc.x; | ||
| 454 | + | ||
| 455 | + self.animate_view_offset(current_x, idx, new_view_offset); | ||
| 456 | + } | ||
| 457 | + | ||
| 429 | fn activate_column(&mut self, idx: usize) { | 458 | fn activate_column(&mut self, idx: usize) { |
| 430 | if self.active_column_idx == idx { | 459 | if self.active_column_idx == idx { |
| 431 | return; | 460 | return; |
| 432 | } | 461 | } |
| 433 | 462 | ||
| 434 | let current_x = self.view_pos(); | 463 | let current_x = self.view_pos(); |
| 435 | - self.animate_view_offset_to_column(current_x, idx); | 464 | + match self.options.center_focused_column { |
| 465 | + CenterFocusedColumn::Always => { | ||
| 466 | + self.animate_view_offset_to_column_centered(current_x, idx) | ||
| 467 | + } | ||
| 468 | + CenterFocusedColumn::OnOverflow => { | ||
| 469 | + // Always take the left or right neighbor of the target as the source. | ||
| 470 | + let source_idx = if self.active_column_idx > idx { | ||
| 471 | + min(idx + 1, self.columns.len() - 1) | ||
| 472 | + } else { | ||
| 473 | + idx.saturating_sub(1) | ||
| 474 | + }; | ||
| 475 | + | ||
| 476 | + let source_x = self.column_x(source_idx); | ||
| 477 | + let source_width = self.columns[source_idx].width(); | ||
| 478 | + | ||
| 479 | + let target_x = self.column_x(idx); | ||
| 480 | + let target_width = self.columns[idx].width(); | ||
| 481 | + | ||
| 482 | + let total_width = if source_x < target_x { | ||
| 483 | + // Source is left from target. | ||
| 484 | + target_x - source_x + target_width | ||
| 485 | + } else { | ||
| 486 | + // Source is right from target. | ||
| 487 | + source_x - target_x + source_width | ||
| 488 | + } + self.options.gaps * 2; | ||
| 489 | + | ||
| 490 | + // If it fits together, do a normal animation, otherwise center the new column. | ||
| 491 | + if total_width <= self.working_area.size.w { | ||
| 492 | + self.animate_view_offset_to_column(current_x, idx); | ||
| 493 | + } else { | ||
| 494 | + self.animate_view_offset_to_column_centered(current_x, idx); | ||
| 495 | + } | ||
| 496 | + } | ||
| 497 | + CenterFocusedColumn::Never => self.animate_view_offset_to_column(current_x, idx), | ||
| 498 | + }; | ||
| 436 | 499 | ||
| 437 | self.active_column_idx = idx; | 500 | self.active_column_idx = idx; |
| 438 | 501 | ||
| @@ -488,15 +551,21 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 488 | width, | 551 | width, |
| 489 | is_full_width, | 552 | is_full_width, |
| 490 | ); | 553 | ); |
| 554 | + let width = column.width(); | ||
| 491 | self.columns.insert(idx, column); | 555 | self.columns.insert(idx, column); |
| 492 | 556 | ||
| 493 | if activate { | 557 | if activate { |
| 494 | // If this is the first window on an empty workspace, skip the animation from whatever | 558 | // If this is the first window on an empty workspace, skip the animation from whatever |
| 495 | // view_offset was left over. | 559 | // view_offset was left over. |
| 496 | if was_empty { | 560 | if was_empty { |
| 497 | - // Try to make the code produce a left-aligned offset, even in presence of left | 561 | + if self.options.center_focused_column == CenterFocusedColumn::Always { |
| 498 | - // exclusive zones. | 562 | + self.view_offset = |
| 499 | - self.view_offset = self.compute_new_view_offset_for_column(self.column_x(0), 0); | 563 | + -(self.working_area.size.w - width) / 2 - self.working_area.loc.x; |
| 564 | + } else { | ||
| 565 | + // Try to make the code produce a left-aligned offset, even in presence of left | ||
| 566 | + // exclusive zones. | ||
| 567 | + self.view_offset = self.compute_new_view_offset_for_column(self.column_x(0), 0); | ||
| 568 | + } | ||
| 500 | self.view_offset_anim = None; | 569 | self.view_offset_anim = None; |
| 501 | } | 570 | } |
| 502 | 571 | ||
| @@ -574,7 +643,14 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 574 | if idx == self.active_column_idx { | 643 | if idx == self.active_column_idx { |
| 575 | // We might need to move the view to ensure the resized window is still visible. | 644 | // We might need to move the view to ensure the resized window is still visible. |
| 576 | let current_x = self.view_pos(); | 645 | let current_x = self.view_pos(); |
| 577 | - self.animate_view_offset_to_column(current_x, idx); | 646 | + |
| 647 | + if self.options.center_focused_column == CenterFocusedColumn::Always { | ||
| 648 | + // FIXME: we will want to skip the animation in some cases here to make | ||
| 649 | + // continuously resizing windows not look janky. | ||
| 650 | + self.animate_view_offset_to_column_centered(current_x, idx); | ||
| 651 | + } else { | ||
| 652 | + self.animate_view_offset_to_column(current_x, idx); | ||
| 653 | + } | ||
| 578 | } | 654 | } |
| 579 | } | 655 | } |
| 580 | 656 | ||
| @@ -654,6 +730,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 654 | let column = self.columns.remove(self.active_column_idx); | 730 | let column = self.columns.remove(self.active_column_idx); |
| 655 | self.columns.insert(new_idx, column); | 731 | self.columns.insert(new_idx, column); |
| 656 | 732 | ||
| 733 | + // FIXME: should this be different when always centering? | ||
| 657 | self.view_offset = | 734 | self.view_offset = |
| 658 | self.compute_new_view_offset_for_column(current_x, self.active_column_idx); | 735 | self.compute_new_view_offset_for_column(current_x, self.active_column_idx); |
| 659 | 736 | ||
| @@ -738,42 +815,8 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 738 | } | 815 | } |
| 739 | 816 | ||
| 740 | pub fn center_column(&mut self) { | 817 | pub fn center_column(&mut self) { |
| 741 | - if self.columns.is_empty() { | 818 | + let center_x = self.view_pos(); |
| 742 | - return; | 819 | + self.animate_view_offset_to_column_centered(center_x, self.active_column_idx); |
| 743 | - } | ||
| 744 | - | ||
| 745 | - let col = &self.columns[self.active_column_idx]; | ||
| 746 | - if col.is_fullscreen { | ||
| 747 | - return; | ||
| 748 | - } | ||
| 749 | - | ||
| 750 | - let width = col.width(); | ||
| 751 | - | ||
| 752 | - // If the column is wider than the working area, then on commit it will be shifted to left | ||
| 753 | - // edge alignment by the usual positioning code, so there's no use in doing anything here. | ||
| 754 | - if self.working_area.size.w <= width { | ||
| 755 | - return; | ||
| 756 | - } | ||
| 757 | - | ||
| 758 | - let new_view_offset = -(self.working_area.size.w - width) / 2 - self.working_area.loc.x; | ||
| 759 | - | ||
| 760 | - // If we're already animating towards that, don't restart it. | ||
| 761 | - if let Some(anim) = &self.view_offset_anim { | ||
| 762 | - if anim.to().round() as i32 == new_view_offset { | ||
| 763 | - return; | ||
| 764 | - } | ||
| 765 | - } | ||
| 766 | - | ||
| 767 | - // If our view offset is already this, we don't need to do anything. | ||
| 768 | - if self.view_offset == new_view_offset { | ||
| 769 | - return; | ||
| 770 | - } | ||
| 771 | - | ||
| 772 | - self.view_offset_anim = Some(Animation::new( | ||
| 773 | - self.view_offset as f64, | ||
| 774 | - new_view_offset as f64, | ||
| 775 | - Duration::from_millis(250), | ||
| 776 | - )); | ||
| 777 | } | 820 | } |
| 778 | 821 | ||
| 779 | fn view_pos(&self) -> i32 { | 822 | fn view_pos(&self) -> i32 { |