Implement pure version of todos example 🎉

The `Widget` trait in `iced_pure` needed to change a bit to make the
implementation of `Element::map` possible.

Specifically, the `children` method has been split into `diff` and
`children_state`.
This commit is contained in:
Héctor Ramón Jiménez 2022-02-12 17:21:28 +07:00
parent e3108494e5
commit bd22cc0bc0
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
18 changed files with 917 additions and 96 deletions

View file

@ -53,7 +53,8 @@ impl State {
impl<'a, Message, Renderer> iced_native::Widget<Message, Renderer>
for Pure<'a, Message, Renderer>
where
Renderer: iced_native::Renderer,
Message: 'a,
Renderer: iced_native::Renderer + 'a,
{
fn width(&self) -> Length {
self.element.as_widget().width()

View file

@ -33,7 +33,9 @@ pub trait Widget<Message, Renderer> {
fn state(&self) -> Box<dyn Any>;
fn children(&self) -> &[Element<Message, Renderer>];
fn diff(&self, tree: &mut Tree);
fn children_state(&self) -> Vec<Tree>;
fn width(&self) -> Length;
@ -99,11 +101,13 @@ pub fn row<'a, Message, Renderer>() -> Row<'a, Message, Renderer> {
Row::new()
}
pub fn scrollable<'a, Message, Renderer>() -> Scrollable<'a, Message, Renderer>
pub fn scrollable<'a, Message, Renderer>(
content: impl Into<Element<'a, Message, Renderer>>,
) -> Scrollable<'a, Message, Renderer>
where
Renderer: iced_native::Renderer,
{
Scrollable::new()
Scrollable::new(content)
}
pub fn button<'a, Message, Renderer>(

View file

@ -85,8 +85,12 @@ where
Box::new(State::new())
}
fn children(&self) -> &[Element<Message, Renderer>] {
std::slice::from_ref(&self.content)
fn diff(&self, tree: &mut Tree) {
tree.diff_children(std::slice::from_ref(&self.content))
}
fn children_state(&self) -> Vec<Tree> {
vec![Tree::new(&self.content)]
}
fn width(&self) -> Length {

View file

@ -22,8 +22,10 @@ where
Box::new(())
}
fn children(&self) -> &[Element<Message, Renderer>] {
&[]
fn diff(&self, _tree: &mut Tree) {}
fn children_state(&self) -> Vec<Tree> {
Vec::new()
}
fn width(&self) -> Length {

View file

@ -85,8 +85,12 @@ where
Box::new(())
}
fn children(&self) -> &[Element<Message, Renderer>] {
&self.children
fn diff(&self, tree: &mut Tree) {
tree.diff_children(&self.children);
}
fn children_state(&self) -> Vec<Tree> {
self.children.iter().map(Tree::new).collect()
}
fn width(&self) -> Length {

View file

@ -132,8 +132,12 @@ where
Box::new(())
}
fn children(&self) -> &[Element<Message, Renderer>] {
std::slice::from_ref(&self.content)
fn diff(&self, tree: &mut Tree) {
tree.diff_children(std::slice::from_ref(&self.content))
}
fn children_state(&self) -> Vec<Tree> {
vec![Tree::new(&self.content)]
}
fn width(&self) -> Length {

View file

@ -1,4 +1,12 @@
use crate::Widget;
use crate::widget::{Tree, Widget};
use iced_native::event::{self, Event};
use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell};
use std::any::{self, Any};
pub struct Element<'a, Message, Renderer> {
widget: Box<dyn Widget<Message, Renderer> + 'a>,
@ -18,4 +26,143 @@ impl<'a, Message, Renderer> Element<'a, Message, Renderer> {
pub fn as_widget_mut(&mut self) -> &mut dyn Widget<Message, Renderer> {
self.widget.as_mut()
}
pub fn map<B>(
self,
f: impl Fn(Message) -> B + 'a,
) -> Element<'a, B, Renderer>
where
Message: 'a,
Renderer: iced_native::Renderer + 'a,
B: 'a,
{
Element::new(Map::new(self.widget, f))
}
}
struct Map<'a, A, B, Renderer> {
widget: Box<dyn Widget<A, Renderer> + 'a>,
mapper: Box<dyn Fn(A) -> B + 'a>,
}
impl<'a, A, B, Renderer> Map<'a, A, B, Renderer> {
pub fn new<F>(
widget: Box<dyn Widget<A, Renderer> + 'a>,
mapper: F,
) -> Map<'a, A, B, Renderer>
where
F: 'a + Fn(A) -> B,
{
Map {
widget,
mapper: Box::new(mapper),
}
}
}
impl<'a, A, B, Renderer> Widget<B, Renderer> for Map<'a, A, B, Renderer>
where
Renderer: iced_native::Renderer + 'a,
A: 'a,
B: 'a,
{
fn tag(&self) -> any::TypeId {
self.widget.tag()
}
fn state(&self) -> Box<dyn Any> {
self.widget.state()
}
fn diff(&self, tree: &mut Tree) {
self.widget.diff(tree)
}
fn children_state(&self) -> Vec<Tree> {
self.widget.children_state()
}
fn width(&self) -> Length {
self.widget.width()
}
fn height(&self) -> Length {
self.widget.height()
}
fn layout(
&self,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
self.widget.layout(renderer, limits)
}
fn on_event(
&mut self,
tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, B>,
) -> event::Status {
let mut local_messages = Vec::new();
let mut local_shell = Shell::new(&mut local_messages);
let status = self.widget.on_event(
tree,
event,
layout,
cursor_position,
renderer,
clipboard,
&mut local_shell,
);
shell.merge(local_shell, &self.mapper);
status
}
fn draw(
&self,
tree: &Tree,
renderer: &mut Renderer,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) {
self.widget.draw(
tree,
renderer,
style,
layout,
cursor_position,
viewport,
)
}
fn mouse_interaction(
&self,
tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
self.widget.mouse_interaction(
tree,
layout,
cursor_position,
viewport,
renderer,
)
}
fn hash_layout(&self, state: &mut Hasher) {
self.widget.hash_layout(state);
}
}

View file

@ -85,8 +85,12 @@ where
Box::new(())
}
fn children(&self) -> &[Element<Message, Renderer>] {
&self.children
fn diff(&self, tree: &mut Tree) {
tree.diff_children(&self.children)
}
fn children_state(&self) -> Vec<Tree> {
self.children.iter().map(Tree::new).collect()
}
fn width(&self) -> Length {

View file

@ -1,4 +1,4 @@
use crate::widget::{Column, Tree};
use crate::widget::Tree;
use crate::{Element, Widget};
use iced_native::event::{self, Event};
@ -6,9 +6,7 @@ use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::widget::scrollable;
use iced_native::{
Alignment, Clipboard, Hasher, Length, Padding, Point, Rectangle, Shell,
};
use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell};
pub use iced_style::scrollable::StyleSheet;
@ -22,61 +20,33 @@ pub struct Scrollable<'a, Message, Renderer> {
scrollbar_width: u16,
scrollbar_margin: u16,
scroller_width: u16,
content: Column<'a, Message, Renderer>,
on_scroll: Option<Box<dyn Fn(f32) -> Message>>,
style_sheet: Box<dyn StyleSheet + 'a>,
content: Element<'a, Message, Renderer>,
}
impl<'a, Message, Renderer: iced_native::Renderer>
Scrollable<'a, Message, Renderer>
{
/// Creates a new [`Scrollable`] with the given [`State`].
pub fn new() -> Self {
/// Creates a new [`Scrollable`].
pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self {
Scrollable {
height: Length::Shrink,
scrollbar_width: 10,
scrollbar_margin: 0,
scroller_width: 10,
content: Column::new(),
on_scroll: None,
style_sheet: Default::default(),
content: content.into(),
}
}
/// Sets the vertical spacing _between_ elements.
///
/// Custom margins per element do not exist in Iced. You should use this
/// method instead! While less flexible, it helps you keep spacing between
/// elements consistent.
pub fn spacing(mut self, units: u16) -> Self {
self.content = self.content.spacing(units);
self
}
/// Sets the [`Padding`] of the [`Scrollable`].
pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
self.content = self.content.padding(padding);
self
}
/// Sets the width of the [`Scrollable`].
pub fn width(mut self, width: Length) -> Self {
self.content = self.content.width(width);
self
}
/// Sets the height of the [`Scrollable`].
pub fn height(mut self, height: Length) -> Self {
self.height = height;
self
}
/// Sets the horizontal alignment of the contents of the [`Scrollable`] .
pub fn align_items(mut self, align_items: Alignment) -> Self {
self.content = self.content.align_items(align_items);
self
}
/// Sets the scrollbar width of the [`Scrollable`] .
/// Silently enforces a minimum value of 1.
pub fn scrollbar_width(mut self, scrollbar_width: u16) -> Self {
@ -115,15 +85,6 @@ impl<'a, Message, Renderer: iced_native::Renderer>
self.style_sheet = style_sheet.into();
self
}
/// Adds an element to the [`Scrollable`].
pub fn push<E>(mut self, child: E) -> Self
where
E: Into<Element<'a, Message, Renderer>>,
{
self.content = self.content.push(child);
self
}
}
impl<'a, Message, Renderer> Widget<Message, Renderer>
@ -139,12 +100,16 @@ where
Box::new(scrollable::State::new())
}
fn children(&self) -> &[Element<Message, Renderer>] {
self.content.children()
fn diff(&self, tree: &mut Tree) {
tree.diff_children(std::slice::from_ref(&self.content))
}
fn children_state(&self) -> Vec<Tree> {
vec![Tree::new(&self.content)]
}
fn width(&self) -> Length {
Widget::<Message, Renderer>::width(&self.content)
self.content.as_widget().width()
}
fn height(&self) -> Length {
@ -156,7 +121,7 @@ where
self.tag().hash(state);
self.height.hash(state);
self.content.hash_layout(state)
self.content.as_widget().hash_layout(state)
}
fn layout(
@ -169,7 +134,9 @@ where
limits,
Widget::<Message, Renderer>::width(self),
self.height,
|renderer, limits| self.content.layout(renderer, limits),
|renderer, limits| {
self.content.as_widget().layout(renderer, limits)
},
)
}
@ -195,7 +162,7 @@ where
self.scroller_width,
&self.on_scroll,
|event, layout, cursor_position, clipboard, shell| {
self.content.on_event(
self.content.as_widget_mut().on_event(
&mut tree.children[0],
event,
layout,
@ -227,7 +194,7 @@ where
self.scroller_width,
self.style_sheet.as_ref(),
|renderer, layout, cursor_position, viewport| {
self.content.draw(
self.content.as_widget().draw(
&tree.children[0],
renderer,
style,
@ -255,7 +222,7 @@ where
self.scrollbar_margin,
self.scroller_width,
|layout, cursor_position, viewport| {
self.content.mouse_interaction(
self.content.as_widget().mouse_interaction(
&tree.children[0],
layout,
cursor_position,
@ -266,3 +233,16 @@ where
)
}
}
impl<'a, Message, Renderer> From<Scrollable<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Message: 'a + Clone,
Renderer: 'a + iced_native::Renderer,
{
fn from(
text_input: Scrollable<'a, Message, Renderer>,
) -> Element<'a, Message, Renderer> {
Element::new(text_input)
}
}

View file

@ -21,8 +21,10 @@ where
Box::new(())
}
fn children(&self) -> &[Element<Message, Renderer>] {
&[]
fn diff(&self, _tree: &mut Tree) {}
fn children_state(&self) -> Vec<Tree> {
Vec::new()
}
fn width(&self) -> Length {

View file

@ -146,8 +146,10 @@ where
Box::new(text_input::State::new())
}
fn children(&self) -> &[Element<Message, Renderer>] {
&[]
fn diff(&self, _tree: &mut Tree) {}
fn children_state(&self) -> Vec<Tree> {
Vec::new()
}
fn width(&self) -> Length {
@ -237,3 +239,16 @@ where
text_input::mouse_interaction(layout, cursor_position)
}
}
impl<'a, Message, Renderer> From<TextInput<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Message: 'a + Clone,
Renderer: 'a + text::Renderer,
{
fn from(
text_input: TextInput<'a, Message, Renderer>,
) -> Element<'a, Message, Renderer> {
Element::new(text_input)
}
}

View file

@ -23,12 +23,7 @@ impl Tree {
Self {
tag: element.as_widget().tag(),
state: State(element.as_widget().state()),
children: element
.as_widget()
.children()
.iter()
.map(Self::new)
.collect(),
children: element.as_widget().children_state(),
}
}
@ -37,27 +32,32 @@ impl Tree {
new: &Element<'_, Message, Renderer>,
) {
if self.tag == new.as_widget().tag() {
let new_children = new.as_widget().children();
if self.children.len() > new_children.len() {
self.children.truncate(new_children.len());
}
for (child_state, new) in
self.children.iter_mut().zip(new_children.iter())
{
child_state.diff(new);
}
if self.children.len() < new_children.len() {
self.children.extend(
new_children[self.children.len()..].iter().map(Self::new),
);
}
new.as_widget().diff(self)
} else {
*self = Self::new(new);
}
}
pub fn diff_children<Message, Renderer>(
&mut self,
new_children: &[Element<'_, Message, Renderer>],
) {
if self.children.len() > new_children.len() {
self.children.truncate(new_children.len());
}
for (child_state, new) in
self.children.iter_mut().zip(new_children.iter())
{
child_state.diff(new);
}
if self.children.len() < new_children.len() {
self.children.extend(
new_children[self.children.len()..].iter().map(Self::new),
);
}
}
}
pub struct State(Box<dyn Any>);