Use f32 in Length::Units and rename it to Fixed

This commit is contained in:
Héctor Ramón Jiménez 2023-02-04 12:24:13 +01:00
parent f75e020257
commit 7b8b01f560
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
43 changed files with 269 additions and 262 deletions

View file

@ -1,5 +1,5 @@
/// The strategy used to fill space in a specific dimension. /// The strategy used to fill space in a specific dimension.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum Length { pub enum Length {
/// Fill all the remaining space /// Fill all the remaining space
Fill, Fill,
@ -17,7 +17,7 @@ pub enum Length {
Shrink, Shrink,
/// Fill a fixed amount of space /// Fill a fixed amount of space
Units(u16), Fixed(f32),
} }
impl Length { impl Length {
@ -31,13 +31,19 @@ impl Length {
Length::Fill => 1, Length::Fill => 1,
Length::FillPortion(factor) => *factor, Length::FillPortion(factor) => *factor,
Length::Shrink => 0, Length::Shrink => 0,
Length::Units(_) => 0, Length::Fixed(_) => 0,
} }
} }
} }
impl From<f32> for Length {
fn from(amount: f32) -> Self {
Length::Fixed(amount)
}
}
impl From<u16> for Length { impl From<u16> for Length {
fn from(units: u16) -> Self { fn from(units: u16) -> Self {
Length::Units(units) Length::Fixed(f32::from(units))
} }
} }

View file

@ -35,6 +35,7 @@ mod content_fit;
mod font; mod font;
mod length; mod length;
mod padding; mod padding;
mod pixels;
mod point; mod point;
mod rectangle; mod rectangle;
mod size; mod size;
@ -47,6 +48,7 @@ pub use content_fit::ContentFit;
pub use font::Font; pub use font::Font;
pub use length::Length; pub use length::Length;
pub use padding::Padding; pub use padding::Padding;
pub use pixels::Pixels;
pub use point::Point; pub use point::Point;
pub use rectangle::Rectangle; pub use rectangle::Rectangle;
pub use size::Size; pub use size::Size;

22
core/src/pixels.rs Normal file
View file

@ -0,0 +1,22 @@
/// An amount of logical pixels.
///
/// Normally used to represent an amount of space, or the size of something.
///
/// This type is normally asked as an argument in a generic way
/// (e.g. `impl Into<Pixels>`) and, since `Pixels` implements `From` both for
/// `f32` and `u16`, you should be able to provide both integers and float
/// literals as needed.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Pixels(pub f32);
impl From<f32> for Pixels {
fn from(amount: f32) -> Self {
Self(amount)
}
}
impl From<u16> for Pixels {
fn from(amount: u16) -> Self {
Self(f32::from(amount))
}
}

View file

@ -301,11 +301,11 @@ impl<C: ColorSpace + Copy> ColorPicker<C> {
} }
row![ row![
text(C::LABEL).width(Length::Units(50)), text(C::LABEL).width(50),
slider(cr1, c1, move |v| C::new(v, c2, c3)), slider(cr1, c1, move |v| C::new(v, c2, c3)),
slider(cr2, c2, move |v| C::new(c1, v, c3)), slider(cr2, c2, move |v| C::new(c1, v, c3)),
slider(cr3, c3, move |v| C::new(c1, c2, v)), slider(cr3, c3, move |v| C::new(c1, c2, v)),
text(color.to_string()).width(Length::Units(185)).size(14), text(color.to_string()).width(185).size(14),
] ]
.spacing(10) .spacing(10)
.align_items(Alignment::Center) .align_items(Alignment::Center)

View file

@ -127,7 +127,7 @@ mod numeric_input {
.horizontal_alignment(alignment::Horizontal::Center) .horizontal_alignment(alignment::Horizontal::Center)
.vertical_alignment(alignment::Vertical::Center), .vertical_alignment(alignment::Vertical::Center),
) )
.width(Length::Units(50)) .width(50)
.on_press(on_press) .on_press(on_press)
}; };

View file

@ -93,7 +93,7 @@ impl Application for Events {
.width(Length::Fill) .width(Length::Fill)
.horizontal_alignment(alignment::Horizontal::Center), .horizontal_alignment(alignment::Horizontal::Center),
) )
.width(Length::Units(100)) .width(100)
.padding(10) .padding(10)
.on_press(Message::Exit); .on_press(Message::Exit);

View file

