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

Introduce custom method to widget::Operation trait #1649

Merged
merged 1 commit into from
Jan 14, 2023
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
5 changes: 5 additions & 0 deletions native/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
Clipboard, Color, Layout, Length, Point, Rectangle, Shell, Widget,
};

use std::any::Any;
use std::borrow::Borrow;

/// A generic [`Widget`].
Expand Down Expand Up @@ -333,6 +334,10 @@ where
) {
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(
Expand Down
6 changes: 6 additions & 0 deletions native/src/overlay/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::renderer;
use crate::widget;
use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size, Vector};

use std::any::Any;

/// A generic [`Overlay`].
#[allow(missing_debug_implementations)]
pub struct Element<'a, Message, Renderer> {
Expand Down Expand Up @@ -188,6 +190,10 @@ where
) {
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
Expand Down
9 changes: 9 additions & 0 deletions native/src/widget/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<B> {
match self.operation.finish() {
operation::Outcome::None => operation::Outcome::None,
Expand Down
4 changes: 4 additions & 0 deletions native/src/widget/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +34,9 @@ pub trait Operation<T> {
/// 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<T> {
Outcome::None
Expand Down