Skip to content

Commit bdef75d

Browse files
wash2Drakulix
authored andcommitted
refactor: swap to the activated element's workspace if different
1 parent cd5efd0 commit bdef75d

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

src/wayland/handlers/xdg_activation.rs

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::shell::focus::target::KeyboardFocusTarget;
2+
use crate::shell::WorkspaceDelta;
23
use crate::{shell::ActivationKey, state::ClientState, utils::prelude::*};
34
use crate::{
45
state::State,
@@ -12,7 +13,7 @@ use smithay::{
1213
XdgActivationHandler, XdgActivationState, XdgActivationToken, XdgActivationTokenData,
1314
},
1415
};
15-
use tracing::debug;
16+
use tracing::{debug, warn};
1617

1718
#[derive(Debug, Clone, Copy)]
1819
pub enum ActivationContext {
@@ -123,21 +124,39 @@ impl XdgActivationHandler for State {
123124
shell.unminimize_request(&surface, &seat, &self.common.event_loop_handle);
124125
}
125126

126-
let element_workspace = shell.space_for(&element).map(|w| w.handle.clone());
127-
let current_workspace = shell.active_space_mut(&current_output).unwrap();
128-
129-
let in_current_workspace = element_workspace
130-
.as_ref()
131-
.map(|w| *w == current_workspace.handle)
132-
.unwrap_or(false);
127+
let Some((element_output, element_workspace)) = shell
128+
.space_for(&element)
129+
.map(|w| (w.output.clone(), w.handle.clone()))
130+
else {
131+
return;
132+
};
133+
let in_current_workspace =
134+
element_workspace == shell.active_space(&current_output).unwrap().handle;
135+
136+
if !in_current_workspace {
137+
let Some(idx) = shell
138+
.workspaces
139+
.idx_for_handle(&element_output, &element_workspace)
140+
else {
141+
warn!("Couldn't determine idx for elements workspace?");
142+
return;
143+
};
133144

134-
if in_current_workspace {
135-
current_workspace
136-
.floating_layer
137-
.space
138-
.raise_element(&element, true);
145+
if let Err(err) = shell.activate(
146+
&element_output,
147+
idx,
148+
WorkspaceDelta::new_shortcut(),
149+
&mut self.common.workspace_state.update(),
150+
) {
151+
warn!("Failed to activate the workspace: {err:?}");
152+
}
139153
}
140154

155+
let current_workspace = shell.active_space_mut(&current_output).unwrap();
156+
current_workspace
157+
.floating_layer
158+
.space
159+
.raise_element(&element, true);
141160
if element.is_stack() {
142161
if let Some((window, _)) = element.windows().find(|(window, _)| {
143162
let mut found = false;
@@ -149,38 +168,34 @@ impl XdgActivationHandler for State {
149168
found
150169
}) {
151170
element.set_active(&window);
171+
} else {
172+
warn!("Failed to find activated window in the stack");
173+
return;
152174
}
153175
}
154176

155-
if in_current_workspace {
156-
if seat.get_keyboard().unwrap().current_focus()
157-
!= Some(element.clone().into())
158-
&& current_workspace.is_tiled(&surface)
177+
if seat.get_keyboard().unwrap().current_focus() != Some(element.clone().into())
178+
&& current_workspace.is_tiled(&surface)
179+
{
180+
for mapped in current_workspace
181+
.mapped()
182+
.filter(|m| m.maximized_state.lock().unwrap().is_some())
183+
.cloned()
184+
.collect::<Vec<_>>()
185+
.into_iter()
159186
{
160-
for mapped in current_workspace
161-
.mapped()
162-
.filter(|m| m.maximized_state.lock().unwrap().is_some())
163-
.cloned()
164-
.collect::<Vec<_>>()
165-
.into_iter()
166-
{
167-
current_workspace.unmaximize_request(&mapped);
168-
}
187+
current_workspace.unmaximize_request(&mapped);
169188
}
170-
171-
std::mem::drop(shell);
172-
Shell::set_focus(
173-
self,
174-
Some(&KeyboardFocusTarget::Element(element.clone())),
175-
&seat,
176-
None,
177-
false,
178-
);
179-
} else if let Some(w) = element_workspace {
180-
shell.append_focus_stack(element, &seat);
181-
let mut workspace_guard = self.common.workspace_state.update();
182-
workspace_guard.add_workspace_state(&w, WState::Urgent);
183189
}
190+
191+
std::mem::drop(shell);
192+
Shell::set_focus(
193+
self,
194+
Some(&KeyboardFocusTarget::Element(element.clone())),
195+
&seat,
196+
None,
197+
false,
198+
);
184199
} else if let Some((workspace, _)) = shell.workspace_for_surface(&surface) {
185200
let current_workspace = shell.active_space(&current_output).unwrap();
186201
if workspace == current_workspace.handle {

0 commit comments

Comments
 (0)