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

feat: update advertised drag destinations after rebuilding an interface #120

Merged
merged 3 commits into from
Apr 1, 2024
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
48 changes: 47 additions & 1 deletion core/src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Access the clipboard.

use std::{any::Any, borrow::Cow, sync::Arc};
use std::{any::Any, sync::Arc};

use dnd::{DndAction, DndDestinationRectangle, DndSurface};
use mime::{self, AllowedMimeTypes, AsMimeTypes, ClipboardStoreData};

Check failure on line 6 in core/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all (ubuntu-latest, beta)

the item `mime` is imported redundantly

Check failure on line 6 in core/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all (ubuntu-latest, beta)

the item `mime` is imported redundantly

Check failure on line 6 in core/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all (macOS-latest, beta)

the item `mime` is imported redundantly

use crate::{widget::tree::State, window, Element};

Expand Down Expand Up @@ -148,7 +148,7 @@
clipboard: &mut dyn Clipboard,
mime: Option<String>,
) -> Option<T> {
let Some(mime) = mime.or_else(|| T::allowed().first().cloned().into())

Check warning on line 151 in core/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all

this `let...else` may be rewritten with the `?` operator

Check warning on line 151 in core/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all

useless conversion to the same type: `std::option::Option<std::string::String>`

Check warning on line 151 in core/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all

this `let...else` may be rewritten with the `?` operator

Check warning on line 151 in core/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all

useless conversion to the same type: `std::option::Option<std::string::String>`
else {
return None;
};
Expand All @@ -165,3 +165,49 @@
/// A surface is the source of the DnD operation.
Surface(window::Id),
}

/// A list of DnD destination rectangles.
#[derive(Debug, Clone)]
pub struct DndDestinationRectangles {
/// The rectangle of the DnD destination.
rectangles: Vec<DndDestinationRectangle>,
}

impl DndDestinationRectangles {
/// Creates a new [`DndDestinationRectangles`].
pub fn new() -> Self {

Check warning on line 178 in core/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all

you should consider adding a `Default` implementation for `DndDestinationRectangles`

Check warning on line 178 in core/src/clipboard.rs

View workflow job for this annotation

GitHub Actions / all

you should consider adding a `Default` implementation for `DndDestinationRectangles`
Self {
rectangles: Vec::new(),
}
}

/// Creates a new [`DndDestinationRectangles`] with the given capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self {
rectangles: Vec::with_capacity(capacity),
}
}

/// Pushes a new rectangle to the list of DnD destination rectangles.
pub fn push(&mut self, rectangle: DndDestinationRectangle) {
self.rectangles.push(rectangle);
}

/// Appends the list of DnD destination rectangles to the current list.
pub fn append(&mut self, other: &mut Vec<DndDestinationRectangle>) {
self.rectangles.append(other);
}

/// Returns the list of DnD destination rectangles.
/// This consumes the [`DndDestinationRectangles`].
pub fn into_rectangles(mut self) -> Vec<DndDestinationRectangle> {
self.rectangles.reverse();
self.rectangles
}
}

impl AsRef<[DndDestinationRectangle]> for DndDestinationRectangles {
fn as_ref(&self) -> &[DndDestinationRectangle] {
&self.rectangles
}
}
10 changes: 10 additions & 0 deletions core/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,14 @@ where
/// Sets the id of the widget
/// This may be called while diffing the widget tree
fn set_id(&mut self, _id: Id) {}

/// Adds the drag destination rectangles of the widget.
/// Runs after the layout phase for each widget in the tree.
fn drag_destinations(
&self,
_state: &Tree,
_layout: Layout<'_>,
_dnd_rectangles: &mut crate::clipboard::DndDestinationRectangles,
) {
}
}
15 changes: 15 additions & 0 deletions runtime/src/user_interface.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Implement your own event loop to drive a user interface.
use iced_core::clipboard::DndDestinationRectangles;
use iced_core::widget::{Operation, OperationOutputWrapper};

use crate::core::event::{self, Event};
Expand Down Expand Up @@ -625,6 +626,20 @@ where
pub fn find(&self, id: &widget::Id) -> Option<&widget::Tree> {
self.state.find(id)
}

