Merge pull request #1646 from nicksenger/feat/dependency-in-lazy
Provide `&Dependency` to `Lazy` widget view fn
This commit is contained in:
commit
902131eb51
3 changed files with 10 additions and 6 deletions
|
|
@ -167,7 +167,7 @@ impl Sandbox for App {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
let options = lazy(self.version, || {
|
let options = lazy(self.version, |_| {
|
||||||
let mut items: Vec<_> = self.items.iter().cloned().collect();
|
let mut items: Vec<_> = self.items.iter().cloned().collect();
|
||||||
|
|
||||||
items.sort_by(|a, b| match self.order {
|
items.sort_by(|a, b| match self.order {
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ use std::rc::Rc;
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Lazy<'a, Message, Renderer, Dependency, View> {
|
pub struct Lazy<'a, Message, Renderer, Dependency, View> {
|
||||||
dependency: Dependency,
|
dependency: Dependency,
|
||||||
view: Box<dyn Fn() -> View + 'a>,
|
view: Box<dyn Fn(&Dependency) -> View + 'a>,
|
||||||
element: RefCell<
|
element: RefCell<
|
||||||
Option<Rc<RefCell<Option<Element<'static, Message, Renderer>>>>>,
|
Option<Rc<RefCell<Option<Element<'static, Message, Renderer>>>>>,
|
||||||
>,
|
>,
|
||||||
|
|
@ -28,7 +28,10 @@ where
|
||||||
Dependency: Hash + 'a,
|
Dependency: Hash + 'a,
|
||||||
View: Into<Element<'static, Message, Renderer>>,
|
View: Into<Element<'static, Message, Renderer>>,
|
||||||
{
|
{
|
||||||
pub fn new(dependency: Dependency, view: impl Fn() -> View + 'a) -> Self {
|
pub fn new(
|
||||||
|
dependency: Dependency,
|
||||||
|
view: impl Fn(&Dependency) -> View + 'a,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
dependency,
|
dependency,
|
||||||
view: Box::new(view),
|
view: Box::new(view),
|
||||||
|
|
@ -88,7 +91,8 @@ where
|
||||||
self.dependency.hash(&mut hasher);
|
self.dependency.hash(&mut hasher);
|
||||||
let hash = hasher.finish();
|
let hash = hasher.finish();
|
||||||
|
|
||||||
let element = Rc::new(RefCell::new(Some((self.view)().into())));
|
let element =
|
||||||
|
Rc::new(RefCell::new(Some((self.view)(&self.dependency).into())));
|
||||||
|
|
||||||
(*self.element.borrow_mut()) = Some(element.clone());
|
(*self.element.borrow_mut()) = Some(element.clone());
|
||||||
|
|
||||||
|
|
@ -109,7 +113,7 @@ where
|
||||||
if current.hash != new_hash {
|
if current.hash != new_hash {
|
||||||
current.hash = new_hash;
|
current.hash = new_hash;
|
||||||
|
|
||||||
let element = (self.view)().into();
|
let element = (self.view)(&self.dependency).into();
|
||||||
current.element = Rc::new(RefCell::new(Some(element)));
|
current.element = Rc::new(RefCell::new(Some(element)));
|
||||||
|
|
||||||
(*self.element.borrow_mut()) = Some(current.element.clone());
|
(*self.element.borrow_mut()) = Some(current.element.clone());
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ use std::hash::Hash;
|
||||||
|
|
||||||
pub fn lazy<'a, Message, Renderer, Dependency, View>(
|
pub fn lazy<'a, Message, Renderer, Dependency, View>(
|
||||||
dependency: Dependency,
|
dependency: Dependency,
|
||||||
view: impl Fn() -> View + 'a,
|
view: impl Fn(&Dependency) -> View + 'a,
|
||||||
) -> Lazy<'a, Message, Renderer, Dependency, View>
|
) -> Lazy<'a, Message, Renderer, Dependency, View>
|
||||||
where
|
where
|
||||||
Dependency: Hash + 'a,
|
Dependency: Hash + 'a,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue