From 8f0839e786f8d521f7319dd0e188d43284f526b7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 9 Feb 2022 19:42:15 +0700 Subject: Draft `iced_virtual` subcrate The idea here is to expose a set of "virtual widgets" that can be used with a `Virtual` widget and its `virtual::State`. A virtual widget is a widget that does not contain any state, but instead is a "virtual" representation of the "real" widget. The real widgets are stored in the `virtual::State`. Every time a new virtual widget tree is created during `view`, it is compared to the previous one and "real" widgets are added / removed to the `virtual::State`. Effectively, this removes the need to keep track of local widget state in the application state and allows `view` to take an immutable reference to `self`. To summarize, using this crate should allow users to remove `State` structs in their application state. Eventually, the strategy used here may be adopted generally and, as a result, all of the widgets in `iced_native` would be replaced! --- virtual/src/lib.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 virtual/src/lib.rs (limited to 'virtual/src/lib.rs') diff --git a/virtual/src/lib.rs b/virtual/src/lib.rs new file mode 100644 index 00000000..80953872 --- /dev/null +++ b/virtual/src/lib.rs @@ -0,0 +1,61 @@ +mod element; +pub mod widget; + +pub use element::Element; +pub use widget::Widget; + +use iced_native::layout::{self, Layout}; +use iced_native::renderer; +use iced_native::{Hasher, Length, Point, Rectangle}; + +pub struct Virtual<'a, Message, Renderer> { + state: &'a mut State, +} + +pub struct State { + widget_tree: widget::Tree, + last_element: Element, +} + +impl<'a, Message, Renderer> iced_native::Widget + for Virtual<'a, Message, Renderer> +where + Renderer: iced_native::Renderer, +{ + fn width(&self) -> Length { + self.state.widget_tree.width() + } + + fn height(&self) -> Length { + self.state.widget_tree.height() + } + + fn layout( + &self, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + self.state.widget_tree.layout(renderer, limits) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.state.widget_tree.hash_layout(state) + } + + fn draw( + &self, + renderer: &mut Renderer, + style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + viewport: &Rectangle, + ) { + self.state.widget_tree.draw( + renderer, + style, + layout, + cursor_position, + viewport, + ) + } +} -- cgit