forge
Explore
Log in

Don't you dare go Hollow

Activate newly mapped windows with a valid activation token

Browse files at this commit

305fc3b5576c4c6e3d899b3413dbbb8727922cfeChristian MeisslParent: 61f2ac0
most of the time the activation token is passed
while the window is still unmapped. in this case
store the intend to activate the window for
later retrieval on map.
niri-visual-tests/src/cases/layout.rsmodified+3 -2
@@ -3,7 +3,7 @@ use std::time::Duration;
3 3
4 use niri::animation::Clock;4 use niri::animation::Clock;
5 use niri::layout::workspace::ColumnWidth;5 use niri::layout::workspace::ColumnWidth;
6-use niri::layout::{LayoutElement as _, Options};6+use niri::layout::{ActivateWindow, LayoutElement as _, Options};
7 use niri::render_helpers::RenderTarget;7 use niri::render_helpers::RenderTarget;
8 use niri_config::{Color, FloatOrInt, OutputName};8 use niri_config::{Color, FloatOrInt, OutputName};
9 use smithay::backend::renderer::element::RenderElement;9 use smithay::backend::renderer::element::RenderElement;
@@ -162,7 +162,8 @@ impl Layout {
162 window.request_size(ws.new_window_size(width, window.rules()), false, None);162 window.request_size(ws.new_window_size(width, window.rules()), false, None);
163 window.communicate();163 window.communicate();
164 164
165- self.layout.add_window(window.clone(), width, false);165+ self.layout
166+ .add_window(window.clone(), width, false, ActivateWindow::default());
166 self.windows.push(window);167 self.windows.push(window);
167 }168 }
168 169
src/handlers/compositor.rsmodified+30 -5
@@ -18,6 +18,8 @@ use smithay::wayland::shm::{ShmHandler, ShmState};
18 use smithay::{delegate_compositor, delegate_shm};18 use smithay::{delegate_compositor, delegate_shm};
19 19
20 use super::xdg_shell::add_mapped_toplevel_pre_commit_hook;20 use super::xdg_shell::add_mapped_toplevel_pre_commit_hook;
21+use crate::handlers::XDG_ACTIVATION_TOKEN_TIMEOUT;
22+use crate::layout::ActivateWindow;
21 use crate::niri::{ClientState, State};23 use crate::niri::{ClientState, State};
22 use crate::utils::send_scale_transform;24 use crate::utils::send_scale_transform;
23 use crate::utils::transaction::Transaction;25 use crate::utils::transaction::Transaction;
@@ -84,7 +86,11 @@ impl CompositorHandler for State {
84 86
85 if is_mapped {87 if is_mapped {
86 // The toplevel got mapped.88 // The toplevel got mapped.
87- let Unmapped { window, state } = entry.remove();89+ let Unmapped {
90+ window,
91+ state,
92+ activation_token_data,
93+ } = entry.remove();
88 94
89 window.on_commit();95 window.on_commit();
90 96
@@ -133,8 +139,20 @@ impl CompositorHandler for State {
133 let mapped = Mapped::new(window, rules, hook);139 let mapped = Mapped::new(window, rules, hook);
134 let window = mapped.window.clone();140 let window = mapped.window.clone();
135 141
142+ // Check the token timestamp again in case the window took a while between
143+ // requesting activation and mapping.
144+ let activate = match activation_token_data
145+ .filter(|token| token.timestamp.elapsed() < XDG_ACTIVATION_TOKEN_TIMEOUT)
146+ {
147+ Some(_) => ActivateWindow::Yes,
148+ None => ActivateWindow::Smart,
149+ };
150+
136 let output = if let Some(p) = parent {151 let output = if let Some(p) = parent {
137 // Open dialogs immediately to the right of their parent window.152 // Open dialogs immediately to the right of their parent window.
153+ //
154+ // FIXME: do we want to use activate here? How do we want things to behave
155+ // exactly?
138 self.niri156 self.niri
139 .layout157 .layout
140 .add_window_right_of(&p, mapped, width, is_full_width)158 .add_window_right_of(&p, mapped, width, is_full_width)
@@ -144,14 +162,21 @@ impl CompositorHandler for State {
144 mapped,162 mapped,
145 width,163 width,
146 is_full_width,164 is_full_width,
165+ activate,
147 )166 )
148 } else if let Some(output) = &output {167 } else if let Some(output) = &output {
149- self.niri168+ self.niri.layout.add_window_on_output(
150- .layout169+ output,
151- .add_window_on_output(output, mapped, width, is_full_width);170+ mapped,
171+ width,
172+ is_full_width,
173+ activate,
174+ );
152 Some(output)175 Some(output)
153 } else {176 } else {
154- self.niri.layout.add_window(mapped, width, is_full_width)177+ self.niri
178+ .layout
179+ .add_window(mapped, width, is_full_width, activate)
155 };180 };
156 181
157 if let Some(output) = output.cloned() {182 if let Some(output) = output.cloned() {
src/handlers/mod.rsmodified+4 -2
@@ -670,10 +670,12 @@ impl XdgActivationHandler for State {
670 self.niri.layout.activate_window(&window);670 self.niri.layout.activate_window(&window);
671 self.niri.layer_shell_on_demand_focus = None;671 self.niri.layer_shell_on_demand_focus = None;
672 self.niri.queue_redraw_all();672 self.niri.queue_redraw_all();
673-673+ } else if let Some(unmapped) = self.niri.unmapped_windows.get_mut(&surface) {
674- self.niri.activation_state.remove_token(&token);674+ unmapped.activation_token_data = Some(token_data);
675 }675 }
676 }676 }
677+
678+ self.niri.activation_state.remove_token(&token);
677 }679 }
678 }680 }
679 delegate_xdg_activation!(State);681 delegate_xdg_activation!(State);
src/handlers/xdg_shell.rsmodified+1 -1
@@ -752,7 +752,7 @@ impl State {
752 self.niri.is_at_startup,752 self.niri.is_at_startup,
753 );753 );
754 754
755- let Unmapped { window, state } = unmapped;755+ let Unmapped { window, state, .. } = unmapped;
756 756
757 let InitialConfigureState::NotConfigured { wants_fullscreen } = state else {757 let InitialConfigureState::NotConfigured { wants_fullscreen } = state else {
758 error!("window must not be already configured in send_initial_configure()");758 error!("window must not be already configured in send_initial_configure()");
src/layout/mod.rsmodified+78 -32
@@ -358,6 +358,18 @@ pub struct RemovedTile<W: LayoutElement> {
358 is_full_width: bool,358 is_full_width: bool,
359 }359 }
360 360
361+/// Whether to activate a newly added window.
362+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
363+pub enum ActivateWindow {
364+ /// Activate unconditionally.
365+ Yes,
366+ /// Activate based on heuristics.
367+ #[default]
368+ Smart,
369+ /// Do not activate.
370+ No,
371+}
372+
361 impl<W: LayoutElement> InteractiveMoveState<W> {373 impl<W: LayoutElement> InteractiveMoveState<W> {
362 fn moving(&self) -> Option<&InteractiveMoveData<W>> {374 fn moving(&self) -> Option<&InteractiveMoveData<W>> {
363 match self {375 match self {
@@ -383,6 +395,16 @@ impl<W: LayoutElement> InteractiveMoveData<W> {
383 }395 }
384 }396 }
385 397
398+impl ActivateWindow {
399+ pub fn map_smart(self, f: impl FnOnce() -> bool) -> bool {
400+ match self {
401+ ActivateWindow::Yes => true,
402+ ActivateWindow::Smart => f(),
403+ ActivateWindow::No => false,
404+ }
405+ }
406+}
407+
386 impl Options {408 impl Options {
387 fn from_config(config: &Config) -> Self {409 fn from_config(config: &Config) -> Self {
388 let layout = &config.layout;410 let layout = &config.layout;
@@ -753,6 +775,7 @@ impl<W: LayoutElement> Layout<W> {
753 window: W,775 window: W,
754 width: Option<ColumnWidth>,776 width: Option<ColumnWidth>,
755 is_full_width: bool,777 is_full_width: bool,
778+ activate: ActivateWindow,
756 ) -> Option<&Output> {779 ) -> Option<&Output> {
757 let width = self.resolve_default_width(&window, width);780 let width = self.resolve_default_width(&window, width);
758 781
@@ -771,20 +794,27 @@ impl<W: LayoutElement> Layout<W> {
771 })794 })
772 .unwrap();795 .unwrap();
773 796
774- // Don't steal focus from an active fullscreen window.797+ if activate == ActivateWindow::Yes {
775- let mut activate = true;798+ *active_monitor_idx = mon_idx;
776- let ws = &mon.workspaces[ws_idx];
777- if mon_idx == *active_monitor_idx
778- && !ws.columns.is_empty()
779- && ws.columns[ws.active_column_idx].is_fullscreen
780- {
781- activate = false;
782 }799 }
783 800
784- // Don't activate if on a different workspace.801+ let activate = activate.map_smart(|| {
785- if mon.active_workspace_idx != ws_idx {802+ // Don't steal focus from an active fullscreen window.
786- activate = false;803+ let ws = &mon.workspaces[ws_idx];
787- }804+ if mon_idx == *active_monitor_idx
805+ && !ws.columns.is_empty()
806+ && ws.columns[ws.active_column_idx].is_fullscreen
807+ {
808+ return false;
809+ }
810+
811+ // Don't activate if on a different workspace.
812+ if mon.active_workspace_idx != ws_idx {
813+ return false;
814+ }
815+
816+ true
817+ });
788 818
789 mon.add_window(ws_idx, window, activate, width, is_full_width);819 mon.add_window(ws_idx, window, activate, width, is_full_width);
790 Some(&mon.output)820 Some(&mon.output)
@@ -798,7 +828,8 @@ impl<W: LayoutElement> Layout<W> {
798 .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name))828 .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name))
799 })829 })
800 .unwrap();830 .unwrap();
801- ws.add_window(None, window, true, width, is_full_width);831+ let activate = activate.map_smart(|| true);
832+ ws.add_window(None, window, activate, width, is_full_width);
802 None833 None
803 }834 }
804 }835 }
@@ -835,6 +866,7 @@ impl<W: LayoutElement> Layout<W> {
835 window: W,866 window: W,
836 width: Option<ColumnWidth>,867 width: Option<ColumnWidth>,
837 is_full_width: bool,868 is_full_width: bool,
869+ activate: ActivateWindow,
838 ) -> Option<&Output> {870 ) -> Option<&Output> {
839 let width = self.resolve_default_width(&window, width);871 let width = self.resolve_default_width(&window, width);
840 872
@@ -846,12 +878,11 @@ impl<W: LayoutElement> Layout<W> {
846 } => {878 } => {
847 let mon = &mut monitors[*active_monitor_idx];879 let mon = &mut monitors[*active_monitor_idx];
848 880
849- // Don't steal focus from an active fullscreen window.881+ let activate = activate.map_smart(|| {
850- let mut activate = true;882+ // Don't steal focus from an active fullscreen window.
851- let ws = &mon.workspaces[mon.active_workspace_idx];883+ let ws = &mon.workspaces[mon.active_workspace_idx];
852- if !ws.columns.is_empty() && ws.columns[ws.active_column_idx].is_fullscreen {884+ ws.columns.is_empty() || !ws.columns[ws.active_column_idx].is_fullscreen
853- activate = false;885+ });
854- }
855 886
856 mon.add_window(887 mon.add_window(
857 mon.active_workspace_idx,888 mon.active_workspace_idx,
@@ -872,7 +903,8 @@ impl<W: LayoutElement> Layout<W> {
872 ));903 ));
873 &mut workspaces[0]904 &mut workspaces[0]
874 };905 };
875- ws.add_window(None, window, true, width, is_full_width);906+ let activate = activate.map_smart(|| true);
907+ ws.add_window(None, window, activate, width, is_full_width);
876 None908 None
877 }909 }
878 }910 }
@@ -893,11 +925,12 @@ impl<W: LayoutElement> Layout<W> {
893 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {925 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
894 if right_of == move_.tile.window().id() {926 if right_of == move_.tile.window().id() {
895 let output = move_.output.clone();927 let output = move_.output.clone();
928+ let activate = ActivateWindow::default();
896 if self.monitor_for_output(&output).is_some() {929 if self.monitor_for_output(&output).is_some() {
897- self.add_window_on_output(&output, window, width, is_full_width);930+ self.add_window_on_output(&output, window, width, is_full_width, activate);
898 return Some(&self.monitor_for_output(&output).unwrap().output);931 return Some(&self.monitor_for_output(&output).unwrap().output);
899 } else {932 } else {
900- return self.add_window(window, width, is_full_width);933+ return self.add_window(window, width, is_full_width, activate);
901 }934 }
902 }935 }
903 }936 }
@@ -932,6 +965,7 @@ impl<W: LayoutElement> Layout<W> {
932 window: W,965 window: W,
933 width: Option<ColumnWidth>,966 width: Option<ColumnWidth>,
934 is_full_width: bool,967 is_full_width: bool,
968+ activate: ActivateWindow,
935 ) {969 ) {
936 let width = self.resolve_default_width(&window, width);970 let width = self.resolve_default_width(&window, width);
937 971
@@ -950,16 +984,22 @@ impl<W: LayoutElement> Layout<W> {
950 .find(|(_, mon)| mon.output == *output)984 .find(|(_, mon)| mon.output == *output)
951 .unwrap();985 .unwrap();
952 986
953- // Don't steal focus from an active fullscreen window.987+ if activate == ActivateWindow::Yes {
954- let mut activate = true;988+ *active_monitor_idx = mon_idx;
955- let ws = &mon.workspaces[mon.active_workspace_idx];
956- if mon_idx == *active_monitor_idx
957- && !ws.columns.is_empty()
958- && ws.columns[ws.active_column_idx].is_fullscreen
959- {
960- activate = false;
961 }989 }
962 990
991+ let activate = activate.map_smart(|| {
992+ // Don't steal focus from an active fullscreen window.
993+ let ws = &mon.workspaces[mon.active_workspace_idx];
994+ if mon_idx == *active_monitor_idx
995+ && !ws.columns.is_empty()
996+ && ws.columns[ws.active_column_idx].is_fullscreen
997+ {
998+ return false;
999+ }
1000+ true
1001+ });
1002+
963 mon.add_window(1003 mon.add_window(
964 mon.active_workspace_idx,1004 mon.active_workspace_idx,
965 window,1005 window,
@@ -4365,7 +4405,7 @@ mod tests {
4365 }4405 }
4366 4406
4367 let win = TestWindow::new(id, bbox, min_max_size.0, min_max_size.1);4407 let win = TestWindow::new(id, bbox, min_max_size.0, min_max_size.1);
4368- layout.add_window(win, None, false);4408+ layout.add_window(win, None, false, ActivateWindow::default());
4369 }4409 }
4370 Op::AddWindowRightOf {4410 Op::AddWindowRightOf {
4371 id,4411 id,
@@ -4482,7 +4522,13 @@ mod tests {
4482 }4522 }
4483 4523
4484 let win = TestWindow::new(id, bbox, min_max_size.0, min_max_size.1);4524 let win = TestWindow::new(id, bbox, min_max_size.0, min_max_size.1);
4485- layout.add_window_to_named_workspace(&ws_name, win, None, false);4525+ layout.add_window_to_named_workspace(
4526+ &ws_name,
4527+ win,
4528+ None,
4529+ false,
4530+ ActivateWindow::default(),
4531+ );
4486 }4532 }
4487 Op::CloseWindow(id) => {4533 Op::CloseWindow(id) => {
4488 layout.remove_window(&id, Transaction::new());4534 layout.remove_window(&id, Transaction::new());
src/window/unmapped.rsmodified+4 -0
@@ -1,6 +1,7 @@
1 use smithay::desktop::Window;1 use smithay::desktop::Window;
2 use smithay::output::Output;2 use smithay::output::Output;
3 use smithay::wayland::shell::xdg::ToplevelSurface;3 use smithay::wayland::shell::xdg::ToplevelSurface;
4+use smithay::wayland::xdg_activation::XdgActivationTokenData;
4 5
5 use super::ResolvedWindowRules;6 use super::ResolvedWindowRules;
6 use crate::layout::workspace::ColumnWidth;7 use crate::layout::workspace::ColumnWidth;
@@ -9,6 +10,8 @@ use crate::layout::workspace::ColumnWidth;
9 pub struct Unmapped {10 pub struct Unmapped {
10 pub window: Window,11 pub window: Window,
11 pub state: InitialConfigureState,12 pub state: InitialConfigureState,
13+ /// Activation token, if one was used on this unmapped window.
14+ pub activation_token_data: Option<XdgActivationTokenData>,
12 }15 }
13 16
14 #[allow(clippy::large_enum_variant)]17 #[allow(clippy::large_enum_variant)]
@@ -57,6 +60,7 @@ impl Unmapped {
57 state: InitialConfigureState::NotConfigured {60 state: InitialConfigureState::NotConfigured {
58 wants_fullscreen: None,61 wants_fullscreen: None,
59 },62 },
63+ activation_token_data: None,
60 }64 }
61 }65 }
62 66