diff options
Diffstat (limited to 'native/src/widget')
-rw-r--r-- | native/src/widget/button.rs | 12 | ||||
-rw-r--r-- | native/src/widget/checkbox.rs | 74 | ||||
-rw-r--r-- | native/src/widget/column.rs | 30 | ||||
-rw-r--r-- | native/src/widget/container.rs | 91 | ||||
-rw-r--r-- | native/src/widget/helpers.rs | 14 | ||||
-rw-r--r-- | native/src/widget/image.rs | 10 | ||||
-rw-r--r-- | native/src/widget/image/viewer.rs | 22 | ||||
-rw-r--r-- | native/src/widget/pane_grid.rs | 71 | ||||
-rw-r--r-- | native/src/widget/pane_grid/title_bar.rs | 5 | ||||
-rw-r--r-- | native/src/widget/pick_list.rs | 63 | ||||
-rw-r--r-- | native/src/widget/progress_bar.rs | 14 | ||||
-rw-r--r-- | native/src/widget/radio.rs | 36 | ||||
-rw-r--r-- | native/src/widget/row.rs | 22 | ||||
-rw-r--r-- | native/src/widget/rule.rs | 12 | ||||
-rw-r--r-- | native/src/widget/scrollable.rs | 101 | ||||
-rw-r--r-- | native/src/widget/slider.rs | 20 | ||||
-rw-r--r-- | native/src/widget/space.rs | 15 | ||||
-rw-r--r-- | native/src/widget/svg.rs | 8 | ||||
-rw-r--r-- | native/src/widget/text.rs | 20 | ||||
-rw-r--r-- | native/src/widget/text_input.rs | 38 | ||||
-rw-r--r-- | native/src/widget/toggler.rs | 36 | ||||
-rw-r--r-- | native/src/widget/tooltip.rs | 28 | ||||
-rw-r--r-- | native/src/widget/vertical_slider.rs | 18 |
23 files changed, 401 insertions, 359 deletions
diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index b4276317..39387173 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -76,20 +76,20 @@ where on_press: None, width: Length::Shrink, height: Length::Shrink, - padding: Padding::new(5), + padding: Padding::new(5.0), style: <Renderer::Theme as StyleSheet>::Style::default(), } } /// Sets the width of the [`Button`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Button`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); self } @@ -434,7 +434,7 @@ pub fn layout<Renderer>( let padding = padding.fit(content.size(), limits.max()); let size = limits.pad(padding).resolve(content.size()).pad(padding); - content.move_to(Point::new(padding.left.into(), padding.top.into())); + content.move_to(Point::new(padding.left, padding.top)); layout::Node::with_children(size, vec![content]) } diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index b46433c2..9b69e574 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -8,12 +8,23 @@ use crate::text; use crate::touch; use crate::widget::{self, Row, Text, Tree}; use crate::{ - Alignment, Clipboard, Element, Layout, Length, Point, Rectangle, Shell, - Widget, + Alignment, Clipboard, Element, Layout, Length, Pixels, Point, Rectangle, + Shell, Widget, }; pub use iced_style::checkbox::{Appearance, StyleSheet}; +/// The icon in a [`Checkbox`]. +#[derive(Debug, Clone, PartialEq)] +pub struct Icon<Font> { + /// Font that will be used to display the `code_point`, + pub font: Font, + /// The unicode code point that will be used as the icon. + pub code_point: char, + /// Font size of the content. + pub size: Option<f32>, +} + /// A box that can be checked. /// /// # Example @@ -41,10 +52,11 @@ where on_toggle: Box<dyn Fn(bool) -> Message + 'a>, label: String, width: Length, - size: u16, - spacing: u16, - text_size: Option<u16>, + size: f32, + spacing: f32, + text_size: Option<f32>, font: Renderer::Font, + icon: Icon<Renderer::Font>, style: <Renderer::Theme as StyleSheet>::Style, } @@ -54,10 +66,10 @@ where Renderer::Theme: StyleSheet + widget::text::StyleSheet, { /// The default size of a [`Checkbox`]. - const DEFAULT_SIZE: u16 = 20; + const DEFAULT_SIZE: f32 = 20.0; /// The default spacing of a [`Checkbox`]. - const DEFAULT_SPACING: u16 = 15; + const DEFAULT_SPACING: f32 = 15.0; /// Creates a new [`Checkbox`]. /// @@ -80,31 +92,36 @@ where spacing: Self::DEFAULT_SPACING, text_size: None, font: Renderer::Font::default(), + icon: Icon { + font: Renderer::ICON_FONT, + code_point: Renderer::CHECKMARK_ICON, + size: None, + }, style: Default::default(), } } /// Sets the size of the [`Checkbox`]. - pub fn size(mut self, size: u16) -> Self { - self.size = size; + pub fn size(mut self, size: impl Into<Pixels>) -> Self { + self.size = size.into().0; self } /// Sets the width of the [`Checkbox`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the spacing between the [`Checkbox`] and the text. - pub fn spacing(mut self, spacing: u16) -> Self { - self.spacing = spacing; + pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self { + self.spacing = spacing.into().0; self } /// Sets the text size of the [`Checkbox`]. - pub fn text_size(mut self, text_size: u16) -> Self { - self.text_size = Some(text_size); + pub fn text_size(mut self, text_size: impl Into<Pixels>) -> Self { + self.text_size = Some(text_size.into().0); self } @@ -116,6 +133,12 @@ where self } + /// Sets the [`Icon`] of the [`Checkbox`]. + pub fn icon(mut self, icon: Icon<Renderer::Font>) -> Self { + self.icon = icon; + self + } + /// Sets the style of the [`Checkbox`]. pub fn style( mut self, @@ -149,11 +172,7 @@ where .width(self.width) .spacing(self.spacing) .align_items(Alignment::Center) - .push( - Row::new() - .width(Length::Units(self.size)) - .height(Length::Units(self.size)), - ) + .push(Row::new().width(self.size).height(self.size)) .push( Text::new(&self.label) .font(self.font.clone()) @@ -243,17 +262,24 @@ where custom_style.background, ); + let Icon { + font, + code_point, + size, + } = &self.icon; + let size = size.map(f32::from).unwrap_or(bounds.height * 0.7); + if self.is_checked { renderer.fill_text(text::Text { - content: &Renderer::CHECKMARK_ICON.to_string(), - font: Renderer::ICON_FONT, - size: bounds.height * 0.7, + content: &code_point.to_string(), + font: font.clone(), + size, bounds: Rectangle { x: bounds.center_x(), y: bounds.center_y(), ..bounds }, - color: custom_style.checkmark_color, + color: custom_style.icon_color, horizontal_alignment: alignment::Horizontal::Center, vertical_alignment: alignment::Vertical::Center, }); diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs index 5ad4d858..ebe579d5 100644 --- a/native/src/widget/column.rs +++ b/native/src/widget/column.rs @@ -6,18 +6,18 @@ use crate::overlay; use crate::renderer; use crate::widget::{Operation, Tree}; use crate::{ - Alignment, Clipboard, Element, Layout, Length, Padding, Point, Rectangle, - Shell, Widget, + Alignment, Clipboard, Element, Layout, Length, Padding, Pixels, Point, + Rectangle, Shell, Widget, }; /// A container that distributes its contents vertically. #[allow(missing_debug_implementations)] pub struct Column<'a, Message, Renderer> { - spacing: u16, + spacing: f32, padding: Padding, width: Length, height: Length, - max_width: u32, + max_width: f32, align_items: Alignment, children: Vec<Element<'a, Message, Renderer>>, } @@ -33,11 +33,11 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { children: Vec<Element<'a, Message, Renderer>>, ) -> Self { Column { - spacing: 0, + spacing: 0.0, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, - max_width: u32::MAX, + max_width: f32::INFINITY, align_items: Alignment::Start, children, } @@ -48,8 +48,8 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { /// Custom margins per element do not exist in iced. You should use this /// method instead! While less flexible, it helps you keep spacing between /// elements consistent. - pub fn spacing(mut self, units: u16) -> Self { - self.spacing = units; + pub fn spacing(mut self, amount: impl Into<Pixels>) -> Self { + self.spacing = amount.into().0; self } @@ -60,20 +60,20 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { } /// Sets the width of the [`Column`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Column`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); self } /// Sets the maximum width of the [`Column`]. - pub fn max_width(mut self, max_width: u32) -> Self { - self.max_width = max_width; + pub fn max_width(mut self, max_width: impl Into<Pixels>) -> Self { + self.max_width = max_width.into().0; self } @@ -135,7 +135,7 @@ where renderer, &limits, self.padding, - self.spacing as f32, + self.spacing, self.align_items, &self.children, ) diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index cdf1c859..b77bf50d 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -5,14 +5,12 @@ use crate::layout; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{Operation, Tree}; +use crate::widget::{self, Operation, Tree}; use crate::{ - Background, Clipboard, Color, Element, Layout, Length, Padding, Point, - Rectangle, Shell, Widget, + Background, Clipboard, Color, Element, Layout, Length, Padding, Pixels, + Point, Rectangle, Shell, Widget, }; -use std::u32; - pub use iced_style::container::{Appearance, StyleSheet}; /// An element decorating some content. @@ -24,11 +22,12 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet, { + id: Option<Id>, padding: Padding, width: Length, height: Length, - max_width: u32, - max_height: u32, + max_width: f32, + max_height: f32, horizontal_alignment: alignment::Horizontal, vertical_alignment: alignment::Vertical, style: <Renderer::Theme as StyleSheet>::Style, @@ -46,11 +45,12 @@ where T: Into<Element<'a, Message, Renderer>>, { Container { + id: None, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, - max_width: u32::MAX, - max_height: u32::MAX, + max_width: f32::INFINITY, + max_height: f32::INFINITY, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, style: Default::default(), @@ -58,6 +58,12 @@ where } } + /// Sets the [`Id`] of the [`Container`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the [`Padding`] of the [`Container`]. pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self { self.padding = padding.into(); @@ -65,26 +71,26 @@ where } /// Sets the width of the [`Container`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Container`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); self } /// Sets the maximum width of the [`Container`]. - pub fn max_width(mut self, max_width: u32) -> Self { - self.max_width = max_width; + pub fn max_width(mut self, max_width: impl Into<Pixels>) -> Self { + self.max_width = max_width.into().0; self } - /// Sets the maximum height of the [`Container`] in pixels. - pub fn max_height(mut self, max_height: u32) -> Self { - self.max_height = max_height; + /// Sets the maximum height of the [`Container`]. + pub fn max_height(mut self, max_height: impl Into<Pixels>) -> Self { + self.max_height = max_height.into().0; self } @@ -172,14 +178,17 @@ where renderer: &Renderer, operation: &mut dyn Operation<Message>, ) { - operation.container(None, &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }, + ); } fn on_event( @@ -283,8 +292,8 @@ pub fn layout<Renderer>( limits: &layout::Limits, width: Length, height: Length, - max_width: u32, - max_height: u32, + max_width: f32, + max_height: f32, padding: Padding, horizontal_alignment: alignment::Horizontal, vertical_alignment: alignment::Vertical, @@ -301,7 +310,7 @@ pub fn layout<Renderer>( let padding = padding.fit(content.size(), limits.max()); let size = limits.pad(padding).resolve(content.size()); - content.move_to(Point::new(padding.left.into(), padding.top.into())); + content.move_to(Point::new(padding.left, padding.top)); content.align( Alignment::from(horizontal_alignment), Alignment::from(vertical_alignment), @@ -333,3 +342,27 @@ pub fn draw_background<Renderer>( ); } } + +/// The identifier of a [`Container`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into<std::borrow::Cow<'static, str>>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From<Id> for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} diff --git a/native/src/widget/helpers.rs b/native/src/widget/helpers.rs index dfd949f6..d13eca75 100644 --- a/native/src/widget/helpers.rs +++ b/native/src/widget/helpers.rs @@ -1,7 +1,7 @@ //! Helper functions to create pure widgets. use crate::overlay; use crate::widget; -use crate::{Element, Length}; +use crate::{Element, Length, Pixels}; use std::borrow::Cow; use std::ops::RangeInclusive; @@ -247,21 +247,23 @@ pub fn image<Handle>(handle: impl Into<Handle>) -> widget::Image<Handle> { /// Creates a new horizontal [`Space`] with the given [`Length`]. /// /// [`Space`]: widget::Space -pub fn horizontal_space(width: Length) -> widget::Space { +pub fn horizontal_space(width: impl Into<Length>) -> widget::Space { widget::Space::with_width(width) } /// Creates a new vertical [`Space`] with the given [`Length`]. /// /// [`Space`]: widget::Space -pub fn vertical_space(height: Length) -> widget::Space { +pub fn vertical_space(height: impl Into<Length>) -> widget::Space { widget::Space::with_height(height) } /// Creates a horizontal [`Rule`] with the given height. /// /// [`Rule`]: widget::Rule -pub fn horizontal_rule<Renderer>(height: u16) -> widget::Rule<Renderer> +pub fn horizontal_rule<Renderer>( + height: impl Into<Pixels>, +) -> widget::Rule<Renderer> where Renderer: crate::Renderer, Renderer::Theme: widget::rule::StyleSheet, @@ -272,7 +274,9 @@ where /// Creates a vertical [`Rule`] with the given width. /// /// [`Rule`]: widget::Rule -pub fn vertical_rule<Renderer>(width: u16) -> widget::Rule<Renderer> +pub fn vertical_rule<Renderer>( + width: impl Into<Pixels>, +) -> widget::Rule<Renderer> where Renderer: crate::Renderer, Renderer::Theme: widget::rule::StyleSheet, diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index 3ff06a76..73257a74 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -29,7 +29,7 @@ pub fn viewer<Handle>(handle: Handle) -> Viewer<Handle> { /// ``` /// /// <img src="https://github.com/iced-rs/iced/blob/9712b319bb7a32848001b96bd84977430f14b623/examples/resources/ferris.png?raw=true" width="300"> -#[derive(Debug, Hash)] +#[derive(Debug)] pub struct Image<Handle> { handle: Handle, width: Length, @@ -49,14 +49,14 @@ impl<Handle> Image<Handle> { } /// Sets the width of the [`Image`] boundaries. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Image`] boundaries. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); self } diff --git a/native/src/widget/image/viewer.rs b/native/src/widget/image/viewer.rs index fdbd3216..1f8d5d7a 100644 --- a/native/src/widget/image/viewer.rs +++ b/native/src/widget/image/viewer.rs @@ -6,8 +6,8 @@ use crate::mouse; use crate::renderer; use crate::widget::tree::{self, Tree}; use crate::{ - Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector, - Widget, + Clipboard, Element, Layout, Length, Pixels, Point, Rectangle, Shell, Size, + Vector, Widget, }; use std::hash::Hash; @@ -15,7 +15,7 @@ use std::hash::Hash; /// A frame that displays an image with the ability to zoom in/out and pan. #[allow(missing_debug_implementations)] pub struct Viewer<Handle> { - padding: u16, + padding: f32, width: Length, height: Length, min_scale: f32, @@ -28,7 +28,7 @@ impl<Handle> Viewer<Handle> { /// Creates a new [`Viewer`] with the given [`State`]. pub fn new(handle: Handle) -> Self { Viewer { - padding: 0, + padding: 0.0, width: Length::Shrink, height: Length::Shrink, min_scale: 0.25, @@ -39,20 +39,20 @@ impl<Handle> Viewer<Handle> { } /// Sets the padding of the [`Viewer`]. - pub fn padding(mut self, units: u16) -> Self { - self.padding = units; + pub fn padding(mut self, padding: impl Into<Pixels>) -> Self { + self.padding = padding.into().0; self } /// Sets the width of the [`Viewer`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Viewer`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); self } @@ -124,7 +124,7 @@ where // Only calculate viewport sizes if the images are constrained to a limited space. // If they are Fill|Portion let them expand within their alotted space. match expansion_size { - Length::Shrink | Length::Units(_) => { + Length::Shrink | Length::Fixed(_) => { let aspect_ratio = width as f32 / height as f32; let viewport_aspect_ratio = size.width / size.height; if viewport_aspect_ratio > aspect_ratio { diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index eb04c0ba..bcb17ebd 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -6,7 +6,7 @@ //! The [`pane_grid` example] showcases how to use a [`PaneGrid`] with resizing, //! drag and drop, and hotkey support. //! -//! [`pane_grid` example]: https://github.com/iced-rs/iced/tree/0.7/examples/pane_grid +//! [`pane_grid` example]: https://github.com/iced-rs/iced/tree/0.8/examples/pane_grid mod axis; mod configuration; mod content; @@ -42,8 +42,8 @@ use crate::widget; use crate::widget::container; use crate::widget::tree::{self, Tree}; use crate::{ - Clipboard, Color, Element, Layout, Length, Point, Rectangle, Shell, Size, - Vector, Widget, + Clipboard, Color, Element, Layout, Length, Pixels, Point, Rectangle, Shell, + Size, Vector, Widget, }; /// A collection of panes distributed using either vertical or horizontal splits @@ -104,10 +104,10 @@ where contents: Contents<'a, Content<'a, Message, Renderer>>, width: Length, height: Length, - spacing: u16, + spacing: f32, on_click: Option<Box<dyn Fn(Pane) -> Message + 'a>>, on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>, - on_resize: Option<(u16, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>, + on_resize: Option<(f32, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>, style: <Renderer::Theme as StyleSheet>::Style, } @@ -150,7 +150,7 @@ where contents, width: Length::Fill, height: Length::Fill, - spacing: 0, + spacing: 0.0, on_click: None, on_drag: None, on_resize: None, @@ -159,20 +159,20 @@ where } /// Sets the width of the [`PaneGrid`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the height of the [`PaneGrid`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); self } /// Sets the spacing _between_ the panes of the [`PaneGrid`]. - pub fn spacing(mut self, units: u16) -> Self { - self.spacing = units; + pub fn spacing(mut self, amount: impl Into<Pixels>) -> Self { + self.spacing = amount.into().0; self } @@ -205,11 +205,11 @@ where /// The grabbable area of a split will have a length of `spacing + leeway`, /// properly centered. In other words, a length of /// `(spacing + leeway) / 2.0` on either side of the split line. - pub fn on_resize<F>(mut self, leeway: u16, f: F) -> Self + pub fn on_resize<F>(mut self, leeway: impl Into<Pixels>, f: F) -> Self where F: 'a + Fn(ResizeEvent) -> Message, { - self.on_resize = Some((leeway, Box::new(f))); + self.on_resize = Some((leeway.into().0, Box::new(f))); self } @@ -485,14 +485,14 @@ pub fn layout<Renderer, T>( node: &Node, width: Length, height: Length, - spacing: u16, + spacing: f32, contents: impl Iterator<Item = (Pane, T)>, layout_content: impl Fn(T, &Renderer, &layout::Limits) -> layout::Node, ) -> layout::Node { let limits = limits.width(width).height(height); let size = limits.resolve(Size::ZERO); - let regions = node.pane_regions(f32::from(spacing), size); + let regions = node.pane_regions(spacing, size); let children = contents .filter_map(|(pane, content)| { let region = regions.get(&pane)?; @@ -522,11 +522,11 @@ pub fn update<'a, Message, T: Draggable>( layout: Layout<'_>, cursor_position: Point, shell: &mut Shell<'_, Message>, - spacing: u16, + spacing: f32, contents: impl Iterator<Item = (Pane, T)>, on_click: &Option<Box<dyn Fn(Pane) -> Message + 'a>>, on_drag: &Option<Box<dyn Fn(DragEvent) -> Message + 'a>>, - on_resize: &Option<(u16, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>, + on_resize: &Option<(f32, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>, ) -> event::Status { let mut event_status = event::Status::Ignored; @@ -546,13 +546,13 @@ pub fn update<'a, Message, T: Draggable>( ); let splits = node.split_regions( - f32::from(spacing), + spacing, Size::new(bounds.width, bounds.height), ); let clicked_split = hovered_split( splits.iter(), - f32::from(spacing + leeway), + spacing + leeway, relative_cursor, ); @@ -624,7 +624,7 @@ pub fn update<'a, Message, T: Draggable>( let bounds = layout.bounds(); let splits = node.split_regions( - f32::from(spacing), + spacing, Size::new(bounds.width, bounds.height), ); @@ -698,8 +698,8 @@ pub fn mouse_interaction( node: &Node, layout: Layout<'_>, cursor_position: Point, - spacing: u16, - resize_leeway: Option<u16>, + spacing: f32, + resize_leeway: Option<f32>, ) -> Option<mouse::Interaction> { if action.picked_pane().is_some() { return Some(mouse::Interaction::Grabbing); @@ -710,20 +710,15 @@ pub fn mouse_interaction( resize_leeway.and_then(|leeway| { let bounds = layout.bounds(); - let splits = - node.split_regions(f32::from(spacing), bounds.size()); + let splits = node.split_regions(spacing, bounds.size()); let relative_cursor = Point::new( cursor_position.x - bounds.x, cursor_position.y - bounds.y, ); - hovered_split( - splits.iter(), - f32::from(spacing + leeway), - relative_cursor, - ) - .map(|(_, axis, _)| axis) + hovered_split(splits.iter(), spacing + leeway, relative_cursor) + .map(|(_, axis, _)| axis) }) }); @@ -747,8 +742,8 @@ pub fn draw<Renderer, T>( theme: &Renderer::Theme, default_style: &renderer::Style, viewport: &Rectangle, - spacing: u16, - resize_leeway: Option<u16>, + spacing: f32, + resize_leeway: Option<f32>, style: &<Renderer::Theme as StyleSheet>::Style, contents: impl Iterator<Item = (Pane, T)>, draw_pane: impl Fn( @@ -770,12 +765,11 @@ pub fn draw<Renderer, T>( .and_then(|(split, axis)| { let bounds = layout.bounds(); - let splits = node.split_regions(f32::from(spacing), bounds.size()); + let splits = node.split_regions(spacing, bounds.size()); let (_axis, region, ratio) = splits.get(&split)?; - let region = - axis.split_line_bounds(*region, *ratio, f32::from(spacing)); + let region = axis.split_line_bounds(*region, *ratio, spacing); Some((axis, region + Vector::new(bounds.x, bounds.y), true)) }) @@ -788,12 +782,11 @@ pub fn draw<Renderer, T>( cursor_position.y - bounds.y, ); - let splits = - node.split_regions(f32::from(spacing), bounds.size()); + let splits = node.split_regions(spacing, bounds.size()); let (_split, axis, region) = hovered_split( splits.iter(), - f32::from(spacing + leeway), + spacing + leeway, relative_cursor, )?; diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs index ea0969aa..107078ef 100644 --- a/native/src/widget/pane_grid/title_bar.rs +++ b/native/src/widget/pane_grid/title_bar.rs @@ -249,10 +249,7 @@ where ) }; - node.move_to(Point::new( - self.padding.left.into(), - self.padding.top.into(), - )); + node.move_to(Point::new(self.padding.left, self.padding.top)); layout::Node::with_children(node.size().pad(self.padding), vec![node]) } diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index b1cdfad4..17528db4 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -13,8 +13,8 @@ use crate::widget::container; use crate::widget::scrollable; use crate::widget::tree::{self, Tree}; use crate::{ - Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Size, - Widget, + Clipboard, Element, Layout, Length, Padding, Pixels, Point, Rectangle, + Shell, Size, Widget, }; use std::borrow::Cow; @@ -34,7 +34,7 @@ where selected: Option<T>, width: Length, padding: Padding, - text_size: Option<u16>, + text_size: Option<f32>, font: Renderer::Font, handle: Handle<Renderer::Font>, style: <Renderer::Theme as StyleSheet>::Style, @@ -53,7 +53,7 @@ where From<<Renderer::Theme as StyleSheet>::Style>, { /// The default padding of a [`PickList`]. - pub const DEFAULT_PADDING: Padding = Padding::new(5); + pub const DEFAULT_PADDING: Padding = Padding::new(5.0); /// Creates a new [`PickList`] with the given list of options, the current /// selected value, and the message to produce when an option is selected. @@ -83,8 +83,8 @@ where } /// Sets the width of the [`PickList`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } @@ -95,8 +95,8 @@ where } /// Sets the text size of the [`PickList`]. - pub fn text_size(mut self, size: u16) -> Self { - self.text_size = Some(size); + pub fn text_size(mut self, size: impl Into<Pixels>) -> Self { + self.text_size = Some(size.into().0); self } @@ -297,14 +297,14 @@ impl<T> Default for State<T> { } /// The handle to the right side of the [`PickList`]. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq)] pub enum Handle<Font> { /// Displays an arrow icon (â–¼). /// /// This is the default. Arrow { /// Font size of the content. - size: Option<u16>, + size: Option<f32>, }, /// A custom static handle. Static(Icon<Font>), @@ -326,14 +326,14 @@ impl<Font> Default for Handle<Font> { } /// The icon of a [`Handle`]. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq)] pub struct Icon<Font> { /// Font that will be used to display the `code_point`, pub font: Font, /// The unicode code point that will be used as the icon. pub code_point: char, /// Font size of the content. - pub size: Option<u16>, + pub size: Option<f32>, } /// Computes the layout of a [`PickList`]. @@ -342,7 +342,7 @@ pub fn layout<Renderer, T>( limits: &layout::Limits, width: Length, padding: Padding, - text_size: Option<u16>, + text_size: Option<f32>, font: &Renderer::Font, placeholder: Option<&str>, options: &[T], @@ -354,12 +354,11 @@ where use std::f32; let limits = limits.width(width).height(Length::Shrink).pad(padding); - let text_size = text_size.unwrap_or_else(|| renderer.default_size()); let max_width = match width { Length::Shrink => { - let measure = |label: &str| -> u32 { + let measure = |label: &str| -> f32 { let (width, _) = renderer.measure( label, text_size, @@ -367,26 +366,25 @@ where Size::new(f32::INFINITY, f32::INFINITY), ); - width.round() as u32 + width.round() }; let labels = options.iter().map(ToString::to_string); - let labels_width = - labels.map(|label| measure(&label)).max().unwrap_or(100); + let labels_width = labels + .map(|label| measure(&label)) + .fold(100.0, |candidate, current| current.max(candidate)); - let placeholder_width = placeholder.map(measure).unwrap_or(100); + let placeholder_width = placeholder.map(measure).unwrap_or(100.0); labels_width.max(placeholder_width) } - _ => 0, + _ => 0.0, }; let size = { - let intrinsic = Size::new( - max_width as f32 + f32::from(text_size) + f32::from(padding.left), - f32::from(text_size), - ); + let intrinsic = + Size::new(max_width + text_size + padding.left, text_size); limits.resolve(intrinsic).pad(padding) }; @@ -514,7 +512,7 @@ pub fn overlay<'a, T, Message, Renderer>( layout: Layout<'_>, state: &'a mut State<T>, padding: Padding, - text_size: Option<u16>, + text_size: Option<f32>, font: Renderer::Font, options: &'a [T], style: <Renderer::Theme as StyleSheet>::Style, @@ -539,7 +537,7 @@ where &mut state.hovered_option, &mut state.last_selection, ) - .width(bounds.width.round() as u16) + .width(bounds.width) .padding(padding) .font(font) .style(style); @@ -561,7 +559,7 @@ pub fn draw<'a, T, Renderer>( layout: Layout<'_>, cursor_position: Point, padding: Padding, - text_size: Option<u16>, + text_size: Option<f32>, font: &Renderer::Font, placeholder: Option<&str>, selected: Option<&T>, @@ -613,7 +611,7 @@ pub fn draw<'a, T, Renderer>( }; if let Some((font, code_point, size)) = handle { - let size = f32::from(size.unwrap_or_else(|| renderer.default_size())); + let size = size.unwrap_or_else(|| renderer.default_size()); renderer.fill_text(Text { content: &code_point.to_string(), @@ -621,7 +619,7 @@ pub fn draw<'a, T, Renderer>( font, color: style.handle_color, bounds: Rectangle { - x: bounds.x + bounds.width - f32::from(padding.horizontal()), + x: bounds.x + bounds.width - padding.horizontal(), y: bounds.center_y() - size / 2.0, height: size, ..bounds @@ -634,8 +632,7 @@ pub fn draw<'a, T, Renderer>( let label = selected.map(ToString::to_string); if let Some(label) = label.as_deref().or(placeholder) { - let text_size = - f32::from(text_size.unwrap_or_else(|| renderer.default_size())); + let text_size = text_size.unwrap_or_else(|| renderer.default_size()); renderer.fill_text(Text { content: label, @@ -647,9 +644,9 @@ pub fn draw<'a, T, Renderer>( style.placeholder_color }, bounds: Rectangle { - x: bounds.x + f32::from(padding.left), + x: bounds.x + padding.left, y: bounds.center_y() - text_size / 2.0, - width: bounds.width - f32::from(padding.horizontal()), + width: bounds.width - padding.horizontal(), height: text_size, }, horizontal_alignment: alignment::Horizontal::Left, diff --git a/native/src/widget/progress_bar.rs b/native/src/widget/progress_bar.rs index 7d5d5be5..dd46fa76 100644 --- a/native/src/widget/progress_bar.rs +++ b/native/src/widget/progress_bar.rs @@ -38,7 +38,7 @@ where Renderer::Theme: StyleSheet, { /// The default height of a [`ProgressBar`]. - pub const DEFAULT_HEIGHT: u16 = 30; + pub const DEFAULT_HEIGHT: f32 = 30.0; /// Creates a new [`ProgressBar`]. /// @@ -56,14 +56,14 @@ where } /// Sets the width of the [`ProgressBar`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the height of the [`ProgressBar`]. - pub fn height(mut self, height: Length) -> Self { - self.height = Some(height); + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = Some(height.into()); self } @@ -87,7 +87,7 @@ where } fn height(&self) -> Length { - self.height.unwrap_or(Length::Units(Self::DEFAULT_HEIGHT)) + self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT)) } fn layout( @@ -97,7 +97,7 @@ where ) -> layout::Node { let limits = limits .width(self.width) - .height(self.height.unwrap_or(Length::Units(Self::DEFAULT_HEIGHT))); + .height(self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT))); let size = limits.resolve(Size::ZERO); diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index b95ccc5b..9daddfbc 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -8,8 +8,8 @@ use crate::text; use crate::touch; use crate::widget::{self, Row, Text, Tree}; use crate::{ - Alignment, Clipboard, Color, Element, Layout, Length, Point, Rectangle, - Shell, Widget, + Alignment, Clipboard, Color, Element, Layout, Length, Pixels, Point, + Rectangle, Shell, Widget, }; pub use iced_style::radio::{Appearance, StyleSheet}; @@ -50,9 +50,9 @@ where on_click: Message, label: String, width: Length, - size: u16, - spacing: u16, - text_size: Option<u16>, + size: f32, + spacing: f32, + text_size: Option<f32>, font: Renderer::Font, style: <Renderer::Theme as StyleSheet>::Style, } @@ -64,10 +64,10 @@ where Renderer::Theme: StyleSheet, { /// The default size of a [`Radio`] button. - pub const DEFAULT_SIZE: u16 = 28; + pub const DEFAULT_SIZE: f32 = 28.0; /// The default spacing of a [`Radio`] button. - pub const DEFAULT_SPACING: u16 = 15; + pub const DEFAULT_SPACING: f32 = 15.0; /// Creates a new [`Radio`] button. /// @@ -101,26 +101,26 @@ where } /// Sets the size of the [`Radio`] button. - pub fn size(mut self, size: u16) -> Self { - self.size = size; + pub fn size(mut self, size: impl Into<Pixels>) -> Self { + self.size = size.into().0; self } /// Sets the width of the [`Radio`] button. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the spacing between the [`Radio`] button and the text. - pub fn spacing(mut self, spacing: u16) -> Self { - self.spacing = spacing; + pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self { + self.spacing = spacing.into().0; self } /// Sets the text size of the [`Radio`] button. - pub fn text_size(mut self, text_size: u16) -> Self { - self.text_size = Some(text_size); + pub fn text_size(mut self, text_size: impl Into<Pixels>) -> Self { + self.text_size = Some(text_size.into().0); self } @@ -163,11 +163,7 @@ where .width(self.width) .spacing(self.spacing) .align_items(Alignment::Center) - .push( - Row::new() - .width(Length::Units(self.size)) - .height(Length::Units(self.size)), - ) + .push(Row::new().width(self.size).height(self.size)) .push(Text::new(&self.label).width(self.width).size( self.text_size.unwrap_or_else(|| renderer.default_size()), )) diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs index 108e98e4..286c1c2d 100644 --- a/native/src/widget/row.rs +++ b/native/src/widget/row.rs @@ -6,14 +6,14 @@ use crate::overlay; use crate::renderer; use crate::widget::{Operation, Tree}; use crate::{ - Alignment, Clipboard, Element, Length, Padding, Point, Rectangle, Shell, - Widget, + Alignment, Clipboard, Element, Length, Padding, Pixels, Point, Rectangle, + Shell, Widget, }; /// A container that distributes its contents horizontally. #[allow(missing_debug_implementations)] pub struct Row<'a, Message, Renderer> { - spacing: u16, + spacing: f32, padding: Padding, width: Length, height: Length, @@ -32,7 +32,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { children: Vec<Element<'a, Message, Renderer>>, ) -> Self { Row { - spacing: 0, + spacing: 0.0, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -46,8 +46,8 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { /// Custom margins per element do not exist in iced. You should use this /// method instead! While less flexible, it helps you keep spacing between /// elements consistent. - pub fn spacing(mut self, units: u16) -> Self { - self.spacing = units; + pub fn spacing(mut self, amount: impl Into<Pixels>) -> Self { + self.spacing = amount.into().0; self } @@ -58,14 +58,14 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { } /// Sets the width of the [`Row`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Row`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); self } @@ -124,7 +124,7 @@ where renderer, &limits, self.padding, - self.spacing as f32, + self.spacing, self.align_items, &self.children, ) diff --git a/native/src/widget/rule.rs b/native/src/widget/rule.rs index 2dc7b6f0..1ab6a0d3 100644 --- a/native/src/widget/rule.rs +++ b/native/src/widget/rule.rs @@ -2,7 +2,9 @@ use crate::layout; use crate::renderer; use crate::widget::Tree; -use crate::{Color, Element, Layout, Length, Point, Rectangle, Size, Widget}; +use crate::{ + Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Widget, +}; pub use iced_style::rule::{Appearance, FillMode, StyleSheet}; @@ -25,19 +27,19 @@ where Renderer::Theme: StyleSheet, { /// Creates a horizontal [`Rule`] with the given height. - pub fn horizontal(height: u16) -> Self { + pub fn horizontal(height: impl Into<Pixels>) -> Self { Rule { width: Length::Fill, - height: Length::Units(height), + height: Length::Fixed(height.into().0), is_horizontal: true, style: Default::default(), } } /// Creates a vertical [`Rule`] with the given width. - pub fn vertical(width: u16) -> Self { + pub fn vertical(width: impl Into<Pixels>) -> Self { Rule { - width: Length::Units(width), + width: Length::Fixed(width.into().0), height: Length::Fill, is_horizontal: false, style: Default::default(), diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 82286036..c1df8c39 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -10,8 +10,8 @@ use crate::widget; use crate::widget::operation::{self, Operation}; use crate::widget::tree::{self, Tree}; use crate::{ - Background, Clipboard, Color, Command, Element, Layout, Length, Point, - Rectangle, Shell, Size, Vector, Widget, + Background, Clipboard, Color, Command, Element, Layout, Length, Pixels, + Point, Rectangle, Shell, Size, Vector, Widget, }; pub use iced_style::scrollable::StyleSheet; @@ -66,8 +66,8 @@ where } /// Sets the height of the [`Scrollable`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); self } @@ -108,17 +108,17 @@ where /// Properties of a scrollbar within a [`Scrollable`]. #[derive(Debug)] pub struct Properties { - width: u16, - margin: u16, - scroller_width: u16, + width: f32, + margin: f32, + scroller_width: f32, } impl Default for Properties { fn default() -> Self { Self { - width: 10, - margin: 0, - scroller_width: 10, + width: 10.0, + margin: 0.0, + scroller_width: 10.0, } } } @@ -131,21 +131,21 @@ impl Properties { /// Sets the scrollbar width of the [`Scrollable`] . /// Silently enforces a minimum width of 1. - pub fn width(mut self, width: u16) -> Self { - self.width = width.max(1); + pub fn width(mut self, width: impl Into<Pixels>) -> Self { + self.width = width.into().0.max(1.0); self } /// Sets the scrollbar margin of the [`Scrollable`] . - pub fn margin(mut self, margin: u16) -> Self { - self.margin = margin; + pub fn margin(mut self, margin: impl Into<Pixels>) -> Self { + self.margin = margin.into().0; self } /// Sets the scroller width of the [`Scrollable`] . /// Silently enforces a minimum width of 1. - pub fn scroller_width(mut self, scroller_width: u16) -> Self { - self.scroller_width = scroller_width.max(1); + pub fn scroller_width(mut self, scroller_width: impl Into<Pixels>) -> Self { + self.scroller_width = scroller_width.into().0.max(1.0); self } } @@ -208,14 +208,17 @@ where operation.scrollable(state, self.id.as_ref().map(|id| &id.0)); - operation.container(None, &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }, + ); } fn on_event( @@ -395,11 +398,11 @@ pub fn layout<Renderer>( layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, ) -> layout::Node { let limits = limits - .max_height(u32::MAX) + .max_height(f32::INFINITY) .max_width(if horizontal_enabled { - u32::MAX + f32::INFINITY } else { - limits.max().width as u32 + limits.max().width }) .width(width) .height(height); @@ -1097,26 +1100,27 @@ impl Scrollbars { // Adjust the height of the vertical scrollbar if the horizontal scrollbar // is present - let x_scrollbar_height = show_scrollbar_x.map_or(0.0, |h| { - (h.width.max(h.scroller_width) + h.margin) as f32 - }); + let x_scrollbar_height = show_scrollbar_x + .map_or(0.0, |h| h.width.max(h.scroller_width) + h.margin); - let total_scrollbar_width = width.max(scroller_width) + 2 * margin; + let total_scrollbar_width = + width.max(scroller_width) + 2.0 * margin; // Total bounds of the scrollbar + margin + scroller width let total_scrollbar_bounds = Rectangle { - x: bounds.x + bounds.width - total_scrollbar_width as f32, + x: bounds.x + bounds.width - total_scrollbar_width, y: bounds.y, - width: total_scrollbar_width as f32, + width: total_scrollbar_width, height: (bounds.height - x_scrollbar_height).max(0.0), }; // Bounds of just the scrollbar let scrollbar_bounds = Rectangle { x: bounds.x + bounds.width - - f32::from(total_scrollbar_width / 2 + width / 2), + - total_scrollbar_width / 2.0 + - width / 2.0, y: bounds.y, - width: width as f32, + width, height: (bounds.height - x_scrollbar_height).max(0.0), }; @@ -1127,10 +1131,11 @@ impl Scrollbars { let scroller_bounds = Rectangle { x: bounds.x + bounds.width - - f32::from(total_scrollbar_width / 2 + scroller_width / 2), + - total_scrollbar_width / 2.0 + - scroller_width / 2.0, y: (scrollbar_bounds.y + scroller_offset - x_scrollbar_height) .max(0.0), - width: scroller_width as f32, + width: scroller_width, height: scroller_height, }; @@ -1155,27 +1160,28 @@ impl Scrollbars { // Need to adjust the width of the horizontal scrollbar if the vertical scrollbar // is present let scrollbar_y_width = y_scrollbar.map_or(0.0, |_| { - (vertical.width.max(vertical.scroller_width) + vertical.margin) - as f32 + vertical.width.max(vertical.scroller_width) + vertical.margin }); - let total_scrollbar_height = width.max(scroller_width) + 2 * margin; + let total_scrollbar_height = + width.max(scroller_width) + 2.0 * margin; // Total bounds of the scrollbar + margin + scroller width let total_scrollbar_bounds = Rectangle { x: bounds.x, - y: bounds.y + bounds.height - total_scrollbar_height as f32, + y: bounds.y + bounds.height - total_scrollbar_height, width: (bounds.width - scrollbar_y_width).max(0.0), - height: total_scrollbar_height as f32, + height: total_scrollbar_height, }; // Bounds of just the scrollbar let scrollbar_bounds = Rectangle { x: bounds.x, y: bounds.y + bounds.height - - f32::from(total_scrollbar_height / 2 + width / 2), + - total_scrollbar_height / 2.0 + - width / 2.0, width: (bounds.width - scrollbar_y_width).max(0.0), - height: width as f32, + height: width, }; let ratio = bounds.width / content_bounds.width; @@ -1187,11 +1193,10 @@ impl Scrollbars { x: (scrollbar_bounds.x + scroller_offset - scrollbar_y_width) .max(0.0), y: bounds.y + bounds.height - - f32::from( - total_scrollbar_height / 2 + scroller_width / 2, - ), + - total_scrollbar_height / 2.0 + - scroller_width / 2.0, width: scroller_length, - height: scroller_width as f32, + height: scroller_width, }; Some(Scrollbar { diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index f5251dfa..d3715b1c 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -8,8 +8,8 @@ use crate::renderer; use crate::touch; use crate::widget::tree::{self, Tree}; use crate::{ - Background, Clipboard, Color, Element, Layout, Length, Point, Rectangle, - Shell, Size, Widget, + Background, Clipboard, Color, Element, Layout, Length, Pixels, Point, + Rectangle, Shell, Size, Widget, }; use std::ops::RangeInclusive; @@ -54,7 +54,7 @@ where on_change: Box<dyn Fn(T) -> Message + 'a>, on_release: Option<Message>, width: Length, - height: u16, + height: f32, style: <Renderer::Theme as StyleSheet>::Style, } @@ -66,7 +66,7 @@ where Renderer::Theme: StyleSheet, { /// The default height of a [`Slider`]. - pub const DEFAULT_HEIGHT: u16 = 22; + pub const DEFAULT_HEIGHT: f32 = 22.0; /// Creates a new [`Slider`]. /// @@ -116,14 +116,14 @@ where } /// Sets the width of the [`Slider`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Slider`]. - pub fn height(mut self, height: u16) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Pixels>) -> Self { + self.height = height.into().0; self } @@ -172,9 +172,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = - limits.width(self.width).height(Length::Units(self.height)); - + let limits = limits.width(self.width).height(self.height); let size = limits.resolve(Size::ZERO); layout::Node::new(size) diff --git a/native/src/widget/space.rs b/native/src/widget/space.rs index 9f835893..a6fc977e 100644 --- a/native/src/widget/space.rs +++ b/native/src/widget/space.rs @@ -15,23 +15,26 @@ pub struct Space { impl Space { /// Creates an amount of empty [`Space`] with the given width and height. - pub fn new(width: Length, height: Length) -> Self { - Space { width, height } + pub fn new(width: impl Into<Length>, height: impl Into<Length>) -> Self { + Space { + width: width.into(), + height: height.into(), + } } /// Creates an amount of horizontal [`Space`]. - pub fn with_width(width: Length) -> Self { + pub fn with_width(width: impl Into<Length>) -> Self { Space { - width, + width: width.into(), height: Length::Shrink, } } /// Creates an amount of vertical [`Space`]. - pub fn with_height(height: Length) -> Self { + pub fn with_height(height: impl Into<Length>) -> Self { Space { width: Length::Shrink, - height, + height: height.into(), } } } diff --git a/native/src/widget/svg.rs b/native/src/widget/svg.rs index f83f5acf..f5ed0a6c 100644 --- a/native/src/widget/svg.rs +++ b/native/src/widget/svg.rs @@ -56,15 +56,15 @@ where /// Sets the width of the [`Svg`]. #[must_use] - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Svg`]. #[must_use] - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); self } diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index be9e775e..3fee48f2 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -4,7 +4,7 @@ use crate::layout; use crate::renderer; use crate::text; use crate::widget::Tree; -use crate::{Element, Layout, Length, Point, Rectangle, Size, Widget}; +use crate::{Element, Layout, Length, Pixels, Point, Rectangle, Size, Widget}; use std::borrow::Cow; @@ -32,7 +32,7 @@ where Renderer::Theme: StyleSheet, { content: Cow<'a, str>, - size: Option<u16>, + size: Option<f32>, width: Length, height: Length, horizontal_alignment: alignment::Horizontal, @@ -61,8 +61,8 @@ where } /// Sets the size of the [`Text`]. - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); + pub fn size(mut self, size: impl Into<Pixels>) -> Self { + self.size = Some(size.into().0); self } @@ -84,14 +84,14 @@ where } /// Sets the width of the [`Text`] boundaries. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Text`] boundaries. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); self } @@ -185,7 +185,7 @@ pub fn draw<Renderer>( style: &renderer::Style, layout: Layout<'_>, content: &str, - size: Option<u16>, + size: Option<f32>, font: Renderer::Font, appearance: Appearance, horizontal_alignment: alignment::Horizontal, @@ -209,7 +209,7 @@ pub fn draw<Renderer>( renderer.fill_text(crate::text::Text { content, - size: f32::from(size.unwrap_or_else(|| renderer.default_size())), + size: size.unwrap_or_else(|| renderer.default_size()), bounds: Rectangle { x, y, ..bounds }, color: appearance.color.unwrap_or(style.text_color), font, diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 5bfc918c..ee0473ea 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -25,7 +25,7 @@ use crate::widget::operation::{self, Operation}; use crate::widget::tree::{self, Tree}; use crate::window; use crate::{ - Clipboard, Color, Command, Element, Layout, Length, Padding, Point, + Clipboard, Color, Command, Element, Layout, Length, Padding, Pixels, Point, Rectangle, Shell, Size, Vector, Widget, }; @@ -64,7 +64,7 @@ where font: Renderer::Font, width: Length, padding: Padding, - size: Option<u16>, + size: Option<f32>, on_change: Box<dyn Fn(String) -> Message + 'a>, on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>, on_submit: Option<Message>, @@ -94,7 +94,7 @@ where is_secure: false, font: Default::default(), width: Length::Fill, - padding: Padding::new(5), + padding: Padding::new(5.0), size: None, on_change: Box::new(on_change), on_paste: None, @@ -133,8 +133,8 @@ where self } /// Sets the width of the [`TextInput`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } @@ -145,8 +145,8 @@ where } /// Sets the text size of the [`TextInput`]. - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); + pub fn size(mut self, size: impl Into<Pixels>) -> Self { + self.size = Some(size.into().0); self } @@ -379,7 +379,7 @@ pub fn layout<Renderer>( limits: &layout::Limits, width: Length, padding: Padding, - size: Option<u16>, + size: Option<f32>, ) -> layout::Node where Renderer: text::Renderer, @@ -387,14 +387,10 @@ where let text_size = size.unwrap_or_else(|| renderer.default_size()); let padding = padding.fit(Size::ZERO, limits.max()); - - let limits = limits - .width(width) - .pad(padding) - .height(Length::Units(text_size)); + let limits = limits.width(width).pad(padding).height(text_size); let mut text = layout::Node::new(limits.resolve(Size::ZERO)); - text.move_to(Point::new(padding.left.into(), padding.top.into())); + text.move_to(Point::new(padding.left, padding.top)); layout::Node::with_children(text.size().pad(padding), vec![text]) } @@ -409,7 +405,7 @@ pub fn update<'a, Message, Renderer>( clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, value: &mut Value, - size: Option<u16>, + size: Option<f32>, font: &Renderer::Font, is_secure: bool, on_change: &dyn Fn(String) -> Message, @@ -815,7 +811,7 @@ pub fn draw<Renderer>( state: &State, value: &Value, placeholder: &str, - size: Option<u16>, + size: Option<f32>, font: &Renderer::Font, is_secure: bool, style: &<Renderer::Theme as StyleSheet>::Style, @@ -969,7 +965,7 @@ pub fn draw<Renderer>( width: f32::INFINITY, ..text_bounds }, - size: f32::from(size), + size, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Center, }); @@ -1128,7 +1124,7 @@ fn offset<Renderer>( renderer: &Renderer, text_bounds: Rectangle, font: Renderer::Font, - size: u16, + size: f32, value: &Value, state: &State, ) -> f32 @@ -1162,7 +1158,7 @@ fn measure_cursor_and_scroll_offset<Renderer>( renderer: &Renderer, text_bounds: Rectangle, value: &Value, - size: u16, + size: f32, cursor_index: usize, font: Renderer::Font, ) -> (f32, f32) @@ -1185,7 +1181,7 @@ fn find_cursor_position<Renderer>( renderer: &Renderer, text_bounds: Rectangle, font: Renderer::Font, - size: Option<u16>, + size: Option<f32>, value: &Value, state: &State, x: f32, @@ -1201,7 +1197,7 @@ where renderer .hit_test( &value.to_string(), - size.into(), + size, font, Size::INFINITY, Point::new(x + offset, text_bounds.height / 2.0), diff --git a/native/src/widget/toggler.rs b/native/src/widget/toggler.rs index f0a944a3..a434af65 100644 --- a/native/src/widget/toggler.rs +++ b/native/src/widget/toggler.rs @@ -7,8 +7,8 @@ use crate::renderer; use crate::text; use crate::widget::{self, Row, Text, Tree}; use crate::{ - Alignment, Clipboard, Element, Event, Layout, Length, Point, Rectangle, - Shell, Widget, + Alignment, Clipboard, Element, Event, Layout, Length, Pixels, Point, + Rectangle, Shell, Widget, }; pub use iced_style::toggler::{Appearance, StyleSheet}; @@ -38,10 +38,10 @@ where on_toggle: Box<dyn Fn(bool) -> Message + 'a>, label: Option<String>, width: Length, - size: u16, - text_size: Option<u16>, + size: f32, + text_size: Option<f32>, text_alignment: alignment::Horizontal, - spacing: u16, + spacing: f32, font: Renderer::Font, style: <Renderer::Theme as StyleSheet>::Style, } @@ -52,7 +52,7 @@ where Renderer::Theme: StyleSheet, { /// The default size of a [`Toggler`]. - pub const DEFAULT_SIZE: u16 = 20; + pub const DEFAULT_SIZE: f32 = 20.0; /// Creates a new [`Toggler`]. /// @@ -78,27 +78,27 @@ where size: Self::DEFAULT_SIZE, text_size: None, text_alignment: alignment::Horizontal::Left, - spacing: 0, + spacing: 0.0, font: Renderer::Font::default(), style: Default::default(), } } /// Sets the size of the [`Toggler`]. - pub fn size(mut self, size: u16) -> Self { - self.size = size; + pub fn size(mut self, size: impl Into<Pixels>) -> Self { + self.size = size.into().0; self } /// Sets the width of the [`Toggler`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Length>) -> Self { + self.width = width.into(); self } /// Sets the text size o the [`Toggler`]. - pub fn text_size(mut self, text_size: u16) -> Self { - self.text_size = Some(text_size); + pub fn text_size(mut self, text_size: impl Into<Pixels>) -> Self { + self.text_size = Some(text_size.into().0); self } @@ -109,8 +109,8 @@ where } /// Sets the spacing between the [`Toggler`] and the text. - pub fn spacing(mut self, spacing: u16) -> Self { - self.spacing = spacing; + pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self { + self.spacing = spacing.into().0; self } @@ -169,11 +169,7 @@ where ); } - row = row.push( - Row::new() - .width(Length::Units(2 * self.size)) - .height(Length::Units(self.size)), - ); + row = row.push(Row::new().width(2.0 * self.size).height(self.size)); row.layout(renderer, limits) } diff --git a/native/src/widget/tooltip.rs b/native/src/widget/tooltip.rs index 084dc269..2a24c055 100644 --- a/native/src/widget/tooltip.rs +++ b/native/src/widget/tooltip.rs @@ -9,8 +9,8 @@ use crate::widget::container; use crate::widget::overlay; use crate::widget::{Text, Tree}; use crate::{ - Clipboard, Element, Event, Layout, Length, Padding, Point, Rectangle, - Shell, Size, Vector, Widget, + Clipboard, Element, Event, Layout, Length, Padding, Pixels, Point, + Rectangle, Shell, Size, Vector, Widget, }; use std::borrow::Cow; @@ -25,8 +25,8 @@ where content: Element<'a, Message, Renderer>, tooltip: Text<'a, Renderer>, position: Position, - gap: u16, - padding: u16, + gap: f32, + padding: f32, snap_within_viewport: bool, style: <Renderer::Theme as container::StyleSheet>::Style, } @@ -37,7 +37,7 @@ where Renderer::Theme: container::StyleSheet + widget::text::StyleSheet, { /// The default padding of a [`Tooltip`] drawn by this renderer. - const DEFAULT_PADDING: u16 = 5; + const DEFAULT_PADDING: f32 = 5.0; /// Creates a new [`Tooltip`]. /// @@ -51,7 +51,7 @@ where content: content.into(), tooltip: Text::new(tooltip), position, - gap: 0, + gap: 0.0, padding: Self::DEFAULT_PADDING, snap_within_viewport: true, style: Default::default(), @@ -59,7 +59,7 @@ where } /// Sets the size of the text of the [`Tooltip`]. - pub fn size(mut self, size: u16) -> Self { + pub fn size(mut self, size: impl Into<Pixels>) -> Self { self.tooltip = self.tooltip.size(size); self } @@ -73,14 +73,14 @@ where } /// Sets the gap between the content and its [`Tooltip`]. - pub fn gap(mut self, gap: u16) -> Self { - self.gap = gap; + pub fn gap(mut self, gap: impl Into<Pixels>) -> Self { + self.gap = gap.into().0; self } /// Sets the padding of the [`Tooltip`]. - pub fn padding(mut self, padding: u16) -> Self { - self.padding = padding; + pub fn padding(mut self, padding: impl Into<Pixels>) -> Self { + self.padding = padding.into().0; self } @@ -272,8 +272,8 @@ pub fn draw<Renderer>( cursor_position: Point, viewport: &Rectangle, position: Position, - gap: u16, - padding: u16, + gap: f32, + padding: f32, snap_within_viewport: bool, style: &<Renderer::Theme as container::StyleSheet>::Style, layout_text: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, @@ -293,7 +293,6 @@ pub fn draw<Renderer>( let bounds = layout.bounds(); if bounds.contains(cursor_position) { - let gap = f32::from(gap); let style = theme.appearance(style); let defaults = renderer::Style { @@ -311,7 +310,6 @@ pub fn draw<Renderer>( .pad(Padding::new(padding)), ); - let padding = f32::from(padding); let text_bounds = text_layout.bounds(); let x_center = bounds.x + (bounds.width - text_bounds.width) / 2.0; let y_center = bounds.y + (bounds.height - text_bounds.height) / 2.0; diff --git a/native/src/widget/vertical_slider.rs b/native/src/widget/vertical_slider.rs index 0302f3cf..f1687e38 100644 --- a/native/src/widget/vertical_slider.rs +++ b/native/src/widget/vertical_slider.rs @@ -9,7 +9,7 @@ use crate::event::{self, Event}; use crate::widget::tree::{self, Tree}; use crate::{ layout, mouse, renderer, touch, Background, Clipboard, Color, Element, - Layout, Length, Point, Rectangle, Shell, Size, Widget, + Layout, Length, Pixels, Point, Rectangle, Shell, Size, Widget, }; /// An vertical bar and a handle that selects a single value from a range of @@ -47,7 +47,7 @@ where value: T, on_change: Box<dyn Fn(T) -> Message + 'a>, on_release: Option<Message>, - width: u16, + width: f32, height: Length, style: <Renderer::Theme as StyleSheet>::Style, } @@ -60,7 +60,7 @@ where Renderer::Theme: StyleSheet, { /// The default width of a [`VerticalSlider`]. - pub const DEFAULT_WIDTH: u16 = 22; + pub const DEFAULT_WIDTH: f32 = 22.0; /// Creates a new [`VerticalSlider`]. /// @@ -110,14 +110,14 @@ where } /// Sets the width of the [`VerticalSlider`]. - pub fn width(mut self, width: u16) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into<Pixels>) -> Self { + self.width = width.into().0; self } /// Sets the height of the [`VerticalSlider`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into<Length>) -> Self { + self.height = height.into(); self } @@ -166,9 +166,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = - limits.width(Length::Units(self.width)).height(self.height); - + let limits = limits.width(self.width).height(self.height); let size = limits.resolve(Size::ZERO); layout::Node::new(size) |