@ -42,7 +42,7 @@ impl Program for Controls {
let background_color = self.background_color; let background_color = self.background_color;
let sliders = Row::new() let sliders = Row::new()
.width(Length::Units(500)) .width(500)
.spacing(20) .spacing(20)
.push( .push(
Slider::new(0.0..=1.0, background_color.r, move |r| { Slider::new(0.0..=1.0, background_color.r, move |r| {

View file

@ -48,7 +48,7 @@ impl Program for Controls {
let text = &self.text; let text = &self.text;
let sliders = Row::new() let sliders = Row::new()
.width(Length::Units(500)) .width(500)
.spacing(20) .spacing(20)
.push( .push(
slider(0.0..=1.0, background_color.r, move |r| { slider(0.0..=1.0, background_color.r, move |r| {

View file

@ -156,7 +156,7 @@ impl Application for App {
] ]
.spacing(20), .spacing(20),
) )
.width(Length::Units(300)) .width(300)
.padding(10) .padding(10)
.style(theme::Container::Box); .style(theme::Container::Box);

View file

@ -43,10 +43,10 @@ impl Sandbox for Example {
.placeholder("Choose a language..."); .placeholder("Choose a language...");
let content = column![ let content = column![
vertical_space(Length::Units(600)), vertical_space(600),
"Which is your favorite language?", "Which is your favorite language?",
pick_list, pick_list,
vertical_space(Length::Units(600)), vertical_space(600),
] ]
.width(Length::Fill) .width(Length::Fill)
.align_items(Alignment::Center) .align_items(Alignment::Center)

View file

@ -58,7 +58,7 @@ impl Sandbox for QRGenerator {
.padding(15); .padding(15);
let mut content = column![title, input] let mut content = column![title, input]
.width(Length::Units(700)) .width(700)
.spacing(20) .spacing(20)
.align_items(Alignment::Center); .align_items(Alignment::Center);

View file

@ -187,9 +187,9 @@ impl Application for ScrollableDemo {
column![ column![
scroll_to_end_button(), scroll_to_end_button(),
text("Beginning!"), text("Beginning!"),
vertical_space(Length::Units(1200)), vertical_space(1200),
text("Middle!"), text("Middle!"),
vertical_space(Length::Units(1200)), vertical_space(1200),
text("End!"), text("End!"),
scroll_to_beginning_button(), scroll_to_beginning_button(),
] ]
@ -211,13 +211,13 @@ impl Application for ScrollableDemo {
row![ row![
scroll_to_end_button(), scroll_to_end_button(),
text("Beginning!"), text("Beginning!"),
horizontal_space(Length::Units(1200)), horizontal_space(1200),
text("Middle!"), text("Middle!"),
horizontal_space(Length::Units(1200)), horizontal_space(1200),
text("End!"), text("End!"),
scroll_to_beginning_button(), scroll_to_beginning_button(),
] ]
.height(Length::Units(450)) .height(450)
.align_items(Alignment::Center) .align_items(Alignment::Center)
.padding([0, 40, 0, 40]) .padding([0, 40, 0, 40])
.spacing(40), .spacing(40),
@ -237,26 +237,26 @@ impl Application for ScrollableDemo {
row![ row![
column![ column![
text("Let's do some scrolling!"), text("Let's do some scrolling!"),
vertical_space(Length::Units(2400)) vertical_space(2400)
], ],
scroll_to_end_button(), scroll_to_end_button(),
text("Horizontal - Beginning!"), text("Horizontal - Beginning!"),
horizontal_space(Length::Units(1200)), horizontal_space(1200),
//vertical content //vertical content
column![ column![
text("Horizontal - Middle!"), text("Horizontal - Middle!"),
scroll_to_end_button(), scroll_to_end_button(),
text("Vertical - Beginning!"), text("Vertical - Beginning!"),
vertical_space(Length::Units(1200)), vertical_space(1200),
text("Vertical - Middle!"), text("Vertical - Middle!"),
vertical_space(Length::Units(1200)), vertical_space(1200),
text("Vertical - End!"), text("Vertical - End!"),
scroll_to_beginning_button(), scroll_to_beginning_button(),
vertical_space(Length::Units(40)), vertical_space(40),
] ]
.align_items(Alignment::Fill) .align_items(Alignment::Fill)
.spacing(40), .spacing(40),
horizontal_space(Length::Units(1200)), horizontal_space(1200),
text("Horizontal - End!"), text("Horizontal - End!"),
scroll_to_beginning_button(), scroll_to_beginning_button(),
] ]

View file

@ -38,11 +38,11 @@ impl Sandbox for Slider {
let h_slider = let h_slider =
container(slider(0..=100, value, Message::SliderChanged)) container(slider(0..=100, value, Message::SliderChanged))
.width(Length::Units(250)); .width(250);
let v_slider = let v_slider =
container(vertical_slider(0..=100, value, Message::SliderChanged)) container(vertical_slider(0..=100, value, Message::SliderChanged))
.height(Length::Units(200)); .height(200);
let text = text(format!("{value}")); let text = text(format!("{value}"));

View file

@ -105,7 +105,7 @@ impl Application for Stopwatch {
text(label).horizontal_alignment(alignment::Horizontal::Center), text(label).horizontal_alignment(alignment::Horizontal::Center),
) )
.padding(10) .padding(10)
.width(Length::Units(80)) .width(80)
}; };
let toggle_button = { let toggle_button = {

View file

@ -108,14 +108,10 @@ impl Sandbox for Styling {
let progress_bar = progress_bar(0.0..=100.0, self.slider_value); let progress_bar = progress_bar(0.0..=100.0, self.slider_value);
let scrollable = scrollable( let scrollable = scrollable(
column![ column!["Scroll me!", vertical_space(800), "You did it!"]
"Scroll me!", .width(Length::Fill),
vertical_space(Length::Units(800)),
"You did it!"
]
.width(Length::Fill),
) )
.height(Length::Units(100)); .height(100);
let checkbox = checkbox( let checkbox = checkbox(
"Check me!", "Check me!",
@ -143,7 +139,7 @@ impl Sandbox for Styling {
column![checkbox, toggler].spacing(20) column![checkbox, toggler].spacing(20)
] ]
.spacing(10) .spacing(10)
.height(Length::Units(100)) .height(100)
.align_items(Alignment::Center), .align_items(Alignment::Center),
] ]
.spacing(20) .spacing(20)

View file

@ -460,7 +460,7 @@ fn empty_message(message: &str) -> Element<'_, Message> {
.style(Color::from([0.7, 0.7, 0.7])), .style(Color::from([0.7, 0.7, 0.7])),
) )
.width(Length::Fill) .width(Length::Fill)
.height(Length::Units(200)) .height(200)
.center_y() .center_y()
.into() .into()
} }
@ -474,7 +474,7 @@ const ICONS: Font = Font::External {
fn icon(unicode: char) -> Text<'static> { fn icon(unicode: char) -> Text<'static> {
text(unicode.to_string()) text(unicode.to_string())
.font(ICONS) .font(ICONS)
.width(Length::Units(20)) .width(20)
.horizontal_alignment(alignment::Horizontal::Center) .horizontal_alignment(alignment::Horizontal::Center)
.size(20) .size(20)
} }

View file

@ -513,14 +513,14 @@ impl<'a> Step {
text("Tip: You can use the scrollbar to scroll down faster!") text("Tip: You can use the scrollbar to scroll down faster!")
.size(16), .size(16),
) )
.push(vertical_space(Length::Units(4096))) .push(vertical_space(4096))
.push( .push(
text("You are halfway there!") text("You are halfway there!")
.width(Length::Fill) .width(Length::Fill)
.size(30) .size(30)
.horizontal_alignment(alignment::Horizontal::Center), .horizontal_alignment(alignment::Horizontal::Center),
) )
.push(vertical_space(Length::Units(4096))) .push(vertical_space(4096))
.push(ferris(300)) .push(ferris(300))
.push( .push(
text("You made it!") text("You made it!")
@ -605,7 +605,7 @@ fn ferris<'a>(width: u16) -> Container<'a, StepMessage> {
} else { } else {
image(format!("{}/images/ferris.png", env!("CARGO_MANIFEST_DIR"))) image(format!("{}/images/ferris.png", env!("CARGO_MANIFEST_DIR")))
} }
.width(Length::Units(width)), .width(width),
) )
.width(Length::Fill) .width(Length::Fill)
.center_x() .center_x()
@ -616,7 +616,7 @@ fn button<'a, Message: Clone>(label: &str) -> Button<'a, Message> {
text(label).horizontal_alignment(alignment::Horizontal::Center), text(label).horizontal_alignment(alignment::Horizontal::Center),
) )
.padding(12) .padding(12)
.width(Length::Units(100)) .width(100)
} }
fn color_slider<'a>( fn color_slider<'a>(

View file

@ -100,13 +100,13 @@ impl<Message, Theme, P> Canvas<Message, Theme, P>
where where
P: Program<Message, Theme>, P: Program<Message, Theme>,
{ {
const DEFAULT_SIZE: u16 = 100; const DEFAULT_SIZE: f32 = 100.0;
/// Creates a new [`Canvas`]. /// Creates a new [`Canvas`].
pub fn new(program: P) -> Self { pub fn new(program: P) -> Self {
Canvas { Canvas {
width: Length::Units(Self::DEFAULT_SIZE), width: Length::Fixed(Self::DEFAULT_SIZE),
height: Length::Units(Self::DEFAULT_SIZE), height: Length::Fixed(Self::DEFAULT_SIZE),
program, program,
message_: PhantomData, message_: PhantomData,
theme_: PhantomData, theme_: PhantomData,
@ -114,14 +114,14 @@ where
} }
/// Sets the width of the [`Canvas`]. /// Sets the width of the [`Canvas`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`Canvas`]. /// Sets the height of the [`Canvas`].
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }
} }

View file

@ -42,7 +42,7 @@ where
content: RefCell::new(Content { content: RefCell::new(Content {
size: Size::ZERO, size: Size::ZERO,
layout: layout::Node::new(Size::ZERO), layout: layout::Node::new(Size::ZERO),
element: Element::new(horizontal_space(Length::Units(0))), element: Element::new(horizontal_space(0)),
}), }),
} }
} }

View file

@ -42,17 +42,16 @@ impl Limits {
} }
/// Applies a width constraint to the current [`Limits`]. /// Applies a width constraint to the current [`Limits`].
pub fn width(mut self, width: Length) -> Limits { pub fn width(mut self, width: impl Into<Length>) -> Limits {
match width { match width.into() {
Length::Shrink => { Length::Shrink => {
self.fill.width = self.min.width; self.fill.width = self.min.width;
} }
Length::Fill | Length::FillPortion(_) => { Length::Fill | Length::FillPortion(_) => {
self.fill.width = self.fill.width.min(self.max.width); self.fill.width = self.fill.width.min(self.max.width);
} }
Length::Units(units) => { Length::Fixed(amount) => {
let new_width = let new_width = amount.min(self.max.width).max(self.min.width);
(units as f32).min(self.max.width).max(self.min.width);
self.min.width = new_width; self.min.width = new_width;
self.max.width = new_width; self.max.width = new_width;
@ -64,17 +63,17 @@ impl Limits {
} }
/// Applies a height constraint to the current [`Limits`]. /// Applies a height constraint to the current [`Limits`].
pub fn height(mut self, height: Length) -> Limits { pub fn height(mut self, height: impl Into<Length>) -> Limits {
match height { match height.into() {
Length::Shrink => { Length::Shrink => {
self.fill.height = self.min.height; self.fill.height = self.min.height;
} }
Length::Fill | Length::FillPortion(_) => { Length::Fill | Length::FillPortion(_) => {
self.fill.height = self.fill.height.min(self.max.height); self.fill.height = self.fill.height.min(self.max.height);
} }
Length::Units(units) => { Length::Fixed(amount) => {
let new_height = let new_height =
(units as f32).min(self.max.height).max(self.min.height); amount.min(self.max.height).max(self.min.height);
self.min.height = new_height; self.min.height = new_height;
self.max.height = new_height; self.max.height = new_height;
@ -86,33 +85,29 @@ impl Limits {
} }
/// Applies a minimum width constraint to the current [`Limits`]. /// Applies a minimum width constraint to the current [`Limits`].
pub fn min_width(mut self, min_width: u32) -> Limits { pub fn min_width(mut self, min_width: f32) -> Limits {
self.min.width = self.min.width = self.min.width.max(min_width).min(self.max.width);
self.min.width.max(min_width as f32).min(self.max.width);
self self
} }
/// Applies a maximum width constraint to the current [`Limits`]. /// Applies a maximum width constraint to the current [`Limits`].
pub fn max_width(mut self, max_width: u32) -> Limits { pub fn max_width(mut self, max_width: f32) -> Limits {
self.max.width = self.max.width = self.max.width.min(max_width).max(self.min.width);
self.max.width.min(max_width as f32).max(self.min.width);
self self
} }
/// Applies a minimum height constraint to the current [`Limits`]. /// Applies a minimum height constraint to the current [`Limits`].
pub fn min_height(mut self, min_height: u32) -> Limits { pub fn min_height(mut self, min_height: f32) -> Limits {
self.min.height = self.min.height = self.min.height.max(min_height).min(self.max.height);
self.min.height.max(min_height as f32).min(self.max.height);
self self
} }
/// Applies a maximum height constraint to the current [`Limits`]. /// Applies a maximum height constraint to the current [`Limits`].
pub fn max_height(mut self, max_height: u32) -> Limits { pub fn max_height(mut self, max_height: f32) -> Limits {
self.max.height = self.max.height = self.max.height.min(max_height).max(self.min.height);
self.max.height.min(max_height as f32).max(self.min.height);
self self
} }

View file

@ -81,7 +81,7 @@ pub use iced_core::alignment;
pub use iced_core::time; pub use iced_core::time;
pub use iced_core::{ pub use iced_core::{
color, Alignment, Background, Color, ContentFit, Font, Length, Padding, color, Alignment, Background, Color, ContentFit, Font, Length, Padding,
Point, Rectangle, Size, Vector, Pixels, Point, Rectangle, Size, Vector,
}; };
pub use iced_futures::{executor, futures}; pub use iced_futures::{executor, futures};
pub use iced_style::application; pub use iced_style::application;

View file

@ -28,7 +28,7 @@ where
options: &'a [T], options: &'a [T],
hovered_option: &'a mut Option<usize>, hovered_option: &'a mut Option<usize>,
last_selection: &'a mut Option<T>, last_selection: &'a mut Option<T>,
width: u16, width: f32,
padding: Padding, padding: Padding,
text_size: Option<u16>, text_size: Option<u16>,
font: Renderer::Font, font: Renderer::Font,
@ -55,7 +55,7 @@ where
options, options,
hovered_option, hovered_option,
last_selection, last_selection,
width: 0, width: 0.0,
padding: Padding::ZERO, padding: Padding::ZERO,
text_size: None, text_size: None,
font: Default::default(), font: Default::default(),
@ -64,7 +64,7 @@ where
} }
/// Sets the width of the [`Menu`]. /// Sets the width of the [`Menu`].
pub fn width(mut self, width: u16) -> Self { pub fn width(mut self, width: f32) -> Self {
self.width = width; self.width = width;
self self
} }
@ -142,7 +142,7 @@ where
{ {
state: &'a mut Tree, state: &'a mut Tree,
container: Container<'a, Message, Renderer>, container: Container<'a, Message, Renderer>,
width: u16, width: f32,
target_height: f32, target_height: f32,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -219,7 +219,7 @@ where
}, },
), ),
) )
.width(Length::Units(self.width)); .width(self.width);
let mut node = self.container.layout(renderer, &limits); let mut node = self.container.layout(renderer, &limits);

View file

@ -82,14 +82,14 @@ where
} }
/// Sets the width of the [`Button`]. /// Sets the width of the [`Button`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`Button`]. /// Sets the height of the [`Button`].
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }

View file

@ -8,8 +8,8 @@ use crate::text;
use crate::touch; use crate::touch;
use crate::widget::{self, Row, Text, Tree}; use crate::widget::{self, Row, Text, Tree};
use crate::{ use crate::{
Alignment, Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Alignment, Clipboard, Element, Layout, Length, Pixels, Point, Rectangle,
Widget, Shell, Widget,
}; };
pub use iced_style::checkbox::{Appearance, StyleSheet}; pub use iced_style::checkbox::{Appearance, StyleSheet};
@ -52,7 +52,7 @@ where
on_toggle: Box<dyn Fn(bool) -> Message + 'a>, on_toggle: Box<dyn Fn(bool) -> Message + 'a>,
label: String, label: String,
width: Length, width: Length,
size: u16, size: f32,
spacing: u16, spacing: u16,
text_size: Option<u16>, text_size: Option<u16>,
font: Renderer::Font, font: Renderer::Font,
@ -66,7 +66,7 @@ where
Renderer::Theme: StyleSheet + widget::text::StyleSheet, Renderer::Theme: StyleSheet + widget::text::StyleSheet,
{ {
/// The default size of a [`Checkbox`]. /// The default size of a [`Checkbox`].
const DEFAULT_SIZE: u16 = 20; const DEFAULT_SIZE: f32 = 20.0;
/// The default spacing of a [`Checkbox`]. /// The default spacing of a [`Checkbox`].
const DEFAULT_SPACING: u16 = 15; const DEFAULT_SPACING: u16 = 15;
@ -102,14 +102,14 @@ where
} }
/// Sets the size of the [`Checkbox`]. /// Sets the size of the [`Checkbox`].
pub fn size(mut self, size: u16) -> Self { pub fn size(mut self, size: impl Into<Pixels>) -> Self {
self.size = size; self.size = size.into().0;
self self
} }
/// Sets the width of the [`Checkbox`]. /// Sets the width of the [`Checkbox`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
@ -172,11 +172,7 @@ where
.width(self.width) .width(self.width)
.spacing(self.spacing) .spacing(self.spacing)
.align_items(Alignment::Center) .align_items(Alignment::Center)
.push( .push(Row::new().width(self.size).height(self.size))
Row::new()
.width(Length::Units(self.size))
.height(Length::Units(self.size)),
)
.push( .push(
Text::new(&self.label) Text::new(&self.label)
.font(self.font.clone()) .font(self.font.clone())

View file

@ -6,8 +6,8 @@ use crate::overlay;
use crate::renderer; use crate::renderer;
use crate::widget::{Operation, Tree}; use crate::widget::{Operation, Tree};
use crate::{ use crate::{
Alignment, Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Alignment, Clipboard, Element, Layout, Length, Padding, Pixels, Point,
Shell, Widget, Rectangle, Shell, Widget,
}; };
/// A container that distributes its contents vertically. /// A container that distributes its contents vertically.
@ -17,7 +17,7 @@ pub struct Column<'a, Message, Renderer> {
padding: Padding, padding: Padding,
width: Length, width: Length,
height: Length, height: Length,
max_width: u32, max_width: f32,
align_items: Alignment, align_items: Alignment,
children: Vec<Element<'a, Message, Renderer>>, children: Vec<Element<'a, Message, Renderer>>,
} }
@ -37,7 +37,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
padding: Padding::ZERO, padding: Padding::ZERO,
width: Length::Shrink, width: Length::Shrink,
height: Length::Shrink, height: Length::Shrink,
max_width: u32::MAX, max_width: f32::INFINITY,
align_items: Alignment::Start, align_items: Alignment::Start,
children, children,
} }
@ -60,20 +60,20 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
} }
/// Sets the width of the [`Column`]. /// Sets the width of the [`Column`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`Column`]. /// Sets the height of the [`Column`].
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }
/// Sets the maximum width of the [`Column`]. /// Sets the maximum width of the [`Column`].
pub fn max_width(mut self, max_width: u32) -> Self { pub fn max_width(mut self, max_width: impl Into<Pixels>) -> Self {
self.max_width = max_width; self.max_width = max_width.into().0;
self self
} }

