Merge pull request #1337 from iced-rs/generic-widget-tree
Take `Borrow<dyn Widget>` instead of `Element` in `Tree` API
This commit is contained in:
commit
76e7c307df
2 changed files with 38 additions and 13 deletions
|
|
@ -8,6 +8,8 @@ use iced_native::mouse;
|
||||||
use iced_native::renderer;
|
use iced_native::renderer;
|
||||||
use iced_native::{Clipboard, Length, Point, Rectangle, Shell};
|
use iced_native::{Clipboard, Length, Point, Rectangle, Shell};
|
||||||
|
|
||||||
|
use std::borrow::Borrow;
|
||||||
|
|
||||||
/// A generic [`Widget`].
|
/// A generic [`Widget`].
|
||||||
///
|
///
|
||||||
/// It is useful to build composable user interfaces that do not leak
|
/// It is useful to build composable user interfaces that do not leak
|
||||||
|
|
@ -321,3 +323,19 @@ where
|
||||||
.map(move |overlay| overlay.map(mapper))
|
.map(move |overlay| overlay.map(mapper))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, Message, Renderer> Borrow<dyn Widget<Message, Renderer> + 'a>
|
||||||
|
for Element<'a, Message, Renderer>
|
||||||
|
{
|
||||||
|
fn borrow(&self) -> &(dyn Widget<Message, Renderer> + 'a) {
|
||||||
|
self.widget.borrow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Message, Renderer> Borrow<dyn Widget<Message, Renderer> + 'a>
|
||||||
|
for &Element<'a, Message, Renderer>
|
||||||
|
{
|
||||||
|
fn borrow(&self) -> &(dyn Widget<Message, Renderer> + 'a) {
|
||||||
|
self.widget.borrow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
//! Store internal widget state in a state tree to ensure continuity.
|
//! Store internal widget state in a state tree to ensure continuity.
|
||||||
use crate::Element;
|
use crate::Widget;
|
||||||
|
|
||||||
use std::any::{self, Any};
|
use std::any::{self, Any};
|
||||||
|
use std::borrow::Borrow;
|
||||||
|
|
||||||
/// A persistent state widget tree.
|
/// A persistent state widget tree.
|
||||||
///
|
///
|
||||||
|
|
@ -28,13 +29,15 @@ impl Tree {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new [`Tree`] for the provided [`Element`].
|
/// Creates a new [`Tree`] for the provided [`Element`].
|
||||||
pub fn new<Message, Renderer>(
|
pub fn new<'a, Message, Renderer>(
|
||||||
element: &Element<'_, Message, Renderer>,
|
widget: impl Borrow<dyn Widget<Message, Renderer> + 'a>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
let widget = widget.borrow();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
tag: element.as_widget().tag(),
|
tag: widget.tag(),
|
||||||
state: element.as_widget().state(),
|
state: widget.state(),
|
||||||
children: element.as_widget().children(),
|
children: widget.children(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,23 +49,27 @@ impl Tree {
|
||||||
/// Otherwise, the whole [`Tree`] is recreated.
|
/// Otherwise, the whole [`Tree`] is recreated.
|
||||||
///
|
///
|
||||||
/// [`Widget::diff`]: crate::Widget::diff
|
/// [`Widget::diff`]: crate::Widget::diff
|
||||||
pub fn diff<Message, Renderer>(
|
pub fn diff<'a, Message, Renderer>(
|
||||||
&mut self,
|
&mut self,
|
||||||
new: &Element<'_, Message, Renderer>,
|
new: impl Borrow<dyn Widget<Message, Renderer> + 'a>,
|
||||||
) {
|
) {
|
||||||
if self.tag == new.as_widget().tag() {
|
if self.tag == new.borrow().tag() {
|
||||||
new.as_widget().diff(self)
|
new.borrow().diff(self)
|
||||||
} else {
|
} else {
|
||||||
*self = Self::new(new);
|
*self = Self::new(new);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reconciliates the children of the tree with the provided list of [`Element`].
|
/// Reconciliates the children of the tree with the provided list of [`Element`].
|
||||||
pub fn diff_children<Message, Renderer>(
|
pub fn diff_children<'a, Message, Renderer>(
|
||||||
&mut self,
|
&mut self,
|
||||||
new_children: &[Element<'_, Message, Renderer>],
|
new_children: &[impl Borrow<dyn Widget<Message, Renderer> + 'a>],
|
||||||
) {
|
) {
|
||||||
self.diff_children_custom(new_children, Self::diff, Self::new)
|
self.diff_children_custom(
|
||||||
|
new_children,
|
||||||
|
|tree, widget| tree.diff(widget.borrow()),
|
||||||
|
|widget| Self::new(widget.borrow()),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reconciliates the children of the tree with the provided list of [`Element`] using custom
|
/// Reconciliates the children of the tree with the provided list of [`Element`] using custom
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue