Rename iced_lazy::Cached to Lazy 🎉

This commit is contained in:
Héctor Ramón Jiménez 2022-11-03 02:46:31 +01:00
parent adf541d432
commit 1cdc1fcd06
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
3 changed files with 50 additions and 40 deletions

View file

@ -3,7 +3,7 @@ use iced::widget::{
button, column, horizontal_space, row, scrollable, text, text_input,
};
use iced::{Element, Length, Sandbox, Settings};
use iced_lazy::Cached;
use iced_lazy::lazy;
use std::collections::HashSet;
@ -71,39 +71,36 @@ impl Sandbox for App {
}
fn view(&self) -> Element<Message> {
let options =
Cached::new((&self.sort_order, self.options.len()), || {
let mut options: Vec<_> = self.options.iter().collect();
let options = lazy((&self.sort_order, self.options.len()), || {
let mut options: Vec<_> = self.options.iter().collect();
options.sort_by(|a, b| match self.sort_order {
SortOrder::Ascending => {
a.to_lowercase().cmp(&b.to_lowercase())
}
SortOrder::Descending => {
b.to_lowercase().cmp(&a.to_lowercase())
}
});
column(
options
.into_iter()
.map(|option| {
row![
text(option),
horizontal_space(Length::Fill),
button("Delete")
.on_press(Message::DeleteOption(
option.to_string(),
),)
.style(theme::Button::Destructive)
]
.into()
})
.collect(),
)
.spacing(10)
options.sort_by(|a, b| match self.sort_order {
SortOrder::Ascending => a.to_lowercase().cmp(&b.to_lowercase()),
SortOrder::Descending => {
b.to_lowercase().cmp(&a.to_lowercase())
}
});
column(
options
.into_iter()
.map(|option| {
row![
text(option),
horizontal_space(Length::Fill),
button("Delete")
.on_press(Message::DeleteOption(
option.to_string(),
),)
.style(theme::Button::Destructive)
]
.into()
})
.collect(),
)
.spacing(10)
});
column![
scrollable(options).height(Length::Fill),
row![

View file

@ -15,14 +15,14 @@ use std::marker::PhantomData;
use std::rc::Rc;
#[allow(missing_debug_implementations)]
pub struct Cached<'a, Message, Renderer, Dependency, View> {
pub struct Lazy<'a, Message, Renderer, Dependency, View> {
dependency: Dependency,
view: Box<dyn Fn() -> View + 'a>,
element: RefCell<Option<Rc<RefCell<Element<'static, Message, Renderer>>>>>,
}
impl<'a, Message, Renderer, Dependency, View>
Cached<'a, Message, Renderer, Dependency, View>
Lazy<'a, Message, Renderer, Dependency, View>
where
Dependency: Hash + 'a,
View: Into<Element<'static, Message, Renderer>>,
@ -56,7 +56,7 @@ struct Internal<Message, Renderer> {
}
impl<'a, Message, Renderer, Dependency, View> Widget<Message, Renderer>
for Cached<'a, Message, Renderer, Dependency, View>
for Lazy<'a, Message, Renderer, Dependency, View>
where
View: Into<Element<'static, Message, Renderer>> + 'static,
Dependency: Hash + 'a,
@ -237,7 +237,7 @@ where
#[self_referencing]
struct Overlay<'a, 'b, Message, Renderer, Dependency, View> {
cached: &'a Cached<'b, Message, Renderer, Dependency, View>,
cached: &'a Lazy<'b, Message, Renderer, Dependency, View>,
tree: &'a mut Tree,
types: PhantomData<(Message, Dependency, View)>,
@ -348,7 +348,7 @@ where
}
impl<'a, Message, Renderer, Dependency, View>
From<Cached<'a, Message, Renderer, Dependency, View>>
From<Lazy<'a, Message, Renderer, Dependency, View>>
for Element<'a, Message, Renderer>
where
View: Into<Element<'static, Message, Renderer>> + 'static,
@ -356,7 +356,7 @@ where
Message: 'static,
Dependency: Hash + 'a,
{
fn from(cached: Cached<'a, Message, Renderer, Dependency, View>) -> Self {
Self::new(cached)
fn from(lazy: Lazy<'a, Message, Renderer, Dependency, View>) -> Self {
Self::new(lazy)
}
}

View file

@ -17,17 +17,30 @@
clippy::type_complexity
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
pub mod cached;
mod lazy;
pub mod component;
pub mod responsive;
pub use cached::Cached;
pub use component::Component;
pub use lazy::Lazy;
pub use responsive::Responsive;
mod cache;
use iced_native::{Element, Size};
use std::hash::Hash;
pub fn lazy<'a, Message, Renderer, Dependency, View>(
dependency: Dependency,
view: impl Fn() -> View + 'a,
) -> Lazy<'a, Message, Renderer, Dependency, View>
where
Dependency: Hash + 'a,
View: Into<Element<'static, Message, Renderer>>,
{
Lazy::new(dependency, view)
}
/// Turns an implementor of [`Component`] into an [`Element`] that can be
/// embedded in any application.