View file

@ -7,12 +7,10 @@ use crate::overlay;
use crate::renderer; use crate::renderer;
use crate::widget::{self, Operation, Tree}; use crate::widget::{self, Operation, Tree};
use crate::{ use crate::{
Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Background, Clipboard, Color, Element, Layout, Length, Padding, Pixels,
Rectangle, Shell, Widget, Point, Rectangle, Shell, Widget,
}; };
use std::u32;
pub use iced_style::container::{Appearance, StyleSheet}; pub use iced_style::container::{Appearance, StyleSheet};
/// An element decorating some content. /// An element decorating some content.
@ -28,8 +26,8 @@ where
padding: Padding, padding: Padding,
width: Length, width: Length,
height: Length, height: Length,
max_width: u32, max_width: f32,
max_height: u32, max_height: f32,
horizontal_alignment: alignment::Horizontal, horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical, vertical_alignment: alignment::Vertical,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
@ -51,8 +49,8 @@ where
padding: Padding::ZERO, padding: Padding::ZERO,
width: Length::Shrink, width: Length::Shrink,
height: Length::Shrink, height: Length::Shrink,
max_width: u32::MAX, max_width: f32::INFINITY,
max_height: u32::MAX, max_height: f32::INFINITY,
horizontal_alignment: alignment::Horizontal::Left, horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top, vertical_alignment: alignment::Vertical::Top,
style: Default::default(), style: Default::default(),
@ -73,26 +71,26 @@ where
} }
/// Sets the width of the [`Container`]. /// Sets the width of the [`Container`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`Container`]. /// Sets the height of the [`Container`].
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }
/// Sets the maximum width of the [`Container`]. /// Sets the maximum width of the [`Container`].
pub fn max_width(mut self, max_width: u32) -> Self { pub fn max_width(mut self, max_width: impl Into<Pixels>) -> Self {
self.max_width = max_width; self.max_width = max_width.into().0;
self self
} }
/// Sets the maximum height of the [`Container`] in pixels. /// Sets the maximum height of the [`Container`].
pub fn max_height(mut self, max_height: u32) -> Self { pub fn max_height(mut self, max_height: impl Into<Pixels>) -> Self {
self.max_height = max_height; self.max_height = max_height.into().0;
self self
} }
@ -294,8 +292,8 @@ pub fn layout<Renderer>(
limits: &layout::Limits, limits: &layout::Limits,
width: Length, width: Length,
height: Length, height: Length,
max_width: u32, max_width: f32,
max_height: u32, max_height: f32,
padding: Padding, padding: Padding,
horizontal_alignment: alignment::Horizontal, horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical, vertical_alignment: alignment::Vertical,

View file

@ -1,7 +1,7 @@
//! Helper functions to create pure widgets. //! Helper functions to create pure widgets.
use crate::overlay; use crate::overlay;
use crate::widget; use crate::widget;
use crate::{Element, Length}; use crate::{Element, Length, Pixels};
use std::borrow::Cow; use std::borrow::Cow;
use std::ops::RangeInclusive; 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`]. /// Creates a new horizontal [`Space`] with the given [`Length`].
/// ///
/// [`Space`]: widget::Space /// [`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) widget::Space::with_width(width)
} }
/// Creates a new vertical [`Space`] with the given [`Length`]. /// Creates a new vertical [`Space`] with the given [`Length`].
/// ///
/// [`Space`]: widget::Space /// [`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) widget::Space::with_height(height)
} }
/// Creates a horizontal [`Rule`] with the given height. /// Creates a horizontal [`Rule`] with the given height.
/// ///
/// [`Rule`]: widget::Rule /// [`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 where
Renderer: crate::Renderer, Renderer: crate::Renderer,
Renderer::Theme: widget::rule::StyleSheet, Renderer::Theme: widget::rule::StyleSheet,
@ -272,7 +274,9 @@ where
/// Creates a vertical [`Rule`] with the given width. /// Creates a vertical [`Rule`] with the given width.
/// ///
/// [`Rule`]: widget::Rule /// [`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 where
Renderer: crate::Renderer, Renderer: crate::Renderer,
Renderer::Theme: widget::rule::StyleSheet, Renderer::Theme: widget::rule::StyleSheet,

