Open dialogs to the right of their parent, don't steal focus
Browse files at this commit9896fd67a0dcdade5f05568aa7b12fa17605976bParent: 15ec6994 changed files +216 -2
- src/handlers/compositor.rs +16 -2
- src/layout/mod.rs +138 -0
- src/layout/monitor.rs +25 -0
- src/layout/workspace.rs +37 -0
src/handlers/compositor.rsmodified+16 -2
| @@ -104,8 +104,22 @@ impl CompositorHandler for State { | |||
| 104 | let window = entry.remove(); | 104 | let window = entry.remove(); |
| 105 | window.on_commit(); | 105 | window.on_commit(); |
| 106 | 106 | ||
| 107 | - if let Some(output) = self.niri.layout.add_window(window, None, false).cloned() | 107 | + let parent = window |
| 108 | - { | 108 | + .toplevel() |
| 109 | + .parent() | ||
| 110 | + .and_then(|parent| self.niri.layout.find_window_and_output(&parent)) | ||
| 111 | + .map(|(win, _)| win.clone()); | ||
| 112 | + | ||
| 113 | + let win = window.clone(); | ||
| 114 | + | ||
| 115 | + // Open dialogs immediately to the right of their parent window. | ||
| 116 | + let output = if let Some(p) = parent { | ||
| 117 | + self.niri.layout.add_window_right_of(&p, win, None, false) | ||
| 118 | + } else { | ||
| 119 | + self.niri.layout.add_window(win, None, false) | ||
| 120 | + }; | ||
| 121 | + | ||
| 122 | + if let Some(output) = output.cloned() { | ||
| 109 | self.niri.queue_redraw(output); | 123 | self.niri.queue_redraw(output); |
| 110 | } | 124 | } |
| 111 | return; | 125 | return; |
src/layout/mod.rsmodified+138 -0
| @@ -564,6 +564,58 @@ impl<W: LayoutElement> Layout<W> { | |||
| 564 | } | 564 | } |
| 565 | } | 565 | } |
| 566 | 566 | ||
| 567 | + /// Adds a new window to the layout immediately to the right of another window. | ||
| 568 | + /// | ||
| 569 | + /// If that another window was active, activates the new window. | ||
| 570 | + /// | ||
| 571 | + /// Returns an output that the window was added to, if there were any outputs. | ||
| 572 | + pub fn add_window_right_of( | ||
| 573 | + &mut self, | ||
| 574 | + right_of: &W, | ||
| 575 | + window: W, | ||
| 576 | + width: Option<ColumnWidth>, | ||
| 577 | + is_full_width: bool, | ||
| 578 | + ) -> Option<&Output> { | ||
| 579 | + let width = width | ||
| 580 | + .or(self.options.default_width) | ||
| 581 | + .unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); | ||
| 582 | + | ||
| 583 | + match &mut self.monitor_set { | ||
| 584 | + MonitorSet::Normal { | ||
| 585 | + monitors, | ||
| 586 | + active_monitor_idx, | ||
| 587 | + .. | ||
| 588 | + } => { | ||
| 589 | + let mon_idx = monitors | ||
| 590 | + .iter() | ||
| 591 | + .position(|mon| mon.workspaces.iter().any(|ws| ws.has_window(right_of))) | ||
| 592 | + .unwrap(); | ||
| 593 | + let mon = &mut monitors[mon_idx]; | ||
| 594 | + | ||
| 595 | + let mut activate = false; | ||
| 596 | + if mon_idx == *active_monitor_idx { | ||
| 597 | + let active_ws = &mon.workspaces[mon.active_workspace_idx]; | ||
| 598 | + if !active_ws.columns.is_empty() { | ||
| 599 | + let active_col = &active_ws.columns[active_ws.active_column_idx]; | ||
| 600 | + let active_tile = &active_col.tiles[active_col.active_tile_idx]; | ||
| 601 | + activate = active_tile.window() == right_of; | ||
| 602 | + } | ||
| 603 | + } | ||
| 604 | + | ||
| 605 | + mon.add_window_right_of(right_of, window, activate, width, is_full_width); | ||
| 606 | + Some(&mon.output) | ||
| 607 | + } | ||
| 608 | + MonitorSet::NoOutputs { workspaces } => { | ||
| 609 | + let ws = workspaces | ||
| 610 | + .iter_mut() | ||
| 611 | + .find(|ws| ws.has_window(right_of)) | ||
| 612 | + .unwrap(); | ||
| 613 | + ws.add_window_right_of(right_of, window, true, width, is_full_width); | ||
| 614 | + None | ||
| 615 | + } | ||
| 616 | + } | ||
| 617 | + } | ||
| 618 | + | ||
| 567 | pub fn remove_window(&mut self, window: &W) { | 619 | pub fn remove_window(&mut self, window: &W) { |
| 568 | match &mut self.monitor_set { | 620 | match &mut self.monitor_set { |
| 569 | MonitorSet::Normal { monitors, .. } => { | 621 | MonitorSet::Normal { monitors, .. } => { |
| @@ -1777,6 +1829,16 @@ mod tests { | |||
| 1777 | #[proptest(strategy = "arbitrary_min_max_size()")] | 1829 | #[proptest(strategy = "arbitrary_min_max_size()")] |
| 1778 | min_max_size: (Size<i32, Logical>, Size<i32, Logical>), | 1830 | min_max_size: (Size<i32, Logical>, Size<i32, Logical>), |
| 1779 | }, | 1831 | }, |
| 1832 | + AddWindowRightOf { | ||
| 1833 | + #[proptest(strategy = "1..=5usize")] | ||
| 1834 | + id: usize, | ||
| 1835 | + #[proptest(strategy = "1..=5usize")] | ||
| 1836 | + right_of_id: usize, | ||
| 1837 | + #[proptest(strategy = "arbitrary_bbox()")] | ||
| 1838 | + bbox: Rectangle<i32, Logical>, | ||
| 1839 | + #[proptest(strategy = "arbitrary_min_max_size()")] | ||
| 1840 | + min_max_size: (Size<i32, Logical>, Size<i32, Logical>), | ||
| 1841 | + }, | ||
| 1780 | CloseWindow(#[proptest(strategy = "1..=5usize")] usize), | 1842 | CloseWindow(#[proptest(strategy = "1..=5usize")] usize), |
| 1781 | FullscreenWindow(#[proptest(strategy = "1..=5usize")] usize), | 1843 | FullscreenWindow(#[proptest(strategy = "1..=5usize")] usize), |
| 1782 | FocusColumnLeft, | 1844 | FocusColumnLeft, |
| @@ -1897,6 +1959,58 @@ mod tests { | |||
| 1897 | let win = TestWindow::new(id, bbox, min_max_size.0, min_max_size.1); | 1959 | let win = TestWindow::new(id, bbox, min_max_size.0, min_max_size.1); |
| 1898 | layout.add_window(win, None, false); | 1960 | layout.add_window(win, None, false); |
| 1899 | } | 1961 | } |
| 1962 | + Op::AddWindowRightOf { | ||
| 1963 | + id, | ||
| 1964 | + right_of_id, | ||
| 1965 | + bbox, | ||
| 1966 | + min_max_size, | ||
| 1967 | + } => { | ||
| 1968 | + let mut found_right_of = false; | ||
| 1969 | + | ||
| 1970 | + match &mut layout.monitor_set { | ||
| 1971 | + MonitorSet::Normal { monitors, .. } => { | ||
| 1972 | + for mon in monitors { | ||
| 1973 | + for ws in &mut mon.workspaces { | ||
| 1974 | + for win in ws.windows() { | ||
| 1975 | + if win.0.id == id { | ||
| 1976 | + return; | ||
| 1977 | + } | ||
| 1978 | + | ||
| 1979 | + if win.0.id == right_of_id { | ||
| 1980 | + found_right_of = true; | ||
| 1981 | + } | ||
| 1982 | + } | ||
| 1983 | + } | ||
| 1984 | + } | ||
| 1985 | + } | ||
| 1986 | + MonitorSet::NoOutputs { workspaces, .. } => { | ||
| 1987 | + for ws in workspaces { | ||
| 1988 | + for win in ws.windows() { | ||
| 1989 | + if win.0.id == id { | ||
| 1990 | + return; | ||
| 1991 | + } | ||
| 1992 | + | ||
| 1993 | + if win.0.id == right_of_id { | ||
| 1994 | + found_right_of = true; | ||
| 1995 | + } | ||
| 1996 | + } | ||
| 1997 | + } | ||
| 1998 | + } | ||
| 1999 | + } | ||
| 2000 | + | ||
| 2001 | + if !found_right_of { | ||
| 2002 | + return; | ||
| 2003 | + } | ||
| 2004 | + | ||
| 2005 | + let right_of_win = TestWindow::new( | ||
| 2006 | + right_of_id, | ||
| 2007 | + Rectangle::default(), | ||
| 2008 | + Size::default(), | ||
| 2009 | + Size::default(), | ||
| 2010 | + ); | ||
| 2011 | + let win = TestWindow::new(id, bbox, min_max_size.0, min_max_size.1); | ||
| 2012 | + layout.add_window_right_of(&right_of_win, win, None, false); | ||
| 2013 | + } | ||
| 1900 | Op::CloseWindow(id) => { | 2014 | Op::CloseWindow(id) => { |
| 1901 | let dummy = | 2015 | let dummy = |
| 1902 | TestWindow::new(id, Rectangle::default(), Size::default(), Size::default()); | 2016 | TestWindow::new(id, Rectangle::default(), Size::default(), Size::default()); |
| @@ -2052,6 +2166,18 @@ mod tests { | |||
| 2052 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), | 2166 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 2053 | min_max_size: Default::default(), | 2167 | min_max_size: Default::default(), |
| 2054 | }, | 2168 | }, |
| 2169 | + Op::AddWindowRightOf { | ||
| 2170 | + id: 3, | ||
| 2171 | + right_of_id: 0, | ||
| 2172 | + bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), | ||
| 2173 | + min_max_size: Default::default(), | ||
| 2174 | + }, | ||
| 2175 | + Op::AddWindowRightOf { | ||
| 2176 | + id: 4, | ||
| 2177 | + right_of_id: 1, | ||
| 2178 | + bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), | ||
| 2179 | + min_max_size: Default::default(), | ||
| 2180 | + }, | ||
| 2055 | Op::CloseWindow(0), | 2181 | Op::CloseWindow(0), |
| 2056 | Op::CloseWindow(1), | 2182 | Op::CloseWindow(1), |
| 2057 | Op::CloseWindow(2), | 2183 | Op::CloseWindow(2), |
| @@ -2185,6 +2311,18 @@ mod tests { | |||
| 2185 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), | 2311 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 2186 | min_max_size: Default::default(), | 2312 | min_max_size: Default::default(), |
| 2187 | }, | 2313 | }, |
| 2314 | + Op::AddWindowRightOf { | ||
| 2315 | + id: 6, | ||
| 2316 | + right_of_id: 0, | ||
| 2317 | + bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), | ||
| 2318 | + min_max_size: Default::default(), | ||
| 2319 | + }, | ||
| 2320 | + Op::AddWindowRightOf { | ||
| 2321 | + id: 7, | ||
| 2322 | + right_of_id: 1, | ||
| 2323 | + bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), | ||
| 2324 | + min_max_size: Default::default(), | ||
| 2325 | + }, | ||
| 2188 | Op::CloseWindow(0), | 2326 | Op::CloseWindow(0), |
| 2189 | Op::CloseWindow(1), | 2327 | Op::CloseWindow(1), |
| 2190 | Op::CloseWindow(2), | 2328 | Op::CloseWindow(2), |
src/layout/monitor.rsmodified+25 -0
| @@ -126,6 +126,31 @@ impl<W: LayoutElement> Monitor<W> { | |||
| 126 | } | 126 | } |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | + pub fn add_window_right_of( | ||
| 130 | + &mut self, | ||
| 131 | + right_of: &W, | ||
| 132 | + window: W, | ||
| 133 | + activate: bool, | ||
| 134 | + width: ColumnWidth, | ||
| 135 | + is_full_width: bool, | ||
| 136 | + ) { | ||
| 137 | + let workspace_idx = self | ||
| 138 | + .workspaces | ||
| 139 | + .iter_mut() | ||
| 140 | + .position(|ws| ws.has_window(right_of)) | ||
| 141 | + .unwrap(); | ||
| 142 | + let workspace = &mut self.workspaces[workspace_idx]; | ||
| 143 | + | ||
| 144 | + workspace.add_window_right_of(right_of, window, activate, width, is_full_width); | ||
| 145 | + | ||
| 146 | + // After adding a new window, workspace becomes this output's own. | ||
| 147 | + workspace.original_output = OutputId::new(&self.output); | ||
| 148 | + | ||
| 149 | + if activate { | ||
| 150 | + self.activate_workspace(workspace_idx); | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | + | ||
| 129 | pub fn add_column(&mut self, workspace_idx: usize, column: Column<W>, activate: bool) { | 154 | pub fn add_column(&mut self, workspace_idx: usize, column: Column<W>, activate: bool) { |
| 130 | let workspace = &mut self.workspaces[workspace_idx]; | 155 | let workspace = &mut self.workspaces[workspace_idx]; |
| 131 | 156 | ||
src/layout/workspace.rsmodified+37 -0
| @@ -555,6 +555,43 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 555 | } | 555 | } |
| 556 | } | 556 | } |
| 557 | 557 | ||
| 558 | + pub fn add_window_right_of( | ||
| 559 | + &mut self, | ||
| 560 | + right_of: &W, | ||
| 561 | + window: W, | ||
| 562 | + activate: bool, | ||
| 563 | + width: ColumnWidth, | ||
| 564 | + is_full_width: bool, | ||
| 565 | + ) { | ||
| 566 | + self.enter_output_for_window(&window); | ||
| 567 | + | ||
| 568 | + let idx = self | ||
| 569 | + .columns | ||
| 570 | + .iter() | ||
| 571 | + .position(|col| col.contains(right_of)) | ||
| 572 | + .unwrap() | ||
| 573 | + + 1; | ||
| 574 | + | ||
| 575 | + let column = Column::new( | ||
| 576 | + window, | ||
| 577 | + self.view_size, | ||
| 578 | + self.working_area, | ||
| 579 | + self.options.clone(), | ||
| 580 | + width, | ||
| 581 | + is_full_width, | ||
| 582 | + ); | ||
| 583 | + self.columns.insert(idx, column); | ||
| 584 | + | ||
| 585 | + if self.active_column_idx >= idx { | ||
| 586 | + self.active_column_idx += 1; | ||
| 587 | + } | ||
| 588 | + | ||
| 589 | + if activate { | ||
| 590 | + self.activate_column(idx); | ||
| 591 | + self.activate_prev_column_on_removal = true; | ||
| 592 | + } | ||
| 593 | + } | ||
| 594 | + | ||
| 558 | pub fn add_column(&mut self, mut column: Column<W>, activate: bool) { | 595 | pub fn add_column(&mut self, mut column: Column<W>, activate: bool) { |
| 559 | for tile in &column.tiles { | 596 | for tile in &column.tiles { |
| 560 | self.enter_output_for_window(tile.window()); | 597 | self.enter_output_for_window(tile.window()); |