From f0a38aa1ad3559e0a983fedcb24088d85a8f3a15 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Fri, 18 Oct 2024 11:16:14 -0400 Subject: [PATCH] cleanup --- src/widget/min_size_tracker/mod.rs | 319 -------------------- src/widget/min_size_tracker/subscription.rs | 88 ------ src/widget/rectangle_tracker/mod.rs | 3 - 3 files changed, 410 deletions(-) delete mode 100644 src/widget/min_size_tracker/mod.rs delete mode 100644 src/widget/min_size_tracker/subscription.rs diff --git a/src/widget/min_size_tracker/mod.rs b/src/widget/min_size_tracker/mod.rs deleted file mode 100644 index b190fecd..00000000 --- a/src/widget/min_size_tracker/mod.rs +++ /dev/null @@ -1,319 +0,0 @@ -mod subscription; - -use iced::futures::channel::mpsc::UnboundedSender; -use iced::widget::Container; -use iced::Vector; -pub use subscription::*; - -use iced_core::alignment; -use iced_core::event::{self, Event}; -use iced_core::layout; -use iced_core::mouse; -use iced_core::overlay; -use iced_core::renderer; -use iced_core::widget::Tree; -use iced_core::{Clipboard, Element, Layout, Length, Padding, Rectangle, Shell, Widget}; -use std::{fmt::Debug, hash::Hash}; - -pub use iced_widget::container::{Catalog, Style}; - -pub fn min_size_tracker<'a, Message, I, T>( - content: T, - id: I, - tx: UnboundedSender<(I, Rectangle)>, -) -> MinSizeTrackerContainer<'a, Message, crate::Renderer, I> -where - I: Hash + Copy + Send + Sync + Debug + 'a, - T: Into>, -{ - MinSizeTrackerContainer::new(content, id, tx) -} - -pub fn subscription< - I: 'static + Hash + Copy + Send + Sync + Debug, - R: 'static + Hash + Copy + Send + Sync + Debug + Eq, ->( - id: I, -) -> iced::Subscription<(I, RectangleUpdate)> { - subscription::rectangle_tracker_subscription(id) -} - -#[derive(Clone, Debug)] -pub struct MinSizeTracker { - tx: UnboundedSender<(I, Rectangle)>, -} - -impl MinSizeTracker -where - I: Hash + Copy + Send + Sync + Debug, -{ - pub fn container<'a, Message: 'static, T>( - &self, - id: I, - content: T, - ) -> MinSizeTracker<'a, Message, crate::Renderer, I> - where - I: 'a, - T: Into>, - { - MinSizeTracker::new(content, id, self.tx.clone()) - } -} - -/// An element decorating some content. -/// -/// It is normally used for alignment purposes. -#[allow(missing_debug_implementations)] -pub struct MinSizeTrackerContainer<'a, Message, Renderer, I> -where - Renderer: iced_core::Renderer, -{ - tx: UnboundedSender<(I, Rectangle)>, - id: I, - container: Container<'a, Message, crate::Theme, Renderer>, - ignore_bounds: bool, -} - -impl<'a, Message, Renderer, I> MinSizeTrackerContainer<'a, Message, Renderer, I> -where - Renderer: iced_core::Renderer, - I: 'a + Hash + Copy + Send + Sync + Debug, -{ - /// Creates an empty [`Container`]. - pub(crate) fn new(content: T, id: I, tx: UnboundedSender<(I, Rectangle)>) -> Self - where - T: Into>, - { - MinSizeTrackerContainer { - id, - tx, - container: Container::new(content), - ignore_bounds: false, - } - } - - pub fn diff(&mut self, tree: &mut Tree) { - self.container.diff(tree); - } - - /// Sets the [`Padding`] of the [`Container`]. - #[must_use] - pub fn padding>(mut self, padding: P) -> Self { - self.container = self.container.padding(padding); - self - } - - /// Sets the width of the [`self.`]. - #[must_use] - pub fn width(mut self, width: Length) -> Self { - self.container = self.container.width(width); - self - } - - /// Sets the height of the [`Container`]. - #[must_use] - pub fn height(mut self, height: Length) -> Self { - self.container = self.container.height(height); - self - } - - /// Sets the maximum width of the [`Container`]. - #[must_use] - pub fn max_width(mut self, max_width: f32) -> Self { - self.container = self.container.max_width(max_width); - self - } - - /// Sets the maximum height of the [`Container`] in pixels. - #[must_use] - pub fn max_height(mut self, max_height: f32) -> Self { - self.container = self.container.max_height(max_height); - self - } - - /// Sets the content alignment for the horizontal axis of the [`Container`]. - #[must_use] - pub fn align_x(mut self, alignment: alignment::Horizontal) -> Self { - self.container = self.container.align_x(alignment); - self - } - - /// Sets the content alignment for the vertical axis of the [`Container`]. - #[must_use] - pub fn align_y(mut self, alignment: alignment::Vertical) -> Self { - self.container = self.container.align_y(alignment); - self - } - - /// Centers the contents in the horizontal axis of the [`Container`]. - #[must_use] - pub fn center_x(mut self, width: Length) -> Self { - self.container = self.container.center_x(width); - self - } - - /// Centers the contents in the vertical axis of the [`Container`]. - #[must_use] - pub fn center_y(mut self, height: Length) -> Self { - self.container = self.container.center_y(height); - self - } - - /// Sets the style of the [`Container`]. - #[must_use] - pub fn style(mut self, style: impl Into<::Class<'a>>) -> Self { - self.container = self.container.class(style); - self - } - - /// Set to true to ignore parent container bounds when performing layout. - /// This can be useful for widgets that are in auto-sized surfaces. - #[must_use] - pub fn ignore_bounds(mut self, ignore_bounds: bool) -> Self { - self.ignore_bounds = ignore_bounds; - self - } -} - -impl<'a, Message, Renderer, I> Widget - for MinSizeTrackerContainer<'a, Message, Renderer, I> -where - Renderer: iced_core::Renderer, - I: 'a + Hash + Copy + Send + Sync + Debug, -{ - fn children(&self) -> Vec { - self.container.children() - } - - fn state(&self) -> iced_core::widget::tree::State { - self.container.state() - } - - fn diff(&mut self, tree: &mut Tree) { - self.container.diff(tree); - } - - fn size(&self) -> iced_core::Size { - self.container.size() - } - - fn layout( - &self, - tree: &mut Tree, - renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node { - self.container.layout( - tree, - renderer, - if self.ignore_bounds { - &layout::Limits::NONE - } else { - limits - }, - ) - } - - fn operate( - &self, - tree: &mut Tree, - layout: Layout<'_>, - renderer: &Renderer, - operation: &mut dyn iced_core::widget::Operation<()>, - ) { - self.container.operate(tree, layout, renderer, operation); - } - - fn on_event( - &mut self, - tree: &mut Tree, - event: Event, - layout: Layout<'_>, - cursor_position: mouse::Cursor, - renderer: &Renderer, - clipboard: &mut dyn Clipboard, - shell: &mut Shell<'_, Message>, - viewport: &iced_core::Rectangle, - ) -> event::Status { - self.container.on_event( - tree, - event, - layout, - cursor_position, - renderer, - clipboard, - shell, - viewport, - ) - } - - fn mouse_interaction( - &self, - tree: &Tree, - layout: Layout<'_>, - cursor_position: mouse::Cursor, - viewport: &Rectangle, - renderer: &Renderer, - ) -> mouse::Interaction { - self.container - .mouse_interaction(tree, layout, cursor_position, viewport, renderer) - } - - fn draw( - &self, - tree: &Tree, - renderer: &mut Renderer, - theme: &crate::Theme, - renderer_style: &renderer::Style, - layout: Layout<'_>, - cursor_position: mouse::Cursor, - viewport: &Rectangle, - ) { - let _ = self.tx.unbounded_send((self.id, layout.bounds())); - - self.container.draw( - tree, - renderer, - theme, - renderer_style, - layout, - cursor_position, - viewport, - ); - } - - fn overlay<'b>( - &'b mut self, - tree: &'b mut Tree, - layout: Layout<'_>, - renderer: &Renderer, - translation: Vector, - ) -> Option> { - self.container.overlay(tree, layout, renderer, translation) - } - - fn drag_destinations( - &self, - state: &Tree, - layout: Layout<'_>, - renderer: &Renderer, - dnd_rectangles: &mut iced_core::clipboard::DndDestinationRectangles, - ) { - self.container - .drag_destinations(state, layout, renderer, dnd_rectangles); - } -} - -impl<'a, Message, Renderer, I> From> - for Element<'a, Message, crate::Theme, Renderer> -where - Message: 'a, - Renderer: 'a + iced_core::Renderer, - I: 'a + Hash + Copy + Send + Sync + Debug, -{ - fn from( - column: MinSizeTrackerContainer<'a, Message, Renderer, I>, - ) -> Element<'a, Message, crate::Theme, Renderer> { - Element::new(column) - } -} diff --git a/src/widget/min_size_tracker/subscription.rs b/src/widget/min_size_tracker/subscription.rs deleted file mode 100644 index c7711b43..00000000 --- a/src/widget/min_size_tracker/subscription.rs +++ /dev/null @@ -1,88 +0,0 @@ -use iced::{ - futures::{ - channel::mpsc::{unbounded, UnboundedReceiver}, - stream, StreamExt, - }, - Rectangle, -}; -use iced_futures::Subscription; -use std::{collections::HashMap, fmt::Debug, hash::Hash}; - -use super::MinSizeTrackerContainer; - -pub fn rectangle_tracker_subscription< - I: 'static + Hash + Copy + Send + Sync + Debug, - R: 'static + Hash + Copy + Send + Sync + Debug + Eq, ->( - id: I, -) -> Subscription<(I, RectangleUpdate)> { - Subscription::run_with_id( - id, - stream::unfold(State::Ready, move |state| start_listening(id, state)), - ) -} - -pub enum State { - Ready, - Waiting(UnboundedReceiver<(I, Rectangle)>, HashMap), - Finished, -} - -async fn start_listening( - id: I, - mut state: State, -) -> Option<((I, RectangleUpdate), State)> { - loop { - let (update, new_state) = match state { - State::Ready => { - let (tx, rx) = unbounded(); - - ( - Some((id, RectangleUpdate::Init(MinSizeTracker { tx }))), - State::Waiting(rx, HashMap::new()), - ) - } - State::Waiting(mut rx, mut map) => match rx.next().await { - Some(u) => { - if let Some(prev) = map.get(&u.0) { - let new = u.1; - if (prev.width - new.width).abs() > 0.1 - || (prev.height - new.height).abs() > 0.1 - || (prev.x - new.x).abs() > 0.1 - || (prev.y - new.y).abs() > 0.1 - { - map.insert(u.0, new); - ( - Some((id, RectangleUpdate::Rectangle(u))), - State::Waiting(rx, map), - ) - } else { - (None, State::Waiting(rx, map)) - } - } else { - map.insert(u.0, u.1); - ( - Some((id, RectangleUpdate::Rectangle(u))), - State::Waiting(rx, map), - ) - } - } - None => (None, State::Finished), - }, - State::Finished => return None, - }; - state = new_state; - if let Some(u) = update { - return Some((u, state)); - } - } -} - -#[derive(Clone, Debug)] -pub enum RectangleUpdate -where - I: 'static + Hash + Copy + Send + Sync + Debug, -{ - Rectangle((I, Rectangle)), - Init(MinSizeTrackerContainer), -} diff --git a/src/widget/rectangle_tracker/mod.rs b/src/widget/rectangle_tracker/mod.rs index 6ce11396..d502badb 100644 --- a/src/widget/rectangle_tracker/mod.rs +++ b/src/widget/rectangle_tracker/mod.rs @@ -72,7 +72,6 @@ where id: I, container: Container<'a, Message, crate::Theme, Renderer>, ignore_bounds: bool, - request_size: bool, } impl<'a, Message, Renderer, I> RectangleTrackingContainer<'a, Message, Renderer, I> @@ -90,7 +89,6 @@ where tx, container: Container::new(content), ignore_bounds: false, - request_size: true, } } @@ -214,7 +212,6 @@ where limits }, ); - if self.request_size {} layout }