/// Get the destination rectangles for the user interface.
pub fn dnd_rectangles(
&self,
prev_capacity: usize,
) -> DndDestinationRectangles {
let ret = DndDestinationRectangles::with_capacity(prev_capacity);
self.root.as_widget().drag_destinations(
&self.state,
Layout::new(&self.base),
&mut ret.clone(),
);
ret
}
}

/// Reusable data of a specific [`UserInterface`].
Expand Down
17 changes: 17 additions & 0 deletions widget/src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,23 @@ where
}),
)
}

fn drag_destinations(
&self,
state: &Tree,
layout: Layout<'_>,
dnd_rectangles: &mut iced_style::core::clipboard::DndDestinationRectangles,
) {
for ((e, layout), state) in self
.children
.iter()
.zip(layout.children())
.zip(state.children.iter())
{
e.as_widget()
.drag_destinations(state, layout, dnd_rectangles);
}
}
}

impl<'a, Message, Theme, Renderer> From<Column<'a, Message, Theme, Renderer>>
Expand Down
13 changes: 13 additions & 0 deletions widget/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ where
cursor,
)
}

fn drag_destinations(
&self,
state: &Tree,
layout: Layout<'_>,
dnd_rectangles: &mut iced_style::core::clipboard::DndDestinationRectangles,
) {
self.content.as_widget().drag_destinations(
state,
layout,
dnd_rectangles,
);
}
}

impl<'a, Message, Theme, Renderer> From<Container<'a, Message, Theme, Renderer>>
Expand Down
15 changes: 15 additions & 0 deletions widget/src/mouse_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,21 @@ where
renderer,
)
}

fn drag_destinations(
&self,
state: &Tree,
layout: Layout<'_>,
dnd_rectangles: &mut iced_style::core::clipboard::DndDestinationRectangles,
) {
if let Some(state) = state.children.iter().next() {
self.content.as_widget().drag_destinations(
state,
layout,
dnd_rectangles,
);
}
}
}

impl<'a, Message, Theme, Renderer> From<MouseArea<'a, Message, Theme, Renderer>>
Expand Down
17 changes: 17 additions & 0 deletions widget/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ where
}),
)
}

fn drag_destinations(
&self,
state: &Tree,
layout: Layout<'_>,
dnd_rectangles: &mut iced_style::core::clipboard::DndDestinationRectangles,
) {
for ((e, layout), state) in self
.children
.iter()
.zip(layout.children())
.zip(state.children.iter())
{
e.as_widget()
.drag_destinations(state, layout, dnd_rectangles);
}
}
}

impl<'a, Message, Theme, Renderer> From<Row<'a, Message, Theme, Renderer>>
Expand Down
31 changes: 31 additions & 0 deletions widget/src/scrollable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Navigate an endless amount of content with a scrollbar.
use iced_runtime::core::widget::Id;
use iced_style::core::clipboard::DndDestinationRectangles;
#[cfg(feature = "a11y")]
use std::borrow::Cow;

Expand Down Expand Up @@ -588,6 +589,36 @@ where
}
}
}

fn drag_destinations(
&self,
state: &Tree,
layout: Layout<'_>,
dnd_rectangles: &mut iced_style::core::clipboard::DndDestinationRectangles,
) {
if let Some((c_layout, c_state)) =
layout.children().zip(state.children.iter()).next()
{
let mut my_dnd_rectangles = DndDestinationRectangles::new();
self.content.as_widget().drag_destinations(
c_state,
layout,
&mut my_dnd_rectangles,
);
let mut my_dnd_rectangles = my_dnd_rectangles.into_rectangles();

let bounds = layout.bounds();
let content_bounds = c_layout.bounds();
let state = state.state.downcast_ref::<State>();
for r in &mut my_dnd_rectangles {
let translation =
state.translation(self.direction, bounds, content_bounds);
r.rectangle.x -= translation.x as f64;
r.rectangle.y -= translation.y as f64;
}
dnd_rectangles.append(&mut my_dnd_rectangles);
}
}
}

impl<'a, Message, Theme, Renderer>
Expand Down
19 changes: 18 additions & 1 deletion winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::conversion;
use crate::core;
use crate::core::mouse;
use crate::core::renderer;
use crate::core::renderer::Renderer;
use crate::core::time::Instant;
use crate::core::widget::operation;
use crate::core::window;
Expand Down Expand Up @@ -411,6 +410,8 @@ async fn run_instance<A, E, C>(
&mut debug,
));

let mut prev_dnd_rectangles_count = 0;

