Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP workspace pin and move requests #128

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ mock-backend = []
[profile.dev]
# Not usable at opt-level 0, at least with software renderer
opt-level = 1

[patch."https://github.com/pop-os/cosmic-protocols"]
cosmic-protocols = { git = "https://github.com/pop-os/cosmic-protocols//", branch = "workspace-pin-move" }
cosmic-client-toolkit = { git = "https://github.com/pop-os/cosmic-protocols//", branch = "workspace-pin-move" }
3 changes: 3 additions & 0 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,8 @@ pub enum Cmd {
ZcosmicWorkspaceHandleV1,
wl_output::WlOutput,
),
MoveWorkspaceBefore(ZcosmicWorkspaceHandleV1, ZcosmicWorkspaceHandleV1),
MoveWorkspaceAfter(ZcosmicWorkspaceHandleV1, ZcosmicWorkspaceHandleV1),
ActivateWorkspace(ZcosmicWorkspaceHandleV1),
SetWorkspacePinned(ZcosmicWorkspaceHandleV1, bool),
}
26 changes: 26 additions & 0 deletions src/backend/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use calloop_wayland_source::WaylandSource;
use cctk::{
cosmic_protocols::workspace::v1::client::zcosmic_workspace_handle_v1,
screencopy::{CaptureSource, ScreencopyState},
sctk::{
self,
Expand Down Expand Up @@ -102,12 +103,37 @@ impl AppData {
}
}
}
Cmd::MoveWorkspaceBefore(workspace_handle, other_workspace_handle) => {
if let Ok(workspace_manager) = self.workspace_state.workspace_manager().get() {
workspace_handle.move_before(&other_workspace_handle, 0);
workspace_manager.commit();
}
}
Cmd::MoveWorkspaceAfter(workspace_handle, other_workspace_handle) => {
if let Ok(workspace_manager) = self.workspace_state.workspace_manager().get() {
workspace_handle.move_after(&other_workspace_handle, 0);
workspace_manager.commit();
}
}
Cmd::ActivateWorkspace(workspace_handle) => {
if let Ok(workspace_manager) = self.workspace_state.workspace_manager().get() {
workspace_handle.activate();
workspace_manager.commit();
}
}
Cmd::SetWorkspacePinned(workspace_handle, pinned) => {
if let Ok(workspace_manager) = self.workspace_state.workspace_manager().get() {
if workspace_handle.version() >= zcosmic_workspace_handle_v1::REQ_PIN_SINCE {
// TODO check capability
if pinned {
workspace_handle.pin();
} else {
workspace_handle.unpin();
}
workspace_manager.commit();
}
}
}
}
}

Expand Down
57 changes: 55 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ enum Msg {
BgConfig(cosmic_bg_config::state::State),
UpdateToplevelIcon(String, Option<PathBuf>),
OnScroll(wl_output::WlOutput, ScrollDelta),
TogglePinned(ZcosmicWorkspaceHandleV1),
Ignore,
}

