From daa3f3324d001b634f73779423da605c213e7626 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Jan 2023 03:20:30 +0100 Subject: Introduce `custom` method to `widget::Operation` trait This allows users to write operations for their custom widgets. --- native/src/widget/action.rs | 9 +++++++++ native/src/widget/operation.rs | 4 ++++ 2 files changed, 13 insertions(+) (limited to 'native/src/widget') diff --git a/native/src/widget/action.rs b/native/src/widget/action.rs index 9aa79dec..1e21ff38 100644 --- a/native/src/widget/action.rs +++ b/native/src/widget/action.rs @@ -3,6 +3,7 @@ use crate::widget::Id; use iced_futures::MaybeSend; +use std::any::Any; use std::rc::Rc; /// An operation to be performed on the widget tree. @@ -84,6 +85,10 @@ where ) { self.operation.focusable(state, id); } + + fn custom(&mut self, state: &mut dyn Any, id: Option<&Id>) { + self.operation.custom(state, id); + } } let Self { operation, .. } = self; @@ -118,6 +123,10 @@ where self.operation.text_input(state, id); } + fn custom(&mut self, state: &mut dyn Any, id: Option<&Id>) { + self.operation.custom(state, id); + } + fn finish(&self) -> operation::Outcome { match self.operation.finish() { operation::Outcome::None => operation::Outcome::None, diff --git a/native/src/widget/operation.rs b/native/src/widget/operation.rs index a0aa4117..73e6a6b0 100644 --- a/native/src/widget/operation.rs +++ b/native/src/widget/operation.rs @@ -9,6 +9,7 @@ pub use text_input::TextInput; use crate::widget::Id; +use std::any::Any; use std::fmt; /// A piece of logic that can traverse the widget tree of an application in @@ -33,6 +34,9 @@ pub trait Operation { /// Operates on a widget that has text input. fn text_input(&mut self, _state: &mut dyn TextInput, _id: Option<&Id>) {} + /// Operates on a custom widget with some state. + fn custom(&mut self, _state: &mut dyn Any, _id: Option<&Id>) {} + /// Finishes the [`Operation`] and returns its [`Outcome`]. fn finish(&self) -> Outcome { Outcome::None -- cgit