View file

@ -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"> /// <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> { pub struct Image<Handle> {
handle: Handle, handle: Handle,
width: Length, width: Length,
@ -49,14 +49,14 @@ impl<Handle> Image<Handle> {
} }
/// Sets the width of the [`Image`] boundaries. /// Sets the width of the [`Image`] boundaries.
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`Image`] boundaries. /// Sets the height of the [`Image`] boundaries.
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }

View file

@ -45,14 +45,14 @@ impl<Handle> Viewer<Handle> {
} }
/// Sets the width of the [`Viewer`]. /// Sets the width of the [`Viewer`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`Viewer`]. /// Sets the height of the [`Viewer`].
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }
@ -124,7 +124,7 @@ where
// Only calculate viewport sizes if the images are constrained to a limited space. // 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. // If they are Fill|Portion let them expand within their alotted space.
match expansion_size { match expansion_size {
Length::Shrink | Length::Units(_) => { Length::Shrink | Length::Fixed(_) => {
let aspect_ratio = width as f32 / height as f32; let aspect_ratio = width as f32 / height as f32;
let viewport_aspect_ratio = size.width / size.height; let viewport_aspect_ratio = size.width / size.height;
if viewport_aspect_ratio > aspect_ratio { if viewport_aspect_ratio > aspect_ratio {

View file

@ -159,14 +159,14 @@ where
} }
/// Sets the width of the [`PaneGrid`]. /// Sets the width of the [`PaneGrid`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`PaneGrid`]. /// Sets the height of the [`PaneGrid`].
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }

View file

@ -83,8 +83,8 @@ where
} }
/// Sets the width of the [`PickList`]. /// Sets the width of the [`PickList`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
@ -539,7 +539,7 @@ where
&mut state.hovered_option, &mut state.hovered_option,
&mut state.last_selection, &mut state.last_selection,
) )
.width(bounds.width.round() as u16) .width(bounds.width)
.padding(padding) .padding(padding)
.font(font) .font(font)
.style(style); .style(style);