Expand All @@ -120,7 +121,9 @@ struct Workspace {
img: Option<backend::CaptureImage>,
handle: ZcosmicWorkspaceHandleV1,
outputs: HashSet<wl_output::WlOutput>,
coordinates: Vec<u32>,
is_active: bool,
is_pinned: bool,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -377,13 +380,17 @@ impl Application for App {
backend::Event::CmdSender(sender) => {
self.wayland_cmd_sender = Some(sender);
}
backend::Event::Workspaces(workspaces) => {
backend::Event::Workspaces(mut workspaces) => {
workspaces.sort_by(|(_, w1), (_, w2)| w1.coordinates.cmp(&w2.coordinates));
let old_workspaces = mem::take(&mut self.workspaces);
self.workspaces = Vec::new();
for (outputs, workspace) in workspaces {
let is_active = workspace.state.contains(&WEnum::Value(
zcosmic_workspace_handle_v1::State::Active,
));
let is_pinned = workspace.state.contains(&WEnum::Value(
zcosmic_workspace_handle_v1::State::Pinned,
));

// XXX efficiency
#[allow(clippy::mutable_key_type)]
Expand All @@ -397,8 +404,10 @@ impl Application for App {
name: workspace.name,
handle: workspace.handle,
outputs,
coordinates: workspace.coordinates.clone(),
img,
is_active,
is_pinned,
});
}
self.update_capture_filter();
Expand Down Expand Up @@ -615,7 +624,51 @@ impl Application for App {
}
}
Msg::DndWorkspaceDrag => {}
Msg::DndWorkspaceDrop(_workspace) => {}
Msg::DndWorkspaceDrop(_workspace) => {
if let Some((DragSurface::Workspace(handle), _)) = &self.drag_surface {
match self.drop_target.take() {
Some(DropTarget::WorkspaceSidebarEntry(other_handle, _output)) => {
let workspace = self.workspaces.iter().find(|i| i.handle == *handle);
let other_workspace =
self.workspaces.iter().find(|i| i.handle == other_handle);
if let (Some(workspace), Some(other_workspace)) =
(workspace, other_workspace)
{
self.send_wayland_cmd(
if workspace.outputs == other_workspace.outputs
&& workspace.coordinates[0] + 1
== other_workspace.coordinates[0]
{
backend::Cmd::MoveWorkspaceAfter(
handle.clone(),
other_handle,
)
} else {
backend::Cmd::MoveWorkspaceBefore(
handle.clone(),
other_handle,
)
},
);
}
}
Some(DropTarget::OutputToplevels(_, _) | DropTarget::WorkspacesBar(_))
| None => {}
}
}
}
Msg::TogglePinned(workspace_handle) => {
if let Some(workspace) = self
.workspaces
.iter()
.find(|w| w.handle == workspace_handle)
{
self.send_wayland_cmd(backend::Cmd::SetWorkspacePinned(
workspace_handle,
!workspace.is_pinned,
));
}
}
Msg::Ignore => {}
}

Expand Down
69 changes: 57 additions & 12 deletions src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,20 @@ fn workspace_item<'a>(
widget::button::custom(
column![
image,
widget::text::body(fl!(
"workspace",
HashMap::from([("number", &workspace.name)])
))
row![
widget::text::body(fl!(
"workspace",
HashMap::from([("number", &workspace.name)])
)),
widget::horizontal_space(),
// TODO in Adwaita, but not pop?
widget::button::custom(widget::icon::from_name("pin-symbolic").size(16))
//.class(cosmic::theme::Button::Icon)
.class(cosmic::theme::Button::Image)
// TODO style selected correctly
.selected(workspace.is_pinned)
.on_press(Msg::TogglePinned(workspace.handle.clone()))
]
]
.align_x(iced::Alignment::Center)
.spacing(4),
Expand All @@ -191,6 +201,23 @@ fn workspace_item<'a>(
.into()
}

fn workspace_drag_placeholder(
other_workspace: &Workspace,
other_output: &wl_output::WlOutput,
) -> cosmic::Element<'static, Msg> {
let (width, height) = other_workspace.img.as_ref().map(|img| (img.width, img.height)).unwrap_or((1, 1));
let drop_target = DropTarget::WorkspaceSidebarEntry(other_workspace.handle.clone(), other_output.clone());
// XXX
let data = [0, 0, 0, 255].repeat(width as usize * height as usize);
/*
let placeholder =
widget::Image::new(widget::image::Handle::from_rgba(width, height, data)).into();
*/
// .height(iced::Length::Fill)
let placeholder = workspace_item(other_workspace, other_output, true).into(); // XXX
workspace_dnd_destination(drop_target, placeholder)
}

fn workspace_sidebar_entry<'a>(
workspace: &'a Workspace,
output: &'a wl_output::WlOutput,
Expand All @@ -204,7 +231,6 @@ fn workspace_sidebar_entry<'a>(
};
*/
let item = workspace_item(workspace, output, is_drop_target);
/* TODO allow moving workspaces (needs compositor support)
let workspace_clone = workspace.clone(); // TODO avoid clone
let output_clone = output.clone();
let source = cosmic::widget::dnd_source(item)
Expand All @@ -223,13 +249,23 @@ fn workspace_sidebar_entry<'a>(
.on_finish(Some(Msg::SourceFinished))
.on_cancel(Some(Msg::SourceFinished))
.into();
*/
//crate::widgets::mouse_interaction_wrapper(
// mouse_interaction,
toplevel_dnd_destination(
DropTarget::WorkspaceSidebarEntry(workspace.handle.clone(), output.clone()),
item,
)
let drop_target = DropTarget::WorkspaceSidebarEntry(workspace.handle.clone(), output.clone());
let destination = toplevel_dnd_destination(drop_target.clone(), source);
// TODO test if workspace is being dragged
if is_drop_target {
/*
println!("foo");
// XXX
let item2 = workspace_item(workspace, output, is_drop_target);
column![
workspace_dnd_destination(drop_target, item2),
destination,
].into()
*/
destination
} else {
workspace_dnd_destination(drop_target, destination)
}
}

fn workspaces_sidebar<'a>(
Expand All @@ -238,9 +274,18 @@ fn workspaces_sidebar<'a>(
layout: WorkspaceLayout,
drop_target: Option<&backend::ZcosmicWorkspaceHandleV1>,
) -> cosmic::Element<'a, Msg> {
let mut sidebar_entries = Vec::new();
for w in workspaces {
if drop_target == Some(&w.handle) {
sidebar_entries.push(workspace_drag_placeholder(w, output));
}
sidebar_entries.push(workspace_sidebar_entry(w, output, drop_target == Some(&w.handle)));
}
/*
let sidebar_entries = workspaces
.map(|w| workspace_sidebar_entry(w, output, drop_target == Some(&w.handle)))
.collect();
*/
let axis = match layout {
WorkspaceLayout::Vertical => Axis::Vertical,
WorkspaceLayout::Horizontal => Axis::Horizontal,
Expand Down