Skip to content

Commit

Permalink
WIP workspace drag source
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Feb 10, 2025
1 parent ef10fc2 commit 2dbf03d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 23 deletions.
41 changes: 39 additions & 2 deletions src/dnd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::backend::{ZcosmicToplevelHandleV1, ZcosmicWorkspaceHandleV1};

// Include `pid` in mime. Want to drag between our surfaces, but not another
// process, if we use Wayland object ids.
#[allow(dead_code)]
static WORKSPACE_MIME: LazyLock<String> =
LazyLock::new(|| format!("text/x.cosmic-workspace-id-{}", std::process::id()));

Expand All @@ -19,7 +18,6 @@ static TOPLEVEL_MIME: LazyLock<String> =

#[derive(Clone, Debug)]
pub enum DragSurface {
#[allow(dead_code)]
Workspace(ZcosmicWorkspaceHandleV1),
Toplevel(ZcosmicToplevelHandleV1),
}
Expand Down Expand Up @@ -59,11 +57,46 @@ impl TryFrom<(Vec<u8>, std::string::String)> for DragToplevel {
}
}

#[derive(Clone, Debug)]
pub struct DragWorkspace {}

impl AsMimeTypes for DragWorkspace {
fn available(&self) -> Cow<'static, [String]> {
vec![WORKSPACE_MIME.clone()].into()
}

fn as_bytes(&self, mime_type: &str) -> Option<Cow<'static, [u8]>> {
if mime_type == *WORKSPACE_MIME {
Some(Vec::new().into())
} else {
None
}
}
}

impl cosmic::iced::clipboard::mime::AllowedMimeTypes for DragWorkspace {
fn allowed() -> Cow<'static, [String]> {
vec![WORKSPACE_MIME.clone()].into()
}
}

impl TryFrom<(Vec<u8>, std::string::String)> for DragWorkspace {
type Error = ();
fn try_from((_bytes, mime_type): (Vec<u8>, String)) -> Result<Self, ()> {
if mime_type == *WORKSPACE_MIME {
Ok(Self {})
} else {
Err(())
}
}
}

#[derive(Clone, Debug, PartialEq)]
#[repr(u8)]
pub enum DropTarget {
WorkspaceSidebarEntry(ZcosmicWorkspaceHandleV1, wl_output::WlOutput),
OutputToplevels(ZcosmicWorkspaceHandleV1, wl_output::WlOutput),
WorkspacesBar(wl_output::WlOutput),
}

impl DropTarget {
Expand All @@ -81,6 +114,10 @@ impl DropTarget {
let id = output.id().protocol_id();
(u64::from(discriminant) << 32) | u64::from(id)
}
Self::WorkspacesBar(output) => {
let id = output.id().protocol_id();
(u64::from(discriminant) << 32) | u64::from(id)
}
}
}
}
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use backend::{ToplevelInfo, ZcosmicToplevelHandleV1, ZcosmicWorkspaceHandleV1};
mod dnd;
mod utils;
mod widgets;
use dnd::{DragSurface, DragToplevel, DropTarget};
use dnd::{DragSurface, DragToplevel, DragWorkspace, DropTarget};

#[derive(Clone, Debug, Default, PartialEq, CosmicConfigEntry)]
struct CosmicWorkspacesConfig {
Expand Down Expand Up @@ -98,6 +98,8 @@ enum Msg {
DndEnter(DropTarget, f64, f64, Vec<String>),
DndLeave(DropTarget),
DndToplevelDrop(DragToplevel),
DndWorkspaceDrag,
DndWorkspaceDrop(DragWorkspace),
SourceFinished,
#[allow(dead_code)]
NewWorkspace,
Expand All @@ -110,7 +112,7 @@ enum Msg {
Ignore,
}

#[derive(Debug)]
#[derive(Clone, Debug)]
struct Workspace {
name: String,
// img_for_output: HashMap<wl_output::WlOutput, backend::CaptureImage>,
Expand Down Expand Up @@ -521,10 +523,12 @@ impl Application for App {
output,
));
}
None => {}
Some(DropTarget::WorkspacesBar(_)) | None => {}
}
}
}
Msg::DndWorkspaceDrag => {}
Msg::DndWorkspaceDrop(_workspace) => {}
Msg::NewWorkspace => {
/*
if let WorkspaceAmount::Static(n) = &mut self.conf.workspace_config.workspace_amount
Expand Down
57 changes: 39 additions & 18 deletions src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::collections::HashMap;

use crate::{
backend::{self, CaptureImage},
dnd::{DragSurface, DragToplevel, DropTarget},
dnd::{DragSurface, DragToplevel, DragWorkspace, DropTarget},
App, LayerSurface, Msg, Toplevel, Workspace,
};

Expand All @@ -41,6 +41,24 @@ fn toplevel_dnd_destination<'a>(
.into()
}

fn workspace_dnd_destination<'a>(
target: DropTarget,
child: cosmic::Element<'a, Msg>,
) -> cosmic::Element<'a, Msg> {
let target2 = target.clone();
cosmic::widget::dnd_destination::dnd_destination_for_data(
child,
|data: Option<DragWorkspace>, _action| match data {
Some(workspace) => Msg::DndWorkspaceDrop(workspace),
None => Msg::Ignore,
},
)
.drag_id(target.drag_id())
.on_enter(move |actions, mime, pos| Msg::DndEnter(target.clone(), actions, mime, pos))
.on_leave(move || Msg::DndLeave(target2.clone()))
.into()
}

pub(crate) fn layer_surface<'a>(
app: &'a App,
surface: &'a LayerSurface,
Expand Down Expand Up @@ -138,11 +156,11 @@ fn workspace_item_appearance(
appearance
}

fn workspace_item<'a>(
workspace: &'a Workspace,
fn workspace_item(
workspace: &Workspace,
_output: &wl_output::WlOutput,
is_drop_target: bool,
) -> cosmic::Element<'a, Msg> {
) -> cosmic::Element<'static, Msg> {
let image = capture_image(workspace.img.as_ref(), 1.0);
let is_active = workspace.is_active;
// TODO editable name?
Expand Down Expand Up @@ -194,26 +212,29 @@ fn workspace_sidebar_entry<'a>(
iced::mouse::Interaction::Idle
};
*/
/* TODO allow moving workspaces (needs compositor support)
iced::widget::dnd_source(workspace_item(workspace, output))
.on_drag(|size| {
Msg::StartDrag(
size,
DragSurface::Workspace {
handle: workspace.handle.clone(),
output: output.clone(),
},
let workspace_clone = workspace.clone(); // TODO avoid clone
let output_clone = output.clone();
let source = cosmic::widget::dnd_source(workspace_item(workspace, output, is_drop_target))
.drag_threshold(5.)
.drag_content(|| DragWorkspace {})
.drag_icon(move |offset| {
(
workspace_item(&workspace_clone, &output_clone, false).map(|_| ()),
cosmic::iced_core::widget::tree::State::None,
-offset,
)
})
.on_finished(Msg::SourceFinished)
.on_cancelled(Msg::SourceFinished)
.into()
*/
.on_start(Some(Msg::StartDrag(DragSurface::Workspace(
workspace.handle.clone(),
))))
.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()),
workspace_item(workspace, output, is_drop_target),
source,
)
}

Expand Down

0 comments on commit 2dbf03d

Please sign in to comment.