Merge pull request #2944 from iced-rs/generic-pop-key

Generic key for `pop` widget
This commit is contained in:
Héctor 2025-05-15 14:51:09 +02:00 committed by GitHub
commit 346ce61b5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 145 additions and 27 deletions

View file

@ -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()

View file

@ -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<Element<'a, Message, Theme, Renderer>>,
) -> Pop<'a, Message, Theme, Renderer>
) -> Pop<'a, (), Message, Theme, Renderer>
where
Renderer: core::Renderer,
Message: Clone,

View file

@ -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};
@ -17,9 +16,15 @@ use crate::core::{
///
/// 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,
> {
content: Element<'a, Message, Theme, Renderer>,
key: Option<text::Fragment<'a>>,
key: Key,
on_show: Option<Box<dyn Fn(Size) -> Message + 'a>>,
on_resize: Option<Box<dyn Fn(Size) -> Message + 'a>>,
on_hide: Option<Message>,
@ -27,10 +32,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 +43,7 @@ where
) -> Self {
Self {
content: content.into(),
key: None,
key: (),
on_show: None,
on_resize: None,
on_hide: None,
@ -46,7 +51,14 @@ where
delay: Duration::ZERO,
}
}
}
impl<'a, Key, Message, Theme, Renderer> Pop<'a, Key, Message, Theme, Renderer>
where
Message: Clone,
Key: self::Key,
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 +87,44 @@ 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<K>(
self,
key: K,
) -> Pop<'a, impl self::Key, Message, Theme, Renderer>
where
K: Clone + PartialEq + 'static,
{
Pop {
content: self.content,
key: OwnedKey(key),
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<K>(
self,
key: &'a K,
) -> Pop<'a, &'a K, Message, Theme, Renderer>
where
K: ToOwned + PartialEq<K::Owned> + ?Sized,
K::Owned: 'static,
{
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
@ -104,26 +151,32 @@ where
}
}
#[derive(Debug, Clone, Default)]
struct State {
#[derive(Debug, Clone)]
struct State<Key> {
has_popped_in: bool,
should_notify_at: Option<(bool, Instant)>,
last_size: Option<Size>,
last_key: Option<String>,
last_key: Key,
}
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Pop<'_, Message, Theme, Renderer>
impl<Key, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Pop<'_, Key, Message, Theme, Renderer>
where
Key: self::Key,
Message: Clone,
Renderer: core::Renderer,
{
fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>()
tree::Tag::of::<State<Key::Owned>>()
}
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.to_owned(),
})
}
fn children(&self) -> Vec<Tree> {
@ -146,15 +199,12 @@ where
viewport: &Rectangle,
) {
if let Event::Window(window::Event::RedrawRequested(now)) = &event {
let state = tree.state.downcast_mut::<State>();
let state = tree.state.downcast_mut::<State<Key::Owned>>();
if state.has_popped_in
&& state.last_key.as_deref() != self.key.as_deref()
{
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().cloned().map(text::Fragment::into_owned);
state.last_key = self.key.to_owned();
}
let bounds = layout.bounds();
@ -308,14 +358,82 @@ where
}
}
impl<'a, Message, Theme, Renderer> From<Pop<'a, Message, Theme, Renderer>>
impl<'a, Key, Message, Theme, Renderer>
From<Pop<'a, Key, Message, Theme, Renderer>>
for Element<'a, Message, Theme, Renderer>
where
Message: Clone + 'a,
Key: self::Key + 'a,
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)
}
}
/// 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<T> Key for &T
where
T: ToOwned + PartialEq<T::Owned> + ?Sized,
T::Owned: 'static,
{
type Owned = T::Owned;
fn to_owned(&self) -> <Self as Key>::Owned {
ToOwned::to_owned(*self)
}
fn eq(&self, other: &Self::Owned) -> bool {
*self == other
}
}
struct OwnedKey<T>(T);
impl<T> Key for OwnedKey<T>
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<T> PartialEq<T> for OwnedKey<T>
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
}
}