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

dnd_source: Add suppport for surface offset #780

Merged
merged 1 commit into from
Jan 13, 2025
Merged
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
17 changes: 11 additions & 6 deletions src/widget/dnd_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct DndSource<'a, Message, D> {
container: Element<'a, Message>,
window: Option<window::Id>,
drag_content: Option<Box<dyn Fn() -> D>>,
drag_icon: Option<Box<dyn Fn() -> (Element<'static, ()>, tree::State)>>,
drag_icon: Option<Box<dyn Fn(Vector) -> (Element<'static, ()>, tree::State, Vector)>>,
on_start: Option<Message>,
on_cancelled: Option<Message>,
on_finish: Option<Message>,
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<
#[must_use]
pub fn drag_icon(
mut self,
f: impl Fn() -> (Element<'static, ()>, tree::State) + 'static,
f: impl Fn(Vector) -> (Element<'static, ()>, tree::State, Vector) + 'static,
) -> Self {
self.drag_icon = Some(Box::new(f));
self
Expand All @@ -102,7 +102,7 @@ impl<
self
}

pub fn start_dnd(&self, clipboard: &mut dyn Clipboard, bounds: Rectangle) {
pub fn start_dnd(&self, clipboard: &mut dyn Clipboard, bounds: Rectangle, offset: Vector) {
let Some(content) = self.drag_content.as_ref().map(|f| f()) else {
return;
};
Expand All @@ -116,13 +116,14 @@ impl<
Some(iced_core::clipboard::DndSource::Widget(self.id.clone()))
},
self.drag_icon.as_ref().map(|f| {
let (icon, state) = f();
(
let (icon, state, offset) = f(offset);
iced_core::clipboard::IconSurface::new(
container(icon)
.width(Length::Fixed(bounds.width))
.height(Length::Fixed(bounds.height))
.into(),
state,
offset,
)
}),
Box::new(content),
Expand Down Expand Up @@ -262,7 +263,11 @@ impl<
if let Some(on_start) = self.on_start.as_ref() {
shell.publish(on_start.clone())
}
self.start_dnd(clipboard, state.cached_bounds);
let offset = Vector::new(
left_pressed_position.x - layout.bounds().x,
left_pressed_position.y - layout.bounds().y,
);
self.start_dnd(clipboard, state.cached_bounds, offset);
state.is_dragging = true;
state.left_pressed_position = None;
}
Expand Down
3 changes: 2 additions & 1 deletion src/widget/text_input/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,12 +1350,13 @@ where
clipboard,
false,
id.map(|id| iced_core::clipboard::DndSource::Widget(id)),
Some((
Some(iced_core::clipboard::IconSurface::new(
Element::from(
TextInput::<'static, ()>::new("", input_text.clone())
.dnd_icon(true),
),
iced_core::widget::tree::State::new(state_clone),
Vector::ZERO,
)),
Box::new(TextInputString(input_text)),
DndAction::Move,
Expand Down
Loading