From 508ad512d6e0b1cf8249d25f95400b55103ea117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 13 May 2025 15:10:44 +0200 Subject: [PATCH 1/2] Make `key` in `pop` widget generic --- widget/src/helpers.rs | 2 +- widget/src/pop.rs | 96 ++++++++++++++++++++++++++++++++----------- 2 files changed, 72 insertions(+), 26 deletions(-) diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 9e8d5f6c..5e7b30d7 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -994,7 +994,7 @@ where /// It can even notify you with anticipation at a given distance! pub fn pop<'a, Message, Theme, Renderer>( content: impl Into>, -) -> Pop<'a, Message, Theme, Renderer> +) -> Pop<'a, (), Message, Theme, Renderer> where Renderer: core::Renderer, Message: Clone, diff --git a/widget/src/pop.rs b/widget/src/pop.rs index a736b434..86276cfa 100644 --- a/widget/src/pop.rs +++ b/widget/src/pop.rs @@ -3,7 +3,6 @@ use crate::core::layout; use crate::core::mouse; use crate::core::overlay; use crate::core::renderer; -use crate::core::text; use crate::core::time::{Duration, Instant}; use crate::core::widget; use crate::core::widget::tree::{self, Tree}; @@ -13,13 +12,23 @@ use crate::core::{ Size, Vector, Widget, }; +use std::borrow::Cow; + /// A widget that can generate messages when its content pops in and out of view. /// /// It can even notify you with anticipation at a given distance! #[allow(missing_debug_implementations)] -pub struct Pop<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer> { +pub struct Pop< + 'a, + Key, + Message, + Theme = crate::Theme, + Renderer = crate::Renderer, +> where + Key: ToOwned + ?Sized, +{ content: Element<'a, Message, Theme, Renderer>, - key: Option>, + key: Cow<'a, Key>, on_show: Option Message + 'a>>, on_resize: Option Message + 'a>>, on_hide: Option, @@ -27,10 +36,10 @@ pub struct Pop<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer> { delay: Duration, } -impl<'a, Message, Theme, Renderer> Pop<'a, Message, Theme, Renderer> +impl<'a, Message, Theme, Renderer> Pop<'a, (), Message, Theme, Renderer> where - Renderer: core::Renderer, Message: Clone, + Renderer: core::Renderer, { /// Creates a new [`Pop`] widget with the given content. pub fn new( @@ -38,7 +47,7 @@ where ) -> Self { Self { content: content.into(), - key: None, + key: Cow::Owned(()), on_show: None, on_resize: None, on_hide: None, @@ -46,7 +55,14 @@ where delay: Duration::ZERO, } } +} +impl<'a, Key, Message, Theme, Renderer> Pop<'a, Key, Message, Theme, Renderer> +where + Message: Clone, + Key: ToOwned + ?Sized, + Renderer: core::Renderer, +{ /// Sets the message to be produced when the content pops into view. /// /// The closure will receive the [`Size`] of the content in that moment. @@ -75,9 +91,32 @@ where /// Sets the key of the [`Pop`] widget, for continuity. /// /// If the key changes, the [`Pop`] widget will trigger again. - pub fn key(mut self, key: impl text::IntoFragment<'a>) -> Self { - self.key = Some(key.into_fragment()); - self + pub fn key( + self, + key: impl Into>, + ) -> Pop<'a, K, Message, Theme, Renderer> + where + K: ToOwned + ?Sized, + { + Pop { + content: self.content, + key: key.into(), + on_show: self.on_show, + on_resize: self.on_resize, + on_hide: self.on_hide, + anticipate: self.anticipate, + delay: self.delay, + } + } + + /// Sets the key of the [`Pop`] widget, for continuity; using a reference. + /// + /// If the key changes, the [`Pop`] widget will trigger again. + pub fn key_ref(self, key: &'a K) -> Pop<'a, K, Message, Theme, Renderer> + where + K: ToOwned + ?Sized, + { + self.key(Cow::Borrowed(key)) } /// Sets the distance in [`Pixels`] to use in anticipation of the @@ -104,26 +143,33 @@ where } } -#[derive(Debug, Clone, Default)] -struct State { +#[derive(Debug, Clone)] +struct State { has_popped_in: bool, should_notify_at: Option<(bool, Instant)>, last_size: Option, - last_key: Option, + last_key: Key, } -impl Widget - for Pop<'_, Message, Theme, Renderer> +impl Widget + for Pop<'_, Key, Message, Theme, Renderer> where + Key: ToOwned + PartialEq + ?Sized, + Key::Owned: 'static, Message: Clone, Renderer: core::Renderer, { fn tag(&self) -> tree::Tag { - tree::Tag::of::() + tree::Tag::of::>() } fn state(&self) -> tree::State { - tree::State::new(State::default()) + tree::State::new(State { + has_popped_in: false, + should_notify_at: None, + last_size: None, + last_key: self.key.as_ref().to_owned(), + }) } fn children(&self) -> Vec { @@ -146,15 +192,12 @@ where viewport: &Rectangle, ) { if let Event::Window(window::Event::RedrawRequested(now)) = &event { - let state = tree.state.downcast_mut::(); + let state = tree.state.downcast_mut::>(); - if state.has_popped_in - && state.last_key.as_deref() != self.key.as_deref() - { + if state.has_popped_in && self.key.as_ref() != &state.last_key { state.has_popped_in = false; state.should_notify_at = None; - state.last_key = - self.key.as_ref().cloned().map(text::Fragment::into_owned); + state.last_key = self.key.as_ref().to_owned(); } let bounds = layout.bounds(); @@ -308,14 +351,17 @@ where } } -impl<'a, Message, Theme, Renderer> From> +impl<'a, Key, Message, Theme, Renderer> + From> for Element<'a, Message, Theme, Renderer> where + Message: Clone + 'a, + Key: ToOwned + PartialEq + ?Sized + 'a, + Key::Owned: 'static, Renderer: core::Renderer + 'a, Theme: 'a, - Message: Clone + 'a, { - fn from(pop: Pop<'a, Message, Theme, Renderer>) -> Self { + fn from(pop: Pop<'a, Key, Message, Theme, Renderer>) -> Self { Element::new(pop) } } From 0290364936996debab95960092e2964e1ddc1d0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 13 May 2025 16:41:34 +0200 Subject: [PATCH 2/2] Use `Key` trait to remove `Cow` in `pop` widget --- examples/markdown/src/main.rs | 2 +- widget/src/pop.rs | 116 +++++++++++++++++++++++++++------- 2 files changed, 95 insertions(+), 23 deletions(-) diff --git a/examples/markdown/src/main.rs b/examples/markdown/src/main.rs index d9a23b1b..12d52dff 100644 --- a/examples/markdown/src/main.rs +++ b/examples/markdown/src/main.rs @@ -268,7 +268,7 @@ impl<'a> markdown::Viewer<'a, Message> for CustomViewer<'a> { .into() } else { pop(horizontal_space()) - .key(url.as_str()) + .key_ref(url.as_str()) .delay(milliseconds(500)) .on_show(|_size| Message::ImageShown(url.clone())) .into() diff --git a/widget/src/pop.rs b/widget/src/pop.rs index 86276cfa..44da6a4e 100644 --- a/widget/src/pop.rs +++ b/widget/src/pop.rs @@ -12,8 +12,6 @@ use crate::core::{ Size, Vector, Widget, }; -use std::borrow::Cow; - /// A widget that can generate messages when its content pops in and out of view. /// /// It can even notify you with anticipation at a given distance! @@ -24,11 +22,9 @@ pub struct Pop< Message, Theme = crate::Theme, Renderer = crate::Renderer, -> where - Key: ToOwned + ?Sized, -{ +> { content: Element<'a, Message, Theme, Renderer>, - key: Cow<'a, Key>, + key: Key, on_show: Option Message + 'a>>, on_resize: Option Message + 'a>>, on_hide: Option, @@ -47,7 +43,7 @@ where ) -> Self { Self { content: content.into(), - key: Cow::Owned(()), + key: (), on_show: None, on_resize: None, on_hide: None, @@ -60,7 +56,7 @@ where impl<'a, Key, Message, Theme, Renderer> Pop<'a, Key, Message, Theme, Renderer> where Message: Clone, - Key: ToOwned + ?Sized, + Key: self::Key, Renderer: core::Renderer, { /// Sets the message to be produced when the content pops into view. @@ -93,14 +89,14 @@ where /// If the key changes, the [`Pop`] widget will trigger again. pub fn key( self, - key: impl Into>, - ) -> Pop<'a, K, Message, Theme, Renderer> + key: K, + ) -> Pop<'a, impl self::Key, Message, Theme, Renderer> where - K: ToOwned + ?Sized, + K: Clone + PartialEq + 'static, { Pop { content: self.content, - key: key.into(), + key: OwnedKey(key), on_show: self.on_show, on_resize: self.on_resize, on_hide: self.on_hide, @@ -112,11 +108,23 @@ where /// Sets the key of the [`Pop`] widget, for continuity; using a reference. /// /// If the key changes, the [`Pop`] widget will trigger again. - pub fn key_ref(self, key: &'a K) -> Pop<'a, K, Message, Theme, Renderer> + pub fn key_ref( + self, + key: &'a K, + ) -> Pop<'a, &'a K, Message, Theme, Renderer> where - K: ToOwned + ?Sized, + K: ToOwned + PartialEq + ?Sized, + K::Owned: 'static, { - self.key(Cow::Borrowed(key)) + Pop { + content: self.content, + key, + on_show: self.on_show, + on_resize: self.on_resize, + on_hide: self.on_hide, + anticipate: self.anticipate, + delay: self.delay, + } } /// Sets the distance in [`Pixels`] to use in anticipation of the @@ -154,8 +162,7 @@ struct State { impl Widget for Pop<'_, Key, Message, Theme, Renderer> where - Key: ToOwned + PartialEq + ?Sized, - Key::Owned: 'static, + Key: self::Key, Message: Clone, Renderer: core::Renderer, { @@ -168,7 +175,7 @@ where has_popped_in: false, should_notify_at: None, last_size: None, - last_key: self.key.as_ref().to_owned(), + last_key: self.key.to_owned(), }) } @@ -194,10 +201,10 @@ where if let Event::Window(window::Event::RedrawRequested(now)) = &event { let state = tree.state.downcast_mut::>(); - if state.has_popped_in && self.key.as_ref() != &state.last_key { + if state.has_popped_in && !self.key.eq(&state.last_key) { state.has_popped_in = false; state.should_notify_at = None; - state.last_key = self.key.as_ref().to_owned(); + state.last_key = self.key.to_owned(); } let bounds = layout.bounds(); @@ -356,8 +363,7 @@ impl<'a, Key, Message, Theme, Renderer> for Element<'a, Message, Theme, Renderer> where Message: Clone + 'a, - Key: ToOwned + PartialEq + ?Sized + 'a, - Key::Owned: 'static, + Key: self::Key + 'a, Renderer: core::Renderer + 'a, Theme: 'a, { @@ -365,3 +371,69 @@ where Element::new(pop) } } + +/// The key of a widget. +/// +/// You should generally not need to care about this trait. +pub trait Key { + /// The owned version of the key. + type Owned: 'static; + + /// Returns the owned version of the key. + fn to_owned(&self) -> Self::Owned; + + /// Compares the key with the given owned version. + fn eq(&self, other: &Self::Owned) -> bool; +} + +impl Key for &T +where + T: ToOwned + PartialEq + ?Sized, + T::Owned: 'static, +{ + type Owned = T::Owned; + + fn to_owned(&self) -> ::Owned { + ToOwned::to_owned(*self) + } + + fn eq(&self, other: &Self::Owned) -> bool { + *self == other + } +} + +struct OwnedKey(T); + +impl Key for OwnedKey +where + T: PartialEq + Clone + 'static, +{ + type Owned = T; + + fn to_owned(&self) -> Self::Owned { + self.0.clone() + } + + fn eq(&self, other: &Self::Owned) -> bool { + &self.0 == other + } +} + +impl PartialEq for OwnedKey +where + T: PartialEq, +{ + fn eq(&self, other: &T) -> bool { + &self.0 == other + } +} + +impl Key for () { + type Owned = (); + + fn to_owned(&self) -> Self::Owned {} + + fn eq(&self, _other: &Self::Owned) -> bool { + true + } +}