From 38ec9fe42a6740b0242f4f59acb4898a7bfc86f5 Mon Sep 17 00:00:00 2001 From: Redhawk18 Date: Wed, 13 Sep 2023 20:54:02 -0400 Subject: [PATCH 1/3] refactor so functions show up in order on docs --- .../cupertino/cupertino_alert/src/main.rs | 4 +- src/native/badge.rs | 30 ++-- src/native/card.rs | 51 +++---- src/native/color_picker.rs | 8 +- src/native/cupertino/cupertino_alert.rs | 56 ++++---- src/native/cupertino/cupertino_button.rs | 42 +++--- ...pertino_colours.rs => cupertino_colors.rs} | 13 +- src/native/cupertino/cupertino_spinner.rs | 15 +- src/native/cupertino/cupertino_switch.rs | 57 ++++---- src/native/cupertino/mod.rs | 2 +- src/native/date_picker.rs | 11 +- src/native/floating_element.rs | 19 ++- src/native/floating_element/anchor.rs | 28 ++-- src/native/grid.rs | 52 +++---- src/native/menu/flex.rs | 19 --- src/native/menu/menu_bar.rs | 76 +++++----- src/native/menu/menu_inner.rs | 4 +- src/native/modal.rs | 33 ++--- src/native/number_input.rs | 75 +++++----- src/native/overlay/color_picker.rs | 27 ++-- src/native/overlay/date_picker.rs | 25 ++-- src/native/overlay/floating_element.rs | 4 +- src/native/overlay/time_picker.rs | 8 +- src/native/selection_list.rs | 3 +- src/native/selection_list/list.rs | 12 +- src/native/spinner.rs | 11 +- src/native/split.rs | 1 + src/native/tab_bar.rs | 129 ++++++++--------- src/native/tabs.rs | 132 +++++++++--------- src/native/time_picker.rs | 27 ++-- src/native/wrap.rs | 1 - 31 files changed, 467 insertions(+), 508 deletions(-) rename src/native/cupertino/{cupertino_colours.rs => cupertino_colors.rs} (99%) diff --git a/examples/cupertino/cupertino_alert/src/main.rs b/examples/cupertino/cupertino_alert/src/main.rs index 23649984..dab42e23 100644 --- a/examples/cupertino/cupertino_alert/src/main.rs +++ b/examples/cupertino/cupertino_alert/src/main.rs @@ -2,7 +2,7 @@ use iced::widget::{column, container, Text}; use iced::{alignment, executor, font, Application, Command, Element, Length, Settings, Theme}; use iced_aw::native::cupertino::cupertino_alert::{CupertinoAlert, CupertinoDialogAction}; use iced_aw::native::cupertino::cupertino_button::CupertinoButton; -use iced_aw::native::cupertino::cupertino_colours::system_red; +use iced_aw::native::cupertino::cupertino_colors::system_red; pub fn main() -> iced::Result { Alert::run(Settings { @@ -97,7 +97,7 @@ impl Application for Alert { let cancel_button: CupertinoButton = CupertinoButton::new() .on_pressed(Some(Message::CancelEvent)) - .colour(Some(system_red(1.0))) + .color(Some(system_red(1.0))) .body(Text::new(ALLOW) .size(24) .width(Length::Fixed(100.0)) diff --git a/src/native/badge.rs b/src/native/badge.rs index 3a6038a1..0873af76 100644 --- a/src/native/badge.rs +++ b/src/native/badge.rs @@ -74,17 +74,17 @@ where } } - /// Sets the padding of the [`Badge`]. + /// Sets the horizontal alignment of the content of the [`Badge`]. #[must_use] - pub fn padding(mut self, units: u16) -> Self { - self.padding = units; + pub fn align_x(mut self, alignment: Alignment) -> Self { + self.horizontal_alignment = alignment; self } - /// Sets the width of the [`Badge`]. + /// Sets the vertical alignment of the content of the [`Badge`]. #[must_use] - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn align_y(mut self, alignment: Alignment) -> Self { + self.vertical_alignment = alignment; self } @@ -95,24 +95,24 @@ where self } - /// Sets the horizontal alignment of the content of the [`Badge`]. + /// Sets the padding of the [`Badge`]. #[must_use] - pub fn align_x(mut self, alignment: Alignment) -> Self { - self.horizontal_alignment = alignment; + pub fn padding(mut self, units: u16) -> Self { + self.padding = units; self } - /// Sets the vertical alignment of the content of the [`Badge`]. + /// Sets the style of the [`Badge`]. #[must_use] - pub fn align_y(mut self, alignment: Alignment) -> Self { - self.vertical_alignment = alignment; + pub fn style(mut self, style: ::Style) -> Self { + self.style = style; self } - /// Sets the style of the [`Badge`]. + /// Sets the width of the [`Badge`]. #[must_use] - pub fn style(mut self, style: ::Style) -> Self { - self.style = style; + pub fn width(mut self, width: Length) -> Self { + self.width = width; self } } diff --git a/src/native/card.rs b/src/native/card.rs index e428f81f..caf8a6e8 100644 --- a/src/native/card.rs +++ b/src/native/card.rs @@ -2,6 +2,8 @@ //! //! *This API requires the following crate features to be activated: card* +use crate::graphics::icons::{Icon, ICON_FONT}; + use iced_widget::{ core::{ self, @@ -17,7 +19,6 @@ use iced_widget::{ text::LineHeight, }; -use crate::graphics::icons::{Icon, ICON_FONT}; pub use crate::style::card::{Appearance, StyleSheet}; /// The default padding of a [`Card`]. @@ -95,8 +96,8 @@ where Card { width: Length::Fill, height: Length::Shrink, - max_width: 4_294_967_295.0, - max_height: 4_294_967_295.0, + max_width: u32::MAX as f32, + max_height: u32::MAX as f32, padding_head: DEFAULT_PADDING, padding_body: DEFAULT_PADDING, padding_foot: DEFAULT_PADDING, @@ -119,10 +120,10 @@ where self } - /// Sets the width of the [`Card`]. + /// Sets the size of the close icon of the [`Card`]. #[must_use] - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn close_size(mut self, size: f32) -> Self { + self.close_size = Some(size); self } @@ -133,6 +134,13 @@ where self } + /// Sets the maximum height of the [`Card`]. + #[must_use] + pub fn max_height(mut self, height: f32) -> Self { + self.max_height = height; + self + } + /// Sets the maximum width of the [`Card`]. #[must_use] pub fn max_width(mut self, width: f32) -> Self { @@ -140,10 +148,13 @@ where self } - /// Sets the maximum height of the [`Card`]. + /// Sets the message that will be produced when the close icon of the + /// [`Card`] is pressed. + /// + /// Setting this enables the drawing of a close icon on the [`Card`]. #[must_use] - pub fn max_height(mut self, height: f32) -> Self { - self.max_height = height; + pub fn on_close(mut self, msg: Message) -> Self { + self.on_close = Some(msg); self } @@ -180,27 +191,17 @@ where self } - /// Sets the size of the close icon of the [`Card`]. - #[must_use] - pub fn close_size(mut self, size: f32) -> Self { - self.close_size = Some(size); - self - } - - /// Sets the message that will be produced when the close icon of the - /// [`Card`] is pressed. - /// - /// Setting this enables the drawing of a close icon on the [`Card`]. + /// Sets the style of the [`Card`]. #[must_use] - pub fn on_close(mut self, msg: Message) -> Self { - self.on_close = Some(msg); + pub fn style(mut self, style: ::Style) -> Self { + self.style = style; self } - /// Sets the style of the [`Card`]. + /// Sets the width of the [`Card`]. #[must_use] - pub fn style(mut self, style: ::Style) -> Self { - self.style = style; + pub fn width(mut self, width: Length) -> Self { + self.width = width; self } } diff --git a/src/native/color_picker.rs b/src/native/color_picker.rs index eb14019b..db5facf6 100644 --- a/src/native/color_picker.rs +++ b/src/native/color_picker.rs @@ -2,6 +2,10 @@ //! //! *This API requires the following crate features to be activated: `color_picker`* +use super::overlay::color_picker::{ + self, ColorBarDragged, ColorPickerOverlay, ColorPickerOverlayButtons, +}; + use iced_widget::{ button, core::{ @@ -20,10 +24,6 @@ use iced_widget::{ pub use crate::style::color_picker::{Appearance, StyleSheet}; -use super::overlay::color_picker::{ - self, ColorBarDragged, ColorPickerOverlay, ColorPickerOverlayButtons, -}; - //TODO: Remove ignore when Null is updated. Temp fix for Test runs /// An input element for picking colors. /// diff --git a/src/native/cupertino/cupertino_alert.rs b/src/native/cupertino/cupertino_alert.rs index a2d80408..c4d791ee 100644 --- a/src/native/cupertino/cupertino_alert.rs +++ b/src/native/cupertino/cupertino_alert.rs @@ -1,5 +1,8 @@ #![allow(clippy::todo)] +use crate::graphics::SF_UI_ROUNDED; +use crate::native::cupertino::cupertino_colors::secondary_system_fill; + use iced_widget::{ core::{ self, @@ -16,14 +19,8 @@ use iced_widget::{ style::application, text, Text, }; - use std::ops::Range; -// INTERNAL // -use crate::graphics::SF_UI_ROUNDED; -use crate::native::cupertino::cupertino_colours::secondary_system_fill; -// - /** * `CupertinoDialogAction` * @@ -189,24 +186,17 @@ where self } - /// Sets the `height` of the [`CupertinoAlert`]. - #[must_use] - pub fn height(mut self, height: Length) -> Self { - self.height = height; - self - } - - /// Sets `is_hidden` for the [`CupertinoAlert`]. + /// Sets the `actions` of the [`CupertinoAlert`]. #[must_use] - pub fn is_hidden(mut self, is_hidden: bool) -> Self { - self.is_hidden = is_hidden; + pub fn actions(mut self, actions: Vec>) -> Self { + self.actions = actions; self } - /// Sets the `title` of the [`CupertinoAlert`]. + /// Sets the `backdrop` of the [`CupertinoAlert`]. #[must_use] - pub fn title(mut self, title: String) -> Self { - self.title = title; + pub fn backdrop(mut self, backdrop: Option) -> Self { + self.backdrop = backdrop; self } @@ -217,17 +207,17 @@ where self } - /// Sets the `actions` of the [`CupertinoAlert`]. + /// Sets the `height` of the [`CupertinoAlert`]. #[must_use] - pub fn actions(mut self, actions: Vec>) -> Self { - self.actions = actions; + pub fn height(mut self, height: Length) -> Self { + self.height = height; self } - /// Sets the `backdrop` of the [`CupertinoAlert`]. + /// Sets `is_hidden` for the [`CupertinoAlert`]. #[must_use] - pub fn backdrop(mut self, backdrop: Option) -> Self { - self.backdrop = backdrop; + pub fn is_hidden(mut self, is_hidden: bool) -> Self { + self.is_hidden = is_hidden; self } @@ -238,10 +228,16 @@ where self } - // Internal // - fn _text_with_font(element: T) -> Element<'a, Message, Renderer> + /// Sets the `title` of the [`CupertinoAlert`]. + #[must_use] + pub fn title(mut self, title: String) -> Self { + self.title = title; + self + } + + fn text_with_font(element: E) -> Element<'a, Message, Renderer> where - T: Into>, + E: Into>, { let as_text_element = element.into().font(SF_UI_ROUNDED); @@ -482,7 +478,7 @@ where }); let title: Element<'a, Message, Renderer> = - CupertinoAlert::<'a, Message, Renderer>::_text_with_font( + CupertinoAlert::<'a, Message, Renderer>::text_with_font( Text::new(self.title.clone()).horizontal_alignment(Horizontal::Center), ); @@ -497,7 +493,7 @@ where ); let content: Element<'a, Message, Renderer> = - CupertinoAlert::<'a, Message, Renderer>::_text_with_font( + CupertinoAlert::<'a, Message, Renderer>::text_with_font( Text::new(self.content.clone()).horizontal_alignment(Horizontal::Center), ); diff --git a/src/native/cupertino/cupertino_button.rs b/src/native/cupertino/cupertino_button.rs index ec60a345..9804bf49 100644 --- a/src/native/cupertino/cupertino_button.rs +++ b/src/native/cupertino/cupertino_button.rs @@ -1,3 +1,6 @@ +use crate::graphics::SF_UI_ROUNDED; +use crate::native::cupertino::cupertino_colors::{secondary_system_fill, system_blue}; + use iced_widget::{ core::{ self, event, @@ -13,11 +16,6 @@ use iced_widget::{ text, Text, }; -// INTERNAL // -use crate::graphics::SF_UI_ROUNDED; -use crate::native::cupertino::cupertino_colours::{secondary_system_fill, system_blue}; -// - /** * `CupertinoButton` * @@ -78,20 +76,6 @@ where Self::default() } - /// Sets the `on_pressed` callback of the [`CupertinoButton`]. - #[must_use] - pub fn on_pressed(mut self, on_pressed: Option) -> Self { - self.on_pressed = on_pressed; - self - } - - /// Sets the `is_filled` of the [`CupertinoButton`]. - #[must_use] - pub fn is_filled(mut self, is_filled: bool) -> Self { - self.is_filled = is_filled; - self - } - /// Sets the `body` of the [`CupertinoButton`]. #[must_use] pub fn body(mut self, body: T) -> Self @@ -105,10 +89,24 @@ where self } - /// Sets the `colour` of the [`CupertinoButton`]. + /// Sets the `color` of the [`CupertinoButton`]. + #[must_use] + pub fn color(mut self, color: Option) -> Self { + self.colour = color; + self + } + + /// Sets the `is_filled` of the [`CupertinoButton`]. + #[must_use] + pub fn is_filled(mut self, is_filled: bool) -> Self { + self.is_filled = is_filled; + self + } + + /// Sets the `on_pressed` callback of the [`CupertinoButton`]. #[must_use] - pub fn colour(mut self, colour: Option) -> Self { - self.colour = colour; + pub fn on_pressed(mut self, on_pressed: Option) -> Self { + self.on_pressed = on_pressed; self } } diff --git a/src/native/cupertino/cupertino_colours.rs b/src/native/cupertino/cupertino_colors.rs similarity index 99% rename from src/native/cupertino/cupertino_colours.rs rename to src/native/cupertino/cupertino_colors.rs index 8e330ff6..57d78a40 100644 --- a/src/native/cupertino/cupertino_colours.rs +++ b/src/native/cupertino/cupertino_colors.rs @@ -7,6 +7,12 @@ use iced_widget::core::Color; /// Because iced expects `r`, `g`, and `b` to be between 0 and 1, divide by 255 everywhere. // System Colours // +/// Secondary System Fill +#[must_use] +pub fn secondary_system_fill() -> Color { + Color::from_rgb(209.0 / 255.0, 209.0 / 255.0, 214.0 / 255.0) +} + /// System Blue #[must_use] pub fn system_blue(alpha: f32) -> Color { @@ -60,10 +66,3 @@ pub fn system_teal(alpha: f32) -> Color { pub fn system_yellow(alpha: f32) -> Color { Color::from_rgba(1.0, 204.0 / 255.0, 0.0 / 255.0, alpha) } -// - -/// Secondary System Fill -#[must_use] -pub fn secondary_system_fill() -> Color { - Color::from_rgb(209.0 / 255.0, 209.0 / 255.0, 214.0 / 255.0) -} diff --git a/src/native/cupertino/cupertino_spinner.rs b/src/native/cupertino/cupertino_spinner.rs index 9fb71976..b897ae85 100644 --- a/src/native/cupertino/cupertino_spinner.rs +++ b/src/native/cupertino/cupertino_spinner.rs @@ -12,7 +12,6 @@ use iced_widget::{ graphics::geometry::Renderer as _, renderer::Renderer, }; - use std::f32::consts::PI; const HAND_COUNT: usize = 8; @@ -67,13 +66,6 @@ impl CupertinoSpinner { Self::default() } - /// Sets the width of the [`CupertinoSpinner`]. - #[must_use] - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self - } - /// Sets the height of the [`CupertinoSpinner`]. #[must_use] pub fn height(mut self, height: Length) -> Self { @@ -89,6 +81,13 @@ impl CupertinoSpinner { self.radius = radius; self } + + /// Sets the width of the [`CupertinoSpinner`]. + #[must_use] + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } } impl Widget> for CupertinoSpinner { diff --git a/src/native/cupertino/cupertino_switch.rs b/src/native/cupertino/cupertino_switch.rs index 7342936a..f2cf0bb2 100644 --- a/src/native/cupertino/cupertino_switch.rs +++ b/src/native/cupertino/cupertino_switch.rs @@ -1,3 +1,5 @@ +use crate::native::cupertino::cupertino_colors::{secondary_system_fill, system_green}; + use iced_widget::{ canvas::{Cache, Fill, Geometry, Path}, core::{ @@ -13,10 +15,6 @@ use iced_widget::{ renderer::Renderer, }; -// INTERNAL // -use crate::native::cupertino::cupertino_colours::{secondary_system_fill, system_green}; -// - /** * `CupertinoSwitch` * @@ -100,24 +98,17 @@ where Self::default() } - /// Sets the width of the [`CupertinoSwitch`]. - #[must_use] - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self - } - - /// Sets the height of the [`CupertinoSwitch`]. + /// Sets the active colour of the [`CupertinoSwitch`]. #[must_use] - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn active_colour(mut self, colour: Color) -> Self { + self.active_colour = colour; self } - /// Sets the active colour of the [`CupertinoSwitch`]. + /// Sets `apply_theme` of the [`CupertinoSwitch`]. #[must_use] - pub fn active_colour(mut self, colour: Color) -> Self { - self.active_colour = colour; + pub fn apply_theme(mut self, theme: bool) -> Self { + self.apply_theme = theme; self } @@ -128,31 +119,24 @@ where self } - /// Sets the thumb colour of the [`CupertinoSwitch`]. + /// Sets the height of the [`CupertinoSwitch`]. #[must_use] - pub fn thumb_colour(mut self, colour: Color) -> Self { - self.thumb_colour = colour; + pub fn height(mut self, height: Length) -> Self { + self.height = height; self } - /// Sets `apply_theme` of the [`CupertinoSwitch`]. + /// Sets `on_changed` of the [`CupertinoSwitch`]. #[must_use] - pub fn apply_theme(mut self, theme: bool) -> Self { - self.apply_theme = theme; + pub fn on_changed(mut self, on_changed: Option Message>>) -> Self { + self.on_changed = on_changed; self } - // /// Sets `drag_start_behaviour` of the [`CupertinoSwitch`]. - // #[must_use] - // pub fn drag_start_behaviour(mut self, behaviour: bool) -> Self { - // self.drag_start_behaviour = behaviour; - // self - // } - - /// Sets `on_changed` of the [`CupertinoSwitch`]. + /// Sets the thumb colour of the [`CupertinoSwitch`]. #[must_use] - pub fn on_changed(mut self, on_changed: Option Message>>) -> Self { - self.on_changed = on_changed; + pub fn thumb_colour(mut self, colour: Color) -> Self { + self.thumb_colour = colour; self } @@ -162,6 +146,13 @@ where self.value = value; self } + + /// Sets the width of the [`CupertinoSwitch`]. + #[must_use] + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } } impl Widget> for CupertinoSwitch diff --git a/src/native/cupertino/mod.rs b/src/native/cupertino/mod.rs index bc680080..c138f3c7 100644 --- a/src/native/cupertino/mod.rs +++ b/src/native/cupertino/mod.rs @@ -11,7 +11,7 @@ pub mod cupertino_button; /// Use Cupertino-style colours. /// /// *This API requires the following crate features to be activated: `cupertino`* -pub mod cupertino_colours; +pub mod cupertino_colors; /// Use a Cupertino-style spinner element. /// diff --git a/src/native/date_picker.rs b/src/native/date_picker.rs index cb00210a..914a6267 100644 --- a/src/native/date_picker.rs +++ b/src/native/date_picker.rs @@ -2,6 +2,8 @@ //! //! *This API requires the following crate features to be activated: `date_picker`* +use super::overlay::date_picker::{self, DatePickerOverlay, DatePickerOverlayButtons}; + use chrono::Local; use iced_widget::{ button, container, @@ -20,11 +22,10 @@ use iced_widget::{ text, }; -pub use crate::core::date::Date; - -pub use crate::style::date_picker::{Appearance, StyleSheet}; - -use super::overlay::date_picker::{self, DatePickerOverlay, DatePickerOverlayButtons}; +pub use crate::{ + core::date::Date, + style::date_picker::{Appearance, StyleSheet}, +}; //TODO: Remove ignore when Null is updated. Temp fix for Test runs /// An input element for picking dates. diff --git a/src/native/floating_element.rs b/src/native/floating_element.rs index 3854366a..32a83665 100644 --- a/src/native/floating_element.rs +++ b/src/native/floating_element.rs @@ -2,6 +2,8 @@ //! //! *This API requires the following crate features to be activated: `floating_element`* +use super::overlay::floating_element::FloatingElementOverlay; + use iced_widget::core::{ self, event, layout::{Limits, Node}, @@ -13,12 +15,9 @@ use iced_widget::core::{ pub mod anchor; pub use anchor::Anchor; - pub mod offset; pub use offset::Offset; -use super::overlay::floating_element::FloatingElementOverlay; - /// A floating element floating over some content. /// /// # Example @@ -88,6 +87,13 @@ where self } + /// Hide or unhide the [`Element`] on the [`FloatingElement`]. + #[must_use] + pub fn hide(mut self, hide: bool) -> Self { + self.hidden = hide; + self + } + /// Sets the [`Offset`] of the [`FloatingElement`]. #[must_use] pub fn offset(mut self, offset: O) -> Self @@ -97,13 +103,6 @@ where self.offset = offset.into(); self } - - /// Hide or unhide the [`Element`] on the [`FloatingElement`]. - #[must_use] - pub fn hide(mut self, hide: bool) -> Self { - self.hidden = hide; - self - } } impl<'a, Message, Renderer> Widget for FloatingElement<'a, Message, Renderer> diff --git a/src/native/floating_element/anchor.rs b/src/native/floating_element/anchor.rs index 4208e1f3..90c27c1f 100644 --- a/src/native/floating_element/anchor.rs +++ b/src/native/floating_element/anchor.rs @@ -5,35 +5,35 @@ /// Positional [`Anchor`] for the [`FloatingElement`](super::FloatingElement). #[derive(Copy, Clone, Debug, Hash)] pub enum Anchor { - /// NorthWest [`Anchor`] for positioning the [`Button`](iced_widget::Button) - /// on the top left of the underlying element. - NorthWest, + /// North [`Anchor`] for positioning the [`Button`](iced_widget::Button) + /// on the top of the underlying element. + North, /// NorthEast [`Anchor`] for positioning the [`Button`](iced_widget::Button) /// on the top right of the underlying element. NorthEast, - /// SouthWest [`Anchor`] for positioning the [`Button`](iced_widget::Button) - /// on the bottom left of the underlying element. - SouthWest, + /// East [`Anchor`] for positioning the [`Button`](iced_widget::Button) + /// on the right of the underlying element. + East, /// SouthEast [`Anchor`] for positioning the [`Button`](iced_widget::Button) /// on the bottom right of the underlying element. SouthEast, - /// North [`Anchor`] for positioning the [`Button`](iced_widget::Button) - /// on the top of the underlying element. - North, - - /// East [`Anchor`] for positioning the [`Button`](iced_widget::Button) - /// on the right of the underlying element. - East, - /// South [`Anchor`] for positioning the [`Button`](iced_widget::Button) /// on the bottom of the underlying element. South, + /// SouthWest [`Anchor`] for positioning the [`Button`](iced_widget::Button) + /// on the bottom left of the underlying element. + SouthWest, + /// West [`Anchor`] for positioning the [`Button`](iced_widget::Button) /// on the left of the underlying element. West, + + /// NorthWest [`Anchor`] for positioning the [`Button`](iced_widget::Button) + /// on the top left of the underlying element. + NorthWest, } diff --git a/src/native/grid.rs b/src/native/grid.rs index bea91878..e473ff7b 100644 --- a/src/native/grid.rs +++ b/src/native/grid.rs @@ -63,6 +63,32 @@ where Self::default() } + /// Inserts an [`Element`] into the [`Grid`]. + pub fn insert(&mut self, element: E) + where + E: Into>, + { + self.elements.push(element.into()); + } + + /// Adds an [`Element`] to the [`Grid`]. + #[must_use] + pub fn push(mut self, element: E) -> Self + where + E: Into>, + { + self.elements.push(element.into()); + self + } + + /// Sets the [`Grid`] Strategy. + /// Default is ``Strategy::Columns(1)``. + #[must_use] + pub fn strategy(mut self, strategy: Strategy) -> Self { + self.strategy = strategy; + self + } + /// Creates a [`Grid`] with given elements and ``Strategy::Columns(1)`` /// Use ``strategy()`` to update the Strategy. #[must_use] @@ -92,32 +118,6 @@ where elements: Vec::new(), } } - - /// Sets the [`Grid`] Strategy. - /// Default is ``Strategy::Columns(1)``. - #[must_use] - pub fn strategy(mut self, strategy: Strategy) -> Self { - self.strategy = strategy; - self - } - - /// Adds an [`Element`] to the [`Grid`]. - #[must_use] - pub fn push(mut self, element: E) -> Self - where - E: Into>, - { - self.elements.push(element.into()); - self - } - - /// Inserts an [`Element`] into the [`Grid`]. - pub fn insert(&mut self, element: E) - where - E: Into>, - { - self.elements.push(element.into()); - } } impl<'a, Message, Renderer> Default for Grid<'a, Message, Renderer> diff --git a/src/native/menu/flex.rs b/src/native/menu/flex.rs index 96dcf865..bb8070e2 100644 --- a/src/native/menu/flex.rs +++ b/src/native/menu/flex.rs @@ -1,22 +1,3 @@ -//! Distribute elements using a flex-based layout. -// This code is heavily inspired by the [`druid`] codebase. -// -// [`druid`]: https://github.com/xi-editor/druid -// -// Copyright 2018 The xi-editor Authors, Héctor Ramón -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - use iced_widget::core::{ layout::{Limits, Node}, renderer, Alignment, Element, Padding, Point, Size, diff --git a/src/native/menu/menu_bar.rs b/src/native/menu/menu_bar.rs index 143ec7f4..0b2d4762 100644 --- a/src/native/menu/menu_bar.rs +++ b/src/native/menu/menu_bar.rs @@ -1,9 +1,10 @@ //! A widget that handles menu trees - -use super::menu_inner::{ - CloseCondition, Direction, ItemHeight, ItemWidth, Menu, MenuState, PathHighlight, +use super::{ + menu_inner::{ + CloseCondition, Direction, ItemHeight, ItemWidth, Menu, MenuState, PathHighlight, + }, + menu_tree::MenuTree, }; -use super::menu_tree::MenuTree; use crate::style::menu_bar::StyleSheet; use iced_widget::core::{ @@ -74,6 +75,7 @@ where menu_roots: Vec>, style: ::Style, } + impl<'a, Message, Renderer> MenuBar<'a, Message, Renderer> where Renderer: renderer::Renderer, @@ -106,27 +108,6 @@ where } } - /// Sets the width of the [`MenuBar`] - #[must_use] - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self - } - - /// Sets the height of the [`MenuBar`] - #[must_use] - pub fn height(mut self, height: Length) -> Self { - self.height = height; - self - } - - /// Sets the spacing between menu roots - #[must_use] - pub fn spacing(mut self, units: f32) -> Self { - self.spacing = units; - self - } - /// Sets the expand value for each menu's check bounds /// /// When the cursor goes outside of a menu's check bounds, @@ -138,10 +119,10 @@ where self } - /// Moves all the menus in the vertical open direction + /// [`CloseCondition`] #[must_use] - pub fn main_offset(mut self, value: i32) -> Self { - self.main_offset = value; + pub fn close_condition(mut self, close_condition: CloseCondition) -> Self { + self.close_condition = close_condition; self } @@ -152,17 +133,17 @@ where self } - /// Sets the [`Padding`] of the [`MenuBar`] + /// Sets the height of the [`MenuBar`] #[must_use] - pub fn padding>(mut self, padding: P) -> Self { - self.padding = padding.into(); + pub fn height(mut self, height: Length) -> Self { + self.height = height; self } - /// [`CloseCondition`] + /// [`ItemHeight`] #[must_use] - pub fn close_condition(mut self, close_condition: CloseCondition) -> Self { - self.close_condition = close_condition; + pub fn item_height(mut self, item_height: ItemHeight) -> Self { + self.item_height = item_height; self } @@ -173,10 +154,17 @@ where self } - /// [`ItemHeight`] + /// Moves all the menus in the vertical open direction #[must_use] - pub fn item_height(mut self, item_height: ItemHeight) -> Self { - self.item_height = item_height; + pub fn main_offset(mut self, value: i32) -> Self { + self.main_offset = value; + self + } + + /// Sets the [`Padding`] of the [`MenuBar`] + #[must_use] + pub fn padding>(mut self, padding: P) -> Self { + self.padding = padding.into(); self } @@ -187,12 +175,26 @@ where self } + /// Sets the spacing between menu roots + #[must_use] + pub fn spacing(mut self, units: f32) -> Self { + self.spacing = units; + self + } + /// Sets the style of the menu bar and its menus #[must_use] pub fn style(mut self, style: impl Into<::Style>) -> Self { self.style = style.into(); self } + + /// Sets the width of the [`MenuBar`] + #[must_use] + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } } impl<'a, Message, Renderer> Widget for MenuBar<'a, Message, Renderer> where diff --git a/src/native/menu/menu_inner.rs b/src/native/menu/menu_inner.rs index 01596cc3..64f1bd2d 100644 --- a/src/native/menu/menu_inner.rs +++ b/src/native/menu/menu_inner.rs @@ -1,7 +1,5 @@ //! Menu tree overlay - -use super::menu_bar::MenuBarState; -use super::menu_tree::MenuTree; +use super::{menu_bar::MenuBarState, menu_tree::MenuTree}; use crate::style::menu_bar::StyleSheet; use iced_widget::core::{ diff --git a/src/native/modal.rs b/src/native/modal.rs index 35cfd3b1..b0bb718f 100644 --- a/src/native/modal.rs +++ b/src/native/modal.rs @@ -1,6 +1,9 @@ //! A modal for showing elements as an overlay on top of another. //! //! *This API requires the following crate features to be activated: modal* + +use super::overlay::modal::ModalOverlay; + use iced_widget::core::{ self, alignment, event, layout::{Limits, Node}, @@ -10,8 +13,6 @@ use iced_widget::core::{ Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Widget, }; -use super::overlay::modal::ModalOverlay; - pub use crate::style::modal::StyleSheet; /// A modal content as an overlay. @@ -85,6 +86,20 @@ where } } + /// Sets the content alignment for the horizontal axis of the [`Modal`]. + #[must_use] + pub fn align_x(mut self, alignment: alignment::Horizontal) -> Self { + self.horizontal_alignment = alignment; + self + } + + /// Sets the content alignment for the vertical axis of the [`Modal`]. + #[must_use] + pub fn align_y(mut self, alignment: alignment::Vertical) -> Self { + self.vertical_alignment = alignment; + self + } + /// Sets the message that will be produced when the backdrop of the /// [`Modal`] is clicked. #[must_use] @@ -109,20 +124,6 @@ where self.style = style; self } - - /// Sets the content alignment for the horizontal axis of the [`Modal`]. - #[must_use] - pub fn align_x(mut self, alignment: alignment::Horizontal) -> Self { - self.horizontal_alignment = alignment; - self - } - - /// Sets the content alignment for the vertical axis of the [`Modal`]. - #[must_use] - pub fn align_y(mut self, alignment: alignment::Vertical) -> Self { - self.vertical_alignment = alignment; - self - } } impl<'a, Message, Renderer> Widget for Modal<'a, Message, Renderer> diff --git a/src/native/number_input.rs b/src/native/number_input.rs index c76558d6..e97377c2 100644 --- a/src/native/number_input.rs +++ b/src/native/number_input.rs @@ -17,12 +17,11 @@ use iced_widget::{ Alignment, Background, Clipboard, Color, Element, Event, Layout, Length, Padding, Rectangle, Shell, Size, Widget, }, - style, text, + text, text::LineHeight, text_input::{self, cursor, Value}, Column, Container, Row, Text, TextInput, }; - use num_traits::{Num, NumAssignOps}; use std::{fmt::Display, str::FromStr}; @@ -130,31 +129,6 @@ where } } - /// Sets the step of the [`NumberInput`]. - #[must_use] - pub fn step(mut self, step: T) -> Self { - self.step = step; - self - } - - /// Sets the minimum value of the [`NumberInput`]. - #[must_use] - pub fn min(mut self, min: T) -> Self { - if min <= self.bounds.1 { - self.bounds.0 = min; - } - self - } - - /// Sets the maximum value of the [`NumberInput`]. - #[must_use] - pub fn max(mut self, max: T) -> Self { - if max >= self.bounds.0 { - self.bounds.1 = max; - } - self - } - /// Sets the minimum & maximum value (bound) of the [`NumberInput`]. #[must_use] pub fn bounds(mut self, bounds: (T, T)) -> Self { @@ -164,6 +138,13 @@ where self } + /// Sets the content width of the [`NumberInput`]. + #[must_use] + pub fn content_width(mut self, width: Length) -> Self { + self.content = self.content.width(width); + self + } + /// Sets the [`Font`] of the [`Text`]. /// /// [`Font`]: core::Font @@ -176,17 +157,29 @@ where self } - /// Sets the width of the [`NumberInput`]. + /// Sets the minimum value of the [`NumberInput`]. #[must_use] - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn min(mut self, min: T) -> Self { + if min <= self.bounds.1 { + self.bounds.0 = min; + } self } - /// Sets the content width of the [`NumberInput`]. + /// Sets the maximum value of the [`NumberInput`]. #[must_use] - pub fn content_width(mut self, width: Length) -> Self { - self.content = self.content.width(width); + pub fn max(mut self, max: T) -> Self { + if max >= self.bounds.0 { + self.bounds.1 = max; + } + self + } + + /// Sets the message that should be produced when the [`NumberInput`] is + /// focused and the enter key is pressed. + #[must_use] + pub fn on_submit(mut self, message: Message) -> Self { + self.content = self.content.on_submit(message); self } @@ -206,11 +199,10 @@ where self } - /// Sets the message that should be produced when the [`NumberInput`] is - /// focused and the enter key is pressed. + /// Sets the step of the [`NumberInput`]. #[must_use] - pub fn on_submit(mut self, message: Message) -> Self { - self.content = self.content.on_submit(message); + pub fn step(mut self, step: T) -> Self { + self.step = step; self } @@ -224,13 +216,10 @@ where self } - /// Sets the input style of the [`NumberInput`]. + /// Sets the width of the [`NumberInput`]. #[must_use] - pub fn input_style( - mut self, - style: impl Into<::Style>, - ) -> Self { - self.content = self.content.style(style); + pub fn width(mut self, width: Length) -> Self { + self.width = width; self } diff --git a/src/native/overlay/color_picker.rs b/src/native/overlay/color_picker.rs index 28562a33..949aeb23 100644 --- a/src/native/overlay/color_picker.rs +++ b/src/native/overlay/color_picker.rs @@ -1,7 +1,19 @@ //! Use a color picker as an input element for picking colors. //! //! *This API requires the following crate features to be activated: `color_picker`* -use std::collections::HashMap; + +use crate::{ + color_picker, + core::{ + color::{HexString, Hsv}, + overlay::Position, + }, + graphics::icons::{icon_to_char, Icon}, + style::{ + color_picker::{Appearance, StyleSheet}, + style_state::StyleState, + }, +}; use iced_widget::{ button, @@ -22,19 +34,8 @@ use iced_widget::{ renderer::Renderer, Button, Column, Row, }; +use std::collections::HashMap; -use crate::{ - color_picker, - core::{ - color::{HexString, Hsv}, - overlay::Position, - }, - graphics::icons::{icon_to_char, Icon}, - style::{ - color_picker::{Appearance, StyleSheet}, - style_state::StyleState, - }, -}; /// The padding around the elements. const PADDING: f32 = 10.0; /// The spacing between the element. diff --git a/src/native/overlay/date_picker.rs b/src/native/overlay/date_picker.rs index b90d7a11..c0cb0afc 100644 --- a/src/native/overlay/date_picker.rs +++ b/src/native/overlay/date_picker.rs @@ -2,6 +2,19 @@ //! //! *This API requires the following crate features to be activated: `date_picker`* +use crate::{ + core::{ + date::{Date, IsInMonth}, + overlay::Position, + }, + date_picker, + graphics::icons::{icon_to_char, Icon, ICON_FONT}, + style::{ + date_picker::{Appearance, StyleSheet}, + style_state::StyleState, + }, +}; + use chrono::{Datelike, Local, NaiveDate}; use iced_widget::{ button, container, @@ -23,18 +36,6 @@ use iced_widget::{ }; use std::collections::HashMap; -use crate::{ - core::{ - date::{Date, IsInMonth}, - overlay::Position, - }, - date_picker, - graphics::icons::{icon_to_char, Icon, ICON_FONT}, - style::style_state::StyleState, -}; - -pub use crate::style::date_picker::{Appearance, StyleSheet}; - /// The padding around the elements. const PADDING: f32 = 10.0; /// The spacing between the elements. diff --git a/src/native/overlay/floating_element.rs b/src/native/overlay/floating_element.rs index c78582c2..da783e43 100644 --- a/src/native/overlay/floating_element.rs +++ b/src/native/overlay/floating_element.rs @@ -2,6 +2,8 @@ //! //! *This API requires the following crate features to be activated: `floating_element`* +use crate::native::floating_element::{Anchor, Offset}; + use iced_widget::core::{ self, event, layout, mouse::{self, Cursor}, @@ -10,8 +12,6 @@ use iced_widget::core::{ Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Size, }; -use crate::native::floating_element::{Anchor, Offset}; - /// The internal overlay of a [`FloatingElement`](crate::FloatingElement) for /// rendering a [`Element`](iced_widget::core::Element) as an overlay. #[allow(missing_debug_implementations)] diff --git a/src/native/overlay/time_picker.rs b/src/native/overlay/time_picker.rs index 24164483..b3dde92e 100644 --- a/src/native/overlay/time_picker.rs +++ b/src/native/overlay/time_picker.rs @@ -1,10 +1,7 @@ //! Use a time picker as an input element for picking times. //! //! *This API requires the following crate features to be activated: `time_picker`* -use std::collections::HashMap; -use crate::graphics::icons::{icon_to_char, ICON_FONT}; -use crate::time_picker::{self, Time}; use crate::{ core::clock::{ NearestRadius, HOUR_RADIUS_PERCENTAGE, HOUR_RADIUS_PERCENTAGE_NO_SECONDS, @@ -12,11 +9,13 @@ use crate::{ SECOND_RADIUS_PERCENTAGE, }, core::{clock, overlay::Position, time::Period}, + graphics::icons::{icon_to_char, ICON_FONT}, style::style_state::StyleState, + time_picker::{self, Time}, Icon, }; -use chrono::{Duration, Local, NaiveTime, Timelike}; +use chrono::{Duration, Local, NaiveTime, Timelike}; use iced_widget::{ button, canvas::{self, LineCap, Path, Stroke, Style, Text}, @@ -38,6 +37,7 @@ use iced_widget::{ renderer::Renderer, text, Button, Column, Container, Row, }; +use std::collections::HashMap; pub use crate::style::time_picker::{Appearance, StyleSheet}; diff --git a/src/native/selection_list.rs b/src/native/selection_list.rs index 1a8506a2..cdbfcc38 100644 --- a/src/native/selection_list.rs +++ b/src/native/selection_list.rs @@ -17,10 +17,9 @@ use iced_widget::{ text::LineHeight, Container, Scrollable, }; +use std::{fmt::Display, hash::Hash, marker::PhantomData}; pub use list::List; -use std::hash::Hash; -use std::{fmt::Display, marker::PhantomData}; /// A widget for selecting a single value from a dynamic scrollable list of options. #[allow(missing_debug_implementations)] diff --git a/src/native/selection_list/list.rs b/src/native/selection_list/list.rs index add92a7e..2de9a5bc 100644 --- a/src/native/selection_list/list.rs +++ b/src/native/selection_list/list.rs @@ -1,10 +1,4 @@ //! Build and show dropdown `ListMenus`. -use std::{ - collections::hash_map::DefaultHasher, - fmt::Display, - hash::{Hash, Hasher}, - marker::PhantomData, -}; use crate::selection_list::StyleSheet; @@ -23,6 +17,12 @@ use iced_widget::{ }, text::LineHeight, }; +use std::{ + collections::hash_map::DefaultHasher, + fmt::Display, + hash::{Hash, Hasher}, + marker::PhantomData, +}; /// The Private [`List`] Handles the Actual list rendering. #[allow(missing_debug_implementations)] diff --git a/src/native/spinner.rs b/src/native/spinner.rs index 27ddb16c..4e425da9 100644 --- a/src/native/spinner.rs +++ b/src/native/spinner.rs @@ -1,5 +1,7 @@ //! A spinner to suggest something is loading. +use crate::style::spinner::StyleSheet; + use iced_widget::core::{ self, event::Status, @@ -13,11 +15,10 @@ use iced_widget::core::{ window, Clipboard, Color, Element, Event, Layout, Length, Rectangle, Shell, Size, Vector, Widget, }; - -use std::marker::PhantomData; -use std::time::{Duration, Instant}; - -use crate::style::spinner::StyleSheet; +use std::{ + marker::PhantomData, + time::{Duration, Instant}, +}; /// A spinner widget, a circle spinning around the center of the widget. #[allow(missing_debug_implementations)] diff --git a/src/native/split.rs b/src/native/split.rs index 86d845e9..7b3a76af 100644 --- a/src/native/split.rs +++ b/src/native/split.rs @@ -1,6 +1,7 @@ //! Use a split to split the available space in two parts to display two different elements. //! //! *This API requires the following crate features to be activated: split* + use iced_widget::{ container, core::{ diff --git a/src/native/tab_bar.rs b/src/native/tab_bar.rs index f17455b0..a094713c 100644 --- a/src/native/tab_bar.rs +++ b/src/native/tab_bar.rs @@ -4,6 +4,10 @@ //! to use the [`Tabs`](super::tabs::Tabs) widget instead. //! //! *This API requires the following crate features to be activated: `tab_bar`* + +pub mod tab_label; +use crate::graphics::icons; + use iced_widget::{ core::{ self, @@ -18,14 +22,11 @@ use iced_widget::{ text::{self, LineHeight}, Column, Row, Text, }; +use std::marker::PhantomData; -pub mod tab_label; pub use crate::style::tab_bar::{Appearance, StyleSheet}; pub use tab_label::TabLabel; -use crate::graphics::icons; - -use std::marker::PhantomData; /// The default icon size. const DEFAULT_ICON_SIZE: f32 = 32.0; /// The default text size. @@ -178,10 +179,12 @@ where } } - /// Gets the index of the currently active tab on the [`TabBar`]. + /// Sets the size of the close icon of the + /// [`TabLabel`](crate::tab_bar::TabLabel)s of the [`TabBar`]. #[must_use] - pub fn get_active_tab_idx(&self) -> usize { - self.active_tab + pub fn close_size(mut self, close_size: f32) -> Self { + self.close_size = close_size; + self } /// Gets the id of the currently active tab on the [`TabBar`]. @@ -190,30 +193,16 @@ where self.tab_indices.get(self.active_tab) } - /// Gets the amount of tabs on the [`TabBar`]. - #[must_use] - pub fn size(&self) -> usize { - self.tab_indices.len() - } - - /// Sets the message that will be produced when the close icon of a tab - /// on the [`TabBar`] is pressed. - /// - /// Setting this enables the drawing of a close icon on the tabs. + /// Gets the index of the currently active tab on the [`TabBar`]. #[must_use] - pub fn on_close(mut self, on_close: F) -> Self - where - F: 'static + Fn(TabId) -> Message, - { - self.on_close = Some(Box::new(on_close)); - self + pub fn get_active_tab_idx(&self) -> usize { + self.active_tab } - /// Sets the width of the [`TabBar`]. + /// Gets the width of the [`TabBar`]. #[must_use] - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self + pub fn get_height(&self) -> Length { + self.height } /// Gets the width of the [`TabBar`]. @@ -222,13 +211,6 @@ where self.width } - /// Sets the width of a tab on the [`TabBar`]. - #[must_use] - pub fn tab_width(mut self, width: Length) -> Self { - self.tab_width = width; - self - } - /// Sets the height of the [`TabBar`]. #[must_use] pub fn height(mut self, height: Length) -> Self { @@ -236,16 +218,11 @@ where self } - /// Gets the width of the [`TabBar`]. - #[must_use] - pub fn get_height(&self) -> Length { - self.height - } - - /// Sets the maximum height of the [`TabBar`]. + /// Sets the font of the icons of the + /// [`TabLabel`](crate::tab_bar::TabLabel)s of the [`TabBar`]. #[must_use] - pub fn max_height(mut self, max_height: f32) -> Self { - self.max_height = max_height; + pub fn icon_font(mut self, icon_font: Font) -> Self { + self.icon_font = Some(icon_font); self } @@ -256,18 +233,23 @@ where self } - /// Sets the text size of the [`TabLabel`](crate::tab_bar::TabLabel)s of the [`TabBar`]. + /// Sets the maximum height of the [`TabBar`]. #[must_use] - pub fn text_size(mut self, text_size: f32) -> Self { - self.text_size = text_size; + pub fn max_height(mut self, max_height: f32) -> Self { + self.max_height = max_height; self } - /// Sets the size of the close icon of the - /// [`TabLabel`](crate::tab_bar::TabLabel)s of the [`TabBar`]. + /// Sets the message that will be produced when the close icon of a tab + /// on the [`TabBar`] is pressed. + /// + /// Setting this enables the drawing of a close icon on the tabs. #[must_use] - pub fn close_size(mut self, close_size: f32) -> Self { - self.close_size = close_size; + pub fn on_close(mut self, on_close: F) -> Self + where + F: 'static + Fn(TabId) -> Message, + { + self.on_close = Some(Box::new(on_close)); self } @@ -278,18 +260,24 @@ where self } - /// Sets the spacing between the tabs of the [`TabBar`]. + /// Pushes a [`TabLabel`](crate::tab_bar::TabLabel) to the [`TabBar`]. #[must_use] - pub fn spacing(mut self, spacing: f32) -> Self { - self.spacing = spacing; + pub fn push(mut self, id: TabId, tab_label: TabLabel) -> Self { + self.tab_labels.push(tab_label); + self.tab_indices.push(id); self } - /// Sets the font of the icons of the - /// [`TabLabel`](crate::tab_bar::TabLabel)s of the [`TabBar`]. + /// Gets the amount of tabs on the [`TabBar`]. #[must_use] - pub fn icon_font(mut self, icon_font: Font) -> Self { - self.icon_font = Some(icon_font); + pub fn size(&self) -> usize { + self.tab_indices.len() + } + + /// Sets the spacing between the tabs of the [`TabBar`]. + #[must_use] + pub fn spacing(mut self, spacing: f32) -> Self { + self.spacing = spacing; self } @@ -301,18 +289,17 @@ where self } - /// Sets the style of the [`TabBar`]. + /// Sets the text size of the [`TabLabel`](crate::tab_bar::TabLabel)s of the [`TabBar`]. #[must_use] - pub fn style(mut self, style: ::Style) -> Self { - self.style = style; + pub fn text_size(mut self, text_size: f32) -> Self { + self.text_size = text_size; self } - /// Pushes a [`TabLabel`](crate::tab_bar::TabLabel) to the [`TabBar`]. + /// Sets the width of a tab on the [`TabBar`]. #[must_use] - pub fn push(mut self, id: TabId, tab_label: TabLabel) -> Self { - self.tab_labels.push(tab_label); - self.tab_indices.push(id); + pub fn tab_width(mut self, width: Length) -> Self { + self.tab_width = width; self } @@ -333,6 +320,20 @@ where self.position = position; self } + + /// Sets the style of the [`TabBar`]. + #[must_use] + pub fn style(mut self, style: ::Style) -> Self { + self.style = style; + self + } + + /// Sets the width of the [`TabBar`]. + #[must_use] + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } } impl Widget for TabBar diff --git a/src/native/tabs.rs b/src/native/tabs.rs index 0aa0ce0c..d0f4fa72 100644 --- a/src/native/tabs.rs +++ b/src/native/tabs.rs @@ -6,6 +6,9 @@ //! //! *This API requires the following crate features to be activated: tabs* +pub mod tab_bar_position; +use crate::{native::tab_bar::TabBar, style::tab_bar::StyleSheet, TabLabel}; + use iced_widget::{ core::{ self, event, @@ -19,9 +22,6 @@ use iced_widget::{ text, Row, }; -use crate::{native::tab_bar::TabBar, style::tab_bar::StyleSheet, TabLabel}; - -pub mod tab_bar_position; pub use tab_bar_position::TabBarPosition; /// A [`Tabs`] widget for showing a [`TabBar`](super::tab_bar::TabBar) @@ -89,7 +89,7 @@ where where F: 'static + Fn(TabId) -> Message, { - Self::with_tabs(Vec::new(), on_select) + Self::new_with_tabs(Vec::new(), on_select) } /// Similar to `new` but with a given Vector of the @@ -101,7 +101,7 @@ where /// [`Element`]s of the [`Tabs`]. /// * the function that will be called if a tab is selected by the user. /// It takes the index of the selected tab. - pub fn with_tabs( + pub fn new_with_tabs( tabs: Vec<(TabId, TabLabel, Element<'a, Message, Renderer>)>, on_select: F, ) -> Self @@ -128,6 +128,39 @@ where } } + /// Sets the size of the close icon of the + /// [`TabLabel`](super::tab_bar::TabLabel) of the + /// [`TabBar`](super::tab_bar::TabBar). + #[must_use] + pub fn close_size(mut self, close_size: f32) -> Self { + self.tab_bar = self.tab_bar.close_size(close_size); + self + } + + /// Sets the height of the [`Tabs`]. + #[must_use] + pub fn height(mut self, height: Length) -> Self { + self.height = height; + self + } + + /// Sets the font of the icons of the + /// [`TabLabel`](super::tab_bar::TabLabel)s of the + /// [`TabBar`](super::tab_bar::TabBar). + #[must_use] + pub fn icon_font(mut self, icon_font: Font) -> Self { + self.tab_bar = self.tab_bar.icon_font(icon_font); + self + } + + /// Sets the icon size of the [`TabLabel`](super::tab_bar::TabLabel) of the + /// [`TabBar`](super::tab_bar::TabBar). + #[must_use] + pub fn icon_size(mut self, icon_size: f32) -> Self { + self.tab_bar = self.tab_bar.icon_size(icon_size); + self + } + /// Sets the message that will be produced when the close icon of a tab /// on the [`TabBar`] is pressed. /// @@ -141,24 +174,23 @@ where self } - /// Sets the width of the [`Tabs`]. - #[must_use] - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self - } - - /// Sets the height of the [`Tabs`]. + /// Pushes a [`TabLabel`](super::tab_bar::TabLabel) along with the tabs + /// content to the [`Tabs`]. #[must_use] - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn push(mut self, id: TabId, tab_label: TabLabel, element: E) -> Self + where + E: Into>, + { + self.tab_bar = self.tab_bar.push(id.clone(), tab_label); + self.tabs.push(element.into()); + self.indices.push(id); self } - /// Sets the width of the [`TabBar`](super::tab_bar::TabBar) of the [`Tabs`]. + /// Sets the active tab of the [`Tabs`] using the ``TabId``. #[must_use] - pub fn tab_bar_width(mut self, width: Length) -> Self { - self.tab_bar = self.tab_bar.width(width); + pub fn set_active_tab(mut self, id: &TabId) -> Self { + self.tab_bar = self.tab_bar.set_active_tab(id); self } @@ -176,28 +208,24 @@ where self } - /// Sets the icon size of the [`TabLabel`](super::tab_bar::TabLabel) of the - /// [`TabBar`](super::tab_bar::TabBar). + /// Sets the width of the [`TabBar`](super::tab_bar::TabBar) of the [`Tabs`]. #[must_use] - pub fn icon_size(mut self, icon_size: f32) -> Self { - self.tab_bar = self.tab_bar.icon_size(icon_size); + pub fn tab_bar_width(mut self, width: Length) -> Self { + self.tab_bar = self.tab_bar.width(width); self } - /// Sets the text size of the [`TabLabel`](super::tab_bar::TabLabel) of the - /// [`TabBar`](super::tab_bar::TabBar). + /// Sets the [`TabBarPosition`] of the [`TabBar`](super::tab_bar::TabBar). #[must_use] - pub fn text_size(mut self, text_size: f32) -> Self { - self.tab_bar = self.tab_bar.text_size(text_size); + pub fn tab_bar_position(mut self, position: TabBarPosition) -> Self { + self.tab_bar_position = position; self } - /// Sets the size of the close icon of the - /// [`TabLabel`](super::tab_bar::TabLabel) of the - /// [`TabBar`](super::tab_bar::TabBar). + /// Sets the style of the [`TabBar`](super::tab_bar::TabBar). #[must_use] - pub fn close_size(mut self, close_size: f32) -> Self { - self.tab_bar = self.tab_bar.close_size(close_size); + pub fn tab_bar_style(mut self, style: ::Style) -> Self { + self.tab_bar = self.tab_bar.style(style); self } @@ -216,15 +244,6 @@ where self } - /// Sets the font of the icons of the - /// [`TabLabel`](super::tab_bar::TabLabel)s of the - /// [`TabBar`](super::tab_bar::TabBar). - #[must_use] - pub fn icon_font(mut self, icon_font: Font) -> Self { - self.tab_bar = self.tab_bar.icon_font(icon_font); - self - } - /// Sets the font of the text of the /// [`TabLabel`](super::tab_bar::TabLabel)s of the /// [`TabBar`](super::tab_bar::TabBar). @@ -234,37 +253,18 @@ where self } - /// Sets the style of the [`TabBar`](super::tab_bar::TabBar). - #[must_use] - pub fn tab_bar_style(mut self, style: ::Style) -> Self { - self.tab_bar = self.tab_bar.style(style); - self - } - - /// Sets the [`TabBarPosition`] of the [`TabBar`](super::tab_bar::TabBar). - #[must_use] - pub fn tab_bar_position(mut self, position: TabBarPosition) -> Self { - self.tab_bar_position = position; - self - } - - /// Pushes a [`TabLabel`](super::tab_bar::TabLabel) along with the tabs - /// content to the [`Tabs`]. + /// Sets the text size of the [`TabLabel`](super::tab_bar::TabLabel) of the + /// [`TabBar`](super::tab_bar::TabBar). #[must_use] - pub fn push(mut self, id: TabId, tab_label: TabLabel, element: E) -> Self - where - E: Into>, - { - self.tab_bar = self.tab_bar.push(id.clone(), tab_label); - self.tabs.push(element.into()); - self.indices.push(id); + pub fn text_size(mut self, text_size: f32) -> Self { + self.tab_bar = self.tab_bar.text_size(text_size); self } - /// Sets the active tab of the [`Tabs`] using the ``TabId``. + /// Sets the width of the [`Tabs`]. #[must_use] - pub fn set_active_tab(mut self, id: &TabId) -> Self { - self.tab_bar = self.tab_bar.set_active_tab(id); + pub fn width(mut self, width: Length) -> Self { + self.width = width; self } } diff --git a/src/native/time_picker.rs b/src/native/time_picker.rs index 85951843..a0dfe3df 100644 --- a/src/native/time_picker.rs +++ b/src/native/time_picker.rs @@ -1,8 +1,10 @@ //! Use a time picker as an input element for picking times. //! //! *This API requires the following crate features to be activated: `time_picker`* -use chrono::Local; +use super::overlay::time_picker::{self, TimePickerOverlay, TimePickerOverlayButtons}; + +use chrono::Local; use iced_widget::{ button, container, core::{ @@ -17,11 +19,10 @@ use iced_widget::{ text, }; -use super::overlay::time_picker::{self, TimePickerOverlay, TimePickerOverlayButtons}; - -pub use crate::core::time::{Period, Time}; - -pub use crate::style::time_picker::{Appearance, StyleSheet}; +pub use crate::{ + core::time::{Period, Time}, + style::time_picker::{Appearance, StyleSheet}, +}; //TODO: Remove ignore when Null is updated. Temp fix for Test runs /// An input element for picking times. @@ -112,13 +113,6 @@ where } } - /// Use 24 hour format instead of AM/PM. - #[must_use] - pub fn use_24h(mut self) -> Self { - self.use_24h = true; - self - } - /// Enables the picker to also pick seconds. #[must_use] pub fn show_seconds(mut self) -> Self { @@ -132,6 +126,13 @@ where self.style = style; self } + + /// Use 24 hour format instead of AM/PM. + #[must_use] + pub fn use_24h(mut self) -> Self { + self.use_24h = true; + self + } } /// The state of the [`TimePicker`] / [`TimePickerOverlay`]. diff --git a/src/native/wrap.rs b/src/native/wrap.rs index 734bb834..e80a4e75 100644 --- a/src/native/wrap.rs +++ b/src/native/wrap.rs @@ -10,7 +10,6 @@ use iced_widget::core::{ Alignment, Clipboard, Element, Event, Layout, Length, Padding, Point, Rectangle, Shell, Size, Widget, }; - use std::marker::PhantomData; /// A container that distributes its contents horizontally. From 6f8ee0f32be6d86444da02190b88b87dda3ba0ce Mon Sep 17 00:00:00 2001 From: Redhawk18 Date: Thu, 14 Sep 2023 15:47:29 -0400 Subject: [PATCH 2/3] resort core --- src/core/clock.rs | 63 +++++++++++++++++++++---------------------- src/core/date.rs | 8 ++---- src/core/overlay.rs | 1 + src/core/time.rs | 2 +- src/graphics/icons.rs | 1 + src/native/tab_bar.rs | 4 +-- 6 files changed, 38 insertions(+), 41 deletions(-) diff --git a/src/core/clock.rs b/src/core/clock.rs index 1a453601..821e45d7 100644 --- a/src/core/clock.rs +++ b/src/core/clock.rs @@ -1,8 +1,7 @@ //! Helper functions for calculating the clock -use std::fmt::Display; - use iced_widget::core::Point; +use std::fmt::Display; /// The size of the period on the clock based on the clock's size. pub const PERIOD_PERCENTAGE: f32 = 0.1; @@ -61,26 +60,26 @@ pub enum NearestRadius { Second, } -/// # Panics -/// Determining the nearest radius to the position of the cursor position based -/// on the distance to the center. -/// Will panic if distance vec can not compare a and b +/// Distributes the amount of points on a circle with the given radius around the +/// center. #[must_use] -pub fn nearest_radius( - radii: &[(f32, NearestRadius)], - cursor_position: Point, - center: Point, -) -> NearestRadius { - let distance = cursor_position.distance(center); +pub fn circle_points(distance_radius: f32, center: Point, amount: u16) -> Vec { + let part = std::f32::consts::TAU / f32::from(amount); - let mut distance_vec: Vec<(f32, &NearestRadius)> = radii + let rotation = + |(x, y): (f32, f32), t: f32| (x * t.cos() - y * t.sin(), x * t.sin() + y * t.cos()); + + let points: Vec<(f32, f32)> = (0..amount).fold(Vec::new(), |mut v, i| { + v.push(rotation((0.0, -distance_radius), part * f32::from(i))); + v + }); + + let points: Vec = points .iter() - .map(|(r, n)| ((r - distance).abs(), n)) + .map(|p| Point::new(p.0 + center.x, p.1 + center.y)) .collect(); - distance_vec.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("Should be comparable")); - - distance_vec[0].1.clone() + points } /// # Panics @@ -100,26 +99,26 @@ pub fn nearest_point(points: &[Point], cursor_position: Point) -> usize { distance_vec[0].0 } -/// Distributes the amount of points on a circle with the given radius around the -/// center. +/// # Panics +/// Determining the nearest radius to the position of the cursor position based +/// on the distance to the center. +/// Will panic if distance vec can not compare a and b #[must_use] -pub fn circle_points(distance_radius: f32, center: Point, amount: u16) -> Vec { - let part = std::f32::consts::TAU / f32::from(amount); - - let rotation = - |(x, y): (f32, f32), t: f32| (x * t.cos() - y * t.sin(), x * t.sin() + y * t.cos()); - - let points: Vec<(f32, f32)> = (0..amount).fold(Vec::new(), |mut v, i| { - v.push(rotation((0.0, -distance_radius), part * f32::from(i))); - v - }); +pub fn nearest_radius( + radii: &[(f32, NearestRadius)], + cursor_position: Point, + center: Point, +) -> NearestRadius { + let distance = cursor_position.distance(center); - let points: Vec = points + let mut distance_vec: Vec<(f32, &NearestRadius)> = radii .iter() - .map(|p| Point::new(p.0 + center.x, p.1 + center.y)) + .map(|(r, n)| ((r - distance).abs(), n)) .collect(); - points + distance_vec.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("Should be comparable")); + + distance_vec[0].1.clone() } #[cfg(test)] diff --git a/src/core/date.rs b/src/core/date.rs index 6e8bfd3b..40892d9e 100644 --- a/src/core/date.rs +++ b/src/core/date.rs @@ -1,12 +1,8 @@ //! Helper functions for calculating dates -use std::fmt::Display; - -use chrono::Local; - -use chrono::{Datelike, Duration, NaiveDate}; - +use chrono::{Local, Datelike, Duration, NaiveDate}; use once_cell::sync::Lazy; +use std::fmt::Display; /// The date value #[derive(Clone, Copy, Debug, Default)] diff --git a/src/core/overlay.rs b/src/core/overlay.rs index 7ff3d1e6..c62806e9 100644 --- a/src/core/overlay.rs +++ b/src/core/overlay.rs @@ -1,4 +1,5 @@ //! Helper functions for overlays + use iced_widget::core::{layout, Point, Size}; /// Trait containing functions for positioning of nodes. diff --git a/src/core/time.rs b/src/core/time.rs index 61abeace..32e79ae3 100644 --- a/src/core/time.rs +++ b/src/core/time.rs @@ -1,9 +1,9 @@ //! Use a time picker as an input element for picking times. //! //! *This API requires the following crate features to be activated: `time_picker`* -use std::fmt::Display; use chrono::{Local, Timelike}; +use std::fmt::Display; /// The time value #[derive(Clone, Copy, Debug)] diff --git a/src/graphics/icons.rs b/src/graphics/icons.rs index 0123922b..72df9aa1 100644 --- a/src/graphics/icons.rs +++ b/src/graphics/icons.rs @@ -1,4 +1,5 @@ //! The default icon font of the widgets of this library. + use iced_widget::core::Font; #[cfg(feature = "icons")] diff --git a/src/native/tab_bar.rs b/src/native/tab_bar.rs index a094713c..621cbb72 100644 --- a/src/native/tab_bar.rs +++ b/src/native/tab_bar.rs @@ -118,11 +118,11 @@ pub enum Position { Top, /// Icon is placed right of the text. Right, + /// Icon is placed below of the text. + Bottom, #[default] /// Icon is placed left of the text, the default. Left, - /// Icon is placed below of the text. - Bottom, } impl TabBar From 1675bbce53a65ec3ad48ca3dd4f5c706655aa7bc Mon Sep 17 00:00:00 2001 From: Redhawk18 Date: Thu, 14 Sep 2023 15:49:41 -0400 Subject: [PATCH 3/3] run `cargo fmt` on the wrong project --- src/core/date.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/date.rs b/src/core/date.rs index 40892d9e..96f75402 100644 --- a/src/core/date.rs +++ b/src/core/date.rs @@ -1,6 +1,6 @@ //! Helper functions for calculating dates -use chrono::{Local, Datelike, Duration, NaiveDate}; +use chrono::{Datelike, Duration, Local, NaiveDate}; use once_cell::sync::Lazy; use std::fmt::Display;