View file

@ -38,7 +38,7 @@ where
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
{ {
/// The default height of a [`ProgressBar`]. /// The default height of a [`ProgressBar`].
pub const DEFAULT_HEIGHT: u16 = 30; pub const DEFAULT_HEIGHT: f32 = 30.0;
/// Creates a new [`ProgressBar`]. /// Creates a new [`ProgressBar`].
/// ///
@ -56,14 +56,14 @@ where
} }
/// Sets the width of the [`ProgressBar`]. /// Sets the width of the [`ProgressBar`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`ProgressBar`]. /// Sets the height of the [`ProgressBar`].
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = Some(height); self.height = Some(height.into());
self self
} }
@ -87,7 +87,7 @@ where
} }
fn height(&self) -> Length { fn height(&self) -> Length {
self.height.unwrap_or(Length::Units(Self::DEFAULT_HEIGHT)) self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT))
} }
fn layout( fn layout(
@ -97,7 +97,7 @@ where
) -> layout::Node { ) -> layout::Node {
let limits = limits let limits = limits
.width(self.width) .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); let size = limits.resolve(Size::ZERO);

View file

@ -50,7 +50,7 @@ where
on_click: Message, on_click: Message,
label: String, label: String,
width: Length, width: Length,
size: u16, size: f32,
spacing: u16, spacing: u16,
text_size: Option<u16>, text_size: Option<u16>,
font: Renderer::Font, font: Renderer::Font,
@ -64,7 +64,7 @@ where
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
{ {
/// The default size of a [`Radio`] button. /// 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. /// The default spacing of a [`Radio`] button.
pub const DEFAULT_SPACING: u16 = 15; pub const DEFAULT_SPACING: u16 = 15;
@ -101,14 +101,14 @@ where
} }
/// Sets the size of the [`Radio`] button. /// Sets the size of the [`Radio`] button.
pub fn size(mut self, size: u16) -> Self { pub fn size(mut self, size: f32) -> Self {
self.size = size; self.size = size;
self self
} }
/// Sets the width of the [`Radio`] button. /// Sets the width of the [`Radio`] button.
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
@ -163,11 +163,7 @@ where
.width(self.width) .width(self.width)
.spacing(self.spacing) .spacing(self.spacing)
.align_items(Alignment::Center) .align_items(Alignment::Center)
.push( .push(Row::new().width(self.size).height(self.size))
Row::new()
.width(Length::Units(self.size))
.height(Length::Units(self.size)),
)
.push(Text::new(&self.label).width(self.width).size( .push(Text::new(&self.label).width(self.width).size(
self.text_size.unwrap_or_else(|| renderer.default_size()), self.text_size.unwrap_or_else(|| renderer.default_size()),
)) ))

View file

@ -58,14 +58,14 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
} }
/// Sets the width of the [`Row`]. /// Sets the width of the [`Row`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`Row`]. /// Sets the height of the [`Row`].
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }

