diff options
author | 2024-06-14 01:47:39 +0200 | |
---|---|---|
committer | 2024-06-14 01:47:39 +0200 | |
commit | a25b1af45690bdd8e1cbb20ee3a5b1c4342de455 (patch) | |
tree | 432044cf682dd73d1019a2f964749e78db178865 /core/src | |
parent | e6d0b3bda5042a1017a5944a5227c97e0ed6caf9 (diff) | |
download | iced-a25b1af45690bdd8e1cbb20ee3a5b1c4342de455.tar.gz iced-a25b1af45690bdd8e1cbb20ee3a5b1c4342de455.tar.bz2 iced-a25b1af45690bdd8e1cbb20ee3a5b1c4342de455.zip |
Replace `Command` with a new `Task` API with chain support
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/element.rs | 61 | ||||
-rw-r--r-- | core/src/lib.rs | 2 | ||||
-rw-r--r-- | core/src/maybe.rs | 35 | ||||
-rw-r--r-- | core/src/overlay.rs | 2 | ||||
-rw-r--r-- | core/src/overlay/element.rs | 60 | ||||
-rw-r--r-- | core/src/overlay/group.rs | 2 | ||||
-rw-r--r-- | core/src/widget.rs | 2 | ||||
-rw-r--r-- | core/src/widget/operation.rs | 12 |
8 files changed, 53 insertions, 123 deletions
diff --git a/core/src/element.rs b/core/src/element.rs index 7d918a2e..385d8295 100644 --- a/core/src/element.rs +++ b/core/src/element.rs @@ -10,7 +10,6 @@ use crate::{ Widget, }; -use std::any::Any; use std::borrow::Borrow; /// A generic [`Widget`]. @@ -305,63 +304,9 @@ where tree: &mut Tree, layout: Layout<'_>, renderer: &Renderer, - operation: &mut dyn widget::Operation<B>, + operation: &mut dyn widget::Operation<()>, ) { - struct MapOperation<'a, B> { - operation: &'a mut dyn widget::Operation<B>, - } - - impl<'a, T, B> widget::Operation<T> for MapOperation<'a, B> { - fn container( - &mut self, - id: Option<&widget::Id>, - bounds: Rectangle, - operate_on_children: &mut dyn FnMut( - &mut dyn widget::Operation<T>, - ), - ) { - self.operation.container(id, bounds, &mut |operation| { - operate_on_children(&mut MapOperation { operation }); - }); - } - - fn focusable( - &mut self, - state: &mut dyn widget::operation::Focusable, - id: Option<&widget::Id>, - ) { - self.operation.focusable(state, id); - } - - fn scrollable( - &mut self, - state: &mut dyn widget::operation::Scrollable, - id: Option<&widget::Id>, - bounds: Rectangle, - translation: Vector, - ) { - self.operation.scrollable(state, id, bounds, translation); - } - - fn text_input( - &mut self, - state: &mut dyn widget::operation::TextInput, - id: Option<&widget::Id>, - ) { - self.operation.text_input(state, id); - } - - fn custom(&mut self, state: &mut dyn Any, id: Option<&widget::Id>) { - self.operation.custom(state, id); - } - } - - self.widget.operate( - tree, - layout, - renderer, - &mut MapOperation { operation }, - ); + self.widget.operate(tree, layout, renderer, operation); } fn on_event( @@ -495,7 +440,7 @@ where state: &mut Tree, layout: Layout<'_>, renderer: &Renderer, - operation: &mut dyn widget::Operation<Message>, + operation: &mut dyn widget::Operation<()>, ) { self.element .widget diff --git a/core/src/lib.rs b/core/src/lib.rs index 32156441..db67219c 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -35,6 +35,7 @@ mod color; mod content_fit; mod element; mod length; +mod maybe; mod padding; mod pixels; mod point; @@ -59,6 +60,7 @@ pub use font::Font; pub use gradient::Gradient; pub use layout::Layout; pub use length::Length; +pub use maybe::{MaybeSend, MaybeSync}; pub use overlay::Overlay; pub use padding::Padding; pub use pixels::Pixels; diff --git a/core/src/maybe.rs b/core/src/maybe.rs new file mode 100644 index 00000000..c6a507c1 --- /dev/null +++ b/core/src/maybe.rs @@ -0,0 +1,35 @@ +#[cfg(not(target_arch = "wasm32"))] +mod platform { + /// An extension trait that enforces `Send` only on native platforms. + /// + /// Useful for writing cross-platform async code! + pub trait MaybeSend: Send {} + + impl<T> MaybeSend for T where T: Send {} + + /// An extension trait that enforces `Sync` only on native platforms. + /// + /// Useful for writing cross-platform async code! + pub trait MaybeSync: Sync {} + + impl<T> MaybeSync for T where T: Sync {} +} + +#[cfg(target_arch = "wasm32")] +mod platform { + /// An extension trait that enforces `Send` only on native platforms. + /// + /// Useful for writing cross-platform async code! + pub trait MaybeSend {} + + impl<T> MaybeSend for T {} + + /// An extension trait that enforces `Sync` only on native platforms. + /// + /// Useful for writing cross-platform async code! + pub trait MaybeSync {} + + impl<T> MaybeSync for T {} +} + +pub use platform::{MaybeSend, MaybeSync}; diff --git a/core/src/overlay.rs b/core/src/overlay.rs index 3a57fe16..16f867da 100644 --- a/core/src/overlay.rs +++ b/core/src/overlay.rs @@ -41,7 +41,7 @@ where &mut self, _layout: Layout<'_>, _renderer: &Renderer, - _operation: &mut dyn widget::Operation<Message>, + _operation: &mut dyn widget::Operation<()>, ) { } diff --git a/core/src/overlay/element.rs b/core/src/overlay/element.rs index 695b88b3..61e75e8a 100644 --- a/core/src/overlay/element.rs +++ b/core/src/overlay/element.rs @@ -5,9 +5,7 @@ use crate::layout; use crate::mouse; use crate::renderer; use crate::widget; -use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size, Vector}; - -use std::any::Any; +use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size}; /// A generic [`Overlay`]. #[allow(missing_debug_implementations)] @@ -94,7 +92,7 @@ where &mut self, layout: Layout<'_>, renderer: &Renderer, - operation: &mut dyn widget::Operation<Message>, + operation: &mut dyn widget::Operation<()>, ) { self.overlay.operate(layout, renderer, operation); } @@ -146,59 +144,9 @@ where &mut self, layout: Layout<'_>, renderer: &Renderer, - operation: &mut dyn widget::Operation<B>, + operation: &mut dyn widget::Operation<()>, ) { - struct MapOperation<'a, B> { - operation: &'a mut dyn widget::Operation<B>, - } - - impl<'a, T, B> widget::Operation<T> for MapOperation<'a, B> { - fn container( - &mut self, - id: Option<&widget::Id>, - bounds: Rectangle, - operate_on_children: &mut dyn FnMut( - &mut dyn widget::Operation<T>, - ), - ) { - self.operation.container(id, bounds, &mut |operation| { - operate_on_children(&mut MapOperation { operation }); - }); - } - - fn focusable( - &mut self, - state: &mut dyn widget::operation::Focusable, - id: Option<&widget::Id>, - ) { - self.operation.focusable(state, id); - } - - fn scrollable( - &mut self, - state: &mut dyn widget::operation::Scrollable, - id: Option<&widget::Id>, - bounds: Rectangle, - translation: Vector, - ) { - self.operation.scrollable(state, id, bounds, translation); - } - - fn text_input( - &mut self, - state: &mut dyn widget::operation::TextInput, - id: Option<&widget::Id>, - ) { - self.operation.text_input(state, id); - } - - fn custom(&mut self, state: &mut dyn Any, id: Option<&widget::Id>) { - self.operation.custom(state, id); - } - } - - self.content - .operate(layout, renderer, &mut MapOperation { operation }); + self.content.operate(layout, renderer, operation); } fn on_event( diff --git a/core/src/overlay/group.rs b/core/src/overlay/group.rs index 7e4bebd0..cd12eac9 100644 --- a/core/src/overlay/group.rs +++ b/core/src/overlay/group.rs @@ -132,7 +132,7 @@ where &mut self, layout: Layout<'_>, renderer: &Renderer, - operation: &mut dyn widget::Operation<Message>, + operation: &mut dyn widget::Operation<()>, ) { operation.container(None, layout.bounds(), &mut |operation| { self.children.iter_mut().zip(layout.children()).for_each( diff --git a/core/src/widget.rs b/core/src/widget.rs index b02e3a4f..0d12deba 100644 --- a/core/src/widget.rs +++ b/core/src/widget.rs @@ -105,7 +105,7 @@ where _state: &mut Tree, _layout: Layout<'_>, _renderer: &Renderer, - _operation: &mut dyn Operation<Message>, + _operation: &mut dyn Operation<()>, ) { } diff --git a/core/src/widget/operation.rs b/core/src/widget/operation.rs index b91cf9ac..1fa924a4 100644 --- a/core/src/widget/operation.rs +++ b/core/src/widget/operation.rs @@ -8,15 +8,15 @@ pub use scrollable::Scrollable; pub use text_input::TextInput; use crate::widget::Id; -use crate::{Rectangle, Vector}; +use crate::{MaybeSend, Rectangle, Vector}; use std::any::Any; use std::fmt; -use std::rc::Rc; +use std::sync::Arc; /// A piece of logic that can traverse the widget tree of an application in /// order to query or update some widget state. -pub trait Operation<T> { +pub trait Operation<T>: MaybeSend { /// Operates on a widget that contains other widgets. /// /// The `operate_on_children` function can be called to return control to @@ -81,7 +81,7 @@ where /// Maps the output of an [`Operation`] using the given function. pub fn map<A, B>( operation: Box<dyn Operation<A>>, - f: impl Fn(A) -> B + 'static, + f: impl Fn(A) -> B + Send + Sync + 'static, ) -> impl Operation<B> where A: 'static, @@ -90,7 +90,7 @@ where #[allow(missing_debug_implementations)] struct Map<A, B> { operation: Box<dyn Operation<A>>, - f: Rc<dyn Fn(A) -> B>, + f: Arc<dyn Fn(A) -> B + Send + Sync>, } impl<A, B> Operation<B> for Map<A, B> @@ -197,7 +197,7 @@ where Map { operation, - f: Rc::new(f), + f: Arc::new(f), } } |