2022-07-18 21:26:02 +02:00
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
|
2024-02-23 17:25:40 +01:00
|
|
|
|
use smithay::{
|
2025-10-16 18:53:57 +02:00
|
|
|
|
desktop::{WindowSurfaceType, layer_map_for_output},
|
|
|
|
|
|
input::{Seat, pointer::MotionEvent},
|
2024-02-23 17:25:40 +01:00
|
|
|
|
output::Output,
|
|
|
|
|
|
reexports::wayland_server::DisplayHandle,
|
2025-10-16 18:53:57 +02:00
|
|
|
|
utils::{Point, Rectangle, SERIAL_COUNTER, Size},
|
2025-06-25 17:54:27 +02:00
|
|
|
|
wayland::seat::WaylandFocus,
|
2024-02-23 17:25:40 +01:00
|
|
|
|
};
|
2022-07-18 21:26:02 +02:00
|
|
|
|
|
|
|
|
|
|
use crate::{
|
2025-10-16 18:53:57 +02:00
|
|
|
|
shell::{CosmicSurface, Shell, WorkspaceDelta, focus::target::KeyboardFocusTarget},
|
2022-07-18 21:26:02 +02:00
|
|
|
|
utils::prelude::*,
|
2023-01-16 15:12:25 +01:00
|
|
|
|
wayland::protocols::{
|
|
|
|
|
|
toplevel_info::ToplevelInfoHandler,
|
|
|
|
|
|
toplevel_management::{
|
2025-10-16 18:53:57 +02:00
|
|
|
|
ManagementWindow, ToplevelManagementHandler, ToplevelManagementState,
|
|
|
|
|
|
delegate_toplevel_management, toplevel_rectangle_for,
|
2023-01-16 15:12:25 +01:00
|
|
|
|
},
|
2025-02-20 10:29:23 -08:00
|
|
|
|
workspace::WorkspaceHandle,
|
2022-07-18 21:26:02 +02:00
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
impl ToplevelManagementHandler for State {
|
|
|
|
|
|
fn toplevel_management_state(&mut self) -> &mut ToplevelManagementState {
|
2024-04-10 15:49:08 +02:00
|
|
|
|
&mut self.common.toplevel_management_state
|
2022-07-18 21:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-16 15:12:25 +01:00
|
|
|
|
fn activate(
|
|
|
|
|
|
&mut self,
|
2024-02-23 17:25:40 +01:00
|
|
|
|
dh: &DisplayHandle,
|
2023-01-16 15:12:25 +01:00
|
|
|
|
window: &<Self as ToplevelInfoHandler>::Window,
|
|
|
|
|
|
seat: Option<Seat<Self>>,
|
|
|
|
|
|
) {
|
2024-02-23 17:25:40 +01:00
|
|
|
|
self.unminimize(dh, window);
|
2024-04-10 15:49:08 +02:00
|
|
|
|
|
2025-05-20 17:41:27 +02:00
|
|
|
|
let mut shell = self.common.shell.write();
|
2024-04-10 15:49:08 +02:00
|
|
|
|
for output in shell.outputs().cloned().collect::<Vec<_>>().iter() {
|
|
|
|
|
|
let maybe = shell
|
2022-09-28 12:01:29 +02:00
|
|
|
|
.workspaces
|
|
|
|
|
|
.spaces_for_output(output)
|
|
|
|
|
|
.enumerate()
|
2024-02-23 17:25:40 +01:00
|
|
|
|
.find(|(_, w)| {
|
2026-06-02 09:48:33 -06:00
|
|
|
|
w.get_fullscreen_surfaces().any(|f| &f.surface == window)
|
2025-06-25 17:54:27 +02:00
|
|
|
|
|| w.mapped()
|
|
|
|
|
|
.flat_map(|m| m.windows().map(|(s, _)| s))
|
|
|
|
|
|
.any(|w| &w == window)
|
2024-02-23 17:25:40 +01:00
|
|
|
|
});
|
2022-09-28 12:01:29 +02:00
|
|
|
|
|
2025-08-12 16:13:16 +02:00
|
|
|
|
let seat = seat.clone().unwrap_or(shell.seats.last_active().clone());
|
|
|
|
|
|
let (target, new_pos) = if let Some((idx, workspace)) = maybe {
|
2025-03-26 18:25:22 +01:00
|
|
|
|
let handle = workspace.handle;
|
2025-08-12 16:13:16 +02:00
|
|
|
|
let new_pos = shell.activate(
|
2025-10-16 13:50:32 +02:00
|
|
|
|
output,
|
2024-09-03 12:35:39 +01:00
|
|
|
|
idx,
|
2024-03-07 13:14:53 -06:00
|
|
|
|
WorkspaceDelta::new_shortcut(),
|
2024-04-10 15:49:08 +02:00
|
|
|
|
&mut self.common.workspace_state.update(),
|
2024-08-20 08:50:13 -07:00
|
|
|
|
);
|
2025-03-26 18:25:22 +01:00
|
|
|
|
|
|
|
|
|
|
let workspace = shell.workspaces.space_for_handle_mut(&handle).unwrap();
|
2025-06-25 17:54:27 +02:00
|
|
|
|
if seat
|
|
|
|
|
|
.get_keyboard()
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
.current_focus()
|
|
|
|
|
|
.is_some_and(|focus| !focus.windows().any(|w| w == *window))
|
|
|
|
|
|
&& workspace.is_tiled(window)
|
2025-03-26 18:25:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
for mapped in workspace
|
|
|
|
|
|
.mapped()
|
2025-07-02 15:09:06 +02:00
|
|
|
|
.filter(|m| {
|
|
|
|
|
|
m.maximized_state.lock().unwrap().is_some()
|
|
|
|
|
|
&& !m.windows().any(|(ref w, _)| w == window)
|
|
|
|
|
|
})
|
2025-03-26 18:25:22 +01:00
|
|
|
|
.cloned()
|
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
{
|
|
|
|
|
|
workspace.unmaximize_request(&mapped);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-25 17:54:27 +02:00
|
|
|
|
|
|
|
|
|
|
let target = if let Some(mapped) = workspace.element_for_surface(window) {
|
|
|
|
|
|
mapped.focus_window(window);
|
|
|
|
|
|
KeyboardFocusTarget::Element(mapped.clone())
|
|
|
|
|
|
} else {
|
|
|
|
|
|
KeyboardFocusTarget::Fullscreen(window.clone())
|
|
|
|
|
|
};
|
2024-08-20 08:50:13 -07:00
|
|
|
|
|
2025-08-12 16:13:16 +02:00
|
|
|
|
(target, new_pos.ok())
|
|
|
|
|
|
// sticky window?
|
|
|
|
|
|
} else if let Some(mapped) = shell
|
|
|
|
|
|
.workspaces
|
|
|
|
|
|
.sets
|
|
|
|
|
|
.get(output)
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
.sticky_layer
|
|
|
|
|
|
.mapped()
|
|
|
|
|
|
.find(|m| m.windows().any(|(w, _)| &w == window))
|
|
|
|
|
|
{
|
|
|
|
|
|
mapped.focus_window(window);
|
|
|
|
|
|
|
|
|
|
|
|
let output_geo = output.geometry();
|
|
|
|
|
|
let new_pos =
|
|
|
|
|
|
output_geo.loc + Point::from((output_geo.size.w / 2, output_geo.size.h / 2));
|
|
|
|
|
|
(KeyboardFocusTarget::Element(mapped.clone()), Some(new_pos))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
std::mem::drop(shell);
|
|
|
|
|
|
|
2026-01-30 18:22:55 +01:00
|
|
|
|
// move pointer to window if it’s on a different monitor/output
|
2026-07-02 09:53:03 -06:00
|
|
|
|
if seat.active_output() != *output
|
|
|
|
|
|
&& self.common.config.cosmic_conf.cursor_follows_focus
|
|
|
|
|
|
&& let Some(new_pos) = new_pos
|
|
|
|
|
|
{
|
2026-02-23 16:25:06 +01:00
|
|
|
|
seat.set_active_output(output);
|
|
|
|
|
|
if let Some(ptr) = seat.get_pointer() {
|
|
|
|
|
|
let serial = SERIAL_COUNTER.next_serial();
|
|
|
|
|
|
ptr.motion(
|
|
|
|
|
|
self,
|
|
|
|
|
|
None,
|
|
|
|
|
|
&MotionEvent {
|
|
|
|
|
|
location: new_pos.to_f64().as_logical(),
|
|
|
|
|
|
serial,
|
|
|
|
|
|
time: self.common.clock.now().as_millis(),
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
ptr.frame(self);
|
2024-08-20 08:50:13 -07:00
|
|
|
|
}
|
2022-07-18 21:26:02 +02:00
|
|
|
|
}
|
2025-08-12 16:13:16 +02:00
|
|
|
|
|
|
|
|
|
|
Shell::set_focus(self, Some(&target), &seat, None, false);
|
|
|
|
|
|
return;
|
2022-07-18 21:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-16 15:12:25 +01:00
|
|
|
|
fn close(&mut self, _dh: &DisplayHandle, window: &<Self as ToplevelInfoHandler>::Window) {
|
|
|
|
|
|
window.close();
|
|
|
|
|
|
}
|
2023-12-06 15:13:48 -08:00
|
|
|
|
|
|
|
|
|
|
fn move_to_workspace(
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
_dh: &DisplayHandle,
|
|
|
|
|
|
window: &<Self as ToplevelInfoHandler>::Window,
|
2025-02-20 10:29:23 -08:00
|
|
|
|
to_handle: WorkspaceHandle,
|
2023-12-06 15:13:48 -08:00
|
|
|
|
_output: Output,
|
|
|
|
|
|
) {
|
2025-05-20 17:41:27 +02:00
|
|
|
|
let mut shell = self.common.shell.write();
|
2025-06-25 17:54:27 +02:00
|
|
|
|
let seat = shell.seats.last_active().clone();
|
|
|
|
|
|
let Some(surface) = window.wl_surface() else {
|
|
|
|
|
|
return;
|
|
|
|
|
|
};
|
2025-10-16 13:50:32 +02:00
|
|
|
|
let Some((from_workspace, _)) = shell.workspace_for_surface(&surface) else {
|
2025-06-25 17:54:27 +02:00
|
|
|
|
return;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let res = shell.move_window(
|
|
|
|
|
|
Some(&seat),
|
|
|
|
|
|
window,
|
|
|
|
|
|
&from_workspace,
|
|
|
|
|
|
&to_handle,
|
|
|
|
|
|
false,
|
|
|
|
|
|
None,
|
|
|
|
|
|
&mut self.common.workspace_state.update(),
|
|
|
|
|
|
&self.common.event_loop_handle,
|
|
|
|
|
|
);
|
|
|
|
|
|
if let Some((target, _)) = res {
|
|
|
|
|
|
std::mem::drop(shell);
|
|
|
|
|
|
Shell::set_focus(self, Some(&target), &seat, None, true);
|
2023-12-06 15:13:48 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-02-08 20:29:16 +01:00
|
|
|
|
|
|
|
|
|
|
fn fullscreen(
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
_dh: &DisplayHandle,
|
|
|
|
|
|
window: &<Self as ToplevelInfoHandler>::Window,
|
|
|
|
|
|
output: Option<Output>,
|
|
|
|
|
|
) {
|
2025-05-20 17:41:27 +02:00
|
|
|
|
let mut shell = self.common.shell.write();
|
2024-04-10 15:49:08 +02:00
|
|
|
|
let seat = shell.seats.last_active().clone();
|
2025-06-25 17:54:27 +02:00
|
|
|
|
let output = output
|
|
|
|
|
|
.or_else(|| {
|
|
|
|
|
|
window
|
|
|
|
|
|
.wl_surface()
|
2025-10-16 13:50:32 +02:00
|
|
|
|
.and_then(|surface| shell.visible_output_for_surface(&surface).cloned())
|
2025-06-25 17:54:27 +02:00
|
|
|
|
})
|
|
|
|
|
|
.unwrap_or_else(|| seat.focused_or_active_output());
|
|
|
|
|
|
if let Some(target) =
|
|
|
|
|
|
shell.fullscreen_request(window, output, &self.common.event_loop_handle)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::mem::drop(shell);
|
|
|
|
|
|
Shell::set_focus(self, Some(&target), &seat, None, true);
|
2024-02-08 20:29:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn unfullscreen(
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
_dh: &DisplayHandle,
|
|
|
|
|
|
window: &<Self as ToplevelInfoHandler>::Window,
|
|
|
|
|
|
) {
|
2025-05-20 17:41:27 +02:00
|
|
|
|
let mut shell = self.common.shell.write();
|
2025-07-17 18:29:57 +02:00
|
|
|
|
let _ = shell.unfullscreen_request(window, &self.common.event_loop_handle);
|
|
|
|
|
|
// don't switch focus because of a programmatic action.
|
|
|
|
|
|
// If the toplevel-management client intends to focus the now unfullscreened toplevel, it can send an `activate`-request.
|
2024-02-08 20:29:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn maximize(&mut self, _dh: &DisplayHandle, window: &<Self as ToplevelInfoHandler>::Window) {
|
2025-05-20 17:41:27 +02:00
|
|
|
|
let mut shell = self.common.shell.write();
|
2024-04-10 15:49:08 +02:00
|
|
|
|
if let Some(mapped) = shell.element_for_surface(window).cloned() {
|
|
|
|
|
|
let seat = shell.seats.last_active().clone();
|
2025-06-25 17:54:27 +02:00
|
|
|
|
shell.maximize_request(&mapped, &seat, true, &self.common.event_loop_handle);
|
2024-02-08 20:29:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn unmaximize(&mut self, _dh: &DisplayHandle, window: &<Self as ToplevelInfoHandler>::Window) {
|
2025-05-20 17:41:27 +02:00
|
|
|
|
let mut shell = self.common.shell.write();
|
2024-04-10 15:49:08 +02:00
|
|
|
|
if let Some(mapped) = shell.element_for_surface(window).cloned() {
|
|
|
|
|
|
shell.unmaximize_request(&mapped);
|
2024-02-08 20:29:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn minimize(&mut self, _dh: &DisplayHandle, window: &<Self as ToplevelInfoHandler>::Window) {
|
2025-05-20 17:41:27 +02:00
|
|
|
|
let mut shell = self.common.shell.write();
|
2025-06-25 17:54:27 +02:00
|
|
|
|
shell.minimize_request(window);
|
2024-02-08 20:29:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn unminimize(&mut self, _dh: &DisplayHandle, window: &<Self as ToplevelInfoHandler>::Window) {
|
2025-05-20 17:41:27 +02:00
|
|
|
|
let mut shell = self.common.shell.write();
|
2025-06-25 17:54:27 +02:00
|
|
|
|
let seat = shell.seats.last_active().clone();
|
|
|
|
|
|
shell.unminimize_request(window, &seat, &self.common.event_loop_handle);
|
2024-02-08 20:29:16 +01:00
|
|
|
|
}
|
2024-09-18 16:02:41 +02:00
|
|
|
|
|
|
|
|
|
|
fn set_sticky(&mut self, _dh: &DisplayHandle, window: &<Self as ToplevelInfoHandler>::Window) {
|
|
|
|
|
|
if window.is_sticky() {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-20 17:41:27 +02:00
|
|
|
|
let mut shell = self.common.shell.write();
|
2024-09-18 16:02:41 +02:00
|
|
|
|
if let Some(mapped) = shell.element_for_surface(window).cloned() {
|
|
|
|
|
|
let seat = shell.seats.last_active().clone();
|
|
|
|
|
|
shell.toggle_sticky(&seat, &mapped);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn unset_sticky(
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
_dh: &DisplayHandle,
|
|
|
|
|
|
window: &<Self as ToplevelInfoHandler>::Window,
|
|
|
|
|
|
) {
|
|
|
|
|
|
if !window.is_sticky() {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-20 17:41:27 +02:00
|
|
|
|
let mut shell = self.common.shell.write();
|
2024-09-18 16:02:41 +02:00
|
|
|
|
if let Some(mapped) = shell.element_for_surface(window).cloned() {
|
|
|
|
|
|
let seat = shell.seats.last_active().clone();
|
|
|
|
|
|
shell.toggle_sticky(&seat, &mapped);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-01-16 15:12:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ManagementWindow for CosmicSurface {
|
|
|
|
|
|
fn close(&self) {
|
|
|
|
|
|
CosmicSurface::close(self)
|
2022-07-18 21:26:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-10 15:49:08 +02:00
|
|
|
|
pub fn minimize_rectangle(output: &Output, window: &CosmicSurface) -> Rectangle<i32, Local> {
|
|
|
|
|
|
toplevel_rectangle_for(window)
|
|
|
|
|
|
.find_map(|(surface, relative)| {
|
|
|
|
|
|
let map = layer_map_for_output(output);
|
|
|
|
|
|
let layer = map.layer_for_surface(&surface, WindowSurfaceType::ALL);
|
|
|
|
|
|
layer.and_then(|s| map.layer_geometry(s)).map(|local| {
|
2024-12-26 18:18:35 -08:00
|
|
|
|
Rectangle::new(
|
2024-04-10 15:49:08 +02:00
|
|
|
|
Point::from((local.loc.x + relative.loc.x, local.loc.y + relative.loc.y)),
|
|
|
|
|
|
relative.size,
|
2024-02-23 17:25:40 +01:00
|
|
|
|
)
|
|
|
|
|
|
})
|
2024-04-10 15:49:08 +02:00
|
|
|
|
})
|
|
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
|
|
let output_size = output.geometry().size;
|
2024-12-26 18:18:35 -08:00
|
|
|
|
Rectangle::new(
|
2024-04-10 15:49:08 +02:00
|
|
|
|
Point::from((
|
|
|
|
|
|
(output_size.w / 2) - 100,
|
|
|
|
|
|
output_size.h - (output_size.h / 3) - 50,
|
|
|
|
|
|
)),
|
|
|
|
|
|
Size::from((200, 100)),
|
|
|
|
|
|
)
|
|
|
|
|
|
})
|
|
|
|
|
|
.as_local()
|
2024-02-23 17:25:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-18 21:26:02 +02:00
|
|
|
|
delegate_toplevel_management!(State);
|