Simplify component implementation in iced_lazy

This commit is contained in:
Héctor Ramón Jiménez 2021-12-13 17:46:39 +07:00
parent a92307890f
commit 48b2264bc6
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 141 additions and 221 deletions

View file

@ -71,53 +71,51 @@ struct Cache<'a, Message, Renderer: 'a, Event: 'a> {
overlay: Option<overlay::Element<'this, Event, Renderer>>, overlay: Option<overlay::Element<'this, Event, Renderer>>,
} }
impl<'a, Message, Renderer, Event> Instance<'a, Message, Renderer, Event> {
fn with_element<T>(
&self,
f: impl FnOnce(&Element<'_, Event, Renderer>) -> T,
) -> T {
self.with_element_mut(|element| f(element))
}
fn with_element_mut<T>(
&self,
f: impl FnOnce(&mut Element<'_, Event, Renderer>) -> T,
) -> T {
self.state
.borrow_mut()
.as_mut()
.unwrap()
.with_cache_mut(|cache| {
let mut element = cache.take().unwrap().into_heads().element;
let result = f(&mut element);
*cache = Some(
CacheBuilder {
element,
message: PhantomData,
overlay_builder: |_| None,
}
.build(),
);
result
})
}
}
impl<'a, Message, Renderer, Event> Widget<Message, Renderer> impl<'a, Message, Renderer, Event> Widget<Message, Renderer>
for Instance<'a, Message, Renderer, Event> for Instance<'a, Message, Renderer, Event>
where where
Renderer: iced_native::Renderer, Renderer: iced_native::Renderer,
{ {
fn width(&self) -> Length { fn width(&self) -> Length {
self.state self.with_element(|element| element.width())
.borrow_mut()
.as_mut()
.unwrap()
.with_cache_mut(|cache| {
let element = cache.take().unwrap().into_heads().element;
let width = element.width();
*cache = Some(
CacheBuilder {
element,
message: PhantomData,
overlay_builder: |_| None,
}
.build(),
);
width
})
} }
fn height(&self) -> Length { fn height(&self) -> Length {
self.state self.with_element(|element| element.height())
.borrow_mut()
.as_mut()
.unwrap()
.with_cache_mut(|cache| {
let element = cache.take().unwrap().into_heads().element;
let height = element.height();
*cache = Some(
CacheBuilder {
element,
message: PhantomData,
overlay_builder: |_| None,
}
.build(),
);
height
})
} }
fn layout( fn layout(
@ -125,25 +123,7 @@ where
renderer: &Renderer, renderer: &Renderer,
limits: &layout::Limits, limits: &layout::Limits,
) -> layout::Node { ) -> layout::Node {
self.state self.with_element(|element| element.layout(renderer, limits))
.borrow_mut()
.as_mut()
.unwrap()
.with_cache_mut(|cache| {
let element = cache.take().unwrap().into_heads().element;
let layout = element.layout(renderer, limits);
*cache = Some(
CacheBuilder {
element,
message: PhantomData,
overlay_builder: |_| None,
}
.build(),
);
layout
})
} }
fn on_event( fn on_event(
@ -158,37 +138,25 @@ where
let mut local_messages = Vec::new(); let mut local_messages = Vec::new();
let mut local_shell = Shell::new(&mut local_messages); let mut local_shell = Shell::new(&mut local_messages);
let event_status = self let event_status = self.with_element_mut(|element| {
.state element.on_event(
.borrow_mut() event,
.as_mut() layout,
.unwrap() cursor_position,
.with_cache_mut(|cache| { renderer,
let mut element = cache.take().unwrap().into_heads().element; clipboard,
let event_status = element.on_event( &mut local_shell,
event, )
layout, });
cursor_position,
renderer,
clipboard,
&mut local_shell,
);
*cache = Some(
CacheBuilder {
element,
message: PhantomData,
overlay_builder: |_| None,
}
.build(),
);
event_status
});
if !local_messages.is_empty() { if !local_messages.is_empty() {
let mut component = let mut component = self
self.state.take().unwrap().into_heads().component; .state
.borrow_mut()
.take()
.unwrap()
.into_heads()
.component;
for message in local_messages for message in local_messages
.into_iter() .into_iter()
@ -228,49 +196,15 @@ where
cursor_position: Point, cursor_position: Point,
viewport: &Rectangle, viewport: &Rectangle,
) { ) {
self.state self.with_element(|element| {
.borrow_mut() element.draw(renderer, style, layout, cursor_position, viewport);
.as_mut() });
.unwrap()
.with_cache_mut(|cache| {
let element = cache.take().unwrap().into_heads().element;
element.draw(
renderer,
style,
layout,
cursor_position,
viewport,
);
*cache = Some(
CacheBuilder {
element,
message: PhantomData,
overlay_builder: |_| None,
}
.build(),
);
})
} }
fn hash_layout(&self, state: &mut Hasher) { fn hash_layout(&self, state: &mut Hasher) {
self.state self.with_element(|element| {
.borrow_mut() element.hash_layout(state);
.as_mut() });
.unwrap()
.with_cache_mut(|cache| {
let element = cache.take().unwrap().into_heads().element;
element.hash_layout(state);
*cache = Some(
CacheBuilder {
element,
message: PhantomData,
overlay_builder: |_| None,
}
.build(),
);
})
} }
fn mouse_interaction( fn mouse_interaction(
@ -279,29 +213,9 @@ where
cursor_position: Point, cursor_position: Point,
viewport: &Rectangle, viewport: &Rectangle,
) -> mouse::Interaction { ) -> mouse::Interaction {
self.state self.with_element(|element| {
.borrow_mut() element.mouse_interaction(layout, cursor_position, viewport)
.as_mut() })
.unwrap()
.with_cache_mut(|cache| {
let element = cache.take().unwrap().into_heads().element;
let mouse_interaction = element.mouse_interaction(
layout,
cursor_position,
viewport,
);
*cache = Some(
CacheBuilder {
element,
message: PhantomData,
overlay_builder: |_| None,
}
.build(),
);
mouse_interaction
})
} }
fn overlay( fn overlay(
@ -328,23 +242,60 @@ where
cache.as_ref().unwrap().borrow_overlay().is_some() cache.as_ref().unwrap().borrow_overlay().is_some()
}); });
let Self { state, .. } = self;
has_overlay.then(|| { has_overlay.then(|| {
overlay::Element::new( overlay::Element::new(
layout.position(), layout.position(),
Box::new(Overlay { state }), Box::new(Overlay { instance: self }),
) )
}) })
} }
} }
struct Overlay<'a, 'b, Message, Event, Renderer> { struct Overlay<'a, 'b, Message, Renderer, Event> {
state: &'b RefCell<Option<State<'a, Message, Renderer, Event>>>, instance: &'b mut Instance<'a, Message, Renderer, Event>,
} }
impl<'a, 'b, Message, Event, Renderer> overlay::Overlay<Message, Renderer> impl<'a, 'b, Message, Renderer, Event>
for Overlay<'a, 'b, Message, Event, Renderer> Overlay<'a, 'b, Message, Renderer, Event>
{
fn with_overlay<T>(
&self,
f: impl FnOnce(&overlay::Element<'_, Event, Renderer>) -> T,
) -> T {
f(self
.instance
.state
.borrow()
.as_ref()
.unwrap()
.borrow_cache()
.as_ref()
.unwrap()
.borrow_overlay()
.as_ref()
.unwrap())
}
fn with_overlay_mut<T>(
&self,
f: impl FnOnce(&mut overlay::Element<'_, Event, Renderer>) -> T,
) -> T {
self.instance
.state
.borrow_mut()
.as_mut()
.unwrap()
.with_cache_mut(|cache| {
cache
.as_mut()
.unwrap()
.with_overlay_mut(|overlay| f(overlay.as_mut().unwrap()))
})
}
}
impl<'a, 'b, Message, Renderer, Event> overlay::Overlay<Message, Renderer>
for Overlay<'a, 'b, Message, Renderer, Event>
where where
Renderer: iced_native::Renderer, Renderer: iced_native::Renderer,
{ {
@ -354,22 +305,11 @@ where
bounds: Size, bounds: Size,
position: Point, position: Point,
) -> layout::Node { ) -> layout::Node {
self.state self.with_overlay(|overlay| {
.borrow_mut() let vector = position - overlay.position();
.as_mut()
.unwrap() overlay.layout(renderer, bounds).translate(vector)
.with_cache_mut(|cache| { })
cache.as_mut().unwrap().with_overlay_mut(|overlay| {
*overlay = overlay.take().map(|x| {
let vector = position - x.position();
x.translate(vector)
});
overlay
.as_mut()
.map(|overlay| overlay.layout(renderer, bounds))
.unwrap_or_else(|| layout::Node::new(Size::ZERO))
})
})
} }
fn draw( fn draw(
@ -379,12 +319,8 @@ where
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: Point,
) { ) {
self.state.borrow().as_ref().unwrap().with_cache(|cache| { self.with_overlay(|overlay| {
if let Some(overlay) = overlay.draw(renderer, style, layout, cursor_position);
cache.as_ref().unwrap().borrow_overlay().as_ref()
{
overlay.draw(renderer, style, layout, cursor_position);
}
}) })
} }
@ -394,16 +330,8 @@ where
cursor_position: Point, cursor_position: Point,
viewport: &Rectangle, viewport: &Rectangle,
) -> mouse::Interaction { ) -> mouse::Interaction {
self.state.borrow().as_ref().unwrap().with_cache(|cache| { self.with_overlay(|overlay| {
cache overlay.mouse_interaction(layout, cursor_position, viewport)
.as_ref()
.unwrap()
.borrow_overlay()
.as_ref()
.map(|overlay| {
overlay.mouse_interaction(layout, cursor_position, viewport)
})
.unwrap_or(mouse::Interaction::default())
}) })
} }
@ -414,13 +342,9 @@ where
(position.x as u32).hash(state); (position.x as u32).hash(state);
(position.y as u32).hash(state); (position.y as u32).hash(state);
self.state.borrow().as_ref().unwrap().with_cache(|cache| { self.with_overlay(|overlay| {
if let Some(overlay) = overlay.hash_layout(state);
cache.as_ref().unwrap().borrow_overlay().as_ref() });
{
overlay.hash_layout(state);
}
})
} }
fn on_event( fn on_event(
@ -435,32 +359,20 @@ where
let mut local_messages = Vec::new(); let mut local_messages = Vec::new();
let mut local_shell = Shell::new(&mut local_messages); let mut local_shell = Shell::new(&mut local_messages);
let event_status = self let event_status = self.with_overlay_mut(|overlay| {
.state overlay.on_event(
.borrow_mut() event,
.as_mut() layout,
.unwrap() cursor_position,
.with_cache_mut(|cache| { renderer,
cache.as_mut().unwrap().with_overlay_mut(|overlay| { clipboard,
overlay &mut local_shell,
.as_mut() )
.map(|overlay| { });
overlay.on_event(
event,
layout,
cursor_position,
renderer,
clipboard,
&mut local_shell,
)
})
.unwrap_or(iced_native::event::Status::Ignored)
})
});
if !local_messages.is_empty() { if !local_messages.is_empty() {
let mut component = let mut component =
self.state.take().unwrap().into_heads().component; self.instance.state.take().unwrap().into_heads().component;
for message in local_messages for message in local_messages
.into_iter() .into_iter()
@ -469,7 +381,7 @@ where
shell.publish(message); shell.publish(message);
} }
*self.state.borrow_mut() = Some( self.instance.state = RefCell::new(Some(
StateBuilder { StateBuilder {
component, component,
cache_builder: |state| { cache_builder: |state| {
@ -486,7 +398,7 @@ where
}, },
} }
.build(), .build(),
); ));
shell.invalidate_layout(); shell.invalidate_layout();
} }

View file

@ -1,4 +1,4 @@
use crate::{Alignment, Point, Rectangle, Size}; use crate::{Alignment, Point, Rectangle, Size, Vector};
/// The bounds of an element and its children. /// The bounds of an element and its children.
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
@ -80,4 +80,12 @@ impl Node {
self.bounds.x = position.x; self.bounds.x = position.x;
self.bounds.y = position.y; self.bounds.y = position.y;
} }
/// Translates the [`Node`] by the given translation.
pub fn translate(self, translation: Vector) -> Self {
Self {
bounds: self.bounds + translation,
..self
}
}
} }