// Creates closure for handling the window drag resize state with winit.
let mut drag_resize_window_func = drag_resize::event_func(
&window,
Expand Down Expand Up @@ -893,6 +894,22 @@ async fn run_instance<A, E, C>(
&mut debug,
));

let dnd_rectangles = user_interface
.dnd_rectangles(prev_dnd_rectangles_count);
let new_dnd_rectangles_count =
dnd_rectangles.as_ref().len();

if new_dnd_rectangles_count > 0
|| prev_dnd_rectangles_count > 0
{
clipboard.register_dnd_destination(
DndSurface(Arc::new(Box::new(window.clone()))),
dnd_rectangles.into_rectangles(),
);
}

prev_dnd_rectangles_count = new_dnd_rectangles_count;

if should_exit {
break;
}
Expand Down
42 changes: 29 additions & 13 deletions winit/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::futures::futures::channel::mpsc;
use crate::futures::futures::{task, Future, StreamExt};
use crate::futures::{Executor, Runtime, Subscription};
use crate::graphics::{compositor, Compositor};
use crate::multi_window::operation::focusable::focus;
use crate::multi_window::operation::OperationWrapper;
use crate::multi_window::window_manager::WindowManager;
use crate::runtime::command::{self, Command};
Expand All @@ -32,7 +31,6 @@ use dnd::Icon;
use iced_graphics::Viewport;
use iced_runtime::futures::futures::FutureExt;
use iced_style::core::Length;
use iced_style::Theme;
pub use state::State;
use window_clipboard::mime::ClipboardStoreData;
use winit::raw_window_handle::HasWindowHandle;
Expand Down Expand Up @@ -429,6 +427,7 @@ async fn run_instance<A, E, C>(
window::Id::MAIN,
user_interface::Cache::default(),
)]),
&mut clipboard,
));

run_command(
Expand Down Expand Up @@ -861,6 +860,7 @@ async fn run_instance<A, E, C>(
&mut debug,
&mut window_manager,
cached_interfaces,
&mut clipboard,
));
}

Expand Down Expand Up @@ -1604,6 +1604,7 @@ fn run_command<A, C, E>(
debug,
window_manager,
std::mem::take(ui_caches),
clipboard,
);

while let Some(mut operation) = current_operation.take() {
Expand Down Expand Up @@ -1702,6 +1703,7 @@ pub fn build_user_interfaces<'a, A: Application, C: Compositor>(
debug: &mut Debug,
window_manager: &mut WindowManager<A, C>,
mut cached_user_interfaces: HashMap<window::Id, user_interface::Cache>,
clipboard: &mut Clipboard<A::Message>,
) -> HashMap<window::Id, UserInterface<'a, A::Message, A::Theme, A::Renderer>>
where
A::Theme: StyleSheet,
Expand All @@ -1711,18 +1713,32 @@ where
.drain()
.filter_map(|(id, cache)| {
let window = window_manager.get_mut(id)?;

Some((
let interface = build_user_interface(
application,
cache,
&mut window.renderer,
window.state.logical_size(),
debug,
id,
build_user_interface(
application,
cache,
&mut window.renderer,
window.state.logical_size(),
debug,
id,
),
))
);

let dnd_rectangles = interface
.dnd_rectangles(window.prev_dnd_destination_rectangles_count);
let new_dnd_rectangles_count = dnd_rectangles.as_ref().len();

if new_dnd_rectangles_count > 0
|| window.prev_dnd_destination_rectangles_count > 0
{
clipboard.register_dnd_destination(
DndSurface(Arc::new(Box::new(window.raw.clone()))),
dnd_rectangles.into_rectangles(),
);
}

window.prev_dnd_destination_rectangles_count =
new_dnd_rectangles_count;

Some((id, interface))
})
.collect()
}
Expand Down
2 changes: 2 additions & 0 deletions winit/src/multi_window/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ where
surface,
renderer,
mouse_interaction: mouse::Interaction::Idle,
prev_dnd_destination_rectangles_count: 0,
},
);

Expand Down Expand Up @@ -142,6 +143,7 @@ where
) -> bool,
>,
>,
pub prev_dnd_destination_rectangles_count: usize,
pub mouse_interaction: mouse::Interaction,
pub surface: C::Surface,
pub renderer: A::Renderer,
Expand Down
Loading