View file

@ -2,7 +2,9 @@
use crate::layout; use crate::layout;
use crate::renderer; use crate::renderer;
use crate::widget::Tree; 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}; pub use iced_style::rule::{Appearance, FillMode, StyleSheet};
@ -25,19 +27,19 @@ where
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
{ {
/// Creates a horizontal [`Rule`] with the given height. /// Creates a horizontal [`Rule`] with the given height.
pub fn horizontal(height: u16) -> Self { pub fn horizontal(height: impl Into<Pixels>) -> Self {
Rule { Rule {
width: Length::Fill, width: Length::Fill,
height: Length::Units(height), height: Length::Fixed(height.into().0),
is_horizontal: true, is_horizontal: true,
style: Default::default(), style: Default::default(),
} }
} }
/// Creates a vertical [`Rule`] with the given width. /// Creates a vertical [`Rule`] with the given width.
pub fn vertical(width: u16) -> Self { pub fn vertical(width: impl Into<Pixels>) -> Self {
Rule { Rule {
width: Length::Units(width), width: Length::Fixed(width.into().0),
height: Length::Fill, height: Length::Fill,
is_horizontal: false, is_horizontal: false,
style: Default::default(), style: Default::default(),

View file

@ -10,8 +10,8 @@ use crate::widget;
use crate::widget::operation::{self, Operation}; use crate::widget::operation::{self, Operation};
use crate::widget::tree::{self, Tree}; use crate::widget::tree::{self, Tree};
use crate::{ use crate::{
Background, Clipboard, Color, Command, Element, Layout, Length, Point, Background, Clipboard, Color, Command, Element, Layout, Length, Pixels,
Rectangle, Shell, Size, Vector, Widget, Point, Rectangle, Shell, Size, Vector, Widget,
}; };
pub use iced_style::scrollable::StyleSheet; pub use iced_style::scrollable::StyleSheet;
@ -66,8 +66,8 @@ where
} }
/// Sets the height of the [`Scrollable`]. /// Sets the height of the [`Scrollable`].
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }
@ -108,17 +108,17 @@ where
/// Properties of a scrollbar within a [`Scrollable`]. /// Properties of a scrollbar within a [`Scrollable`].
#[derive(Debug)] #[derive(Debug)]
pub struct Properties { pub struct Properties {
width: u16, width: f32,
margin: u16, margin: f32,
scroller_width: u16, scroller_width: f32,
} }
impl Default for Properties { impl Default for Properties {
fn default() -> Self { fn default() -> Self {
Self { Self {
width: 10, width: 10.0,
margin: 0, margin: 0.0,
scroller_width: 10, scroller_width: 10.0,
} }
} }
} }
@ -131,21 +131,21 @@ impl Properties {
/// Sets the scrollbar width of the [`Scrollable`] . /// Sets the scrollbar width of the [`Scrollable`] .
/// Silently enforces a minimum width of 1. /// Silently enforces a minimum width of 1.
pub fn width(mut self, width: u16) -> Self { pub fn width(mut self, width: impl Into<Pixels>) -> Self {
self.width = width.max(1); self.width = width.into().0.max(1.0);
self self
} }
/// Sets the scrollbar margin of the [`Scrollable`] . /// Sets the scrollbar margin of the [`Scrollable`] .
pub fn margin(mut self, margin: u16) -> Self { pub fn margin(mut self, margin: impl Into<Pixels>) -> Self {
self.margin = margin; self.margin = margin.into().0;
self self
} }
/// Sets the scroller width of the [`Scrollable`] . /// Sets the scroller width of the [`Scrollable`] .
/// Silently enforces a minimum width of 1. /// Silently enforces a minimum width of 1.
pub fn scroller_width(mut self, scroller_width: u16) -> Self { pub fn scroller_width(mut self, scroller_width: impl Into<Pixels>) -> Self {
self.scroller_width = scroller_width.max(1); self.scroller_width = scroller_width.into().0.max(1.0);
self self
} }
} }
@ -398,11 +398,11 @@ pub fn layout<Renderer>(
layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
) -> layout::Node { ) -> layout::Node {
let limits = limits let limits = limits
.max_height(u32::MAX) .max_height(f32::INFINITY)
.max_width(if horizontal_enabled { .max_width(if horizontal_enabled {
u32::MAX f32::INFINITY
} else { } else {
limits.max().width as u32 limits.max().width
}) })
.width(width) .width(width)
.height(height); .height(height);
@ -1100,26 +1100,26 @@ impl Scrollbars {
// Adjust the height of the vertical scrollbar if the horizontal scrollbar // Adjust the height of the vertical scrollbar if the horizontal scrollbar
// is present // is present
let x_scrollbar_height = show_scrollbar_x.map_or(0.0, |h| { let x_scrollbar_height = show_scrollbar_x
(h.width.max(h.scroller_width) + h.margin) as f32 .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 // Total bounds of the scrollbar + margin + scroller width
let total_scrollbar_bounds = Rectangle { 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, y: bounds.y,
width: total_scrollbar_width as f32, width: total_scrollbar_width,
height: (bounds.height - x_scrollbar_height).max(0.0), height: (bounds.height - x_scrollbar_height).max(0.0),
}; };
// Bounds of just the scrollbar // Bounds of just the scrollbar
let scrollbar_bounds = Rectangle { let scrollbar_bounds = Rectangle {
x: bounds.x + bounds.width x: bounds.x + bounds.width - total_scrollbar_width / 2.0
- f32::from(total_scrollbar_width / 2 + width / 2), + width / 2.0,
y: bounds.y, y: bounds.y,
width: width as f32, width,
height: (bounds.height - x_scrollbar_height).max(0.0), height: (bounds.height - x_scrollbar_height).max(0.0),
}; };
@ -1129,11 +1129,11 @@ impl Scrollbars {
let scroller_offset = offset.y * ratio; let scroller_offset = offset.y * ratio;
let scroller_bounds = Rectangle { let scroller_bounds = Rectangle {
x: bounds.x + bounds.width x: bounds.x + bounds.width - total_scrollbar_width / 2.0
- f32::from(total_scrollbar_width / 2 + scroller_width / 2), + scroller_width / 2.0,
y: (scrollbar_bounds.y + scroller_offset - x_scrollbar_height) y: (scrollbar_bounds.y + scroller_offset - x_scrollbar_height)
.max(0.0), .max(0.0),
width: scroller_width as f32, width: scroller_width,
height: scroller_height, height: scroller_height,
}; };
@ -1158,27 +1158,27 @@ impl Scrollbars {
// Need to adjust the width of the horizontal scrollbar if the vertical scrollbar // Need to adjust the width of the horizontal scrollbar if the vertical scrollbar
// is present // is present
let scrollbar_y_width = y_scrollbar.map_or(0.0, |_| { let scrollbar_y_width = y_scrollbar.map_or(0.0, |_| {
(vertical.width.max(vertical.scroller_width) + vertical.margin) vertical.width.max(vertical.scroller_width) + vertical.margin
as f32
}); });
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 // Total bounds of the scrollbar + margin + scroller width
let total_scrollbar_bounds = Rectangle { let total_scrollbar_bounds = Rectangle {
x: bounds.x, 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), width: (bounds.width - scrollbar_y_width).max(0.0),
height: total_scrollbar_height as f32, height: total_scrollbar_height,
}; };
// Bounds of just the scrollbar // Bounds of just the scrollbar
let scrollbar_bounds = Rectangle { let scrollbar_bounds = Rectangle {
x: bounds.x, x: bounds.x,
y: bounds.y + bounds.height y: bounds.y + bounds.height - total_scrollbar_height / 2.0
- f32::from(total_scrollbar_height / 2 + width / 2), + width / 2.0,
width: (bounds.width - scrollbar_y_width).max(0.0), width: (bounds.width - scrollbar_y_width).max(0.0),
height: width as f32, height: width,
}; };
let ratio = bounds.width / content_bounds.width; let ratio = bounds.width / content_bounds.width;
@ -1189,12 +1189,11 @@ impl Scrollbars {
let scroller_bounds = Rectangle { let scroller_bounds = Rectangle {
x: (scrollbar_bounds.x + scroller_offset - scrollbar_y_width) x: (scrollbar_bounds.x + scroller_offset - scrollbar_y_width)
.max(0.0), .max(0.0),
y: bounds.y + bounds.height y: bounds.y + bounds.height - total_scrollbar_height / 2.0
- f32::from( + scroller_width / 2.0,
total_scrollbar_height / 2 + scroller_width / 2,
),
width: scroller_length, width: scroller_length,
height: scroller_width as f32, height: scroller_width,
}; };
Some(Scrollbar { Some(Scrollbar {

View file

@ -54,7 +54,7 @@ where
on_change: Box<dyn Fn(T) -> Message + 'a>, on_change: Box<dyn Fn(T) -> Message + 'a>,
on_release: Option<Message>, on_release: Option<Message>,
width: Length, width: Length,
height: u16, height: f32,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -66,7 +66,7 @@ where
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
{ {
/// The default height of a [`Slider`]. /// The default height of a [`Slider`].
pub const DEFAULT_HEIGHT: u16 = 22; pub const DEFAULT_HEIGHT: f32 = 22.0;
/// Creates a new [`Slider`]. /// Creates a new [`Slider`].
/// ///
@ -116,13 +116,13 @@ where
} }
/// Sets the width of the [`Slider`]. /// Sets the width of the [`Slider`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`Slider`]. /// Sets the height of the [`Slider`].
pub fn height(mut self, height: u16) -> Self { pub fn height(mut self, height: f32) -> Self {
self.height = height; self.height = height;
self self
} }
@ -172,9 +172,7 @@ where
_renderer: &Renderer, _renderer: &Renderer,
limits: &layout::Limits, limits: &layout::Limits,
) -> layout::Node { ) -> layout::Node {
let limits = let limits = limits.width(self.width).height(self.height);
limits.width(self.width).height(Length::Units(self.height));
let size = limits.resolve(Size::ZERO); let size = limits.resolve(Size::ZERO);
layout::Node::new(size) layout::Node::new(size)

View file

@ -15,23 +15,26 @@ pub struct Space {
impl Space { impl Space {
/// Creates an amount of empty [`Space`] with the given width and height. /// Creates an amount of empty [`Space`] with the given width and height.
pub fn new(width: Length, height: Length) -> Self { pub fn new(width: impl Into<Length>, height: impl Into<Length>) -> Self {
Space { width, height } Space {
width: width.into(),
height: height.into(),
}
} }
/// Creates an amount of horizontal [`Space`]. /// Creates an amount of horizontal [`Space`].
pub fn with_width(width: Length) -> Self { pub fn with_width(width: impl Into<Length>) -> Self {
Space { Space {
width, width: width.into(),
height: Length::Shrink, height: Length::Shrink,
} }
} }
/// Creates an amount of vertical [`Space`]. /// Creates an amount of vertical [`Space`].
pub fn with_height(height: Length) -> Self { pub fn with_height(height: impl Into<Length>) -> Self {
Space { Space {
width: Length::Shrink, width: Length::Shrink,
height, height: height.into(),
} }
} }
} }

View file

@ -56,15 +56,15 @@ where
/// Sets the width of the [`Svg`]. /// Sets the width of the [`Svg`].
#[must_use] #[must_use]
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`Svg`]. /// Sets the height of the [`Svg`].
#[must_use] #[must_use]
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }

View file

@ -84,14 +84,14 @@ where
} }
/// Sets the width of the [`Text`] boundaries. /// Sets the width of the [`Text`] boundaries.
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
/// Sets the height of the [`Text`] boundaries. /// Sets the height of the [`Text`] boundaries.
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }

View file

@ -133,8 +133,8 @@ where
self self
} }
/// Sets the width of the [`TextInput`]. /// Sets the width of the [`TextInput`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
@ -387,11 +387,7 @@ where
let text_size = size.unwrap_or_else(|| renderer.default_size()); let text_size = size.unwrap_or_else(|| renderer.default_size());
let padding = padding.fit(Size::ZERO, limits.max()); let padding = padding.fit(Size::ZERO, limits.max());
let limits = limits.width(width).pad(padding).height(text_size);
let limits = limits
.width(width)
.pad(padding)
.height(Length::Units(text_size));
let mut text = layout::Node::new(limits.resolve(Size::ZERO)); 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.into(), padding.top.into()));

View file

@ -38,7 +38,7 @@ where
on_toggle: Box<dyn Fn(bool) -> Message + 'a>, on_toggle: Box<dyn Fn(bool) -> Message + 'a>,
label: Option<String>, label: Option<String>,
width: Length, width: Length,
size: u16, size: f32,
text_size: Option<u16>, text_size: Option<u16>,
text_alignment: alignment::Horizontal, text_alignment: alignment::Horizontal,
spacing: u16, spacing: u16,
@ -52,7 +52,7 @@ where
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
{ {
/// The default size of a [`Toggler`]. /// The default size of a [`Toggler`].
pub const DEFAULT_SIZE: u16 = 20; pub const DEFAULT_SIZE: f32 = 20.0;
/// Creates a new [`Toggler`]. /// Creates a new [`Toggler`].
/// ///
@ -85,14 +85,14 @@ where
} }
/// Sets the size of the [`Toggler`]. /// Sets the size of the [`Toggler`].
pub fn size(mut self, size: u16) -> Self { pub fn size(mut self, size: f32) -> Self {
self.size = size; self.size = size;
self self
} }
/// Sets the width of the [`Toggler`]. /// Sets the width of the [`Toggler`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width; self.width = width.into();
self self
} }
@ -169,11 +169,7 @@ where
); );
} }
row = row.push( row = row.push(Row::new().width(2.0 * self.size).height(self.size));
Row::new()
.width(Length::Units(2 * self.size))
.height(Length::Units(self.size)),
);
row.layout(renderer, limits) row.layout(renderer, limits)
} }

