Skip to content

Commit cbb3475

Browse files
committed
Implement Operation::finish for action::widget::Map
1 parent cfdfec9 commit cbb3475

1 file changed

Lines changed: 34 additions & 11 deletions

File tree

native/src/widget/action.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use crate::widget::Id;
33

44
use iced_futures::MaybeSend;
55

6+
use std::rc::Rc;
7+
68
/// An operation to be performed on the widget tree.
79
#[allow(missing_debug_implementations)]
810
pub struct Action<T>(Box<dyn Operation<T>>);
@@ -24,7 +26,7 @@ impl<T> Action<T> {
2426
{
2527
Action(Box::new(Map {
2628
operation: self.0,
27-
f: Box::new(f),
29+
f: Rc::new(f),
2830
}))
2931
}
3032

@@ -37,7 +39,7 @@ impl<T> Action<T> {
3739
#[allow(missing_debug_implementations)]
3840
struct Map<A, B> {
3941
operation: Box<dyn Operation<A>>,
40-
f: Box<dyn Fn(A) -> B>,
42+
f: Rc<dyn Fn(A) -> B>,
4143
}
4244

4345
impl<A, B> Operation<B> for Map<A, B>
@@ -50,38 +52,44 @@ where
5052
id: Option<&Id>,
5153
operate_on_children: &mut dyn FnMut(&mut dyn Operation<B>),
5254
) {
53-
struct MapRef<'a, A, B> {
55+
struct MapRef<'a, A> {
5456
operation: &'a mut dyn Operation<A>,
55-
f: &'a dyn Fn(A) -> B,
5657
}
5758

58-
impl<'a, A, B> Operation<B> for MapRef<'a, A, B> {
59+
impl<'a, A, B> Operation<B> for MapRef<'a, A> {
5960
fn container(
6061
&mut self,
6162
id: Option<&Id>,
6263
operate_on_children: &mut dyn FnMut(&mut dyn Operation<B>),
6364
) {
64-
let Self { operation, f } = self;
65+
let Self { operation, .. } = self;
6566

6667
operation.container(id, &mut |operation| {
67-
operate_on_children(&mut MapRef { operation, f });
68+
operate_on_children(&mut MapRef { operation });
6869
});
6970
}
7071

71-
fn scrollable(&mut self, state: &mut dyn Scrollable, id: Option<&Id>) {
72+
fn scrollable(
73+
&mut self,
74+
state: &mut dyn Scrollable,
75+
id: Option<&Id>,
76+
) {
7277
self.operation.scrollable(state, id);
7378
}
7479

75-
fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) {
80+
fn focusable(
81+
&mut self,
82+
state: &mut dyn Focusable,
83+
id: Option<&Id>,
84+
) {
7685
self.operation.focusable(state, id);
7786
}
7887
}
7988

80-
let Self { operation, f } = self;
89+
let Self { operation, .. } = self;
8190

8291
MapRef {
8392
operation: operation.as_mut(),
84-
f,
8593
}
8694
.container(id, operate_on_children);
8795
}
@@ -109,4 +117,19 @@ where
109117
) {
110118
self.operation.text_input(state, id);
111119
}
120+
121+
fn finish(&self) -> operation::Outcome<B> {
122+
match self.operation.finish() {
123+
operation::Outcome::None => operation::Outcome::None,
124+
operation::Outcome::Some(output) => {
125+
operation::Outcome::Some((self.f)(output))
126+
}
127+
operation::Outcome::Chain(next) => {
128+
operation::Outcome::Chain(Box::new(Map {
129+
operation: next,
130+
f: self.f.clone(),
131+
}))
132+
}
133+
}
134+
}
112135
}

0 commit comments

Comments
 (0)