Add the beginnings of window rules
Browse files at this commitbefdebfa03399eeed7869fb0788d553f7aa4dcdbParent: 7960a7310 changed files +318 -22
- Cargo.lock +1 -0
- niri-config/Cargo.toml +1 -0
- niri-config/src/lib.rs +50 -0
- niri-visual-tests/src/cases/layout.rs +3 -2
- resources/default-config.kdl +43 -0
- src/handlers/compositor.rs +29 -3
- src/handlers/xdg_shell.rs +109 -7
- src/layout/mod.rs +61 -6
- src/layout/monitor.rs +4 -0
- src/layout/workspace.rs +17 -4
Cargo.lockmodified+1 -0
| @@ -2130,6 +2130,7 @@ dependencies = [ | |||
| 2130 | "knuffel", | 2130 | "knuffel", |
| 2131 | "miette", | 2131 | "miette", |
| 2132 | "niri-ipc", | 2132 | "niri-ipc", |
| 2133 | + "regex", | ||
| 2133 | "smithay", | 2134 | "smithay", |
| 2134 | "tracing", | 2135 | "tracing", |
| 2135 | "tracy-client", | 2136 | "tracy-client", |
niri-config/Cargo.tomlmodified+1 -0
| @@ -12,6 +12,7 @@ bitflags.workspace = true | |||
| 12 | knuffel = "3.2.0" | 12 | knuffel = "3.2.0" |
| 13 | miette = "5.10.0" | 13 | miette = "5.10.0" |
| 14 | niri-ipc = { version = "0.1.1", path = "../niri-ipc" } | 14 | niri-ipc = { version = "0.1.1", path = "../niri-ipc" } |
| 15 | +regex = "1.10.3" | ||
| 15 | smithay = { workspace = true, features = ["backend_libinput"] } | 16 | smithay = { workspace = true, features = ["backend_libinput"] } |
| 16 | tracing.workspace = true | 17 | tracing.workspace = true |
| 17 | tracy-client.workspace = true | 18 | tracy-client.workspace = true |
niri-config/src/lib.rsmodified+50 -0
| @@ -7,6 +7,7 @@ use std::str::FromStr; | |||
| 7 | use bitflags::bitflags; | 7 | use bitflags::bitflags; |
| 8 | use miette::{miette, Context, IntoDiagnostic, NarratableReportHandler}; | 8 | use miette::{miette, Context, IntoDiagnostic, NarratableReportHandler}; |
| 9 | use niri_ipc::{LayoutSwitchTarget, SizeChange}; | 9 | use niri_ipc::{LayoutSwitchTarget, SizeChange}; |
| 10 | +use regex::Regex; | ||
| 10 | use smithay::input::keyboard::keysyms::KEY_NoSymbol; | 11 | use smithay::input::keyboard::keysyms::KEY_NoSymbol; |
| 11 | use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE}; | 12 | use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE}; |
| 12 | use smithay::input::keyboard::{Keysym, XkbConfig}; | 13 | use smithay::input::keyboard::{Keysym, XkbConfig}; |
| @@ -38,6 +39,8 @@ pub struct Config { | |||
| 38 | pub hotkey_overlay: HotkeyOverlay, | 39 | pub hotkey_overlay: HotkeyOverlay, |
| 39 | #[knuffel(child, default)] | 40 | #[knuffel(child, default)] |
| 40 | pub animations: Animations, | 41 | pub animations: Animations, |
| 42 | + #[knuffel(children(name = "window-rule"))] | ||
| 43 | + pub window_rules: Vec<WindowRule>, | ||
| 41 | #[knuffel(child, default)] | 44 | #[knuffel(child, default)] |
| 42 | pub binds: Binds, | 45 | pub binds: Binds, |
| 43 | #[knuffel(child, default)] | 46 | #[knuffel(child, default)] |
| @@ -524,6 +527,34 @@ pub enum AnimationCurve { | |||
| 524 | EaseOutExpo, | 527 | EaseOutExpo, |
| 525 | } | 528 | } |
| 526 | 529 | ||
| 530 | +#[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] | ||
| 531 | +pub struct WindowRule { | ||
| 532 | + #[knuffel(children(name = "match"))] | ||
| 533 | + pub matches: Vec<Match>, | ||
| 534 | + #[knuffel(children(name = "exclude"))] | ||
| 535 | + pub excludes: Vec<Match>, | ||
| 536 | + | ||
| 537 | + #[knuffel(child)] | ||
| 538 | + pub default_column_width: Option<DefaultColumnWidth>, | ||
| 539 | + #[knuffel(child, unwrap(argument))] | ||
| 540 | + pub open_on_output: Option<String>, | ||
| 541 | +} | ||
| 542 | + | ||
| 543 | +#[derive(knuffel::Decode, Debug, Default, Clone)] | ||
| 544 | +pub struct Match { | ||
| 545 | + #[knuffel(property, str)] | ||
| 546 | + pub app_id: Option<Regex>, | ||
| 547 | + #[knuffel(property, str)] | ||
| 548 | + pub title: Option<Regex>, | ||
| 549 | +} | ||
| 550 | + | ||
| 551 | +impl PartialEq for Match { | ||
| 552 | + fn eq(&self, other: &Self) -> bool { | ||
| 553 | + self.app_id.as_ref().map(Regex::as_str) == other.app_id.as_ref().map(Regex::as_str) | ||
| 554 | + && self.title.as_ref().map(Regex::as_str) == other.title.as_ref().map(Regex::as_str) | ||
| 555 | + } | ||
| 556 | +} | ||
| 557 | + | ||
| 527 | #[derive(knuffel::Decode, Debug, Default, PartialEq)] | 558 | #[derive(knuffel::Decode, Debug, Default, PartialEq)] |
| 528 | pub struct Binds(#[knuffel(children)] pub Vec<Bind>); | 559 | pub struct Binds(#[knuffel(children)] pub Vec<Bind>); |
| 529 | 560 | ||
| @@ -965,6 +996,13 @@ mod tests { | |||
| 965 | } | 996 | } |
| 966 | } | 997 | } |
| 967 | 998 | ||
| 999 | + window-rule { | ||
| 1000 | + match app-id=".*alacritty" | ||
| 1001 | + exclude title="~" | ||
| 1002 | + | ||
| 1003 | + open-on-output "eDP-1" | ||
| 1004 | + } | ||
| 1005 | + | ||
| 968 | binds { | 1006 | binds { |
| 969 | Mod+T { spawn "alacritty"; } | 1007 | Mod+T { spawn "alacritty"; } |
| 970 | Mod+Q { close-window; } | 1008 | Mod+Q { close-window; } |
| @@ -1098,6 +1136,18 @@ mod tests { | |||
| 1098 | }, | 1136 | }, |
| 1099 | ..Default::default() | 1137 | ..Default::default() |
| 1100 | }, | 1138 | }, |
| 1139 | + window_rules: vec![WindowRule { | ||
| 1140 | + matches: vec![Match { | ||
| 1141 | + app_id: Some(Regex::new(".*alacritty").unwrap()), | ||
| 1142 | + title: None, | ||
| 1143 | + }], | ||
| 1144 | + excludes: vec![Match { | ||
| 1145 | + app_id: None, | ||
| 1146 | + title: Some(Regex::new("~").unwrap()), | ||
| 1147 | + }], | ||
| 1148 | + open_on_output: Some("eDP-1".to_owned()), | ||
| 1149 | + ..Default::default() | ||
| 1150 | + }], | ||
| 1101 | binds: Binds(vec![ | 1151 | binds: Binds(vec![ |
| 1102 | Bind { | 1152 | Bind { |
| 1103 | key: Key { | 1153 | key: Key { |
niri-visual-tests/src/cases/layout.rsmodified+3 -2
| @@ -143,7 +143,8 @@ impl Layout { | |||
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | fn add_window(&mut self, window: TestWindow, width: Option<ColumnWidth>) { | 145 | fn add_window(&mut self, window: TestWindow, width: Option<ColumnWidth>) { |
| 146 | - self.layout.add_window(window.clone(), width, false); | 146 | + self.layout |
| 147 | + .add_window(window.clone(), width.map(Some), false); | ||
| 147 | if window.communicate() { | 148 | if window.communicate() { |
| 148 | self.layout.update_window(&window); | 149 | self.layout.update_window(&window); |
| 149 | } | 150 | } |
| @@ -157,7 +158,7 @@ impl Layout { | |||
| 157 | width: Option<ColumnWidth>, | 158 | width: Option<ColumnWidth>, |
| 158 | ) { | 159 | ) { |
| 159 | self.layout | 160 | self.layout |
| 160 | - .add_window_right_of(right_of, window.clone(), width, false); | 161 | + .add_window_right_of(right_of, window.clone(), width.map(Some), false); |
| 161 | if window.communicate() { | 162 | if window.communicate() { |
| 162 | self.layout.update_window(&window); | 163 | self.layout.update_window(&window); |
| 163 | } | 164 | } |
resources/default-config.kdlmodified+43 -0
| @@ -248,6 +248,49 @@ animations { | |||
| 248 | } | 248 | } |
| 249 | } | 249 | } |
| 250 | 250 | ||
| 251 | +// Window rules let you adjust behavior for individual windows. | ||
| 252 | +// They are processed in order of appearance in this file. | ||
| 253 | +// (This example rule is commented out with a "/-" in front.) | ||
| 254 | +/-window-rule { | ||
| 255 | + // Match directives control which windows this rule will apply to. | ||
| 256 | + // You can match by app-id and by title. | ||
| 257 | + // The window must match all properties of the match directive. | ||
| 258 | + match app-id="org.myapp.MyApp" title="My Cool App" | ||
| 259 | + | ||
| 260 | + // There can be multiple match directives. A window must match any one | ||
| 261 | + // of the rule's match directives. | ||
| 262 | + // | ||
| 263 | + // If there are no match directives, any window will match the rule. | ||
| 264 | + match title="Second App" | ||
| 265 | + | ||
| 266 | + // You can also add exclude directives which have the same properties. | ||
| 267 | + // If a window matches any exclude directive, it won't match this rule. | ||
| 268 | + // | ||
| 269 | + // Both app-id and title are regular expressions. | ||
| 270 | + // Raw KDL strings are helpful here. | ||
| 271 | + exclude app-id=r#"\.unwanted\."# | ||
| 272 | + | ||
| 273 | + // Here are the properties that you can set on a window rule. | ||
| 274 | + // You can override the default column width. | ||
| 275 | + default-column-width { proportion 0.75; } | ||
| 276 | + | ||
| 277 | + // You can set the output that this window will initially open on. | ||
| 278 | + // If such an output does not exist, it will open on the currently | ||
| 279 | + // focused output as usual. | ||
| 280 | + open-on-output "eDP-1" | ||
| 281 | +} | ||
| 282 | + | ||
| 283 | +// Here's a useful example. Work around WezTerm's initial configure bug | ||
| 284 | +// by setting an empty default-column-width. | ||
| 285 | +window-rule { | ||
| 286 | + // This regular expression is intentially made as specific as possile, | ||
| 287 | + // since this is the default config, and we want no false positives. | ||
| 288 | + // You can get away with just app-id="wezterm" if you want. | ||
| 289 | + // The regular expression can match anywhere in the string. | ||
| 290 | + match app-id=r#"^org\.wezfurlong\.wezterm$"# | ||
| 291 | + default-column-width {} | ||
| 292 | +} | ||
| 293 | + | ||
| 251 | binds { | 294 | binds { |
| 252 | // Keys consist of modifiers separated by + signs, followed by an XKB key name | 295 | // Keys consist of modifiers separated by + signs, followed by an XKB key name |
| 253 | // in the end. To find an XKB name for a particular key, you may use a program | 296 | // in the end. To find an XKB name for a particular key, you may use a program |
src/handlers/compositor.rsmodified+29 -3
| @@ -16,6 +16,7 @@ use smithay::wayland::dmabuf::get_dmabuf; | |||
| 16 | use smithay::wayland::shm::{ShmHandler, ShmState}; | 16 | use smithay::wayland::shm::{ShmHandler, ShmState}; |
| 17 | use smithay::{delegate_compositor, delegate_shm}; | 17 | use smithay::{delegate_compositor, delegate_shm}; |
| 18 | 18 | ||
| 19 | +use super::xdg_shell::{initial_configure_sent, resolve_window_rules}; | ||
| 19 | use crate::niri::{ClientState, State}; | 20 | use crate::niri::{ClientState, State}; |
| 20 | use crate::utils::clone2; | 21 | use crate::utils::clone2; |
| 21 | 22 | ||
| @@ -113,13 +114,28 @@ impl CompositorHandler for State { | |||
| 113 | .and_then(|parent| self.niri.layout.find_window_and_output(&parent)) | 114 | .and_then(|parent| self.niri.layout.find_window_and_output(&parent)) |
| 114 | .map(|(win, _)| win.clone()); | 115 | .map(|(win, _)| win.clone()); |
| 115 | 116 | ||
| 117 | + let (width, output) = { | ||
| 118 | + let config = self.niri.config.borrow(); | ||
| 119 | + let rules = resolve_window_rules(&config.window_rules, window.toplevel()); | ||
| 120 | + let output = rules | ||
| 121 | + .open_on_output | ||
| 122 | + .and_then(|name| self.niri.output_by_name.get(name)) | ||
| 123 | + .cloned(); | ||
| 124 | + (rules.default_width, output) | ||
| 125 | + }; | ||
| 126 | + | ||
| 116 | let win = window.clone(); | 127 | let win = window.clone(); |
| 117 | 128 | ||
| 118 | // Open dialogs immediately to the right of their parent window. | 129 | // Open dialogs immediately to the right of their parent window. |
| 119 | let output = if let Some(p) = parent { | 130 | let output = if let Some(p) = parent { |
| 120 | - self.niri.layout.add_window_right_of(&p, win, None, false) | 131 | + self.niri.layout.add_window_right_of(&p, win, width, false) |
| 132 | + } else if let Some(output) = &output { | ||
| 133 | + self.niri | ||
| 134 | + .layout | ||
| 135 | + .add_window_on_output(output, win, width, false); | ||
| 136 | + Some(output) | ||
| 121 | } else { | 137 | } else { |
| 122 | - self.niri.layout.add_window(win, None, false) | 138 | + self.niri.layout.add_window(win, width, false) |
| 123 | }; | 139 | }; |
| 124 | 140 | ||
| 125 | if let Some(output) = output.cloned() { | 141 | if let Some(output) = output.cloned() { |
| @@ -131,7 +147,17 @@ impl CompositorHandler for State { | |||
| 131 | 147 | ||
| 132 | // The toplevel remains unmapped. | 148 | // The toplevel remains unmapped. |
| 133 | let window = entry.get().clone(); | 149 | let window = entry.get().clone(); |
| 134 | - self.send_initial_configure_if_needed(&window); | 150 | + |
| 151 | + // Send the initial configure in an idle, in case the client sent some more info | ||
| 152 | + // after the initial commit. | ||
| 153 | + if !initial_configure_sent(window.toplevel()) { | ||
| 154 | + self.niri.event_loop.insert_idle(move |state| { | ||
| 155 | + if !window.toplevel().alive() { | ||
| 156 | + return; | ||
| 157 | + } | ||
| 158 | + state.send_initial_configure_if_needed(&window); | ||
| 159 | + }); | ||
| 160 | + } | ||
| 135 | return; | 161 | return; |
| 136 | } | 162 | } |
| 137 | 163 | ||
src/handlers/xdg_shell.rsmodified+109 -7
| @@ -1,3 +1,4 @@ | |||
| 1 | +use niri_config::{Match, WindowRule}; | ||
| 1 | use smithay::desktop::{ | 2 | use smithay::desktop::{ |
| 2 | find_popup_root_surface, get_popup_toplevel_coords, layer_map_for_output, LayerSurface, | 3 | find_popup_root_surface, get_popup_toplevel_coords, layer_map_for_output, LayerSurface, |
| 3 | PopupKeyboardGrab, PopupKind, PopupManager, PopupPointerGrab, PopupUngrabStrategy, Window, | 4 | PopupKeyboardGrab, PopupKind, PopupManager, PopupPointerGrab, PopupUngrabStrategy, Window, |
| @@ -19,13 +20,91 @@ use smithay::wayland::shell::wlr_layer::Layer; | |||
| 19 | use smithay::wayland::shell::xdg::decoration::XdgDecorationHandler; | 20 | use smithay::wayland::shell::xdg::decoration::XdgDecorationHandler; |
| 20 | use smithay::wayland::shell::xdg::{ | 21 | use smithay::wayland::shell::xdg::{ |
| 21 | PopupSurface, PositionerState, ToplevelSurface, XdgPopupSurfaceData, XdgShellHandler, | 22 | PopupSurface, PositionerState, ToplevelSurface, XdgPopupSurfaceData, XdgShellHandler, |
| 22 | - XdgShellState, XdgToplevelSurfaceData, | 23 | + XdgShellState, XdgToplevelSurfaceData, XdgToplevelSurfaceRoleAttributes, |
| 23 | }; | 24 | }; |
| 24 | use smithay::{delegate_kde_decoration, delegate_xdg_decoration, delegate_xdg_shell}; | 25 | use smithay::{delegate_kde_decoration, delegate_xdg_decoration, delegate_xdg_shell}; |
| 25 | 26 | ||
| 27 | +use crate::layout::workspace::ColumnWidth; | ||
| 26 | use crate::niri::{PopupGrabState, State}; | 28 | use crate::niri::{PopupGrabState, State}; |
| 27 | use crate::utils::clone2; | 29 | use crate::utils::clone2; |
| 28 | 30 | ||
| 31 | +#[derive(Debug, Default)] | ||
| 32 | +pub struct ResolvedWindowRule<'a> { | ||
| 33 | + /// Default width for this window. | ||
| 34 | + /// | ||
| 35 | + /// - `None`: unset. | ||
| 36 | + /// - `Some(None)`: set to empty. | ||
| 37 | + /// - `Some(Some(width))`: set to a particular width. | ||
| 38 | + pub default_width: Option<Option<ColumnWidth>>, | ||
| 39 | + | ||
| 40 | + /// Output to open this window on. | ||
| 41 | + pub open_on_output: Option<&'a str>, | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +fn window_matches(role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool { | ||
| 45 | + if let Some(app_id_re) = &m.app_id { | ||
| 46 | + let Some(app_id) = &role.app_id else { | ||
| 47 | + return false; | ||
| 48 | + }; | ||
| 49 | + if !app_id_re.is_match(app_id) { | ||
| 50 | + return false; | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + if let Some(title_re) = &m.title { | ||
| 55 | + let Some(title) = &role.title else { | ||
| 56 | + return false; | ||
| 57 | + }; | ||
| 58 | + if !title_re.is_match(title) { | ||
| 59 | + return false; | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + true | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +pub fn resolve_window_rules<'a>( | ||
| 67 | + rules: &'a [WindowRule], | ||
| 68 | + toplevel: &ToplevelSurface, | ||
| 69 | +) -> ResolvedWindowRule<'a> { | ||
| 70 | + let _span = tracy_client::span!("resolve_window_rules"); | ||
| 71 | + | ||
| 72 | + let mut resolved = ResolvedWindowRule::default(); | ||
| 73 | + | ||
| 74 | + with_states(toplevel.wl_surface(), |states| { | ||
| 75 | + let role = states | ||
| 76 | + .data_map | ||
| 77 | + .get::<XdgToplevelSurfaceData>() | ||
| 78 | + .unwrap() | ||
| 79 | + .lock() | ||
| 80 | + .unwrap(); | ||
| 81 | + | ||
| 82 | + for rule in rules { | ||
| 83 | + if !(rule.matches.is_empty() || rule.matches.iter().any(|m| window_matches(&role, m))) { | ||
| 84 | + continue; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + if rule.excludes.iter().any(|m| window_matches(&role, m)) { | ||
| 88 | + continue; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + if let Some(x) = rule | ||
| 92 | + .default_column_width | ||
| 93 | + .as_ref() | ||
| 94 | + .map(|d| d.0.first().copied().map(ColumnWidth::from)) | ||
| 95 | + { | ||
| 96 | + resolved.default_width = Some(x); | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + if let Some(x) = rule.open_on_output.as_deref() { | ||
| 100 | + resolved.open_on_output = Some(x); | ||
| 101 | + } | ||
| 102 | + } | ||
| 103 | + }); | ||
| 104 | + | ||
| 105 | + resolved | ||
| 106 | +} | ||
| 107 | + | ||
| 29 | impl XdgShellHandler for State { | 108 | impl XdgShellHandler for State { |
| 30 | fn xdg_shell_state(&mut self) -> &mut XdgShellState { | 109 | fn xdg_shell_state(&mut self) -> &mut XdgShellState { |
| 31 | &mut self.niri.xdg_shell_state | 110 | &mut self.niri.xdg_shell_state |
| @@ -237,9 +316,20 @@ impl XdgShellHandler for State { | |||
| 237 | let window = window.clone(); | 316 | let window = window.clone(); |
| 238 | self.niri.layout.set_fullscreen(&window, false); | 317 | self.niri.layout.set_fullscreen(&window, false); |
| 239 | } else if let Some(window) = self.niri.unmapped_windows.get(surface.wl_surface()) { | 318 | } else if let Some(window) = self.niri.unmapped_windows.get(surface.wl_surface()) { |
| 240 | - if let Some(ws) = self.niri.layout.active_workspace() { | 319 | + let config = self.niri.config.borrow(); |
| 320 | + let rules = resolve_window_rules(&config.window_rules, window.toplevel()); | ||
| 321 | + | ||
| 322 | + let output = rules | ||
| 323 | + .open_on_output | ||
| 324 | + .and_then(|name| self.niri.output_by_name.get(name)); | ||
| 325 | + let mon = output.map(|o| self.niri.layout.monitor_for_output(o).unwrap()); | ||
| 326 | + let ws = mon | ||
| 327 | + .map(|mon| mon.active_workspace_ref()) | ||
| 328 | + .or_else(|| self.niri.layout.active_workspace()); | ||
| 329 | + | ||
| 330 | + if let Some(ws) = ws { | ||
| 241 | window.toplevel().with_pending_state(|state| { | 331 | window.toplevel().with_pending_state(|state| { |
| 242 | - state.size = Some(ws.new_window_size()); | 332 | + state.size = Some(ws.new_window_size(rules.default_width)); |
| 243 | state.states.unset(xdg_toplevel::State::Fullscreen); | 333 | state.states.unset(xdg_toplevel::State::Fullscreen); |
| 244 | }); | 334 | }); |
| 245 | } | 335 | } |
| @@ -333,7 +423,7 @@ impl KdeDecorationHandler for State { | |||
| 333 | 423 | ||
| 334 | delegate_kde_decoration!(State); | 424 | delegate_kde_decoration!(State); |
| 335 | 425 | ||
| 336 | -fn initial_configure_sent(toplevel: &ToplevelSurface) -> bool { | 426 | +pub fn initial_configure_sent(toplevel: &ToplevelSurface) -> bool { |
| 337 | with_states(toplevel.wl_surface(), |states| { | 427 | with_states(toplevel.wl_surface(), |states| { |
| 338 | states | 428 | states |
| 339 | .data_map | 429 | .data_map |
| @@ -352,14 +442,26 @@ impl State { | |||
| 352 | return; | 442 | return; |
| 353 | } | 443 | } |
| 354 | 444 | ||
| 445 | + let _span = tracy_client::span!("State::send_initial_configure_if_needed"); | ||
| 446 | + | ||
| 447 | + let config = self.niri.config.borrow(); | ||
| 448 | + let rules = resolve_window_rules(&config.window_rules, toplevel); | ||
| 449 | + | ||
| 450 | + let output = rules | ||
| 451 | + .open_on_output | ||
| 452 | + .and_then(|name| self.niri.output_by_name.get(name)); | ||
| 453 | + let mon = output.map(|o| self.niri.layout.monitor_for_output(o).unwrap()); | ||
| 454 | + let ws = mon | ||
| 455 | + .map(|mon| mon.active_workspace_ref()) | ||
| 456 | + .or_else(|| self.niri.layout.active_workspace()); | ||
| 457 | + | ||
| 355 | // Tell the surface the preferred size and bounds for its likely output. | 458 | // Tell the surface the preferred size and bounds for its likely output. |
| 356 | - if let Some(ws) = self.niri.layout.active_workspace() { | 459 | + if let Some(ws) = ws { |
| 357 | - ws.configure_new_window(window); | 460 | + ws.configure_new_window(window, rules.default_width); |
| 358 | } | 461 | } |
| 359 | 462 | ||
| 360 | // If the user prefers no CSD, it's a reasonable assumption that they would prefer to get | 463 | // If the user prefers no CSD, it's a reasonable assumption that they would prefer to get |
| 361 | // rid of the various client-side rounded corners also by using the tiled state. | 464 | // rid of the various client-side rounded corners also by using the tiled state. |
| 362 | - let config = self.niri.config.borrow(); | ||
| 363 | if config.prefer_no_csd { | 465 | if config.prefer_no_csd { |
| 364 | toplevel.with_pending_state(|state| { | 466 | toplevel.with_pending_state(|state| { |
| 365 | state.states.set(xdg_toplevel::State::TiledLeft); | 467 | state.states.set(xdg_toplevel::State::TiledLeft); |
src/layout/mod.rsmodified+61 -6
| @@ -531,11 +531,14 @@ impl<W: LayoutElement> Layout<W> { | |||
| 531 | pub fn add_window( | 531 | pub fn add_window( |
| 532 | &mut self, | 532 | &mut self, |
| 533 | window: W, | 533 | window: W, |
| 534 | - width: Option<ColumnWidth>, | 534 | + width: Option<Option<ColumnWidth>>, |
| 535 | is_full_width: bool, | 535 | is_full_width: bool, |
| 536 | ) -> Option<&Output> { | 536 | ) -> Option<&Output> { |
| 537 | - let width = width | 537 | + let width = match width { |
| 538 | - .or(self.options.default_width) | 538 | + Some(Some(width)) => Some(width), |
| 539 | + Some(None) => None, | ||
| 540 | + None => self.options.default_width, | ||
| 541 | + } | ||
| 539 | .unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); | 542 | .unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); |
| 540 | 543 | ||
| 541 | match &mut self.monitor_set { | 544 | match &mut self.monitor_set { |
| @@ -584,11 +587,14 @@ impl<W: LayoutElement> Layout<W> { | |||
| 584 | &mut self, | 587 | &mut self, |
| 585 | right_of: &W, | 588 | right_of: &W, |
| 586 | window: W, | 589 | window: W, |
| 587 | - width: Option<ColumnWidth>, | 590 | + width: Option<Option<ColumnWidth>>, |
| 588 | is_full_width: bool, | 591 | is_full_width: bool, |
| 589 | ) -> Option<&Output> { | 592 | ) -> Option<&Output> { |
| 590 | - let width = width | 593 | + let width = match width { |
| 591 | - .or(self.options.default_width) | 594 | + Some(Some(width)) => Some(width), |
| 595 | + Some(None) => None, | ||
| 596 | + None => self.options.default_width, | ||
| 597 | + } | ||
| 592 | .unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); | 598 | .unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); |
| 593 | 599 | ||
| 594 | match &mut self.monitor_set { | 600 | match &mut self.monitor_set { |
| @@ -612,6 +618,55 @@ impl<W: LayoutElement> Layout<W> { | |||
| 612 | } | 618 | } |
| 613 | } | 619 | } |
| 614 | 620 | ||
| 621 | + /// Adds a new window to the layout on a specific output. | ||
| 622 | + pub fn add_window_on_output( | ||
| 623 | + &mut self, | ||
| 624 | + output: &Output, | ||
| 625 | + window: W, | ||
| 626 | + width: Option<Option<ColumnWidth>>, | ||
| 627 | + is_full_width: bool, | ||
| 628 | + ) { | ||
| 629 | + let width = match width { | ||
| 630 | + Some(Some(width)) => Some(width), | ||
| 631 | + Some(None) => None, | ||
| 632 | + None => self.options.default_width, | ||
| 633 | + } | ||
| 634 | + .unwrap_or_else(|| ColumnWidth::Fixed(window.size().w)); | ||
| 635 | + | ||
| 636 | + let MonitorSet::Normal { | ||
| 637 | + monitors, | ||
| 638 | + active_monitor_idx, | ||
| 639 | + .. | ||
| 640 | + } = &mut self.monitor_set | ||
| 641 | + else { | ||
| 642 | + panic!() | ||
| 643 | + }; | ||
| 644 | + | ||
| 645 | + let (mon_idx, mon) = monitors | ||
| 646 | + .iter_mut() | ||
| 647 | + .enumerate() | ||
| 648 | + .find(|(_, mon)| mon.output == *output) | ||
| 649 | + .unwrap(); | ||
| 650 | + | ||
| 651 | + // Don't steal focus from an active fullscreen window. | ||
| 652 | + let mut activate = true; | ||
| 653 | + let ws = &mon.workspaces[mon.active_workspace_idx]; | ||
| 654 | + if mon_idx == *active_monitor_idx | ||
| 655 | + && !ws.columns.is_empty() | ||
| 656 | + && ws.columns[ws.active_column_idx].is_fullscreen | ||
| 657 | + { | ||
| 658 | + activate = false; | ||
| 659 | + } | ||
| 660 | + | ||
| 661 | + mon.add_window( | ||
| 662 | + mon.active_workspace_idx, | ||
| 663 | + window, | ||
| 664 | + activate, | ||
| 665 | + width, | ||
| 666 | + is_full_width, | ||
| 667 | + ); | ||
| 668 | + } | ||
| 669 | + | ||
| 615 | pub fn remove_window(&mut self, window: &W) { | 670 | pub fn remove_window(&mut self, window: &W) { |
| 616 | match &mut self.monitor_set { | 671 | match &mut self.monitor_set { |
| 617 | MonitorSet::Normal { monitors, .. } => { | 672 | MonitorSet::Normal { monitors, .. } => { |
src/layout/monitor.rsmodified+4 -0
| @@ -76,6 +76,10 @@ impl<W: LayoutElement> Monitor<W> { | |||
| 76 | } | 76 | } |
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | + pub fn active_workspace_ref(&self) -> &Workspace<W> { | ||
| 80 | + &self.workspaces[self.active_workspace_idx] | ||
| 81 | + } | ||
| 82 | + | ||
| 79 | pub fn active_workspace(&mut self) -> &mut Workspace<W> { | 83 | pub fn active_workspace(&mut self) -> &mut Workspace<W> { |
| 80 | &mut self.workspaces[self.active_workspace_idx] | 84 | &mut self.workspaces[self.active_workspace_idx] |
| 81 | } | 85 | } |
src/layout/workspace.rsmodified+17 -4
| @@ -321,8 +321,17 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 321 | )) | 321 | )) |
| 322 | } | 322 | } |
| 323 | 323 | ||
| 324 | - pub fn new_window_size(&self) -> Size<i32, Logical> { | 324 | + pub fn new_window_size( |
| 325 | - let width = if let Some(width) = self.options.default_width { | 325 | + &self, |
| 326 | + default_width: Option<Option<ColumnWidth>>, | ||
| 327 | + ) -> Size<i32, Logical> { | ||
| 328 | + let default_width = match default_width { | ||
| 329 | + Some(Some(width)) => Some(width), | ||
| 330 | + Some(None) => None, | ||
| 331 | + None => self.options.default_width, | ||
| 332 | + }; | ||
| 333 | + | ||
| 334 | + let width = if let Some(width) = default_width { | ||
| 326 | let mut width = width.resolve(&self.options, self.working_area.size.w); | 335 | let mut width = width.resolve(&self.options, self.working_area.size.w); |
| 327 | if !self.options.border.off { | 336 | if !self.options.border.off { |
| 328 | width -= self.options.border.width as i32 * 2; | 337 | width -= self.options.border.width as i32 * 2; |
| @@ -340,8 +349,12 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 340 | Size::from((width, max(height, 1))) | 349 | Size::from((width, max(height, 1))) |
| 341 | } | 350 | } |
| 342 | 351 | ||
| 343 | - pub fn configure_new_window(&self, window: &Window) { | 352 | + pub fn configure_new_window( |
| 344 | - let size = self.new_window_size(); | 353 | + &self, |
| 354 | + window: &Window, | ||
| 355 | + default_width: Option<Option<ColumnWidth>>, | ||
| 356 | + ) { | ||
| 357 | + let size = self.new_window_size(default_width); | ||
| 345 | let bounds = self.toplevel_bounds(); | 358 | let bounds = self.toplevel_bounds(); |
| 346 | 359 | ||
| 347 | if let Some(output) = self.output.as_ref() { | 360 | if let Some(output) = self.output.as_ref() { |