View file

@ -47,7 +47,7 @@ where
value: T, value: T,
on_change: Box<dyn Fn(T) -> Message + 'a>, on_change: Box<dyn Fn(T) -> Message + 'a>,
on_release: Option<Message>, on_release: Option<Message>,
width: u16, width: f32,
height: Length, height: Length,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -60,7 +60,7 @@ where
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
{ {
/// The default width of a [`VerticalSlider`]. /// The default width of a [`VerticalSlider`].
pub const DEFAULT_WIDTH: u16 = 22; pub const DEFAULT_WIDTH: f32 = 22.0;
/// Creates a new [`VerticalSlider`]. /// Creates a new [`VerticalSlider`].
/// ///
@ -110,14 +110,14 @@ where
} }
/// Sets the width of the [`VerticalSlider`]. /// Sets the width of the [`VerticalSlider`].
pub fn width(mut self, width: u16) -> Self { pub fn width(mut self, width: f32) -> Self {
self.width = width; self.width = width;
self self
} }
/// Sets the height of the [`VerticalSlider`]. /// Sets the height of the [`VerticalSlider`].
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height; self.height = height.into();
self self
} }
@ -166,9 +166,7 @@ where
_renderer: &Renderer, _renderer: &Renderer,
limits: &layout::Limits, limits: &layout::Limits,
) -> layout::Node { ) -> layout::Node {
let limits = let limits = limits.width(self.width).height(self.height);
limits.width(Length::Units(self.width)).height(self.height);
let size = limits.resolve(Size::ZERO); let size = limits.resolve(Size::ZERO);
layout::Node::new(size) layout::Node::new(size)