Skip to content

Commit

Permalink
workspace-management: Implement move_to_workspace request
Browse files Browse the repository at this point in the history
Needed for `cosmic-workspaces`.
  • Loading branch information
ids1024 committed Dec 12, 2023
1 parent 75990ff commit 0a6bca8
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/shell/grabs/menu/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn move_prev_workspace(state: &mut State, mapped: &CosmicMapped) {
if let Some(prev_handle) = maybe_handle {
Shell::move_window(
state,
&seat,
Some(&seat),
mapped,
&current_handle,
&prev_handle,
Expand Down Expand Up @@ -93,7 +93,7 @@ fn move_next_workspace(state: &mut State, mapped: &CosmicMapped) {
if let Some(next_handle) = maybe_handle {
Shell::move_window(
state,
&seat,
Some(&seat),
mapped,
&current_handle,
&next_handle,
Expand Down
22 changes: 16 additions & 6 deletions src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ impl Shell {

pub fn move_window(
state: &mut State,
seat: &Seat<State>,
seat: Option<&Seat<State>>,
mapped: &CosmicMapped,
from: &WorkspaceHandle,
to: &WorkspaceHandle,
Expand Down Expand Up @@ -1759,7 +1759,9 @@ impl Shell {
state.common.shell.update_reactive_popups(&mapped);
}
let new_pos = if follow {
seat.set_active_output(&to_output);
if let Some(seat) = seat {
seat.set_active_output(&to_output);
}
state
.common
.shell
Expand All @@ -1776,13 +1778,13 @@ impl Shell {
.workspaces
.space_for_handle_mut(to)
.unwrap(); // checked above
let focus_stack = to_workspace.focus_stack.get(&seat);
let focus_stack = seat.map(|seat| to_workspace.focus_stack.get(&seat));
if window_state.layer == ManagedLayer::Floating {
to_workspace.floating_layer.map(mapped.clone(), None);
} else {
to_workspace.tiling_layer.map(
mapped.clone(),
Some(focus_stack.iter()),
focus_stack.as_ref().map(|x| x.iter()),
direction,
true,
);
Expand Down Expand Up @@ -1852,7 +1854,9 @@ impl Shell {
}

if follow {
Common::set_focus(state, Some(&focus_target), &seat, None);
if let Some(seat) = seat {
Common::set_focus(state, Some(&focus_target), &seat, None);
}
}

new_pos
Expand Down Expand Up @@ -1893,7 +1897,13 @@ impl Shell {
let from = from_workspace.handle;

Ok(Shell::move_window(
state, seat, &mapped, &from, &to, follow, direction,
state,
Some(seat),
&mapped,
&from,
&to,
follow,
direction,
))
}

Expand Down
34 changes: 32 additions & 2 deletions src/wayland/handlers/toplevel_management.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// SPDX-License-Identifier: GPL-3.0-only

use smithay::{input::Seat, reexports::wayland_server::DisplayHandle};
use cosmic_protocols::workspace::v1::server::zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1;
use smithay::{input::Seat, output::Output, reexports::wayland_server::DisplayHandle};

use crate::{
shell::CosmicSurface,
shell::{CosmicSurface, Shell},
utils::prelude::*,
wayland::protocols::{
toplevel_info::ToplevelInfoHandler,
Expand Down Expand Up @@ -59,6 +60,35 @@ impl ToplevelManagementHandler for State {
fn close(&mut self, _dh: &DisplayHandle, window: &<Self as ToplevelInfoHandler>::Window) {
window.close();
}

fn move_to_workspace(
&mut self,
_dh: &DisplayHandle,
window: &<Self as ToplevelInfoHandler>::Window,
workspace: ZcosmicWorkspaceHandleV1,
_output: Output,
) {
let Some(to_handle) = self.common.shell.workspace_state.get_workspace_handle(&workspace) else {
return;
};

let from_workspace = self
.common
.shell
.workspaces
.spaces()
.find(|w| w.windows().any(|w| &w == window));
if let Some(from_workspace) = from_workspace {
let mapped = from_workspace
.mapped()
.find(|m| m.windows().any(|(w, _)| &w == window))
.unwrap()
.clone();
let from_handle = from_workspace.handle;
let _ = Shell::move_window(self, None, &mapped, &from_handle, &to_handle, false, None);
return;
}
}
}

impl ManagementWindow for CosmicSurface {
Expand Down
27 changes: 24 additions & 3 deletions src/wayland/protocols/toplevel_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ use smithay::{
};

pub use cosmic_protocols::toplevel_management::v1::server::zcosmic_toplevel_manager_v1::ZcosmicToplelevelManagementCapabilitiesV1 as ManagementCapabilities;
use cosmic_protocols::toplevel_management::v1::server::zcosmic_toplevel_manager_v1::{
self, ZcosmicToplevelManagerV1,
use cosmic_protocols::{
toplevel_management::v1::server::zcosmic_toplevel_manager_v1::{
self, ZcosmicToplevelManagerV1,
},
workspace::v1::server::zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
};

use super::toplevel_info::{window_from_handle, ToplevelInfoHandler, ToplevelState, Window};
Expand Down Expand Up @@ -58,6 +61,14 @@ where
fn unmaximize(&mut self, dh: &DisplayHandle, window: &<Self as ToplevelInfoHandler>::Window) {}
fn minimize(&mut self, dh: &DisplayHandle, window: &<Self as ToplevelInfoHandler>::Window) {}
fn unminimize(&mut self, dh: &DisplayHandle, window: &<Self as ToplevelInfoHandler>::Window) {}
fn move_to_workspace(
&mut self,
dh: &DisplayHandle,
window: &<Self as ToplevelInfoHandler>::Window,
workspace: ZcosmicWorkspaceHandleV1,
output: Output,
) {
}
}

pub struct ToplevelManagerGlobalData {
Expand All @@ -79,7 +90,7 @@ impl ToplevelManagementState {
F: for<'a> Fn(&'a Client) -> bool + Send + Sync + 'static,
{
let global = dh.create_global::<D, ZcosmicToplevelManagerV1, _>(
1,
2,
ToplevelManagerGlobalData {
filter: Box::new(client_filter),
},
Expand Down Expand Up @@ -221,6 +232,16 @@ where
}
}
}
zcosmic_toplevel_manager_v1::Request::MoveToWorkspace {
toplevel,
workspace,
output,
} => {
let window = window_from_handle(toplevel).unwrap();
if let Some(output) = Output::from_resource(&output) {
state.move_to_workspace(dh, &window, workspace, output);
}
}
_ => unreachable!(),
}
}
Expand Down
44 changes: 16 additions & 28 deletions src/wayland/protocols/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,58 +307,36 @@ where
) {
match request {
zcosmic_workspace_handle_v1::Request::Activate => {
if let Some(id) = state
.workspace_state()
.groups
.iter()
.find_map(|g| g.workspaces.iter().find(|w| w.instances.contains(obj)))
.map(|w| w.id)
{
if let Some(workspace_handle) = state.workspace_state().get_workspace_handle(obj) {
let mut state = client
.get_data::<<D as WorkspaceHandler>::Client>()
.unwrap()
.workspace_state()
.lock()
.unwrap();
state
.requests
.push(Request::Activate(WorkspaceHandle { id }));
state.requests.push(Request::Activate(workspace_handle));
}
}
zcosmic_workspace_handle_v1::Request::Deactivate => {
if let Some(id) = state
.workspace_state()
.groups
.iter()
.find_map(|g| g.workspaces.iter().find(|w| w.instances.contains(obj)))
.map(|w| w.id)
{
if let Some(workspace_handle) = state.workspace_state().get_workspace_handle(obj) {
let mut state = client
.get_data::<<D as WorkspaceHandler>::Client>()
.unwrap()
.workspace_state()
.lock()
.unwrap();
state
.requests
.push(Request::Deactivate(WorkspaceHandle { id }));
state.requests.push(Request::Deactivate(workspace_handle));
}
}
zcosmic_workspace_handle_v1::Request::Remove => {
if let Some(id) = state
.workspace_state()
.groups
.iter()
.find_map(|g| g.workspaces.iter().find(|w| w.instances.contains(obj)))
.map(|w| w.id)
{
if let Some(workspace_handle) = state.workspace_state().get_workspace_handle(obj) {
let mut state = client
.get_data::<<D as WorkspaceHandler>::Client>()
.unwrap()
.workspace_state()
.lock()
.unwrap();
state.requests.push(Request::Remove(WorkspaceHandle { id }));
state.requests.push(Request::Remove(workspace_handle));
}
}
zcosmic_workspace_handle_v1::Request::Destroy => {
Expand Down Expand Up @@ -556,6 +534,16 @@ where
}
}

pub fn get_workspace_handle(
&self,
handle: &ZcosmicWorkspaceHandleV1,
) -> Option<WorkspaceHandle> {
self.groups
.iter()
.find_map(|g| g.workspaces.iter().find(|w| w.instances.contains(handle)))
.map(|w| WorkspaceHandle { id: w.id })
}

pub fn global_id(&self) -> GlobalId {
self.global.clone()
}
Expand Down

0 comments on commit 0a6bca8

Please sign in to comment.