From 3ae313f774949dc61846d01552288f867de96cb8 Mon Sep 17 00:00:00 2001 From: Ultraxime Date: Mon, 26 Aug 2024 15:03:05 +0200 Subject: [PATCH] Replacing f32 by iced::Padding and iced::Pixels when possible --- Cargo.lock | 8 ++-- examples/time_picker/Cargo.toml | 2 +- src/widgets/card.rs | 8 ++-- src/widgets/grid/types.rs | 2 +- src/widgets/helpers.rs | 4 +- src/widgets/menu/flex.rs | 8 ++-- src/widgets/menu/menu_bar.rs | 10 ++--- src/widgets/menu/menu_tree.rs | 9 ++-- src/widgets/number_input.rs | 6 +-- src/widgets/overlay/color_picker.rs | 67 +++++++++++++---------------- src/widgets/overlay/date_picker.rs | 36 +++++++++------- src/widgets/overlay/time_picker.rs | 40 ++++++++--------- src/widgets/selection_list.rs | 16 ++++--- src/widgets/selection_list/list.rs | 16 ++++--- src/widgets/sidebar/column.rs | 8 ++-- src/widgets/sidebar/row.rs | 8 ++-- src/widgets/sidebar/sidebar.rs | 24 +++++------ src/widgets/tab_bar.rs | 25 ++++++----- src/widgets/tabs.rs | 6 +-- src/widgets/typed_input.rs | 2 +- src/widgets/wrap.rs | 38 ++++++++-------- 21 files changed, 175 insertions(+), 168 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4fc7dd5d..0528d017 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2946,9 +2946,9 @@ dependencies = [ [[package]] name = "rgb" -version = "0.8.45" +version = "0.8.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ade4539f42266ded9e755c605bdddf546242b2c961b03b06a7375260788a0523" +checksum = "0f86ae463694029097b846d8f99fd5536740602ae00022c0c50c5600720b2f71" dependencies = [ "bytemuck", ] @@ -3339,9 +3339,9 @@ checksum = "20e16a0f46cf5fd675563ef54f26e83e20f2366bcf027bcb3cc3ed2b98aaf2ca" [[package]] name = "svgtypes" -version = "0.15.1" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fae3064df9b89391c9a76a0425a69d124aee9c5c28455204709e72c39868a43c" +checksum = "794de53cc48eaabeed0ab6a3404a65f40b3e38c067e4435883a65d2aa4ca000e" dependencies = [ "kurbo 0.11.0", "siphasher 1.0.1", diff --git a/examples/time_picker/Cargo.toml b/examples/time_picker/Cargo.toml index cd3608de..9dca88f8 100644 --- a/examples/time_picker/Cargo.toml +++ b/examples/time_picker/Cargo.toml @@ -10,4 +10,4 @@ edition = "2021" iced_aw = { workspace = true, features = [ "time_picker", ] } -iced.workspace = true \ No newline at end of file +iced.workspace = true diff --git a/src/widgets/card.rs b/src/widgets/card.rs index b928e987..d78cced0 100644 --- a/src/widgets/card.rs +++ b/src/widgets/card.rs @@ -25,7 +25,7 @@ pub use crate::style::{ }; /// The default padding of a [`Card`]. -const DEFAULT_PADDING: f32 = 10.0; +const DEFAULT_PADDING: Padding = Padding::new(10.0); /// A card consisting of a head, body and optional foot. /// @@ -101,9 +101,9 @@ where height: Length::Shrink, max_width: u32::MAX as f32, max_height: u32::MAX as f32, - padding_head: DEFAULT_PADDING.into(), - padding_body: DEFAULT_PADDING.into(), - padding_foot: DEFAULT_PADDING.into(), + padding_head: DEFAULT_PADDING, + padding_body: DEFAULT_PADDING, + padding_foot: DEFAULT_PADDING, close_size: None, on_close: None, head: head.into(), diff --git a/src/widgets/grid/types.rs b/src/widgets/grid/types.rs index 5d75f3df..5cd6ba56 100644 --- a/src/widgets/grid/types.rs +++ b/src/widgets/grid/types.rs @@ -86,7 +86,7 @@ where /// Sets the spacing between rows and columns. To set row and column spacing separately, use /// [`Self::column_spacing()`] and [`Self::row_spacing()`]. #[must_use] - pub fn spacing(mut self, spacing: f32) -> Self { + pub fn spacing(mut self, spacing: impl Into) -> Self { let spacing: Pixels = spacing.into(); self.row_spacing = spacing; self.column_spacing = spacing; diff --git a/src/widgets/helpers.rs b/src/widgets/helpers.rs index bcc1eaaf..07fa125a 100644 --- a/src/widgets/helpers.rs +++ b/src/widgets/helpers.rs @@ -4,7 +4,7 @@ #[cfg(feature = "selection_list")] use crate::style::{Status, StyleFn}; #[allow(unused_imports)] -use iced::{self, advanced::renderer, Color, Element}; +use iced::{self, advanced::renderer, Color, Element, Padding}; #[cfg(feature = "number_input")] use num_traits::bounds::Bounded; @@ -357,7 +357,7 @@ pub fn selection_list_with<'a, T, Message, Theme, Renderer>( options: &'a [T], on_selected: impl Fn(usize, T) -> Message + 'static, text_size: f32, - padding: f32, + padding: impl Into, style: impl Fn(&Theme, Status) -> crate::style::selection_list::Style + 'a + Clone, selected: Option, font: iced::Font, diff --git a/src/widgets/menu/flex.rs b/src/widgets/menu/flex.rs index 2b5a79c7..5438aed8 100644 --- a/src/widgets/menu/flex.rs +++ b/src/widgets/menu/flex.rs @@ -26,7 +26,7 @@ use iced::{ layout::{Limits, Node}, renderer, widget, }, - Alignment, Element, Length, Padding, Point, Size, + Alignment, Element, Length, Padding, Pixels, Point, Size, }; /// The main axis of a flex layout. @@ -73,7 +73,7 @@ pub fn resolve<'a, E, T, Message, Theme, Renderer>( width: Length, height: Length, padding: Padding, - spacing: f32, + spacing: Pixels, align_items: Alignment, items: &[E], trees: &mut [T], @@ -100,7 +100,7 @@ where }, }; - let mut available = axis.main(limits.max()) - total_spacing; + let mut available = axis.main(limits.max()) - total_spacing.0; let mut nodes: Vec = Vec::with_capacity(items.len()); nodes.resize(items.len(), Node::default()); @@ -218,7 +218,7 @@ where for (i, node) in nodes.iter_mut().enumerate() { if i > 0 { - main += spacing; + main += spacing.0; } let (x, y) = axis.pack(main, pad.1); diff --git a/src/widgets/menu/menu_bar.rs b/src/widgets/menu/menu_bar.rs index 0e06505c..4d5b968f 100644 --- a/src/widgets/menu/menu_bar.rs +++ b/src/widgets/menu/menu_bar.rs @@ -12,7 +12,7 @@ use iced::{ widget::{tree, Operation, Tree}, Clipboard, Layout, Shell, Widget, }, - alignment, event, Element, Event, Length, Padding, Rectangle, Size, + alignment, event, Element, Event, Length, Padding, Pixels, Rectangle, Size, }; use super::{common::*, flex, menu_bar_overlay::MenuBarOverlay, menu_tree::*}; @@ -34,7 +34,7 @@ where Renderer: renderer::Renderer, { roots: Vec>, - spacing: f32, + spacing: Pixels, padding: Padding, width: Length, height: Length, @@ -58,7 +58,7 @@ where Self { roots, - spacing: 0.0, + spacing: Pixels::ZERO, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -85,8 +85,8 @@ where } /// Sets the spacing of the [`MenuBar`]. - pub fn spacing(mut self, spacing: f32) -> Self { - self.spacing = spacing; + pub fn spacing(mut self, spacing: impl Into) -> Self { + self.spacing = spacing.into(); self } diff --git a/src/widgets/menu/menu_tree.rs b/src/widgets/menu/menu_tree.rs index c21d06d4..ecb5a65e 100644 --- a/src/widgets/menu/menu_tree.rs +++ b/src/widgets/menu/menu_tree.rs @@ -14,6 +14,7 @@ use super::common::*; use super::flex; use iced::advanced::overlay::Group; use iced::advanced::widget::Operation; +use iced::Pixels; use iced::{ advanced::{ layout::{Layout, Limits, Node}, @@ -89,7 +90,7 @@ where Renderer: renderer::Renderer, { pub(super) items: Vec>, - pub(super) spacing: f32, + pub(super) spacing: Pixels, pub(super) max_width: f32, pub(super) width: Length, pub(super) height: Length, @@ -105,7 +106,7 @@ where pub fn new(items: Vec>) -> Self { Self { items, - spacing: 0.0, + spacing: Pixels::ZERO, max_width: f32::MAX, width: Length::Fill, height: Length::Shrink, @@ -127,8 +128,8 @@ where } /// Sets the spacing of the [`Menu`]. - pub fn spacing(mut self, spacing: f32) -> Self { - self.spacing = spacing; + pub fn spacing(mut self, spacing: impl Into) -> Self { + self.spacing = spacing.into(); self } diff --git a/src/widgets/number_input.rs b/src/widgets/number_input.rs index 841dc2d1..5df21630 100644 --- a/src/widgets/number_input.rs +++ b/src/widgets/number_input.rs @@ -40,7 +40,7 @@ pub use crate::{ }; /// The default padding -const DEFAULT_PADDING: f32 = 5.0; +const DEFAULT_PADDING: Padding = Padding::new(5.0); /// A field that can only be filled with numeric type. /// @@ -116,7 +116,7 @@ where F: 'static + Fn(T) -> Message + Copy, T: 'static, { - let padding = DEFAULT_PADDING.into(); + let padding = DEFAULT_PADDING; Self { value, @@ -356,7 +356,7 @@ where .center_x(Length::Shrink) }; - let default_padding = Padding::from(DEFAULT_PADDING); + let default_padding = DEFAULT_PADDING; let element = if self.padding.top < default_padding.top || self.padding.bottom < default_padding.bottom diff --git a/src/widgets/overlay/color_picker.rs b/src/widgets/overlay/color_picker.rs index 2adac675..55960522 100644 --- a/src/widgets/overlay/color_picker.rs +++ b/src/widgets/overlay/color_picker.rs @@ -22,36 +22,24 @@ use iced::{ Clipboard, Layout, Overlay, Renderer as _, Shell, Text, Widget, }, alignment::{Horizontal, Vertical}, - event, - keyboard, + event, keyboard, mouse::{self, Cursor}, touch, widget::{ canvas::{self, LineCap, Path, Stroke}, text, Button, Column, Row, }, - Alignment, - Border, - Color, - Element, - Event, - Length, - Padding, - Point, - Rectangle, - Renderer, // the actual type - Shadow, - Size, - Vector, + Alignment, Border, Color, Element, Event, Length, Padding, Pixels, Point, Rectangle, Renderer, + Shadow, Size, Vector, }; use std::collections::HashMap; /// The padding around the elements. -const PADDING: f32 = 10.0; +const PADDING: Padding = Padding::new(10.0); /// The spacing between the element. -const SPACING: f32 = 15.0; +const SPACING: Pixels = Pixels(15.0); /// The spacing between the buttons. -const BUTTON_SPACING: f32 = 5.0; +const BUTTON_SPACING: Pixels = Pixels(5.0); /// The step value of the keyboard change of the sat/value color values. const SAT_VALUE_STEP: f32 = 0.005; @@ -564,7 +552,7 @@ where }; let limits = Limits::new(Size::ZERO, bounds) - .shrink(Padding::from(PADDING)) + .shrink(PADDING) .width(Length::Fill) .height(Length::Fill) .max_width(max_width) @@ -603,13 +591,13 @@ where let (width, height) = if bounds.width > bounds.height { ( - block1_node.size().width + block2_node.size().width + SPACING, // + (2.0 * PADDING as f32), + block1_node.size().width + block2_node.size().width + SPACING.0, // + (2.0 * PADDING as f32), block2_node.size().height, ) } else { ( block2_node.size().width, - block1_node.size().height + block2_node.size().height + SPACING, + block1_node.size().height + block2_node.size().height + SPACING.0, ) }; @@ -912,7 +900,7 @@ where .height(Length::Fill); let block1_node = Column::<(), Theme, Renderer>::new() - .spacing(PADDING) + .spacing(PADDING.vertical() / 2.) // Average vertical padding .push( Row::new() .width(Length::Fill) @@ -925,7 +913,7 @@ where ) .layout(color_picker.tree, renderer, &block1_limits); - block1_node.move_to(Point::new(bounds.x + PADDING, bounds.y + PADDING)) + block1_node.move_to(Point::new(bounds.x + PADDING.left, bounds.y + PADDING.top)) } /// Defines the layout of the 2. block of the color picker containing the RGBA part, Hex and buttons. @@ -957,12 +945,14 @@ where let mut hex_text_layout = Row::::new() .width(Length::Fill) - .height(Length::Fixed(renderer.default_size().0 + 2.0 * PADDING)) + .height(Length::Fixed( + renderer.default_size().0 + PADDING.vertical(), + )) .layout(color_picker.tree, renderer, &hex_text_limits); let block2_limits = block2_limits.shrink(Size::new( 0.0, - cancel_button.bounds().height + hex_text_layout.bounds().height + 2.0 * SPACING, + cancel_button.bounds().height + hex_text_layout.bounds().height + 2.0 * SPACING.0, )); // RGBA Colors @@ -1008,20 +998,23 @@ where .layout(rgba_tree, renderer, &block2_limits); let rgba_bounds = rgba_colors.bounds(); - rgba_colors = rgba_colors.move_to(Point::new(rgba_bounds.x + PADDING, rgba_bounds.y + PADDING)); + rgba_colors = rgba_colors.move_to(Point::new( + rgba_bounds.x + PADDING.left, + rgba_bounds.y + PADDING.top, + )); let rgba_bounds = rgba_colors.bounds(); // Hex text let hex_bounds = hex_text_layout.bounds(); hex_text_layout = hex_text_layout.move_to(Point::new( - hex_bounds.x + PADDING, - hex_bounds.y + rgba_bounds.height + PADDING + SPACING, + hex_bounds.x + PADDING.left, + hex_bounds.y + rgba_bounds.height + PADDING.top + SPACING.0, )); let hex_bounds = hex_text_layout.bounds(); // Buttons let cancel_limits = - block2_limits.max_width(((rgba_bounds.width / 2.0) - BUTTON_SPACING).max(0.0)); + block2_limits.max_width(((rgba_bounds.width / 2.0) - BUTTON_SPACING.0).max(0.0)); let mut cancel_button = color_picker.cancel_button.layout( &mut color_picker.tree.children[0], @@ -1030,7 +1023,7 @@ where ); let submit_limits = - block2_limits.max_width(((rgba_bounds.width / 2.0) - BUTTON_SPACING).max(0.0)); + block2_limits.max_width(((rgba_bounds.width / 2.0) - BUTTON_SPACING.0).max(0.0)); let mut submit_button = color_picker.submit_button.layout( &mut color_picker.tree.children[1], @@ -1040,25 +1033,25 @@ where let cancel_bounds = cancel_button.bounds(); cancel_button = cancel_button.move_to(Point::new( - cancel_bounds.x + PADDING, - cancel_bounds.y + rgba_bounds.height + hex_bounds.height + PADDING + 2.0 * SPACING, + cancel_bounds.x + PADDING.left, + cancel_bounds.y + rgba_bounds.height + hex_bounds.height + PADDING.top + 2.0 * SPACING.0, )); let cancel_bounds = cancel_button.bounds(); let submit_bounds = submit_button.bounds(); submit_button = submit_button.move_to(Point::new( - submit_bounds.x + rgba_colors.bounds().width - submit_bounds.width + PADDING, - submit_bounds.y + rgba_bounds.height + hex_bounds.height + PADDING + 2.0 * SPACING, + submit_bounds.x + rgba_colors.bounds().width - submit_bounds.width + PADDING.left, + submit_bounds.y + rgba_bounds.height + hex_bounds.height + PADDING.top + 2.0 * SPACING.0, )); Node::with_children( Size::new( - rgba_bounds.width + (2.0 * PADDING), + rgba_bounds.width + PADDING.horizontal(), rgba_bounds.height + hex_bounds.height + cancel_bounds.height - + (2.0 * PADDING) - + (2.0 * SPACING), + + PADDING.vertical() + + (2.0 * SPACING.0), ), vec![rgba_colors, hex_text_layout, cancel_button, submit_button], ) diff --git a/src/widgets/overlay/date_picker.rs b/src/widgets/overlay/date_picker.rs index 266305c3..04225035 100644 --- a/src/widgets/overlay/date_picker.rs +++ b/src/widgets/overlay/date_picker.rs @@ -44,13 +44,13 @@ use iced::{ use std::collections::HashMap; /// The padding around the elements. -const PADDING: f32 = 10.0; +const PADDING: Padding = Padding::new(10.0); /// The spacing between the elements. -const SPACING: f32 = 15.0; +const SPACING: Pixels = Pixels(15.0); /// The padding of the day cells. -const DAY_CELL_PADDING: f32 = 7.0; +const DAY_CELL_PADDING: Padding = Padding::new(7.0); /// The spacing between the buttons. -const BUTTON_SPACING: f32 = 5.0; +const BUTTON_SPACING: Pixels = Pixels(5.0); /// The overlay of the [`DatePicker`](crate::widgets::DatePicker). #[allow(missing_debug_implementations)] @@ -377,7 +377,7 @@ where #[allow(clippy::too_many_lines)] fn layout(&mut self, renderer: &Renderer, bounds: Size) -> Node { let limits = Limits::new(Size::ZERO, bounds) - .shrink(Padding::from(PADDING)) + .shrink(PADDING) .width(Length::Shrink) .height(Length::Shrink); @@ -387,7 +387,7 @@ where self.cancel_button .layout(&mut self.tree.children[0], renderer, &cancel_limits); - let limits = limits.shrink(Size::new(0.0, cancel_button.bounds().height + SPACING)); + let limits = limits.shrink(Size::new(0.0, cancel_button.bounds().height + SPACING.0)); // Month/Year let font_size = self.font_size; @@ -499,18 +499,21 @@ where let mut col = element.as_widget().layout(col_tree, renderer, &limits); let col_bounds = col.bounds(); - col = col.move_to(Point::new(col_bounds.x + PADDING, col_bounds.y + PADDING)); + col = col.move_to(Point::new( + col_bounds.x + PADDING.left, + col_bounds.y + PADDING.top, + )); // Buttons let cancel_limits = - limits.max_width(((col.bounds().width / 2.0) - BUTTON_SPACING).max(0.0)); + limits.max_width(((col.bounds().width / 2.0) - BUTTON_SPACING.0).max(0.0)); let mut cancel_button = self.cancel_button .layout(&mut self.tree.children[0], renderer, &cancel_limits); let submit_limits = - limits.max_width(((col.bounds().width / 2.0) - BUTTON_SPACING).max(0.0)); + limits.max_width(((col.bounds().width / 2.0) - BUTTON_SPACING.0).max(0.0)); let mut submit_button = self.submit_button @@ -518,20 +521,23 @@ where let cancel_bounds = cancel_button.bounds(); cancel_button = cancel_button.move_to(Point { - x: cancel_bounds.x + PADDING, - y: cancel_bounds.y + col.bounds().height + PADDING + SPACING, + x: cancel_bounds.x + PADDING.left, + y: cancel_bounds.y + col.bounds().height + PADDING.top + SPACING.0, }); let submit_bounds = submit_button.bounds(); submit_button = submit_button.move_to(Point { - x: submit_bounds.x + col.bounds().width - submit_bounds.width + PADDING, - y: submit_bounds.y + col.bounds().height + PADDING + SPACING, + x: submit_bounds.x + col.bounds().width - submit_bounds.width + PADDING.left, + y: submit_bounds.y + col.bounds().height + PADDING.top + SPACING.0, }); let mut node = Node::with_children( Size::new( - col.bounds().width + (2.0 * PADDING), - col.bounds().height + cancel_button.bounds().height + (2.0 * PADDING) + SPACING, + col.bounds().width + PADDING.horizontal(), + col.bounds().height + + cancel_button.bounds().height + + PADDING.vertical() + + SPACING.0, ), vec![col, cancel_button, submit_button], ); diff --git a/src/widgets/overlay/time_picker.rs b/src/widgets/overlay/time_picker.rs index f23b6395..ec8e914a 100644 --- a/src/widgets/overlay/time_picker.rs +++ b/src/widgets/overlay/time_picker.rs @@ -59,11 +59,11 @@ use iced::{ use std::collections::HashMap; /// The padding around the elements. -const PADDING: f32 = 10.0; +const PADDING: Padding = Padding::new(10.0); /// The spacing between the elements. -const SPACING: f32 = 15.0; +const SPACING: Pixels = Pixels(15.0); /// The spacing between the buttons. -const BUTTON_SPACING: f32 = 5.0; +const BUTTON_SPACING: Pixels = Pixels(5.0); /// The percentage size of the numbers. const NUMBER_SIZE_PERCENTAGE: f32 = 0.15; /// The percentage size of the period. @@ -516,7 +516,7 @@ where { fn layout(&mut self, renderer: &Renderer, bounds: Size) -> Node { let limits = Limits::new(Size::ZERO, bounds) - .shrink(Padding::from(PADDING)) + .shrink(PADDING) .width(Length::Fill) .height(Length::Fill) .max_width(300.0) @@ -534,7 +534,7 @@ where let limits = limits.shrink(Size::new( 0.0, - digital_clock.bounds().height + cancel_button.bounds().height + 2.0 * SPACING, + digital_clock.bounds().height + cancel_button.bounds().height + 2.0 * SPACING.0, )); // Clock-Canvas @@ -545,26 +545,26 @@ where let clock_bounds = clock.bounds(); clock = clock.move_to(Point::new( - clock_bounds.x + PADDING, - clock_bounds.y + PADDING, + clock_bounds.x + PADDING.left, + clock_bounds.y + PADDING.top, )); let digital_bounds = digital_clock.bounds(); digital_clock = digital_clock.move_to(Point::new( - digital_bounds.x + PADDING, - digital_bounds.y + PADDING + SPACING + clock.bounds().height, + digital_bounds.x + PADDING.left, + digital_bounds.y + PADDING.top + SPACING.0 + clock.bounds().height, )); // Buttons let cancel_limits = - limits.max_width(((clock.bounds().width / 2.0) - BUTTON_SPACING).max(0.0)); + limits.max_width(((clock.bounds().width / 2.0) - BUTTON_SPACING.0).max(0.0)); let mut cancel_button = self.cancel_button .layout(&mut self.tree.children[0], renderer, &cancel_limits); let submit_limits = - limits.max_width(((clock.bounds().width / 2.0) - BUTTON_SPACING).max(0.0)); + limits.max_width(((clock.bounds().width / 2.0) - BUTTON_SPACING.0).max(0.0)); let mut submit_button = self.submit_button @@ -572,32 +572,32 @@ where let cancel_bounds = cancel_button.bounds(); cancel_button = cancel_button.move_to(Point { - x: cancel_bounds.x + PADDING, + x: cancel_bounds.x + PADDING.left, y: cancel_bounds.y + clock.bounds().height - + PADDING + + PADDING.top + digital_clock.bounds().height - + 2.0 * SPACING, + + 2.0 * SPACING.0, }); let submit_bounds = submit_button.bounds(); submit_button = submit_button.move_to(Point { - x: submit_bounds.x + clock.bounds().width - submit_bounds.width + PADDING, + x: submit_bounds.x + clock.bounds().width - submit_bounds.width + PADDING.left, y: submit_bounds.y + clock.bounds().height - + PADDING + + PADDING.top + digital_clock.bounds().height - + 2.0 * SPACING, + + 2.0 * SPACING.0, }); let mut node = Node::with_children( Size::new( - clock.bounds().width + (2.0 * PADDING), + clock.bounds().width + PADDING.horizontal(), clock.bounds().height + digital_clock.bounds().height + cancel_button.bounds().height - + (2.0 * PADDING) - + 2.0 * SPACING, + + PADDING.vertical() + + 2.0 * SPACING.0, ), vec![clock, digital_clock, cancel_button, submit_button], ); diff --git a/src/widgets/selection_list.rs b/src/widgets/selection_list.rs index a509fd73..27ee8082 100644 --- a/src/widgets/selection_list.rs +++ b/src/widgets/selection_list.rs @@ -21,7 +21,7 @@ use iced::{ text::{self, LineHeight}, Container, Scrollable, }, - Border, Element, Event, Font, Length, Pixels, Rectangle, Shadow, Size, + Border, Element, Event, Font, Length, Padding, Pixels, Rectangle, Shadow, Size, }; use std::{fmt::Display, hash::Hash, marker::PhantomData}; @@ -48,7 +48,7 @@ where /// The Containers height height: Length, /// The padding Width - padding: f32, + padding: Padding, /// The Text Size text_size: f32, /// Style for Looks @@ -73,7 +73,7 @@ where options, font: Font::default(), text_size: 12.0, - padding: 5.0, + padding: 5.0.into(), class: ::default(), on_selected: Box::new(on_selected), selected: None, @@ -88,7 +88,7 @@ where container, width: Length::Fill, height: Length::Fill, - padding: 5.0, + padding: 5.0.into(), text_size: 12.0, } } @@ -100,7 +100,7 @@ where options: &'a [T], on_selected: impl Fn(usize, T) -> Message + 'static, text_size: f32, - padding: f32, + padding: impl Into, style: impl Fn(&Theme, Status) -> Style + 'a + Clone, selected: Option, font: Font, @@ -113,6 +113,8 @@ where let class2: ::Class<'a> = (Box::new(style) as StyleFn<'a, Theme, Style>).into(); + let padding = padding.into(); + let container = Container::new(Scrollable::new(List { options, font, @@ -230,14 +232,14 @@ where }; state.values[id].update(text); - state.values[id].min_bounds().width.round() as u32 + self.padding as u32 * 2 + (state.values[id].min_bounds().width + self.padding.horizontal()).round() as u32 }) .max() .unwrap_or(100), _ => limits.max().width as u32, }; - let limits = limits.max_width(max_width as f32 + self.padding * 2.0); + let limits = limits.max_width(max_width as f32 + self.padding.horizontal()); let content = self .container diff --git a/src/widgets/selection_list/list.rs b/src/widgets/selection_list/list.rs index 045aa7ec..3ac5ca49 100644 --- a/src/widgets/selection_list/list.rs +++ b/src/widgets/selection_list/list.rs @@ -17,7 +17,7 @@ use iced::{ mouse::{self, Cursor}, touch, widget::text::LineHeight, - Border, Color, Element, Event, Length, Pixels, Point, Rectangle, Shadow, Size, + Border, Color, Element, Event, Length, Padding, Pixels, Point, Rectangle, Shadow, Size, }; use std::{ collections::hash_map::DefaultHasher, @@ -45,7 +45,7 @@ where /// Function Pointer On Select to call on Mouse button press. pub on_selected: Box Message>, /// The padding Width - pub padding: f32, + pub padding: Padding, /// The Text Size pub text_size: f32, /// Set the Selected ID manually. @@ -119,7 +119,7 @@ where #[allow(clippy::cast_precision_loss)] let intrinsic = Size::new( limits.max().width, - (self.text_size + self.padding * 2.0) * self.options.len() as f32, + (self.text_size + self.padding.vertical()) * self.options.len() as f32, ); Node::new(intrinsic) @@ -145,13 +145,15 @@ where match event { Event::Mouse(mouse::Event::CursorMoved { .. }) => { list_state.hovered_option = Some( - ((cursor.y - bounds.y) / (self.text_size + (self.padding * 2.0))) as usize, + ((cursor.y - bounds.y) / (self.text_size + self.padding.vertical())) + as usize, ); } Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) | Event::Touch(touch::Event::FingerPressed { .. }) => { list_state.hovered_option = Some( - ((cursor.y - bounds.y) / (self.text_size + (self.padding * 2.0))) as usize, + ((cursor.y - bounds.y) / (self.text_size + self.padding.vertical())) + as usize, ); if let Some(index) = list_state.hovered_option { @@ -211,7 +213,7 @@ where use std::f32; let bounds = layout.bounds(); - let option_height = self.text_size + (self.padding * 2.0); + let option_height = self.text_size + self.padding.vertical(); let offset = viewport.y - bounds.y; let start = (offset / option_height) as usize; let end = ((offset + viewport.height) / option_height).ceil() as usize; @@ -225,7 +227,7 @@ where x: bounds.x, y: bounds.y + option_height * i as f32, width: bounds.width, - height: self.text_size + (self.padding * 2.0), + height: self.text_size + self.padding.vertical(), }; if (is_selected || is_hovered) && (bounds.width > 0.) && (bounds.height > 0.) { diff --git a/src/widgets/sidebar/column.rs b/src/widgets/sidebar/column.rs index 016de2c7..539b93ae 100644 --- a/src/widgets/sidebar/column.rs +++ b/src/widgets/sidebar/column.rs @@ -21,7 +21,7 @@ use iced::{ /// A container that distributes its contents vertically. #[allow(missing_debug_implementations)] pub struct FlushColumn<'a, Message, Theme = iced::Theme, Renderer = iced::Renderer> { - spacing: f32, + spacing: Pixels, padding: Padding, width: Length, height: Length, @@ -71,7 +71,7 @@ where .map(|x| Element::into(x.into())) .collect(); Self { - spacing: 0.0, + spacing: Pixels::ZERO, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -90,7 +90,7 @@ where /// elements consistent. #[must_use] pub fn spacing(mut self, amount: impl Into) -> Self { - self.spacing = amount.into().0; + self.spacing = amount.into(); self } @@ -229,7 +229,7 @@ where self.width, self.height, self.padding, - self.spacing, + self.spacing.0, self.align, &self.children, &mut tree.children, diff --git a/src/widgets/sidebar/row.rs b/src/widgets/sidebar/row.rs index b92a1522..5e7ee6bf 100644 --- a/src/widgets/sidebar/row.rs +++ b/src/widgets/sidebar/row.rs @@ -21,7 +21,7 @@ use iced::{ /// A container that distributes its contents horizontally. #[allow(missing_debug_implementations)] pub struct FlushRow<'a, Message, Theme = iced::Theme, Renderer = iced::Renderer> { - spacing: f32, + spacing: Pixels, padding: Padding, width: Length, height: Length, @@ -71,7 +71,7 @@ where .map(|x| Element::into(x.into())) .collect(); Self { - spacing: 0.0, + spacing: Pixels::ZERO, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -90,7 +90,7 @@ where /// elements consistent. #[must_use] pub fn spacing(mut self, amount: impl Into) -> Self { - self.spacing = amount.into().0; + self.spacing = amount.into(); self } @@ -232,7 +232,7 @@ where self.width, self.height, self.padding, - self.spacing, + self.spacing.0, self.align, &self.children, &mut tree.children, diff --git a/src/widgets/sidebar/sidebar.rs b/src/widgets/sidebar/sidebar.rs index 7a298cea..9cc12292 100644 --- a/src/widgets/sidebar/sidebar.rs +++ b/src/widgets/sidebar/sidebar.rs @@ -33,8 +33,8 @@ use iced::{ text::{self, LineHeight}, Row, Text, }, - Alignment, Background, Border, Color, Element, Event, Font, Length, Pixels, Point, Rectangle, - Shadow, Size, Vector, + Alignment, Background, Border, Color, Element, Event, Font, Length, Padding, Pixels, Point, + Rectangle, Shadow, Size, Vector, }; use std::marker::PhantomData; @@ -45,9 +45,9 @@ const DEFAULT_TEXT_SIZE: f32 = 16.0; /// The default size of the close icon. const DEFAULT_CLOSE_SIZE: f32 = 16.0; /// The default padding between the tabs. -const DEFAULT_PADDING: f32 = 1.0; +const DEFAULT_PADDING: Padding = Padding::new(1.0); /// The default spacing around the tabs. -const DEFAULT_SPACING: f32 = 0.0; +const DEFAULT_SPACING: Pixels = Pixels::ZERO; /// A [`TabLabel`] showing an icon and/or a text on a tab /// on a [`TabBar`](super::TabBar). @@ -139,9 +139,9 @@ where // The size of the close icon. close_size: f32, /// The padding of the tabs of the [`Sidebar`]. - padding: f32, + padding: Padding, /// The spacing of the tabs of the [`Sidebar`]. - spacing: f32, + spacing: Pixels, /// The optional icon font of the [`Sidebar`]. font: Option, /// The optional text font of the [`Sidebar`]. @@ -287,8 +287,8 @@ where /// Sets the padding of the tabs of the [`Sidebar`]. #[must_use] - pub fn padding(mut self, padding: f32) -> Self { - self.padding = padding; + pub fn padding(mut self, padding: impl Into) -> Self { + self.padding = padding.into(); self } @@ -308,8 +308,8 @@ where /// Sets the spacing between the tabs of the [`Sidebar`]. #[must_use] - pub fn spacing(mut self, spacing: f32) -> Self { - self.spacing = spacing; + pub fn spacing(mut self, spacing: impl Into) -> Self { + self.spacing = spacing.into(); self } @@ -1156,7 +1156,7 @@ where /// Sets the padding of the tabs of the [`Sidebar`](super::sidebar::Sidebar). #[must_use] - pub fn tab_label_padding(mut self, padding: f32) -> Self { + pub fn tab_label_padding(mut self, padding: impl Into) -> Self { self.sidebar = self.sidebar.padding(padding); self } @@ -1164,7 +1164,7 @@ where /// Sets the spacing between the tabs of the /// [`Sidebar`](super::sidebar::Sidebar). #[must_use] - pub fn tab_label_spacing(mut self, spacing: f32) -> Self { + pub fn tab_label_spacing(mut self, spacing: impl Into) -> Self { self.sidebar = self.sidebar.spacing(spacing); self } diff --git a/src/widgets/tab_bar.rs b/src/widgets/tab_bar.rs index b71d295b..0ca2683f 100644 --- a/src/widgets/tab_bar.rs +++ b/src/widgets/tab_bar.rs @@ -19,9 +19,12 @@ use iced::{ event, mouse::{self, Cursor}, touch, - widget::{text, text::LineHeight, Column, Row, Text}, - Alignment, Background, Border, Color, Element, Event, Font, Length, Pixels, Point, Rectangle, - Shadow, Size, + widget::{ + text::{self, LineHeight}, + Column, Row, Text, + }, + Alignment, Background, Border, Color, Element, Event, Font, Length, Padding, Pixels, Point, + Rectangle, Shadow, Size, }; use std::marker::PhantomData; @@ -39,9 +42,9 @@ const DEFAULT_TEXT_SIZE: f32 = 16.0; /// The default size of the close icon. const DEFAULT_CLOSE_SIZE: f32 = 16.0; /// The default padding between the tabs. -const DEFAULT_PADDING: f32 = 5.0; +const DEFAULT_PADDING: Padding = Padding::new(5.0); /// The default spacing around the tabs. -const DEFAULT_SPACING: f32 = 0.0; +const DEFAULT_SPACING: Pixels = Pixels::ZERO; /// A tab bar to show tabs. /// @@ -101,9 +104,9 @@ where /// The size of the close icon. close_size: f32, /// The padding of the tabs of the [`TabBar`]. - padding: f32, + padding: Padding, /// The spacing of the tabs of the [`TabBar`]. - spacing: f32, + spacing: Pixels, /// The optional icon font of the [`TabBar`]. font: Option, /// The optional text font of the [`TabBar`]. @@ -260,8 +263,8 @@ where /// Sets the padding of the tabs of the [`TabBar`]. #[must_use] - pub fn padding(mut self, padding: f32) -> Self { - self.padding = padding; + pub fn padding(mut self, padding: impl Into) -> Self { + self.padding = padding.into(); self } @@ -281,8 +284,8 @@ where /// Sets the spacing between the tabs of the [`TabBar`]. #[must_use] - pub fn spacing(mut self, spacing: f32) -> Self { - self.spacing = spacing; + pub fn spacing(mut self, spacing: impl Into) -> Self { + self.spacing = spacing.into(); self } diff --git a/src/widgets/tabs.rs b/src/widgets/tabs.rs index 1362d7c0..ce594158 100644 --- a/src/widgets/tabs.rs +++ b/src/widgets/tabs.rs @@ -30,7 +30,7 @@ use iced::{ event, mouse::{self, Cursor}, widget::{text, Row}, - Element, Event, Font, Length, Point, Rectangle, Size, Vector, + Element, Event, Font, Length, Padding, Pixels, Point, Rectangle, Size, Vector, }; pub use tab_bar_position::TabBarPosition; @@ -263,7 +263,7 @@ where /// Sets the padding of the tabs of the [`TabBar`](super::tab_bar::TabBar). #[must_use] - pub fn tab_label_padding(mut self, padding: f32) -> Self { + pub fn tab_label_padding(mut self, padding: impl Into) -> Self { self.tab_bar = self.tab_bar.padding(padding); self } @@ -271,7 +271,7 @@ where /// Sets the spacing between the tabs of the /// [`TabBar`](super::tab_bar::TabBar). #[must_use] - pub fn tab_label_spacing(mut self, spacing: f32) -> Self { + pub fn tab_label_spacing(mut self, spacing: impl Into) -> Self { self.tab_bar = self.tab_bar.spacing(spacing); self } diff --git a/src/widgets/typed_input.rs b/src/widgets/typed_input.rs index d9db55ba..3741fba4 100644 --- a/src/widgets/typed_input.rs +++ b/src/widgets/typed_input.rs @@ -19,7 +19,7 @@ use iced::{Element, Length, Padding, Pixels, Rectangle}; use std::{fmt::Display, str::FromStr}; /// The default padding -const DEFAULT_PADDING: f32 = 5.0; +const DEFAULT_PADDING: Padding = Padding::new(5.0); /// A field that can only be filled with a specific type. /// diff --git a/src/widgets/wrap.rs b/src/widgets/wrap.rs index 7db542d7..ebdd4baa 100644 --- a/src/widgets/wrap.rs +++ b/src/widgets/wrap.rs @@ -10,7 +10,7 @@ use iced::{ }, event, mouse::{self, Cursor}, - Alignment, Element, Event, Length, Padding, Point, Rectangle, Size, Vector, + Alignment, Element, Event, Length, Padding, Pixels, Point, Rectangle, Size, Vector, }; use std::marker::PhantomData; @@ -30,11 +30,11 @@ pub struct Wrap<'a, Message, Direction, Theme = iced::Theme, Renderer = iced::Re /// The maximum height of the [`Wrap`]. pub max_height: f32, /// The padding of each element of the [`Wrap`]. - pub padding: f32, + pub padding: Padding, /// The spacing between each element of the [`Wrap`]. - pub spacing: f32, + pub spacing: Pixels, /// The spacing between each line of the [`Wrap`]. - pub line_spacing: f32, + pub line_spacing: Pixels, /// The minimal length of each line of the [`Wrap`]. pub line_minimal_length: f32, #[allow(clippy::missing_docs_in_private_items)] @@ -84,15 +84,15 @@ impl<'a, Message, Theme, Renderer> Wrap<'a, Message, direction::Vertical, Theme, impl<'a, Message, Renderer, Direction, Theme> Wrap<'a, Message, Direction, Theme, Renderer> { /// Sets the spacing of the [`Wrap`]. #[must_use] - pub const fn spacing(mut self, units: f32) -> Self { - self.spacing = units; + pub fn spacing(mut self, spacing: impl Into) -> Self { + self.spacing = spacing.into(); self } /// Sets the spacing of the lines of the [`Wrap`]. #[must_use] - pub const fn line_spacing(mut self, units: f32) -> Self { - self.line_spacing = units; + pub fn line_spacing(mut self, spacing: impl Into) -> Self { + self.line_spacing = spacing.into(); self } @@ -105,8 +105,8 @@ impl<'a, Message, Renderer, Direction, Theme> Wrap<'a, Message, Direction, Theme /// Sets the padding of the elements in the [`Wrap`]. #[must_use] - pub const fn padding(mut self, units: f32) -> Self { - self.padding = units; + pub fn padding(mut self, padding: impl Into) -> Self { + self.padding = padding.into(); self } @@ -324,9 +324,9 @@ impl<'a, Message, Renderer, Direction, Theme> Default height: Length::Shrink, max_width: 4_294_967_295.0, max_height: 4_294_967_295.0, - padding: 0.0, - spacing: 0.0, - line_spacing: 0.0, + padding: Padding::ZERO, + spacing: Pixels::ZERO, + line_spacing: Pixels::ZERO, line_minimal_length: 10.0, _direction: PhantomData, } @@ -349,7 +349,7 @@ where #[allow(clippy::inline_always)] #[inline(always)] fn inner_layout(&self, tree: &mut Tree, renderer: &Renderer, limits: &Limits) -> Node { - let padding = Padding::from(self.padding); + let padding = self.padding; let spacing = self.spacing; let line_spacing = self.line_spacing; #[allow(clippy::cast_precision_loss)] // TODO: possible precision loss @@ -386,11 +386,11 @@ where let size = node.size(); - let offset_init = size.width + spacing; + let offset_init = size.width + spacing.0; let offset = curse + offset_init; if offset > max_width { - deep_curse += current_line_height + line_spacing; + deep_curse += current_line_height + line_spacing.0; align.push((start..end, current_line_height)); start = end; end += 1; @@ -436,7 +436,7 @@ where #[allow(clippy::inline_always)] #[inline(always)] fn inner_layout(&self, tree: &mut Tree, renderer: &Renderer, limits: &Limits) -> Node { - let padding = Padding::from(self.padding); + let padding = self.padding; let spacing = self.spacing; let line_spacing = self.line_spacing; #[allow(clippy::cast_precision_loss)] // TODO: possible precision loss @@ -473,11 +473,11 @@ where let size = node.size(); - let offset_init = size.height + spacing; + let offset_init = size.height + spacing.0; let offset = curse + offset_init; if offset > max_height { - wide_curse += current_line_width + line_spacing; + wide_curse += current_line_width + line_spacing.0; align.push((start..end, current_line_width)); start = end; end += 1;