diff options
author | 2022-07-27 06:49:20 +0200 | |
---|---|---|
committer | 2022-07-27 06:49:20 +0200 | |
commit | ff2519b1d43d481987351a83b6dd7237524c21f0 (patch) | |
tree | 5731eeb7eb1247d4a8951de0d5bc5d8102640559 /native/src/overlay | |
parent | c44267b85f7aaa2997e3caf1323b837d95818c22 (diff) | |
download | iced-ff2519b1d43d481987351a83b6dd7237524c21f0.tar.gz iced-ff2519b1d43d481987351a83b6dd7237524c21f0.tar.bz2 iced-ff2519b1d43d481987351a83b6dd7237524c21f0.zip |
Replace stateful widgets with new `iced_pure` API
Diffstat (limited to '')
-rw-r--r-- | native/src/overlay.rs | 46 | ||||
-rw-r--r-- | native/src/overlay/menu.rs | 54 |
2 files changed, 87 insertions, 13 deletions
diff --git a/native/src/overlay.rs b/native/src/overlay.rs index 792d2905..c2a98693 100644 --- a/native/src/overlay.rs +++ b/native/src/overlay.rs @@ -10,6 +10,7 @@ use crate::event::{self, Event}; use crate::layout; use crate::mouse; use crate::renderer; +use crate::widget::tree::{self, Tree}; use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size}; /// An interactive component that can be displayed on top of other widgets. @@ -40,6 +41,28 @@ where cursor_position: Point, ); + /// Returns the [`Tag`] of the [`Widget`]. + /// + /// [`Tag`]: tree::Tag + fn tag(&self) -> tree::Tag { + tree::Tag::stateless() + } + + /// Returns the [`State`] of the [`Widget`]. + /// + /// [`State`]: tree::State + fn state(&self) -> tree::State { + tree::State::None + } + + /// Returns the state [`Tree`] of the children of the [`Widget`]. + fn children(&self) -> Vec<Tree> { + Vec::new() + } + + /// Reconciliates the [`Widget`] with the provided [`Tree`]. + fn diff(&self, _tree: &mut Tree) {} + /// Processes a runtime [`Event`]. /// /// It receives: @@ -77,3 +100,26 @@ where mouse::Interaction::Idle } } + +/// Obtains the first overlay [`Element`] found in the given children. +/// +/// This method will generally only be used by advanced users that are +/// implementing the [`Widget`](crate::Widget) trait. +pub fn from_children<'a, Message, Renderer>( + children: &'a [crate::Element<'_, Message, Renderer>], + tree: &'a mut Tree, + layout: Layout<'_>, + renderer: &Renderer, +) -> Option<Element<'a, Message, Renderer>> +where + Renderer: crate::Renderer, +{ + children + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .filter_map(|((child, state), layout)| { + child.as_widget().overlay(state, layout, renderer) + }) + .next() +} diff --git a/native/src/overlay/menu.rs b/native/src/overlay/menu.rs index fc3f52b2..b4c77c25 100644 --- a/native/src/overlay/menu.rs +++ b/native/src/overlay/menu.rs @@ -9,6 +9,7 @@ use crate::text::{self, Text}; use crate::touch; use crate::widget::container::{self, Container}; use crate::widget::scrollable::{self, Scrollable}; +use crate::widget::tree::{self, Tree}; use crate::{ Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, Shell, Size, Vector, Widget, @@ -114,15 +115,23 @@ where } /// The local state of a [`Menu`]. -#[derive(Debug, Clone, Default)] +#[derive(Debug)] pub struct State { - scrollable: scrollable::State, + tree: Tree, } impl State { /// Creates a new [`State`] for a [`Menu`]. pub fn new() -> Self { - Self::default() + Self { + tree: Tree::empty(), + } + } +} + +impl Default for State { + fn default() -> Self { + Self::new() } } @@ -131,6 +140,7 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet + container::StyleSheet, { + state: &'a mut Tree, container: Container<'a, Message, Renderer>, width: u16, target_height: f32, @@ -161,18 +171,18 @@ where style, } = menu; - let container = - Container::new(Scrollable::new(&mut state.scrollable).push(List { - options, - hovered_option, - last_selection, - font, - text_size, - padding, - style, - })); + let container = Container::new(Scrollable::new(List { + options, + hovered_option, + last_selection, + font, + text_size, + padding, + style, + })); Self { + state: &mut state.tree, container, width, target_height, @@ -187,6 +197,18 @@ where Renderer: text::Renderer, Renderer::Theme: StyleSheet + container::StyleSheet, { + fn tag(&self) -> tree::Tag { + self.container.tag() + } + + fn state(&self) -> tree::State { + self.container.state() + } + + fn children(&self) -> Vec<Tree> { + self.container.children() + } + fn layout( &self, renderer: &Renderer, @@ -230,6 +252,7 @@ where shell: &mut Shell<'_, Message>, ) -> event::Status { self.container.on_event( + &mut self.state, event, layout, cursor_position, @@ -247,6 +270,7 @@ where renderer: &Renderer, ) -> mouse::Interaction { self.container.mouse_interaction( + &self.state, layout, cursor_position, viewport, @@ -279,6 +303,7 @@ where ); self.container.draw( + &self.state, renderer, theme, style, @@ -344,6 +369,7 @@ where fn on_event( &mut self, + _state: &mut Tree, event: Event, layout: Layout<'_>, cursor_position: Point, @@ -407,6 +433,7 @@ where fn mouse_interaction( &self, + _state: &Tree, layout: Layout<'_>, cursor_position: Point, _viewport: &Rectangle, @@ -423,6 +450,7 @@ where fn draw( &self, + _state: &Tree, renderer: &mut Renderer, theme: &Renderer::Theme, _style: &renderer::Style, |