Add on_resize handler to pop widget 🎉
This commit is contained in:
parent
e14a6f76e9
commit
e78d7f0e51
2 changed files with 36 additions and 9 deletions
|
|
@ -206,7 +206,7 @@ fn card<'a>(
|
||||||
.into()
|
.into()
|
||||||
} else {
|
} else {
|
||||||
pop(card)
|
pop(card)
|
||||||
.on_show(Message::ImagePoppedIn(metadata.id))
|
.on_show(|_| Message::ImagePoppedIn(metadata.id))
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,8 @@ use crate::core::{
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Pop<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer> {
|
pub struct Pop<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer> {
|
||||||
content: Element<'a, Message, Theme, Renderer>,
|
content: Element<'a, Message, Theme, Renderer>,
|
||||||
on_show: Option<Message>,
|
on_show: Option<Box<dyn Fn(Size) -> Message + 'a>>,
|
||||||
|
on_resize: Option<Box<dyn Fn(Size) -> Message + 'a>>,
|
||||||
on_hide: Option<Message>,
|
on_hide: Option<Message>,
|
||||||
anticipate: Pixels,
|
anticipate: Pixels,
|
||||||
}
|
}
|
||||||
|
|
@ -34,14 +35,28 @@ where
|
||||||
Self {
|
Self {
|
||||||
content: content.into(),
|
content: content.into(),
|
||||||
on_show: None,
|
on_show: None,
|
||||||
|
on_resize: None,
|
||||||
on_hide: None,
|
on_hide: None,
|
||||||
anticipate: Pixels::ZERO,
|
anticipate: Pixels::ZERO,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the message to be produced when the content pops into view.
|
/// Sets the message to be produced when the content pops into view.
|
||||||
pub fn on_show(mut self, on_show: Message) -> Self {
|
///
|
||||||
self.on_show = Some(on_show);
|
/// The closure will receive the relative bounds of the content in that moment.
|
||||||
|
pub fn on_show(mut self, on_show: impl Fn(Size) -> Message + 'a) -> Self {
|
||||||
|
self.on_show = Some(Box::new(on_show));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the message to be produced when the content changes [`Size`] once its in view.
|
||||||
|
///
|
||||||
|
/// The closure will receive the new [`Size`] of the content.
|
||||||
|
pub fn on_resize(
|
||||||
|
mut self,
|
||||||
|
on_resize: impl Fn(Size) -> Message + 'a,
|
||||||
|
) -> Self {
|
||||||
|
self.on_resize = Some(Box::new(on_resize));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,6 +80,7 @@ where
|
||||||
#[derive(Debug, Clone, Copy, Default)]
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
struct State {
|
struct State {
|
||||||
has_popped_in: bool,
|
has_popped_in: bool,
|
||||||
|
last_size: Option<Size>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||||
|
|
@ -112,16 +128,27 @@ where
|
||||||
let distance = top_left_distance.min(bottom_right_distance);
|
let distance = top_left_distance.min(bottom_right_distance);
|
||||||
|
|
||||||
if state.has_popped_in {
|
if state.has_popped_in {
|
||||||
if let Some(on_hide) = &self.on_hide {
|
if distance <= self.anticipate.0 {
|
||||||
if distance > self.anticipate.0 {
|
if let Some(on_resize) = &self.on_resize {
|
||||||
state.has_popped_in = false;
|
let size = bounds.size();
|
||||||
shell.publish(on_hide.clone());
|
|
||||||
|
if Some(size) != state.last_size {
|
||||||
|
state.last_size = Some(size);
|
||||||
|
shell.publish(on_resize(size));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else if let Some(on_hide) = &self.on_hide {
|
||||||
|
state.has_popped_in = false;
|
||||||
|
shell.publish(on_hide.clone());
|
||||||
}
|
}
|
||||||
} else if let Some(on_show) = &self.on_show {
|
} else if let Some(on_show) = &self.on_show {
|
||||||
if distance <= self.anticipate.0 {
|
if distance <= self.anticipate.0 {
|
||||||
|
let size = bounds.size();
|
||||||
|
|
||||||
state.has_popped_in = true;
|
state.has_popped_in = true;
|
||||||
shell.publish(on_show.clone());
|
state.last_size = Some(size);
|
||||||
|
|
||||||
|
shell.publish(on_show(size));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue