WIP custom cursor for drag and drop

This commit is contained in:
Ian Douglas Scott 2023-12-15 15:46:41 -08:00
parent d56a591a9f
commit 06c37e39f5
3 changed files with 131 additions and 2 deletions

View file

@ -1,5 +1,8 @@
use cctk::{
cosmic_protocols::toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
cosmic_protocols::{
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
workspace::v1::client::zcosmic_workspace_handle_v1,
},
wayland_client::protocol::wl_output,
};
use cosmic::{
@ -19,6 +22,12 @@ pub(crate) fn layer_surface<'a>(
app: &'a App,
surface: &'a LayerSurface,
) -> cosmic::Element<'a, Msg> {
let mut drop_target = None;
if let Some((workspace, output)) = &app.drop_target {
if output == &surface.output {
drop_target = Some(workspace);
}
}
let layout = app.conf.workspace_config.workspace_layout;
let sidebar = workspaces_sidebar(
app.workspaces
@ -27,6 +36,7 @@ pub(crate) fn layer_surface<'a>(
&surface.output,
layout,
app.conf.workspace_config.workspace_amount,
drop_target,
);
let toplevels = toplevel_previews(
app.toplevels.iter().filter(|i| {
@ -83,7 +93,15 @@ pub(crate) fn workspace_item<'a>(
fn workspace_sidebar_entry<'a>(
workspace: &'a Workspace,
output: &'a wl_output::WlOutput,
is_drop_target: bool,
) -> cosmic::Element<'a, Msg> {
/* XXX
let mouse_interaction = if is_drop_target {
iced::mouse::Interaction::Crosshair
} else {
iced::mouse::Interaction::Idle
};
*/
/* TODO allow moving workspaces (needs compositor support)
iced::widget::dnd_source(workspace_item(workspace, output))
.on_drag(|size| {
@ -99,6 +117,8 @@ fn workspace_sidebar_entry<'a>(
.on_cancelled(Msg::SourceFinished)
.into()
*/
//crate::widgets::mouse_interaction_wrapper(
// mouse_interaction,
iced::widget::dnd_listener(workspace_item(workspace, output))
.on_enter(|actions, mime, pos| {
Msg::DndWorkspaceEnter(workspace.handle.clone(), output.clone(), actions, mime, pos)
@ -106,6 +126,7 @@ fn workspace_sidebar_entry<'a>(
.on_exit(Msg::DndWorkspaceLeave)
.on_drop(Msg::DndWorkspaceDrop)
.on_data(Msg::DndWorkspaceData)
//)
.into()
}
@ -114,9 +135,10 @@ fn workspaces_sidebar<'a>(
output: &'a wl_output::WlOutput,
layout: WorkspaceLayout,
amount: WorkspaceAmount,
drop_target: Option<&zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1>,
) -> cosmic::Element<'a, Msg> {
let sidebar_entries = workspaces
.map(|w| workspace_sidebar_entry(w, output))
.map(|w| workspace_sidebar_entry(w, output, drop_target == Some(&w.handle)))
.collect();
let axis = match layout {
WorkspaceLayout::Vertical => Axis::Vertical,

View file

@ -9,6 +9,8 @@ mod image_bg;
pub use image_bg::image_bg;
mod workspace_bar;
pub use workspace_bar::workspace_bar;
mod mouse_interaction_wrapper;
pub use mouse_interaction_wrapper::mouse_interaction_wrapper;
pub fn layout_wrapper<Msg, T: Widget<Msg, cosmic::Renderer>>(inner: T) -> LayoutWrapper<Msg, T> {
LayoutWrapper {

View file

@ -0,0 +1,105 @@
use cosmic::iced::{
self,
advanced::{
layout::{self, flex::Axis},
mouse, overlay, renderer,
widget::{tree, Operation, OperationOutputWrapper, Tree},
Clipboard, Layout, Shell, Widget,
},
event::{self, Event},
widget::image::{FilterMethod, Handle},
ContentFit, Length, Point, Rectangle, Size, Vector,
};
use cosmic::iced_core::Renderer as _;
use std::marker::PhantomData;
pub fn mouse_interaction_wrapper<'a, Msg, T: Into<cosmic::Element<'a, Msg>>>(
mouse_interaction: mouse::Interaction,
content: T,
) -> MouseInteractionWrapper<'a, Msg> {
MouseInteractionWrapper {
content: content.into(),
mouse_interaction,
_msg: PhantomData,
}
}
pub struct MouseInteractionWrapper<'a, Msg> {
content: cosmic::Element<'a, Msg>,
mouse_interaction: mouse::Interaction,
_msg: PhantomData<Msg>,
}
impl<'a, Msg> Widget<Msg, cosmic::Renderer> for MouseInteractionWrapper<'a, Msg> {
delegate::delegate! {
to self.content.as_widget() {
fn tag(&self) -> tree::Tag;
fn state(&self) -> tree::State;
fn children(&self) -> Vec<Tree>;
fn width(&self) -> Length;
fn height(&self) -> Length;
fn layout(
&self,
tree: &mut Tree,
renderer: &cosmic::Renderer,
limits: &layout::Limits,
) -> layout::Node;
fn operate(
&self,
tree: &mut Tree,
layout: Layout<'_>,
renderer: &cosmic::Renderer,
operation: &mut dyn Operation<OperationOutputWrapper<Msg>>,
);
fn draw(
&self,
state: &Tree,
renderer: &mut cosmic::Renderer,
theme: &cosmic::Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
);
}
to self.content.as_widget_mut() {
fn diff(&mut self, tree: &mut Tree);
fn on_event(
&mut self,
tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor: mouse::Cursor,
renderer: &cosmic::Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Msg>,
viewport: &Rectangle,
) -> event::Status;
fn overlay<'b>(
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &cosmic::Renderer,
) -> Option<overlay::Element<'b, Msg, cosmic::Renderer>>;
}
}
fn mouse_interaction(
&self,
_tree: &Tree,
_layout: Layout<'_>,
_cursor: mouse::Cursor,
_viewport: &Rectangle,
_renderer: &cosmic::Renderer,
) -> mouse::Interaction {
self.mouse_interaction
}
}
impl<'a, Msg: 'static> From<MouseInteractionWrapper<'a, Msg>> for cosmic::Element<'a, Msg> {
fn from(widget: MouseInteractionWrapper<'a, Msg>) -> Self {
cosmic::Element::new(widget)
}
}