From 0655a20ad119e2e9790afcc45039fd4ac0e7d432 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 16 Mar 2023 20:23:25 +0100 Subject: Make `Shrink` have priority over `Fill` in layout --- core/src/layout.rs | 12 ++--- core/src/layout/flex.rs | 29 +++++++---- core/src/layout/limits.rs | 83 +++++++++++++------------------ core/src/layout/node.rs | 29 ++++++++++- core/src/padding.rs | 6 +++ core/src/size.rs | 24 +++++---- core/src/widget/text.rs | 2 +- examples/game_of_life/src/main.rs | 4 +- examples/geometry/src/main.rs | 2 +- examples/integration/src/controls.rs | 39 ++++++--------- examples/loading_spinners/src/circular.rs | 2 +- examples/loading_spinners/src/linear.rs | 2 +- examples/modal/src/main.rs | 13 ++--- examples/pane_grid/src/main.rs | 1 - examples/pick_list/src/main.rs | 1 - examples/scrollable/src/main.rs | 29 ++++------- examples/sierpinski_triangle/src/main.rs | 2 - examples/styling/src/main.rs | 9 ++-- examples/svg/src/main.rs | 1 - examples/toast/src/main.rs | 15 ++---- examples/tour/src/main.rs | 6 +-- examples/websocket/src/main.rs | 2 - widget/src/button.rs | 13 +++-- widget/src/canvas.rs | 3 +- widget/src/column.rs | 9 ++-- widget/src/container.rs | 33 ++++++------ widget/src/image.rs | 2 +- widget/src/image/viewer.rs | 9 ++-- widget/src/keyed/column.rs | 2 + widget/src/overlay/menu.rs | 12 ++--- widget/src/pane_grid.rs | 9 ++-- widget/src/pane_grid/content.rs | 9 ++-- widget/src/pane_grid/title_bar.rs | 18 +++---- widget/src/pick_list.rs | 7 ++- widget/src/progress_bar.rs | 10 ++-- widget/src/row.rs | 6 +-- widget/src/rule.rs | 4 +- widget/src/scrollable.rs | 2 +- widget/src/shader.rs | 2 +- widget/src/slider.rs | 3 +- widget/src/space.rs | 4 +- widget/src/svg.rs | 5 +- widget/src/text_editor.rs | 2 +- widget/src/text_input.rs | 57 ++++++++++----------- widget/src/tooltip.rs | 2 +- widget/src/vertical_slider.rs | 3 +- 46 files changed, 265 insertions(+), 274 deletions(-) diff --git a/core/src/layout.rs b/core/src/layout.rs index caf315b6..277473fe 100644 --- a/core/src/layout.rs +++ b/core/src/layout.rs @@ -71,12 +71,12 @@ pub fn next_to_each_other( left: impl FnOnce(&Limits) -> Node, right: impl FnOnce(&Limits) -> Node, ) -> Node { - let mut left_node = left(limits); + let left_node = left(limits); let left_size = left_node.size(); let right_limits = limits.shrink(Size::new(left_size.width + spacing, 0.0)); - let mut right_node = right(&right_limits); + let right_node = right(&right_limits); let right_size = right_node.size(); let (left_y, right_y) = if left_size.height > right_size.height { @@ -85,14 +85,14 @@ pub fn next_to_each_other( ((right_size.height - left_size.height) / 2.0, 0.0) }; - left_node.move_to(Point::new(0.0, left_y)); - right_node.move_to(Point::new(left_size.width + spacing, right_y)); - Node::with_children( Size::new( left_size.width + spacing + right_size.width, left_size.height.max(right_size.height), ), - vec![left_node, right_node], + vec![ + left_node.move_to(Point::new(0.0, left_y)), + right_node.move_to(Point::new(left_size.width + spacing, right_y)), + ], ) } diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index c02b63d8..738dc81d 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -20,7 +20,7 @@ use crate::Element; use crate::layout::{Limits, Node}; use crate::widget; -use crate::{Alignment, Padding, Point, Size}; +use crate::{Alignment, Length, Padding, Point, Size}; /// The main axis of a flex layout. #[derive(Debug)] @@ -63,6 +63,8 @@ pub fn resolve( axis: Axis, renderer: &Renderer, limits: &Limits, + width: Length, + height: Length, padding: Padding, spacing: f32, align_items: Alignment, @@ -72,12 +74,12 @@ pub fn resolve( where Renderer: crate::Renderer, { - let limits = limits.pad(padding); + let limits = limits.width(width).height(height).shrink(padding); let total_spacing = spacing * items.len().saturating_sub(1) as f32; let max_cross = axis.cross(limits.max()); let mut fill_sum = 0; - let mut cross = axis.cross(limits.min()).max(axis.cross(limits.fill())); + let mut cross = 0.0f32; let mut available = axis.main(limits.max()) - total_spacing; let mut nodes: Vec = Vec::with_capacity(items.len()); @@ -109,7 +111,16 @@ where } } - let remaining = available.max(0.0); + let remaining = match axis { + Axis::Horizontal => match width { + Length::Shrink => 0.0, + _ => available.max(0.0), + }, + Axis::Vertical => match height { + Length::Shrink => 0.0, + _ => available.max(0.0), + }, + }; for (i, (child, tree)) in items.iter().zip(trees).enumerate() { let fill_factor = match axis { @@ -154,18 +165,18 @@ where let (x, y) = axis.pack(main, pad.1); - node.move_to(Point::new(x, y)); + node.move_to_mut(Point::new(x, y)); match axis { Axis::Horizontal => { - node.align( + node.align_mut( Alignment::Start, align_items, Size::new(0.0, cross), ); } Axis::Vertical => { - node.align( + node.align_mut( align_items, Alignment::Start, Size::new(cross, 0.0), @@ -179,7 +190,7 @@ where } let (width, height) = axis.pack(main - pad.0, cross); - let size = limits.resolve(Size::new(width, height)); + let size = limits.resolve(Size::new(width, height), width, height); - Node::with_children(size.pad(padding), nodes) + Node::with_children(size.expand(padding), nodes) } diff --git a/core/src/layout/limits.rs b/core/src/layout/limits.rs index 39a3d98b..eef4c4c9 100644 --- a/core/src/layout/limits.rs +++ b/core/src/layout/limits.rs @@ -1,12 +1,11 @@ #![allow(clippy::manual_clamp)] -use crate::{Length, Padding, Size}; +use crate::{Length, Size}; /// A set of size constraints for layouting. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Limits { min: Size, max: Size, - fill: Size, } impl Limits { @@ -14,16 +13,11 @@ impl Limits { pub const NONE: Limits = Limits { min: Size::ZERO, max: Size::INFINITY, - fill: Size::INFINITY, }; /// Creates new [`Limits`] with the given minimum and maximum [`Size`]. pub const fn new(min: Size, max: Size) -> Limits { - Limits { - min, - max, - fill: Size::INFINITY, - } + Limits { min, max } } /// Returns the minimum [`Size`] of the [`Limits`]. @@ -36,26 +30,15 @@ impl Limits { self.max } - /// Returns the fill [`Size`] of the [`Limits`]. - pub fn fill(&self) -> Size { - self.fill - } - /// Applies a width constraint to the current [`Limits`]. pub fn width(mut self, width: impl Into) -> Limits { match width.into() { - Length::Shrink => { - self.fill.width = self.min.width; - } - Length::Fill | Length::FillPortion(_) => { - self.fill.width = self.fill.width.min(self.max.width); - } + Length::Shrink | Length::Fill | Length::FillPortion(_) => {} Length::Fixed(amount) => { let new_width = amount.min(self.max.width).max(self.min.width); self.min.width = new_width; self.max.width = new_width; - self.fill.width = new_width; } } @@ -65,19 +48,13 @@ impl Limits { /// Applies a height constraint to the current [`Limits`]. pub fn height(mut self, height: impl Into) -> Limits { match height.into() { - Length::Shrink => { - self.fill.height = self.min.height; - } - Length::Fill | Length::FillPortion(_) => { - self.fill.height = self.fill.height.min(self.max.height); - } + Length::Shrink | Length::Fill | Length::FillPortion(_) => {} Length::Fixed(amount) => { let new_height = amount.min(self.max.height).max(self.min.height); self.min.height = new_height; self.max.height = new_height; - self.fill.height = new_height; } } @@ -112,13 +89,10 @@ impl Limits { self } - /// Shrinks the current [`Limits`] to account for the given padding. - pub fn pad(&self, padding: Padding) -> Limits { - self.shrink(Size::new(padding.horizontal(), padding.vertical())) - } - /// Shrinks the current [`Limits`] by the given [`Size`]. - pub fn shrink(&self, size: Size) -> Limits { + pub fn shrink(&self, size: impl Into) -> Limits { + let size = size.into(); + let min = Size::new( (self.min().width - size.width).max(0.0), (self.min().height - size.height).max(0.0), @@ -129,12 +103,7 @@ impl Limits { (self.max().height - size.height).max(0.0), ); - let fill = Size::new( - (self.fill.width - size.width).max(0.0), - (self.fill.height - size.height).max(0.0), - ); - - Limits { min, max, fill } + Limits { min, max } } /// Removes the minimum width constraint for the current [`Limits`]. @@ -142,22 +111,38 @@ impl Limits { Limits { min: Size::ZERO, max: self.max, - fill: self.fill, } } /// Computes the resulting [`Size`] that fits the [`Limits`] given the /// intrinsic size of some content. - pub fn resolve(&self, intrinsic_size: Size) -> Size { - Size::new( - intrinsic_size - .width - .min(self.max.width) - .max(self.fill.width), - intrinsic_size + pub fn resolve( + &self, + intrinsic_size: Size, + width: impl Into, + height: impl Into, + ) -> Size { + let width = match width.into() { + Length::Fill | Length::FillPortion(_) => self.max.width, + Length::Fixed(amount) => { + amount.min(self.max.width).max(self.min.width) + } + Length::Shrink => { + intrinsic_size.width.min(self.max.width).max(self.min.width) + } + }; + + let height = match height.into() { + Length::Fill | Length::FillPortion(_) => self.max.height, + Length::Fixed(amount) => { + amount.min(self.max.height).max(self.min.height) + } + Length::Shrink => intrinsic_size .height .min(self.max.height) - .max(self.fill.height), - ) + .max(self.min.height), + }; + + Size::new(width, height) } } diff --git a/core/src/layout/node.rs b/core/src/layout/node.rs index 2b44a7d5..00087431 100644 --- a/core/src/layout/node.rs +++ b/core/src/layout/node.rs @@ -1,4 +1,4 @@ -use crate::{Alignment, Point, Rectangle, Size, Vector}; +use crate::{Alignment, Padding, Point, Rectangle, Size, Vector}; /// The bounds of an element and its children. #[derive(Debug, Clone, Default)] @@ -26,6 +26,14 @@ impl Node { } } + /// Creates a new [`Node`] that wraps a single child with some [`Padding`]. + pub fn container(child: Self, padding: Padding) -> Self { + Self::with_children( + child.bounds.size().expand(padding), + vec![child.move_to(Point::new(padding.left, padding.top))], + ) + } + /// Returns the [`Size`] of the [`Node`]. pub fn size(&self) -> Size { Size::new(self.bounds.width, self.bounds.height) @@ -43,6 +51,17 @@ impl Node { /// Aligns the [`Node`] in the given space. pub fn align( + mut self, + horizontal_alignment: Alignment, + vertical_alignment: Alignment, + space: Size, + ) -> Self { + self.align_mut(horizontal_alignment, vertical_alignment, space); + self + } + + /// Mutable reference version of [`align`]. + pub fn align_mut( &mut self, horizontal_alignment: Alignment, vertical_alignment: Alignment, @@ -70,7 +89,13 @@ impl Node { } /// Moves the [`Node`] to the given position. - pub fn move_to(&mut self, position: Point) { + pub fn move_to(mut self, position: Point) -> Self { + self.move_to_mut(position); + self + } + + /// Mutable reference version of [`move_to`]. + pub fn move_to_mut(&mut self, position: Point) { self.bounds.x = position.x; self.bounds.y = position.y; } diff --git a/core/src/padding.rs b/core/src/padding.rs index 0b1bba13..a63f6e29 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -154,3 +154,9 @@ impl From<[f32; 4]> for Padding { } } } + +impl From for Size { + fn from(padding: Padding) -> Self { + Self::new(padding.horizontal(), padding.vertical()) + } +} diff --git a/core/src/size.rs b/core/src/size.rs index 7ef2f602..90e50d13 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -1,4 +1,4 @@ -use crate::{Padding, Vector}; +use crate::Vector; /// An amount of space in 2 dimensions. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -26,15 +26,7 @@ impl Size { /// A [`Size`] with infinite width and height. pub const INFINITY: Size = Size::new(f32::INFINITY, f32::INFINITY); - /// Increments the [`Size`] to account for the given padding. - pub fn pad(&self, padding: Padding) -> Self { - Size { - width: self.width + padding.horizontal(), - height: self.height + padding.vertical(), - } - } - - /// Returns the minimum of each component of this size and another + /// Returns the minimum of each component of this size and another. pub fn min(self, other: Self) -> Self { Size { width: self.width.min(other.width), @@ -42,13 +34,23 @@ impl Size { } } - /// Returns the maximum of each component of this size and another + /// Returns the maximum of each component of this size and another. pub fn max(self, other: Self) -> Self { Size { width: self.width.max(other.width), height: self.height.max(other.height), } } + + /// Expands this [`Size`] by the given amount. + pub fn expand(self, other: impl Into) -> Self { + let other = other.into(); + + Size { + width: self.width + other.width, + height: self.height + other.height, + } + } } impl From<[f32; 2]> for Size { diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index e020b030..e47e4178 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -224,7 +224,7 @@ where shaping, }); - let size = limits.resolve(paragraph.min_bounds()); + let size = limits.resolve(paragraph.min_bounds(), width, height); layout::Node::new(size) } diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 96840143..56f7afd5 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -146,7 +146,8 @@ impl Application for GameOfLife { .view() .map(move |message| Message::Grid(message, version)), controls, - ]; + ] + .height(Length::Fill); container(content) .width(Length::Fill) @@ -178,7 +179,6 @@ fn view_controls<'a>( slider(1.0..=1000.0, speed as f32, Message::SpeedChanged), text(format!("x{speed}")).size(16), ] - .width(Length::Fill) .align_items(Alignment::Center) .spacing(10); diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index 8ab3b493..50227f1c 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -30,7 +30,7 @@ mod rainbow { _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let size = limits.width(Length::Fill).resolve(Size::ZERO); + let size = limits.resolve(Size::ZERO, Length::Fill, Length::Shrink); layout::Node::new(Size::new(size.width, size.width)) } diff --git a/examples/integration/src/controls.rs b/examples/integration/src/controls.rs index 4714c397..89a595c1 100644 --- a/examples/integration/src/controls.rs +++ b/examples/integration/src/controls.rs @@ -81,32 +81,25 @@ impl Program for Controls { ); Row::new() - .width(Length::Fill) .height(Length::Fill) .align_items(Alignment::End) .push( - Column::new() - .width(Length::Fill) - .align_items(Alignment::End) - .push( - Column::new() - .padding(10) - .spacing(10) - .push( - Text::new("Background color") - .style(Color::WHITE), - ) - .push(sliders) - .push( - Text::new(format!("{background_color:?}")) - .size(14) - .style(Color::WHITE), - ) - .push( - text_input("Placeholder", text) - .on_input(Message::TextChanged), - ), - ), + Column::new().align_items(Alignment::End).push( + Column::new() + .padding(10) + .spacing(10) + .push(Text::new("Background color").style(Color::WHITE)) + .push(sliders) + .push( + Text::new(format!("{background_color:?}")) + .size(14) + .style(Color::WHITE), + ) + .push( + text_input("Placeholder", text) + .on_input(Message::TextChanged), + ), + ), ) .into() } diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index dca8046a..a92a5dd1 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -259,7 +259,7 @@ where limits: &layout::Limits, ) -> layout::Node { let limits = limits.width(self.size).height(self.size); - let size = limits.resolve(Size::ZERO); + let size = limits.resolve(Size::ZERO, self.size, self.size); layout::Node::new(size) } diff --git a/examples/loading_spinners/src/linear.rs b/examples/loading_spinners/src/linear.rs index db10bfba..da4f1ea1 100644 --- a/examples/loading_spinners/src/linear.rs +++ b/examples/loading_spinners/src/linear.rs @@ -180,7 +180,7 @@ where limits: &layout::Limits, ) -> layout::Node { let limits = limits.width(self.width).height(self.height); - let size = limits.resolve(Size::ZERO); + let size = limits.resolve(Size::ZERO, self.width, self.height); layout::Node::new(size) } diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index acb14372..85ccf8b4 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -420,17 +420,14 @@ mod modal { .width(Length::Fill) .height(Length::Fill); - let mut child = self + let child = self .content .as_widget() - .layout(self.tree, renderer, &limits); + .layout(self.tree, renderer, &limits) + .align(Alignment::Center, Alignment::Center, limits.max()); - child.align(Alignment::Center, Alignment::Center, limits.max()); - - let mut node = layout::Node::with_children(self.size, vec![child]); - node.move_to(position); - - node + layout::Node::with_children(self.size, vec![child]) + .move_to(position) } fn on_event( diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index aa3149bb..96bb8e4e 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -297,7 +297,6 @@ fn view_content<'a>( text(format!("{}x{}", size.width, size.height)).size(24), controls, ] - .width(Length::Fill) .spacing(10) .align_items(Alignment::Center); diff --git a/examples/pick_list/src/main.rs b/examples/pick_list/src/main.rs index 21200621..bfd642f5 100644 --- a/examples/pick_list/src/main.rs +++ b/examples/pick_list/src/main.rs @@ -48,7 +48,6 @@ impl Sandbox for Example { pick_list, vertical_space(600), ] - .width(Length::Fill) .align_items(Alignment::Center) .spacing(10); diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index d82ea841..1042e7a4 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -147,35 +147,30 @@ impl Application for ScrollableDemo { text("Scroller width:"), scroller_width_slider, ] - .spacing(10) - .width(Length::Fill); + .spacing(10); - let scroll_orientation_controls = column(vec![ - text("Scrollbar direction:").into(), + let scroll_orientation_controls = column![ + text("Scrollbar direction:"), radio( "Vertical", Direction::Vertical, Some(self.scrollable_direction), Message::SwitchDirection, - ) - .into(), + ), radio( "Horizontal", Direction::Horizontal, Some(self.scrollable_direction), Message::SwitchDirection, - ) - .into(), + ), radio( "Both!", Direction::Multi, Some(self.scrollable_direction), Message::SwitchDirection, - ) - .into(), - ]) - .spacing(10) - .width(Length::Fill); + ), + ] + .spacing(10); let scroll_alignment_controls = column(vec![ text("Scrollable alignment:").into(), @@ -194,16 +189,14 @@ impl Application for ScrollableDemo { ) .into(), ]) - .spacing(10) - .width(Length::Fill); + .spacing(10); let scroll_controls = row![ scroll_slider_controls, scroll_orientation_controls, scroll_alignment_controls ] - .spacing(20) - .width(Length::Fill); + .spacing(20); let scroll_to_end_button = || { button("Scroll to end") @@ -229,7 +222,6 @@ impl Application for ScrollableDemo { text("End!"), scroll_to_beginning_button(), ] - .width(Length::Fill) .align_items(Alignment::Center) .padding([40, 0, 40, 0]) .spacing(40), @@ -341,7 +333,6 @@ impl Application for ScrollableDemo { let content: Element = column![scroll_controls, scrollable_content, progress_bars] - .width(Length::Fill) .height(Length::Fill) .align_items(Alignment::Center) .spacing(10) diff --git a/examples/sierpinski_triangle/src/main.rs b/examples/sierpinski_triangle/src/main.rs index ef935c33..01a114bb 100644 --- a/examples/sierpinski_triangle/src/main.rs +++ b/examples/sierpinski_triangle/src/main.rs @@ -79,12 +79,10 @@ impl Application for SierpinskiEmulator { row![ text(format!("Iteration: {:?}", self.graph.iteration)), slider(0..=10000, self.graph.iteration, Message::IterationSet) - .width(Length::Fill) ] .padding(10) .spacing(20), ] - .width(Length::Fill) .align_items(iced::Alignment::Center) .into() } diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 51538ec2..f14f6a8f 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -104,10 +104,11 @@ impl Sandbox for Styling { let progress_bar = progress_bar(0.0..=100.0, self.slider_value); - let scrollable = scrollable( - column!["Scroll me!", vertical_space(800), "You did it!"] - .width(Length::Fill), - ) + let scrollable = scrollable(column![ + "Scroll me!", + vertical_space(800), + "You did it!" + ]) .width(Length::Fill) .height(100); diff --git a/examples/svg/src/main.rs b/examples/svg/src/main.rs index 4dc92416..3bf4960f 100644 --- a/examples/svg/src/main.rs +++ b/examples/svg/src/main.rs @@ -63,7 +63,6 @@ impl Sandbox for Tiger { container(apply_color_filter).width(Length::Fill).center_x() ] .spacing(20) - .width(Length::Fill) .height(Length::Fill), ) .width(Length::Fill) diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 31b6f191..711d8223 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -106,9 +106,7 @@ impl Application for App { fn view<'a>(&'a self) -> Element<'a, Message> { let subtitle = |title, content: Element<'a, Message>| { - column![text(title).size(14), content] - .width(Length::Fill) - .spacing(5) + column![text(title).size(14), content].spacing(5) }; let mut add_toast = button("Add Toast"); @@ -153,14 +151,11 @@ impl Application for App { Message::Timeout ) .step(1.0) - .width(Length::Fill) ] .spacing(5) .into() ), - column![add_toast] - .width(Length::Fill) - .align_items(Alignment::End) + column![add_toast].align_items(Alignment::End) ] .spacing(10) .max_width(200), @@ -513,14 +508,14 @@ mod toast { position: Point, _translation: Vector, ) -> layout::Node { - let limits = layout::Limits::new(Size::ZERO, bounds) - .width(Length::Fill) - .height(Length::Fill); + let limits = layout::Limits::new(Size::ZERO, bounds); layout::flex::resolve( layout::flex::Axis::Vertical, renderer, &limits, + Length::Fill, + Length::Fill, 10.into(), 10.0, Alignment::End, diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 7003d8ae..b9ee1e61 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -692,11 +692,7 @@ fn ferris<'a>( } fn button<'a, Message: Clone>(label: &str) -> Button<'a, Message> { - iced::widget::button( - text(label).horizontal_alignment(alignment::Horizontal::Center), - ) - .padding(12) - .width(100) + iced::widget::button(text(label)).padding([12, 24]) } fn color_slider<'a>( diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index 920189f5..5fdf6657 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -116,7 +116,6 @@ impl Application for WebSocket { .map(Element::from) .collect(), ) - .width(Length::Fill) .spacing(10), ) .id(MESSAGE_LOG.clone()) @@ -149,7 +148,6 @@ impl Application for WebSocket { }; column![message_log, new_message_input] - .width(Length::Fill) .height(Length::Fill) .padding(20) .spacing(10) diff --git a/widget/src/button.rs b/widget/src/button.rs index 384a3156..ba68caa5 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -433,13 +433,18 @@ pub fn layout( ) -> layout::Node { let limits = limits.width(width).height(height); - let mut content = layout_content(&limits.pad(padding)); + let content = layout_content(&limits.shrink(padding)); 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, padding.top)); + let size = limits + .shrink(padding) + .resolve(content.size(), width, height) + .expand(padding); - layout::Node::with_children(size, vec![content]) + layout::Node::with_children( + size, + vec![content.move_to(Point::new(padding.left, padding.top))], + ) } /// Returns the [`mouse::Interaction`] of a [`Button`]. diff --git a/widget/src/canvas.rs b/widget/src/canvas.rs index 390f4d92..9e33c113 100644 --- a/widget/src/canvas.rs +++ b/widget/src/canvas.rs @@ -133,8 +133,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - let size = limits.resolve(Size::ZERO); + let size = limits.resolve(Size::ZERO, self.width, self.height); layout::Node::new(size) } diff --git a/widget/src/column.rs b/widget/src/column.rs index abb522be..526509bb 100644 --- a/widget/src/column.rs +++ b/widget/src/column.rs @@ -35,7 +35,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { Column { spacing: 0.0, padding: Padding::ZERO, - width: Length::Shrink, + width: Length::Fill, height: Length::Shrink, max_width: f32::INFINITY, align_items: Alignment::Start, @@ -126,15 +126,14 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits - .max_width(self.max_width) - .width(self.width) - .height(self.height); + let limits = limits.max_width(self.max_width); layout::flex::resolve( layout::flex::Axis::Vertical, renderer, &limits, + self.width, + self.height, self.padding, self.spacing, self.align_items, diff --git a/widget/src/container.rs b/widget/src/container.rs index 5dd7705b..b41a6023 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -312,24 +312,27 @@ pub fn layout( layout_content: impl FnOnce(&layout::Limits) -> layout::Node, ) -> layout::Node { let limits = limits - .loose() - .max_width(max_width) - .max_height(max_height) .width(width) - .height(height); + .height(height) + .max_width(max_width) + .max_height(max_height); - let mut content = layout_content(&limits.pad(padding).loose()); + let content = layout_content(&limits.shrink(padding).loose()); let padding = padding.fit(content.size(), limits.max()); - let size = limits.pad(padding).resolve(content.size()); - - content.move_to(Point::new(padding.left, padding.top)); - content.align( - Alignment::from(horizontal_alignment), - Alignment::from(vertical_alignment), - size, - ); - - layout::Node::with_children(size.pad(padding), vec![content]) + let size = limits + .shrink(padding) + .resolve(content.size(), width, height); + + layout::Node::with_children( + size.expand(padding), + vec![content + .move_to(Point::new(padding.left, padding.top)) + .align( + Alignment::from(horizontal_alignment), + Alignment::from(vertical_alignment), + size, + )], + ) } /// Draws the background of a [`Container`] given its [`Appearance`] and its `bounds`. diff --git a/widget/src/image.rs b/widget/src/image.rs index 67699102..b5f1e907 100644 --- a/widget/src/image.rs +++ b/widget/src/image.rs @@ -99,7 +99,7 @@ where }; // The size to be available to the widget prior to `Shrink`ing - let raw_size = limits.width(width).height(height).resolve(image_size); + let raw_size = limits.resolve(image_size, width, height); // The uncropped size of the image when fit to the bounds above let full_size = content_fit.fit(image_size, raw_size); diff --git a/widget/src/image/viewer.rs b/widget/src/image/viewer.rs index 68015ba8..23c4fe86 100644 --- a/widget/src/image/viewer.rs +++ b/widget/src/image/viewer.rs @@ -113,10 +113,11 @@ where ) -> layout::Node { let Size { width, height } = renderer.dimensions(&self.handle); - let mut size = limits - .width(self.width) - .height(self.height) - .resolve(Size::new(width as f32, height as f32)); + let mut size = limits.resolve( + Size::new(width as f32, height as f32), + self.width, + self.height, + ); let expansion_size = if height > width { self.width diff --git a/widget/src/keyed/column.rs b/widget/src/keyed/column.rs index 0ef82407..1b53b43a 100644 --- a/widget/src/keyed/column.rs +++ b/widget/src/keyed/column.rs @@ -196,6 +196,8 @@ where layout::flex::Axis::Vertical, renderer, &limits, + self.width, + self.height, self.padding, self.spacing, self.align_items, diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index e45b44ae..ef39a952 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -254,15 +254,14 @@ where ) .width(self.width); - let mut node = self.container.layout(self.state, renderer, &limits); + let node = self.container.layout(self.state, renderer, &limits); + let size = node.size(); node.move_to(if space_below > space_above { position + Vector::new(0.0, self.target_height) } else { - position - Vector::new(0.0, node.size().height) - }); - - node + position - Vector::new(0.0, size.height) + }) } fn on_event( @@ -359,7 +358,6 @@ where ) -> layout::Node { use std::f32; - let limits = limits.width(Length::Fill).height(Length::Shrink); let text_size = self.text_size.unwrap_or_else(|| renderer.default_size()); @@ -372,7 +370,7 @@ where * self.options.len() as f32, ); - limits.resolve(intrinsic) + limits.resolve(intrinsic, Length::Fill, Length::Shrink) }; layout::Node::new(size) diff --git a/widget/src/pane_grid.rs b/widget/src/pane_grid.rs index 7057fe59..3d799fd3 100644 --- a/widget/src/pane_grid.rs +++ b/widget/src/pane_grid.rs @@ -490,8 +490,7 @@ pub fn layout( &layout::Limits, ) -> layout::Node, ) -> layout::Node { - let limits = limits.width(width).height(height); - let size = limits.resolve(Size::ZERO); + let size = limits.resolve(Size::ZERO, width, height); let regions = node.pane_regions(spacing, size); let children = contents @@ -500,16 +499,14 @@ pub fn layout( let region = regions.get(&pane)?; let size = Size::new(region.width, region.height); - let mut node = layout_content( + let node = layout_content( content, tree, renderer, &layout::Limits::new(size, size), ); - node.move_to(Point::new(region.x, region.y)); - - Some(node) + Some(node.move_to(Point::new(region.x, region.y))) }) .collect(); diff --git a/widget/src/pane_grid/content.rs b/widget/src/pane_grid/content.rs index 826ea663..ee00f186 100644 --- a/widget/src/pane_grid/content.rs +++ b/widget/src/pane_grid/content.rs @@ -165,7 +165,7 @@ where let title_bar_size = title_bar_layout.size(); - let mut body_layout = self.body.as_widget().layout( + let body_layout = self.body.as_widget().layout( &mut tree.children[0], renderer, &layout::Limits::new( @@ -177,11 +177,12 @@ where ), ); - body_layout.move_to(Point::new(0.0, title_bar_size.height)); - layout::Node::with_children( max_size, - vec![title_bar_layout, body_layout], + vec![ + title_bar_layout, + body_layout.move_to(Point::new(0.0, title_bar_size.height)), + ], ) } else { self.body.as_widget().layout( diff --git a/widget/src/pane_grid/title_bar.rs b/widget/src/pane_grid/title_bar.rs index f4dbb6b1..eb21b743 100644 --- a/widget/src/pane_grid/title_bar.rs +++ b/widget/src/pane_grid/title_bar.rs @@ -217,7 +217,7 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits.pad(self.padding); + let limits = limits.shrink(self.padding); let max_size = limits.max(); let title_layout = self.content.as_widget().layout( @@ -228,8 +228,8 @@ where let title_size = title_layout.size(); - let mut node = if let Some(controls) = &self.controls { - let mut controls_layout = controls.as_widget().layout( + let node = if let Some(controls) = &self.controls { + let controls_layout = controls.as_widget().layout( &mut tree.children[1], renderer, &layout::Limits::new(Size::ZERO, max_size), @@ -240,11 +240,13 @@ where let height = title_size.height.max(controls_size.height); - controls_layout.move_to(Point::new(space_before_controls, 0.0)); - layout::Node::with_children( Size::new(max_size.width, height), - vec![title_layout, controls_layout], + vec![ + title_layout, + controls_layout + .move_to(Point::new(space_before_controls, 0.0)), + ], ) } else { layout::Node::with_children( @@ -253,9 +255,7 @@ where ) }; - node.move_to(Point::new(self.padding.left, self.padding.top)); - - layout::Node::with_children(node.size().pad(self.padding), vec![node]) + layout::Node::container(node, self.padding) } pub(crate) fn operate( diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index 022ca8d9..13110725 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -393,7 +393,7 @@ where { use std::f32; - let limits = limits.width(width).height(Length::Shrink).pad(padding); + let limits = limits.width(width).height(Length::Shrink); let font = font.unwrap_or_else(|| renderer.default_font()); let text_size = text_size.unwrap_or_else(|| renderer.default_size()); @@ -451,7 +451,10 @@ where f32::from(text_line_height.to_absolute(text_size)), ); - limits.resolve(intrinsic).pad(padding) + limits + .shrink(padding) + .resolve(intrinsic, width, Length::Shrink) + .expand(padding) }; layout::Node::new(size) diff --git a/widget/src/progress_bar.rs b/widget/src/progress_bar.rs index 07de72d5..b84ab2dd 100644 --- a/widget/src/progress_bar.rs +++ b/widget/src/progress_bar.rs @@ -99,11 +99,11 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits - .width(self.width) - .height(self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT))); - - let size = limits.resolve(Size::ZERO); + let size = limits.resolve( + Size::ZERO, + self.width, + self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT)), + ); layout::Node::new(size) } diff --git a/widget/src/row.rs b/widget/src/row.rs index d52b8c43..650c2c7d 100644 --- a/widget/src/row.rs +++ b/widget/src/row.rs @@ -34,7 +34,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { Row { spacing: 0.0, padding: Padding::ZERO, - width: Length::Shrink, + width: Length::Fill, height: Length::Shrink, align_items: Alignment::Start, children, @@ -118,12 +118,12 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - layout::flex::resolve( layout::flex::Axis::Horizontal, renderer, &limits, + self.width, + self.height, self.padding, self.spacing, self.align_items, diff --git a/widget/src/rule.rs b/widget/src/rule.rs index b5c5fa55..ecaedf60 100644 --- a/widget/src/rule.rs +++ b/widget/src/rule.rs @@ -76,9 +76,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - - layout::Node::new(limits.resolve(Size::ZERO)) + layout::Node::new(limits.resolve(Size::ZERO, self.width, self.height)) } fn draw( diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 49aed2f0..525463c4 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -489,7 +489,7 @@ pub fn layout( ); let content = layout_content(renderer, &child_limits); - let size = limits.resolve(content.size()); + let size = limits.resolve(content.size(), width, height); layout::Node::with_children(size, vec![content]) } diff --git a/widget/src/shader.rs b/widget/src/shader.rs index 8e334693..5b18ec7d 100644 --- a/widget/src/shader.rs +++ b/widget/src/shader.rs @@ -85,7 +85,7 @@ where limits: &layout::Limits, ) -> layout::Node { let limits = limits.width(self.width).height(self.height); - let size = limits.resolve(Size::ZERO); + let size = limits.resolve(Size::ZERO, self.width, self.height); layout::Node::new(size) } diff --git a/widget/src/slider.rs b/widget/src/slider.rs index ac0982c8..2b600d9d 100644 --- a/widget/src/slider.rs +++ b/widget/src/slider.rs @@ -173,8 +173,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - let size = limits.resolve(Size::ZERO); + let size = limits.resolve(Size::ZERO, self.width, self.height); layout::Node::new(size) } diff --git a/widget/src/space.rs b/widget/src/space.rs index e5a8f169..afa9a7c8 100644 --- a/widget/src/space.rs +++ b/widget/src/space.rs @@ -59,9 +59,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - - layout::Node::new(limits.resolve(Size::ZERO)) + layout::Node::new(limits.resolve(Size::ZERO, self.width, self.height)) } fn draw( diff --git a/widget/src/svg.rs b/widget/src/svg.rs index 2d01d1ab..8367ad18 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -115,10 +115,7 @@ where let image_size = Size::new(width as f32, height as f32); // The size to be available to the widget prior to `Shrink`ing - let raw_size = limits - .width(self.width) - .height(self.height) - .resolve(image_size); + let raw_size = limits.resolve(image_size, self.width, self.height); // The uncropped size of the image when fit to the bounds above let full_size = self.content_fit.fit(image_size, raw_size); diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index a2a186f0..214bce17 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -350,7 +350,7 @@ where } internal.editor.update( - limits.pad(self.padding).max(), + limits.shrink(self.padding).max(), self.font.unwrap_or_else(|| renderer.default_font()), self.text_size.unwrap_or_else(|| renderer.default_size()), self.line_height, diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 65d3e1eb..03eb2fd0 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -506,14 +506,11 @@ where { let font = font.unwrap_or_else(|| renderer.default_font()); 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(line_height.to_absolute(text_size)); + let height = line_height.to_absolute(text_size); - let text_bounds = limits.resolve(Size::ZERO); + let limits = limits.width(width).shrink(padding).height(height); + let text_bounds = limits.resolve(Size::ZERO, width, height); let placeholder_text = Text { font, @@ -552,41 +549,41 @@ where let icon_width = state.icon.min_width(); - let mut text_node = layout::Node::new( - text_bounds - Size::new(icon_width + icon.spacing, 0.0), - ); - - let mut icon_node = - layout::Node::new(Size::new(icon_width, text_bounds.height)); - - match icon.side { - Side::Left => { - text_node.move_to(Point::new( + let (text_position, icon_position) = match icon.side { + Side::Left => ( + Point::new( padding.left + icon_width + icon.spacing, padding.top, - )); - - icon_node.move_to(Point::new(padding.left, padding.top)); - } - Side::Right => { - text_node.move_to(Point::new(padding.left, padding.top)); - - icon_node.move_to(Point::new( + ), + Point::new(padding.left, padding.top), + ), + Side::Right => ( + Point::new(padding.left, padding.top), + Point::new( padding.left + text_bounds.width - icon_width, padding.top, - )); - } + ), + ), }; + let text_node = layout::Node::new( + text_bounds - Size::new(icon_width + icon.spacing, 0.0), + ) + .move_to(text_position); + + let icon_node = + layout::Node::new(Size::new(icon_width, text_bounds.height)) + .move_to(icon_position); + layout::Node::with_children( - text_bounds.pad(padding), + text_bounds.expand(padding), vec![text_node, icon_node], ) } else { - let mut text = layout::Node::new(text_bounds); - text.move_to(Point::new(padding.left, padding.top)); + let text = layout::Node::new(text_bounds) + .move_to(Point::new(padding.left, padding.top)); - layout::Node::with_children(text_bounds.pad(padding), vec![text]) + layout::Node::with_children(text_bounds.expand(padding), vec![text]) } } diff --git a/widget/src/tooltip.rs b/widget/src/tooltip.rs index b888980a..adef13e4 100644 --- a/widget/src/tooltip.rs +++ b/widget/src/tooltip.rs @@ -353,7 +353,7 @@ where .then(|| viewport.size()) .unwrap_or(Size::INFINITY), ) - .pad(Padding::new(self.padding)), + .shrink(Padding::new(self.padding)), ); let text_bounds = text_layout.bounds(); diff --git a/widget/src/vertical_slider.rs b/widget/src/vertical_slider.rs index 01d3359c..e489104c 100644 --- a/widget/src/vertical_slider.rs +++ b/widget/src/vertical_slider.rs @@ -170,8 +170,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - let size = limits.resolve(Size::ZERO); + let size = limits.resolve(Size::ZERO, self.width, self.height); layout::Node::new(size) } -- cgit From ed3b3930180f1971da25fdcc66a4130da32400ba Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 16 Mar 2023 20:37:24 +0100 Subject: Fix needless borrow in `row::layout` --- widget/src/row.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/widget/src/row.rs b/widget/src/row.rs index 650c2c7d..c4a1db56 100644 --- a/widget/src/row.rs +++ b/widget/src/row.rs @@ -121,7 +121,7 @@ where layout::flex::resolve( layout::flex::Axis::Horizontal, renderer, - &limits, + limits, self.width, self.height, self.padding, -- cgit From 89418c1244d14ac6020b31f3f1e19d15b4c0a272 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 23 Mar 2023 16:07:23 +0100 Subject: Determine cross-axis max length based on contents if `Shrink` --- core/src/layout/flex.rs | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 738dc81d..823fb9e6 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -47,7 +47,7 @@ impl Axis { } } - fn pack(&self, main: f32, cross: f32) -> (f32, f32) { + fn pack(&self, main: T, cross: T) -> (T, T) { match self { Axis::Horizontal => (main, cross), Axis::Vertical => (cross, main), @@ -78,7 +78,7 @@ where let total_spacing = spacing * items.len().saturating_sub(1) as f32; let max_cross = axis.cross(limits.max()); - let mut fill_sum = 0; + let mut fill_main_sum = 0; let mut cross = 0.0f32; let mut available = axis.main(limits.max()) - total_spacing; @@ -86,13 +86,12 @@ where nodes.resize(items.len(), Node::default()); for (i, (child, tree)) in items.iter().zip(trees.iter_mut()).enumerate() { - let fill_factor = match axis { - Axis::Horizontal => child.as_widget().width(), - Axis::Vertical => child.as_widget().height(), - } - .fill_factor(); + let (fill_main_factor, fill_cross_factor) = axis.pack( + child.as_widget().width().fill_factor(), + child.as_widget().height().fill_factor(), + ); - if fill_factor == 0 { + if fill_main_factor == 0 && fill_cross_factor == 0 { let (max_width, max_height) = axis.pack(available, max_cross); let child_limits = @@ -107,7 +106,7 @@ where nodes[i] = layout; } else { - fill_sum += fill_factor; + fill_main_sum += fill_main_factor; } } @@ -122,15 +121,27 @@ where }, }; + let max_cross = match axis { + Axis::Horizontal => match height { + Length::Shrink => cross, + _ => max_cross, + }, + Axis::Vertical => match width { + Length::Shrink => cross, + _ => max_cross, + }, + }; + for (i, (child, tree)) in items.iter().zip(trees).enumerate() { - let fill_factor = match axis { - Axis::Horizontal => child.as_widget().width(), - Axis::Vertical => child.as_widget().height(), - } - .fill_factor(); + let (fill_main_factor, fill_cross_factor) = axis.pack( + child.as_widget().width().fill_factor(), + child.as_widget().height().fill_factor(), + ); + + if fill_main_factor != 0 || fill_cross_factor != 0 { + let max_main = + remaining * fill_main_factor as f32 / fill_main_sum as f32; - if fill_factor != 0 { - let max_main = remaining * fill_factor as f32 / fill_sum as f32; let min_main = if max_main.is_infinite() { 0.0 } else { @@ -140,7 +151,8 @@ where let (min_width, min_height) = axis.pack(min_main, axis.cross(limits.min())); - let (max_width, max_height) = axis.pack(max_main, max_cross); + let (max_width, max_height) = axis + .pack(max_main, max_cross * fill_cross_factor.max(1) as f32); let child_limits = Limits::new( Size::new(min_width, min_height), -- cgit From aa3c956516a23af86dfb9d96b769e5f26addbe60 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 24 Mar 2023 03:02:26 +0100 Subject: Fix available space provided to children with non-fill main axis but fill cross axis --- core/src/layout/flex.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 823fb9e6..5ae98b8c 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -139,10 +139,13 @@ where ); if fill_main_factor != 0 || fill_cross_factor != 0 { - let max_main = - remaining * fill_main_factor as f32 / fill_main_sum as f32; + let max_main = if fill_main_factor == 0 { + available.max(0.0) + } else { + remaining * fill_main_factor as f32 / fill_main_sum as f32 + }; - let min_main = if max_main.is_infinite() { + let min_main = if fill_main_factor == 0 || max_main.is_infinite() { 0.0 } else { max_main -- cgit From fd8f980b88df260ce49d46ed6c514f6e382c6494 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 27 Mar 2023 14:40:03 +0200 Subject: Use `max_cross` if all elements are fluid in `layout::flex` --- core/src/layout/flex.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 5ae98b8c..9a4b2cbf 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -123,11 +123,11 @@ where let max_cross = match axis { Axis::Horizontal => match height { - Length::Shrink => cross, + Length::Shrink if cross > 0.0 => cross, _ => max_cross, }, Axis::Vertical => match width { - Length::Shrink => cross, + Length::Shrink if cross > 0.0 => cross, _ => max_cross, }, }; @@ -154,8 +154,7 @@ where let (min_width, min_height) = axis.pack(min_main, axis.cross(limits.min())); - let (max_width, max_height) = axis - .pack(max_main, max_cross * fill_cross_factor.max(1) as f32); + let (max_width, max_height) = axis.pack(max_main, max_cross); let child_limits = Limits::new( Size::new(min_width, min_height), -- cgit From 0322e820eb40d36a7425246278b7bcb22b7010aa Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 27 Mar 2023 15:43:52 +0200 Subject: Create `layout` example --- examples/layout/Cargo.toml | 9 ++++ examples/layout/src/main.rs | 123 ++++++++++++++++++++++++++++++++++++++++++++ src/time.rs | 1 + widget/src/column.rs | 2 +- widget/src/row.rs | 2 +- 5 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 examples/layout/Cargo.toml create mode 100644 examples/layout/src/main.rs diff --git a/examples/layout/Cargo.toml b/examples/layout/Cargo.toml new file mode 100644 index 00000000..c2c3f49b --- /dev/null +++ b/examples/layout/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "layout" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../.." } diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs new file mode 100644 index 00000000..1b0c0c94 --- /dev/null +++ b/examples/layout/src/main.rs @@ -0,0 +1,123 @@ +use iced::executor; +use iced::widget::{column, container, row, text, vertical_rule}; +use iced::{ + Application, Command, Element, Length, Settings, Subscription, Theme, +}; + +pub fn main() -> iced::Result { + Layout::run(Settings::default()) +} + +#[derive(Debug)] +struct Layout { + previous: Vec, + current: Example, + next: Vec, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + Next, + Previous, +} + +impl Application for Layout { + type Message = Message; + type Theme = Theme; + type Executor = executor::Default; + type Flags = (); + + fn new(_flags: Self::Flags) -> (Self, Command) { + ( + Self { + previous: vec![], + current: Example::Centered, + next: vec![Example::NestedQuotes], + }, + Command::none(), + ) + } + + fn title(&self) -> String { + String::from("Counter - Iced") + } + + fn update(&mut self, message: Self::Message) -> Command { + match message { + Message::Next => { + if !self.next.is_empty() { + let previous = std::mem::replace( + &mut self.current, + self.next.remove(0), + ); + + self.previous.push(previous); + } + } + Message::Previous => { + if let Some(previous) = self.previous.pop() { + let next = std::mem::replace(&mut self.current, previous); + + self.next.insert(0, next); + } + } + } + + Command::none() + } + + fn subscription(&self) -> Subscription { + use iced::event::{self, Event}; + use iced::keyboard; + + event::listen_with(|event, status| match event { + Event::Keyboard(keyboard::Event::KeyReleased { + key_code, .. + }) if status == event::Status::Ignored => match key_code { + keyboard::KeyCode::Left => Some(Message::Previous), + keyboard::KeyCode::Right => Some(Message::Next), + _ => None, + }, + _ => None, + }) + } + + fn view(&self) -> Element { + self.current.view() + } +} + +#[derive(Debug)] +enum Example { + Centered, + NestedQuotes, +} + +impl Example { + fn view(&self) -> Element { + match self { + Self::Centered => container(text("I am centered!").size(50)) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into(), + Self::NestedQuotes => container((1..5).fold( + column![text("Original text")].padding(10), + |quotes, i| { + column![ + row![vertical_rule(2), quotes], + text(format!("Reply {i}")) + ] + .spacing(10) + .padding(10) + }, + )) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into(), + } + } +} diff --git a/src/time.rs b/src/time.rs index 37d454ed..f10f7a5e 100644 --- a/src/time.rs +++ b/src/time.rs @@ -1,4 +1,5 @@ //! Listen and react to time. pub use iced_core::time::{Duration, Instant}; +#[allow(unused_imports)] pub use iced_futures::backend::default::time::*; diff --git a/widget/src/column.rs b/widget/src/column.rs index 526509bb..80327458 100644 --- a/widget/src/column.rs +++ b/widget/src/column.rs @@ -35,7 +35,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { Column { spacing: 0.0, padding: Padding::ZERO, - width: Length::Fill, + width: Length::Shrink, height: Length::Shrink, max_width: f32::INFINITY, align_items: Alignment::Start, diff --git a/widget/src/row.rs b/widget/src/row.rs index c4a1db56..50fc4de0 100644 --- a/widget/src/row.rs +++ b/widget/src/row.rs @@ -34,7 +34,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { Row { spacing: 0.0, padding: Padding::ZERO, - width: Length::Fill, + width: Length::Shrink, height: Length::Shrink, align_items: Alignment::Start, children, -- cgit From 22226394f7b1a0e0205b9bb5b3ef9b85a3b406f5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Jan 2024 17:24:43 +0100 Subject: Introduce `Widget::size_hint` and fix further layout inconsistencies --- core/src/layout/flex.rs | 72 +++++++++++++++++++++++++--------- core/src/length.rs | 6 +++ core/src/widget.rs | 10 ++++- examples/download_progress/src/main.rs | 19 +++++---- examples/events/src/main.rs | 3 +- examples/layout/src/main.rs | 2 +- examples/lazy/src/main.rs | 46 ++++++++-------------- examples/loading_spinners/src/main.rs | 11 +++--- examples/scrollable/src/main.rs | 23 ++++------- examples/tour/src/main.rs | 1 - examples/websocket/src/main.rs | 11 ++---- widget/src/column.rs | 39 ++++++++++++------ widget/src/container.rs | 15 ++++++- widget/src/helpers.rs | 22 +++++++---- widget/src/lazy.rs | 7 ++++ widget/src/lazy/component.rs | 7 ++++ widget/src/row.rs | 41 +++++++++++++------ 17 files changed, 211 insertions(+), 124 deletions(-) diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 9a4b2cbf..67cc7f2a 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -91,8 +91,46 @@ where child.as_widget().height().fill_factor(), ); - if fill_main_factor == 0 && fill_cross_factor == 0 { - let (max_width, max_height) = axis.pack(available, max_cross); + if fill_main_factor == 0 { + if fill_cross_factor == 0 { + let (max_width, max_height) = axis.pack(available, max_cross); + + let child_limits = + Limits::new(Size::ZERO, Size::new(max_width, max_height)); + + let layout = + child.as_widget().layout(tree, renderer, &child_limits); + let size = layout.size(); + + available -= axis.main(size); + cross = cross.max(axis.cross(size)); + + nodes[i] = layout; + } + } else { + fill_main_sum += fill_main_factor; + } + } + + let intrinsic_cross = match axis { + Axis::Horizontal => match height { + Length::Shrink => cross, + _ => max_cross, + }, + Axis::Vertical => match width { + Length::Shrink => cross, + _ => max_cross, + }, + }; + + for (i, (child, tree)) in items.iter().zip(trees.iter_mut()).enumerate() { + let (fill_main_factor, fill_cross_factor) = axis.pack( + child.as_widget().width().fill_factor(), + child.as_widget().height().fill_factor(), + ); + + if fill_main_factor == 0 && fill_cross_factor != 0 { + let (max_width, max_height) = axis.pack(available, intrinsic_cross); let child_limits = Limits::new(Size::ZERO, Size::new(max_width, max_height)); @@ -102,11 +140,8 @@ where let size = layout.size(); available -= axis.main(size); - cross = cross.max(axis.cross(size)); nodes[i] = layout; - } else { - fill_main_sum += fill_main_factor; } } @@ -121,24 +156,13 @@ where }, }; - let max_cross = match axis { - Axis::Horizontal => match height { - Length::Shrink if cross > 0.0 => cross, - _ => max_cross, - }, - Axis::Vertical => match width { - Length::Shrink if cross > 0.0 => cross, - _ => max_cross, - }, - }; - for (i, (child, tree)) in items.iter().zip(trees).enumerate() { let (fill_main_factor, fill_cross_factor) = axis.pack( child.as_widget().width().fill_factor(), child.as_widget().height().fill_factor(), ); - if fill_main_factor != 0 || fill_cross_factor != 0 { + if fill_main_factor != 0 { let max_main = if fill_main_factor == 0 { available.max(0.0) } else { @@ -151,6 +175,12 @@ where max_main }; + let max_cross = if fill_cross_factor == 0 { + max_cross + } else { + intrinsic_cross + }; + let (min_width, min_height) = axis.pack(min_main, axis.cross(limits.min())); @@ -203,8 +233,12 @@ where main += axis.main(size); } - let (width, height) = axis.pack(main - pad.0, cross); - let size = limits.resolve(Size::new(width, height), width, height); + let (intrinsic_width, intrinsic_height) = axis.pack(main - pad.0, cross); + let size = limits.resolve( + Size::new(intrinsic_width, intrinsic_height), + width, + height, + ); Node::with_children(size.expand(padding), nodes) } diff --git a/core/src/length.rs b/core/src/length.rs index 3adb996e..6dc15049 100644 --- a/core/src/length.rs +++ b/core/src/length.rs @@ -36,6 +36,12 @@ impl Length { Length::Fixed(_) => 0, } } + + /// Returns `true` iff the [`Length`] is either [`Length::Fill`] or + // [`Length::FillPortion`]. + pub fn is_fill(&self) -> bool { + self.fill_factor() != 0 + } } impl From for Length { diff --git a/core/src/widget.rs b/core/src/widget.rs index 294d5984..890b3773 100644 --- a/core/src/widget.rs +++ b/core/src/widget.rs @@ -15,7 +15,7 @@ use crate::layout::{self, Layout}; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::{Clipboard, Length, Rectangle, Shell}; +use crate::{Clipboard, Length, Rectangle, Shell, Size}; /// A component that displays information and allows interaction. /// @@ -49,6 +49,14 @@ where /// Returns the height of the [`Widget`]. fn height(&self) -> Length; + /// Returns a [`Size`] hint for laying out the [`Widget`]. + /// + /// This hint may be used by some widget containers to adjust their sizing strategy + /// during construction. + fn size_hint(&self) -> Size { + Size::new(self.width(), self.height()) + } + /// Returns the [`layout::Node`] of the [`Widget`]. /// /// This [`layout::Node`] is used by the runtime to compute the [`Layout`] of the diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index a2fcb275..675e9e26 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -73,16 +73,15 @@ impl Application for Example { } fn view(&self) -> Element { - let downloads = Column::with_children( - self.downloads.iter().map(Download::view).collect(), - ) - .push( - button("Add another download") - .on_press(Message::Add) - .padding(10), - ) - .spacing(20) - .align_items(Alignment::End); + let downloads = + Column::with_children(self.downloads.iter().map(Download::view)) + .push( + button("Add another download") + .on_press(Message::Add) + .padding(10), + ) + .spacing(20) + .align_items(Alignment::End); container(downloads) .width(Length::Fill) diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 334b012d..fc51ac4a 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -82,8 +82,7 @@ impl Application for Events { self.last .iter() .map(|event| text(format!("{event:?}")).size(40)) - .map(Element::from) - .collect(), + .map(Element::from), ); let toggle = checkbox( diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index 1b0c0c94..eeaa76b6 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -106,7 +106,7 @@ impl Example { column![text("Original text")].padding(10), |quotes, i| { column![ - row![vertical_rule(2), quotes], + row![vertical_rule(2), quotes].height(Length::Shrink), text(format!("Reply {i}")) ] .spacing(10) diff --git a/examples/lazy/src/main.rs b/examples/lazy/src/main.rs index 01560598..04df0744 100644 --- a/examples/lazy/src/main.rs +++ b/examples/lazy/src/main.rs @@ -178,35 +178,23 @@ impl Sandbox for App { } }); - column( - items - .into_iter() - .map(|item| { - let button = button("Delete") - .on_press(Message::DeleteItem(item.clone())) - .style(theme::Button::Destructive); - - row![ - text(&item.name) - .style(theme::Text::Color(item.color.into())), - horizontal_space(Length::Fill), - pick_list( - Color::ALL, - Some(item.color), - move |color| { - Message::ItemColorChanged( - item.clone(), - color, - ) - } - ), - button - ] - .spacing(20) - .into() - }) - .collect(), - ) + column(items.into_iter().map(|item| { + let button = button("Delete") + .on_press(Message::DeleteItem(item.clone())) + .style(theme::Button::Destructive); + + row![ + text(&item.name) + .style(theme::Text::Color(item.color.into())), + horizontal_space(Length::Fill), + pick_list(Color::ALL, Some(item.color), move |color| { + Message::ItemColorChanged(item.clone(), color) + }), + button + ] + .spacing(20) + .into() + })) .spacing(10) }); diff --git a/examples/loading_spinners/src/main.rs b/examples/loading_spinners/src/main.rs index a78e9590..93a4605e 100644 --- a/examples/loading_spinners/src/main.rs +++ b/examples/loading_spinners/src/main.rs @@ -96,15 +96,14 @@ impl Application for LoadingSpinners { container( column.push( - row(vec![ - text("Cycle duration:").into(), + row![ + text("Cycle duration:"), slider(1.0..=1000.0, self.cycle_duration * 100.0, |x| { Message::CycleDurationChanged(x / 100.0) }) - .width(200.0) - .into(), - text(format!("{:.2}s", self.cycle_duration)).into(), - ]) + .width(200.0), + text(format!("{:.2}s", self.cycle_duration)), + ] .align_items(iced::Alignment::Center) .spacing(20.0), ), diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 1042e7a4..249bc2a5 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -172,23 +172,21 @@ impl Application for ScrollableDemo { ] .spacing(10); - let scroll_alignment_controls = column(vec![ - text("Scrollable alignment:").into(), + let scroll_alignment_controls = column![ + text("Scrollable alignment:"), radio( "Start", scrollable::Alignment::Start, Some(self.alignment), Message::AlignmentChanged, - ) - .into(), + ), radio( "End", scrollable::Alignment::End, Some(self.alignment), Message::AlignmentChanged, ) - .into(), - ]) + ] .spacing(10); let scroll_controls = row![ @@ -226,6 +224,7 @@ impl Application for ScrollableDemo { .padding([40, 0, 40, 0]) .spacing(40), ) + .width(Length::Fill) .height(Length::Fill) .direction(scrollable::Direction::Vertical( Properties::new() @@ -251,6 +250,7 @@ impl Application for ScrollableDemo { .padding([0, 40, 0, 40]) .spacing(40), ) + .width(Length::Fill) .height(Length::Fill) .direction(scrollable::Direction::Horizontal( Properties::new() @@ -293,6 +293,7 @@ impl Application for ScrollableDemo { .padding([0, 40, 0, 40]) .spacing(40), ) + .width(Length::Fill) .height(Length::Fill) .direction({ let properties = Properties::new() @@ -333,19 +334,11 @@ impl Application for ScrollableDemo { let content: Element = column![scroll_controls, scrollable_content, progress_bars] - .height(Length::Fill) .align_items(Alignment::Center) .spacing(10) .into(); - Element::from( - container(content) - .width(Length::Fill) - .height(Length::Fill) - .padding(40) - .center_x() - .center_y(), - ) + Element::from(container(content).padding(40).center_x().center_y()) } fn theme(&self) -> Self::Theme { diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index b9ee1e61..8633bc0a 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -509,7 +509,6 @@ impl<'a> Step { ) }) .map(Element::from) - .collect() ) .spacing(10) ] diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index 5fdf6657..59488e69 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -3,7 +3,7 @@ mod echo; use iced::alignment::{self, Alignment}; use iced::executor; use iced::widget::{ - button, column, container, row, scrollable, text, text_input, Column, + button, column, container, row, scrollable, text, text_input, }; use iced::{ Application, Color, Command, Element, Length, Settings, Subscription, Theme, @@ -108,13 +108,8 @@ impl Application for WebSocket { .into() } else { scrollable( - Column::with_children( - self.messages - .iter() - .cloned() - .map(text) - .map(Element::from) - .collect(), + column( + self.messages.iter().cloned().map(text).map(Element::from), ) .spacing(10), ) diff --git a/widget/src/column.rs b/widget/src/column.rs index 80327458..52cf35ce 100644 --- a/widget/src/column.rs +++ b/widget/src/column.rs @@ -22,16 +22,12 @@ pub struct Column<'a, Message, Renderer = crate::Renderer> { children: Vec>, } -impl<'a, Message, Renderer> Column<'a, Message, Renderer> { +impl<'a, Message, Renderer> Column<'a, Message, Renderer> +where + Renderer: crate::core::Renderer, +{ /// Creates an empty [`Column`]. pub fn new() -> Self { - Self::with_children(Vec::new()) - } - - /// Creates a [`Column`] with the given elements. - pub fn with_children( - children: Vec>, - ) -> Self { Column { spacing: 0.0, padding: Padding::ZERO, @@ -39,10 +35,17 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { height: Length::Shrink, max_width: f32::INFINITY, align_items: Alignment::Start, - children, + children: Vec::new(), } } + /// Creates a [`Column`] with the given elements. + pub fn with_children( + children: impl Iterator>, + ) -> Self { + children.fold(Self::new(), |column, element| column.push(element)) + } + /// Sets the vertical spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -88,12 +91,26 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { mut self, child: impl Into>, ) -> Self { - self.children.push(child.into()); + let child = child.into(); + let size = child.as_widget().size_hint(); + + if size.width.is_fill() { + self.width = Length::Fill; + } + + if size.height.is_fill() { + self.height = Length::Fill; + } + + self.children.push(child); self } } -impl<'a, Message, Renderer> Default for Column<'a, Message, Renderer> { +impl<'a, Message, Renderer> Default for Column<'a, Message, Renderer> +where + Renderer: crate::core::Renderer, +{ fn default() -> Self { Self::new() } diff --git a/widget/src/container.rs b/widget/src/container.rs index b41a6023..fbc68db7 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -46,11 +46,22 @@ where where T: Into>, { + let content = content.into(); + let size = content.as_widget().size_hint(); + Container { id: None, padding: Padding::ZERO, - width: Length::Shrink, - height: Length::Shrink, + width: if size.width.is_fill() { + Length::Fill + } else { + Length::Shrink + }, + height: if size.height.is_fill() { + Length::Fill + } else { + Length::Shrink + }, max_width: f32::INFINITY, max_height: f32::INFINITY, horizontal_alignment: alignment::Horizontal::Left, diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 115198fb..6eaf3392 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -34,7 +34,7 @@ macro_rules! column { $crate::Column::new() ); ($($x:expr),+ $(,)?) => ( - $crate::Column::with_children(vec![$($crate::core::Element::from($x)),+]) + $crate::Column::with_children([$($crate::core::Element::from($x)),+].into_iter()) ); } @@ -47,7 +47,7 @@ macro_rules! row { $crate::Row::new() ); ($($x:expr),+ $(,)?) => ( - $crate::Row::with_children(vec![$($crate::core::Element::from($x)),+]) + $crate::Row::with_children([$($crate::core::Element::from($x)),+].into_iter()) ); } @@ -65,9 +65,12 @@ where } /// Creates a new [`Column`] with the given children. -pub fn column( - children: Vec>, -) -> Column<'_, Message, Renderer> { +pub fn column<'a, Message, Renderer>( + children: impl Iterator>, +) -> Column<'a, Message, Renderer> +where + Renderer: core::Renderer, +{ Column::with_children(children) } @@ -84,9 +87,12 @@ where /// Creates a new [`Row`] with the given children. /// /// [`Row`]: crate::Row -pub fn row( - children: Vec>, -) -> Row<'_, Message, Renderer> { +pub fn row<'a, Message, Renderer>( + children: impl Iterator>, +) -> Row<'a, Message, Renderer> +where + Renderer: core::Renderer, +{ Row::with_children(children) } diff --git a/widget/src/lazy.rs b/widget/src/lazy.rs index 167a055d..4f6513db 100644 --- a/widget/src/lazy.rs +++ b/widget/src/lazy.rs @@ -150,6 +150,13 @@ where self.with_element(|element| element.as_widget().height()) } + fn size_hint(&self) -> Size { + Size { + width: Length::Shrink, + height: Length::Shrink, + } + } + fn layout( &self, tree: &mut Tree, diff --git a/widget/src/lazy/component.rs b/widget/src/lazy/component.rs index ad0c3823..0aff7485 100644 --- a/widget/src/lazy/component.rs +++ b/widget/src/lazy/component.rs @@ -252,6 +252,13 @@ where self.with_element(|element| element.as_widget().height()) } + fn size_hint(&self) -> Size { + Size { + width: Length::Shrink, + height: Length::Shrink, + } + } + fn layout( &self, tree: &mut Tree, diff --git a/widget/src/row.rs b/widget/src/row.rs index 50fc4de0..ef371ddb 100644 --- a/widget/src/row.rs +++ b/widget/src/row.rs @@ -21,26 +21,31 @@ pub struct Row<'a, Message, Renderer = crate::Renderer> { children: Vec>, } -impl<'a, Message, Renderer> Row<'a, Message, Renderer> { +impl<'a, Message, Renderer> Row<'a, Message, Renderer> +where + Renderer: crate::core::Renderer, +{ /// Creates an empty [`Row`]. pub fn new() -> Self { - Self::with_children(Vec::new()) - } - - /// Creates a [`Row`] with the given elements. - pub fn with_children( - children: Vec>, - ) -> Self { Row { spacing: 0.0, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, align_items: Alignment::Start, - children, + children: Vec::new(), } } + /// Creates a [`Row`] with the given elements. + pub fn with_children( + children: impl Iterator>, + ) -> Self { + children + .into_iter() + .fold(Self::new(), |column, element| column.push(element)) + } + /// Sets the horizontal spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -80,12 +85,26 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { mut self, child: impl Into>, ) -> Self { - self.children.push(child.into()); + let child = child.into(); + let size = child.as_widget().size_hint(); + + if size.width.is_fill() { + self.width = Length::Fill; + } + + if size.height.is_fill() { + self.height = Length::Fill; + } + + self.children.push(child); self } } -impl<'a, Message, Renderer> Default for Row<'a, Message, Renderer> { +impl<'a, Message, Renderer> Default for Row<'a, Message, Renderer> +where + Renderer: crate::core::Renderer, +{ fn default() -> Self { Self::new() } -- cgit From d278bfd21d0399009e652560afb9a4d185e92637 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Jan 2024 17:46:33 +0100 Subject: Replace `width` and `height` with `Widget::size` --- core/src/element.rs | 18 +++++------------- core/src/layout/flex.rs | 27 +++++++++++++++------------ core/src/widget.rs | 9 +++------ core/src/widget/text.rs | 15 ++++++++------- examples/custom_quad/src/main.rs | 11 +++++------ examples/custom_widget/src/main.rs | 11 +++++------ examples/geometry/src/main.rs | 11 +++++------ examples/loading_spinners/src/circular.rs | 11 +++++------ examples/loading_spinners/src/linear.rs | 11 +++++------ examples/modal/src/main.rs | 8 ++------ examples/toast/src/main.rs | 8 ++------ widget/src/button.rs | 13 ++++++------- widget/src/canvas.rs | 16 ++++++++-------- widget/src/checkbox.rs | 11 +++++------ widget/src/column.rs | 13 ++++++------- widget/src/combo_box.rs | 12 +++++------- widget/src/container.rs | 11 +++++------ widget/src/image.rs | 11 +++++------ widget/src/image/viewer.rs | 11 +++++------ widget/src/keyed/column.rs | 13 ++++++------- widget/src/lazy.rs | 8 ++------ widget/src/lazy/component.rs | 8 ++------ widget/src/lazy/responsive.rs | 11 +++++------ widget/src/mouse_area.rs | 10 +++------- widget/src/overlay/menu.rs | 11 +++++------ widget/src/pane_grid.rs | 11 +++++------ widget/src/pick_list.rs | 11 +++++------ widget/src/progress_bar.rs | 11 +++++------ widget/src/qr_code.rs | 11 +++++------ widget/src/radio.rs | 11 +++++------ widget/src/row.rs | 13 ++++++------- widget/src/rule.rs | 11 +++++------ widget/src/scrollable.rs | 11 +++++------ widget/src/shader.rs | 11 +++++------ widget/src/slider.rs | 11 +++++------ widget/src/space.rs | 11 +++++------ widget/src/svg.rs | 11 +++++------ widget/src/text_editor.rs | 13 ++++++------- widget/src/text_input.rs | 11 +++++------ widget/src/toggler.rs | 11 +++++------ widget/src/tooltip.rs | 8 ++------ widget/src/vertical_slider.rs | 11 +++++------ 42 files changed, 212 insertions(+), 275 deletions(-) diff --git a/core/src/element.rs b/core/src/element.rs index dea111af..8b510218 100644 --- a/core/src/element.rs +++ b/core/src/element.rs @@ -6,7 +6,7 @@ use crate::renderer; use crate::widget; use crate::widget::tree::{self, Tree}; use crate::{ - Clipboard, Color, Layout, Length, Rectangle, Shell, Vector, Widget, + Clipboard, Color, Layout, Length, Rectangle, Shell, Size, Vector, Widget, }; use std::any::Any; @@ -296,12 +296,8 @@ where self.widget.diff(tree); } - fn width(&self) -> Length { - self.widget.width() - } - - fn height(&self) -> Length { - self.widget.height() + fn size(&self) -> Size { + self.widget.size() } fn layout( @@ -466,12 +462,8 @@ impl<'a, Message, Renderer> Widget where Renderer: crate::Renderer, { - fn width(&self) -> Length { - self.element.widget.width() - } - - fn height(&self) -> Length { - self.element.widget.height() + fn size(&self) -> Size { + self.element.widget.size() } fn tag(&self) -> tree::Tag { diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 67cc7f2a..036b31fd 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -86,10 +86,11 @@ where nodes.resize(items.len(), Node::default()); for (i, (child, tree)) in items.iter().zip(trees.iter_mut()).enumerate() { - let (fill_main_factor, fill_cross_factor) = axis.pack( - child.as_widget().width().fill_factor(), - child.as_widget().height().fill_factor(), - ); + let (fill_main_factor, fill_cross_factor) = { + let size = child.as_widget().size(); + + axis.pack(size.width.fill_factor(), size.height.fill_factor()) + }; if fill_main_factor == 0 { if fill_cross_factor == 0 { @@ -124,10 +125,11 @@ where }; for (i, (child, tree)) in items.iter().zip(trees.iter_mut()).enumerate() { - let (fill_main_factor, fill_cross_factor) = axis.pack( - child.as_widget().width().fill_factor(), - child.as_widget().height().fill_factor(), - ); + let (fill_main_factor, fill_cross_factor) = { + let size = child.as_widget().size(); + + axis.pack(size.width.fill_factor(), size.height.fill_factor()) + }; if fill_main_factor == 0 && fill_cross_factor != 0 { let (max_width, max_height) = axis.pack(available, intrinsic_cross); @@ -157,10 +159,11 @@ where }; for (i, (child, tree)) in items.iter().zip(trees).enumerate() { - let (fill_main_factor, fill_cross_factor) = axis.pack( - child.as_widget().width().fill_factor(), - child.as_widget().height().fill_factor(), - ); + let (fill_main_factor, fill_cross_factor) = { + let size = child.as_widget().size(); + + axis.pack(size.width.fill_factor(), size.height.fill_factor()) + }; if fill_main_factor != 0 { let max_main = if fill_main_factor == 0 { diff --git a/core/src/widget.rs b/core/src/widget.rs index 890b3773..7f5632ae 100644 --- a/core/src/widget.rs +++ b/core/src/widget.rs @@ -43,18 +43,15 @@ pub trait Widget where Renderer: crate::Renderer, { - /// Returns the width of the [`Widget`]. - fn width(&self) -> Length; - - /// Returns the height of the [`Widget`]. - fn height(&self) -> Length; + /// Returns the [`Size`] of the [`Widget`] in lengths. + fn size(&self) -> Size; /// Returns a [`Size`] hint for laying out the [`Widget`]. /// /// This hint may be used by some widget containers to adjust their sizing strategy /// during construction. fn size_hint(&self) -> Size { - Size::new(self.width(), self.height()) + self.size() } /// Returns the [`layout::Node`] of the [`Widget`]. diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index e47e4178..fe3b77d3 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -5,7 +5,9 @@ use crate::mouse; use crate::renderer; use crate::text::{self, Paragraph}; use crate::widget::tree::{self, Tree}; -use crate::{Color, Element, Layout, Length, Pixels, Point, Rectangle, Widget}; +use crate::{ + Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Widget, +}; use std::borrow::Cow; @@ -134,12 +136,11 @@ where tree::State::new(State(Renderer::Paragraph::default())) } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/examples/custom_quad/src/main.rs b/examples/custom_quad/src/main.rs index 13b08250..cc9ad528 100644 --- a/examples/custom_quad/src/main.rs +++ b/examples/custom_quad/src/main.rs @@ -26,12 +26,11 @@ mod quad { where Renderer: renderer::Renderer, { - fn width(&self) -> Length { - Length::Shrink - } - - fn height(&self) -> Length { - Length::Shrink + fn size(&self) -> Size { + Size { + width: Length::Shrink, + height: Length::Shrink, + } } fn layout( diff --git a/examples/custom_widget/src/main.rs b/examples/custom_widget/src/main.rs index 32a14cbe..7ffb4cd0 100644 --- a/examples/custom_widget/src/main.rs +++ b/examples/custom_widget/src/main.rs @@ -33,12 +33,11 @@ mod circle { where Renderer: renderer::Renderer, { - fn width(&self) -> Length { - Length::Shrink - } - - fn height(&self) -> Length { - Length::Shrink + fn size(&self) -> Size { + Size { + width: Length::Shrink, + height: Length::Shrink, + } } fn layout( diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index 50227f1c..d6a4c702 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -16,12 +16,11 @@ mod rainbow { } impl Widget for Rainbow { - fn width(&self) -> Length { - Length::Fill - } - - fn height(&self) -> Length { - Length::Shrink + fn size(&self) -> Size { + Size { + width: Length::Fill, + height: Length::Shrink, + } } fn layout( diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index a92a5dd1..e80617d0 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -244,12 +244,11 @@ where tree::State::new(State::default()) } - fn width(&self) -> Length { - Length::Fixed(self.size) - } - - fn height(&self) -> Length { - Length::Fixed(self.size) + fn size(&self) -> Size { + Size { + width: Length::Fixed(self.size), + height: Length::Fixed(self.size), + } } fn layout( diff --git a/examples/loading_spinners/src/linear.rs b/examples/loading_spinners/src/linear.rs index da4f1ea1..d205d3f1 100644 --- a/examples/loading_spinners/src/linear.rs +++ b/examples/loading_spinners/src/linear.rs @@ -165,12 +165,11 @@ where tree::State::new(State::default()) } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 85ccf8b4..631efe6e 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -281,12 +281,8 @@ mod modal { tree.diff_children(&[&self.base, &self.modal]); } - fn width(&self) -> Length { - self.base.as_widget().width() - } - - fn height(&self) -> Length { - self.base.as_widget().height() + fn size(&self) -> Size { + self.base.as_widget().size() } fn layout( diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 711d8223..300343b9 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -313,12 +313,8 @@ mod toast { } impl<'a, Message> Widget for Manager<'a, Message> { - fn width(&self) -> Length { - self.content.as_widget().width() - } - - fn height(&self) -> Length { - self.content.as_widget().height() + fn size(&self) -> Size { + self.content.as_widget().size() } fn layout( diff --git a/widget/src/button.rs b/widget/src/button.rs index ba68caa5..1ce4f662 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -11,7 +11,7 @@ use crate::core::widget::tree::{self, Tree}; use crate::core::widget::Operation; use crate::core::{ Background, Clipboard, Color, Element, Layout, Length, Padding, Point, - Rectangle, Shell, Vector, Widget, + Rectangle, Shell, Size, Vector, Widget, }; pub use iced_style::button::{Appearance, StyleSheet}; @@ -149,12 +149,11 @@ where tree.diff_children(std::slice::from_ref(&self.content)); } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/canvas.rs b/widget/src/canvas.rs index 9e33c113..2bf09eec 100644 --- a/widget/src/canvas.rs +++ b/widget/src/canvas.rs @@ -14,8 +14,9 @@ use crate::core::layout::{self, Layout}; use crate::core::mouse; use crate::core::renderer; use crate::core::widget::tree::{self, Tree}; -use crate::core::{Clipboard, Element, Shell, Widget}; -use crate::core::{Length, Rectangle, Size, Vector}; +use crate::core::{ + Clipboard, Element, Length, Rectangle, Shell, Size, Vector, Widget, +}; use crate::graphics::geometry; use std::marker::PhantomData; @@ -119,12 +120,11 @@ where tree::State::new(P::State::default()) } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/checkbox.rs b/widget/src/checkbox.rs index a0d9559b..0353b3ad 100644 --- a/widget/src/checkbox.rs +++ b/widget/src/checkbox.rs @@ -174,12 +174,11 @@ where tree::State::new(widget::text::State::::default()) } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - Length::Shrink + fn size(&self) -> Size { + Size { + width: self.width, + height: Length::Shrink, + } } fn layout( diff --git a/widget/src/column.rs b/widget/src/column.rs index 52cf35ce..9867d97e 100644 --- a/widget/src/column.rs +++ b/widget/src/column.rs @@ -7,7 +7,7 @@ use crate::core::renderer; use crate::core::widget::{Operation, Tree}; use crate::core::{ Alignment, Clipboard, Element, Layout, Length, Padding, Pixels, Rectangle, - Shell, Widget, + Shell, Size, Widget, }; /// A container that distributes its contents vertically. @@ -129,12 +129,11 @@ where tree.diff_children(&self.children); } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 31ec27fc..1b2fa947 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -8,7 +8,9 @@ use crate::core::renderer; use crate::core::text; use crate::core::time::Instant; use crate::core::widget::{self, Widget}; -use crate::core::{Clipboard, Element, Length, Padding, Rectangle, Shell}; +use crate::core::{ + Clipboard, Element, Length, Padding, Rectangle, Shell, Size, +}; use crate::overlay::menu; use crate::text::LineHeight; use crate::{container, scrollable, text_input, TextInput}; @@ -297,12 +299,8 @@ where + scrollable::StyleSheet + menu::StyleSheet, { - fn width(&self) -> Length { - Widget::::width(&self.text_input) - } - - fn height(&self) -> Length { - Widget::::height(&self.text_input) + fn size(&self) -> Size { + Widget::::size(&self.text_input) } fn layout( diff --git a/widget/src/container.rs b/widget/src/container.rs index fbc68db7..93d8daba 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -163,12 +163,11 @@ where self.content.as_widget().diff(tree); } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/image.rs b/widget/src/image.rs index b5f1e907..6750c1b3 100644 --- a/widget/src/image.rs +++ b/widget/src/image.rs @@ -164,12 +164,11 @@ where Renderer: image::Renderer, Handle: Clone + Hash, { - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/image/viewer.rs b/widget/src/image/viewer.rs index 23c4fe86..dc910f1f 100644 --- a/widget/src/image/viewer.rs +++ b/widget/src/image/viewer.rs @@ -97,12 +97,11 @@ where tree::State::new(State::new()) } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/keyed/column.rs b/widget/src/keyed/column.rs index 1b53b43a..32320300 100644 --- a/widget/src/keyed/column.rs +++ b/widget/src/keyed/column.rs @@ -8,7 +8,7 @@ use crate::core::widget::tree::{self, Tree}; use crate::core::widget::Operation; use crate::core::{ Alignment, Clipboard, Element, Layout, Length, Padding, Pixels, Rectangle, - Shell, Widget, + Shell, Size, Widget, }; /// A container that distributes its contents vertically. @@ -173,12 +173,11 @@ where } } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/lazy.rs b/widget/src/lazy.rs index 4f6513db..e9edbb4c 100644 --- a/widget/src/lazy.rs +++ b/widget/src/lazy.rs @@ -142,12 +142,8 @@ where } } - fn width(&self) -> Length { - self.with_element(|element| element.as_widget().width()) - } - - fn height(&self) -> Length { - self.with_element(|element| element.as_widget().height()) + fn size(&self) -> Size { + self.with_element(|element| element.as_widget().size()) } fn size_hint(&self) -> Size { diff --git a/widget/src/lazy/component.rs b/widget/src/lazy/component.rs index 0aff7485..3684e0c9 100644 --- a/widget/src/lazy/component.rs +++ b/widget/src/lazy/component.rs @@ -244,12 +244,8 @@ where self.rebuild_element_if_necessary(); } - fn width(&self) -> Length { - self.with_element(|element| element.as_widget().width()) - } - - fn height(&self) -> Length { - self.with_element(|element| element.as_widget().height()) + fn size(&self) -> Size { + self.with_element(|element| element.as_widget().size()) } fn size_hint(&self) -> Size { diff --git a/widget/src/lazy/responsive.rs b/widget/src/lazy/responsive.rs index 86d37b6c..1df0866f 100644 --- a/widget/src/lazy/responsive.rs +++ b/widget/src/lazy/responsive.rs @@ -135,12 +135,11 @@ where }) } - fn width(&self) -> Length { - Length::Fill - } - - fn height(&self) -> Length { - Length::Fill + fn size(&self) -> Size { + Size { + width: Length::Fill, + height: Length::Fill, + } } fn layout( diff --git a/widget/src/mouse_area.rs b/widget/src/mouse_area.rs index 3a5b01a3..87cac3a7 100644 --- a/widget/src/mouse_area.rs +++ b/widget/src/mouse_area.rs @@ -8,7 +8,7 @@ use crate::core::renderer; use crate::core::touch; use crate::core::widget::{tree, Operation, Tree}; use crate::core::{ - Clipboard, Element, Layout, Length, Rectangle, Shell, Widget, + Clipboard, Element, Layout, Length, Rectangle, Shell, Size, Widget, }; /// Emit messages on mouse events. @@ -110,12 +110,8 @@ where tree.diff_children(std::slice::from_ref(&self.content)); } - fn width(&self) -> Length { - self.content.as_widget().width() - } - - fn height(&self) -> Length { - self.content.as_widget().height() + fn size(&self) -> Size { + self.content.as_widget().size() } fn layout( diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index ef39a952..b9e06de8 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -342,12 +342,11 @@ where Renderer: text::Renderer, Renderer::Theme: StyleSheet, { - fn width(&self) -> Length { - Length::Fill - } - - fn height(&self) -> Length { - Length::Shrink + fn size(&self) -> Size { + Size { + width: Length::Fill, + height: Length::Shrink, + } } fn layout( diff --git a/widget/src/pane_grid.rs b/widget/src/pane_grid.rs index 3d799fd3..36c785b7 100644 --- a/widget/src/pane_grid.rs +++ b/widget/src/pane_grid.rs @@ -265,12 +265,11 @@ where } } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index 13110725..d83b0624 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -164,12 +164,11 @@ where tree::State::new(State::::new()) } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - Length::Shrink + fn size(&self) -> Size { + Size { + width: self.width, + height: Length::Shrink, + } } fn layout( diff --git a/widget/src/progress_bar.rs b/widget/src/progress_bar.rs index b84ab2dd..a05923a2 100644 --- a/widget/src/progress_bar.rs +++ b/widget/src/progress_bar.rs @@ -85,12 +85,11 @@ where Renderer: crate::core::Renderer, Renderer::Theme: StyleSheet, { - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT)) + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT)), + } } fn layout( diff --git a/widget/src/qr_code.rs b/widget/src/qr_code.rs index 1dc4da7f..a229eb59 100644 --- a/widget/src/qr_code.rs +++ b/widget/src/qr_code.rs @@ -50,12 +50,11 @@ impl<'a> QRCode<'a> { } impl<'a, Message, Theme> Widget> for QRCode<'a> { - fn width(&self) -> Length { - Length::Shrink - } - - fn height(&self) -> Length { - Length::Shrink + fn size(&self) -> Size { + Size { + width: Length::Shrink, + height: Length::Shrink, + } } fn layout( diff --git a/widget/src/radio.rs b/widget/src/radio.rs index ae2365dd..f91b20b1 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -201,12 +201,11 @@ where tree::State::new(widget::text::State::::default()) } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - Length::Shrink + fn size(&self) -> Size { + Size { + width: self.width, + height: Length::Shrink, + } } fn layout( diff --git a/widget/src/row.rs b/widget/src/row.rs index ef371ddb..bcbe9267 100644 --- a/widget/src/row.rs +++ b/widget/src/row.rs @@ -7,7 +7,7 @@ use crate::core::renderer; use crate::core::widget::{Operation, Tree}; use crate::core::{ Alignment, Clipboard, Element, Length, Padding, Pixels, Rectangle, Shell, - Widget, + Size, Widget, }; /// A container that distributes its contents horizontally. @@ -123,12 +123,11 @@ where tree.diff_children(&self.children); } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/rule.rs b/widget/src/rule.rs index ecaedf60..4ab16c40 100644 --- a/widget/src/rule.rs +++ b/widget/src/rule.rs @@ -62,12 +62,11 @@ where Renderer: crate::core::Renderer, Renderer::Theme: StyleSheet, { - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 525463c4..5197afde 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -220,12 +220,11 @@ where tree.diff_children(std::slice::from_ref(&self.content)); } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/shader.rs b/widget/src/shader.rs index 5b18ec7d..82432c6c 100644 --- a/widget/src/shader.rs +++ b/widget/src/shader.rs @@ -70,12 +70,11 @@ where tree::State::new(P::State::default()) } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/slider.rs b/widget/src/slider.rs index 2b600d9d..27588852 100644 --- a/widget/src/slider.rs +++ b/widget/src/slider.rs @@ -159,12 +159,11 @@ where tree::State::new(State::new()) } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - Length::Shrink + fn size(&self) -> Size { + Size { + width: self.width, + height: Length::Shrink, + } } fn layout( diff --git a/widget/src/space.rs b/widget/src/space.rs index afa9a7c8..9fd4dcb9 100644 --- a/widget/src/space.rs +++ b/widget/src/space.rs @@ -45,12 +45,11 @@ impl Widget for Space where Renderer: core::Renderer, { - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/svg.rs b/widget/src/svg.rs index 8367ad18..75ab238a 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -96,12 +96,11 @@ where Renderer: svg::Renderer, Renderer::Theme: iced_style::svg::StyleSheet, { - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 214bce17..9118d124 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -9,7 +9,7 @@ use crate::core::text::highlighter::{self, Highlighter}; use crate::core::text::{self, LineHeight}; use crate::core::widget::{self, Widget}; use crate::core::{ - Clipboard, Color, Element, Length, Padding, Pixels, Rectangle, Shell, + Clipboard, Color, Element, Length, Padding, Pixels, Rectangle, Shell, Size, Vector, }; @@ -316,12 +316,11 @@ where }) } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: self.width, + height: self.height, + } } fn layout( diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 03eb2fd0..7e91105c 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -283,12 +283,11 @@ where } } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - Length::Shrink + fn size(&self) -> Size { + Size { + width: self.width, + height: Length::Shrink, + } } fn layout( diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index d8723080..941159ea 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -168,12 +168,11 @@ where tree::State::new(widget::text::State::::default()) } - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - Length::Shrink + fn size(&self) -> Size { + Size { + width: self.width, + height: Length::Shrink, + } } fn layout( diff --git a/widget/src/tooltip.rs b/widget/src/tooltip.rs index adef13e4..d09a9255 100644 --- a/widget/src/tooltip.rs +++ b/widget/src/tooltip.rs @@ -131,12 +131,8 @@ where widget::tree::Tag::of::() } - fn width(&self) -> Length { - self.content.as_widget().width() - } - - fn height(&self) -> Length { - self.content.as_widget().height() + fn size(&self) -> Size { + self.content.as_widget().size() } fn layout( diff --git a/widget/src/vertical_slider.rs b/widget/src/vertical_slider.rs index e489104c..35bc2fe2 100644 --- a/widget/src/vertical_slider.rs +++ b/widget/src/vertical_slider.rs @@ -156,12 +156,11 @@ where tree::State::new(State::new()) } - fn width(&self) -> Length { - Length::Shrink - } - - fn height(&self) -> Length { - self.height + fn size(&self) -> Size { + Size { + width: Length::Shrink, + height: self.height, + } } fn layout( -- cgit From 4bdd8a62791cfa4864d3d4cf1d5b19c6f227d537 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Jan 2024 17:54:10 +0100 Subject: Fix `cross` axis calculation in `flex` layout --- core/src/layout/flex.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 036b31fd..2a12d57f 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -142,6 +142,7 @@ where let size = layout.size(); available -= axis.main(size); + cross = cross.max(axis.cross(layout.size())); nodes[i] = layout; } -- cgit From d24e50c1a61eee7bca887224ad583eca60e14d32 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jan 2024 02:12:29 +0100 Subject: Reduce `padding` of `scrollable` example --- examples/scrollable/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 249bc2a5..4b57a5a4 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -338,7 +338,7 @@ impl Application for ScrollableDemo { .spacing(10) .into(); - Element::from(container(content).padding(40).center_x().center_y()) + container(content).padding(20).center_x().center_y().into() } fn theme(&self) -> Self::Theme { -- cgit From d62bb8193c1c43f565fcc5c52293d564c91e215d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jan 2024 06:35:33 +0100 Subject: Introduce useful helpers in `layout` module --- core/src/layout.rs | 94 ++++++++++++++++++++++++++++++- core/src/layout/flex.rs | 2 +- core/src/layout/limits.rs | 7 ++- core/src/layout/node.rs | 10 +++- core/src/point.rs | 20 ++++--- core/src/widget/text.rs | 43 +++++++------- examples/geometry/src/main.rs | 4 +- examples/loading_spinners/src/circular.rs | 5 +- examples/loading_spinners/src/linear.rs | 5 +- widget/src/button.rs | 19 +------ widget/src/canvas.rs | 4 +- widget/src/container.rs | 28 ++++----- widget/src/image.rs | 2 +- widget/src/image/viewer.rs | 2 +- widget/src/overlay/menu.rs | 2 +- widget/src/pane_grid.rs | 2 +- widget/src/pick_list.rs | 5 +- widget/src/progress_bar.rs | 8 +-- widget/src/rule.rs | 2 +- widget/src/scrollable.rs | 39 ++++++------- widget/src/shader.rs | 5 +- widget/src/slider.rs | 4 +- widget/src/space.rs | 2 +- widget/src/svg.rs | 2 +- widget/src/text_input.rs | 4 +- widget/src/vertical_slider.rs | 4 +- 26 files changed, 193 insertions(+), 131 deletions(-) diff --git a/core/src/layout.rs b/core/src/layout.rs index 277473fe..95720aba 100644 --- a/core/src/layout.rs +++ b/core/src/layout.rs @@ -7,7 +7,7 @@ pub mod flex; pub use limits::Limits; pub use node::Node; -use crate::{Point, Rectangle, Size, Vector}; +use crate::{Length, Padding, Point, Rectangle, Size, Vector}; /// The bounds of a [`Node`] and its children, using absolute coordinates. #[derive(Debug, Clone, Copy)] @@ -96,3 +96,95 @@ pub fn next_to_each_other( ], ) } + +/// Computes the resulting [`Node`] that fits the [`Limits`] given +/// some width and height requirements and no intrinsic size. +pub fn atomic( + limits: &Limits, + width: impl Into, + height: impl Into, +) -> Node { + let width = width.into(); + let height = height.into(); + + Node::new(limits.resolve(width, height, Size::ZERO)) +} + +/// Computes the resulting [`Node`] that fits the [`Limits`] given +/// some width and height requirements and a closure that produces +/// the intrinsic [`Size`] inside the given [`Limits`]. +pub fn sized( + limits: &Limits, + width: impl Into, + height: impl Into, + f: impl FnOnce(&Limits) -> Size, +) -> Node { + let width = width.into(); + let height = height.into(); + + let limits = limits.width(width).height(height); + let intrinsic_size = f(&limits); + + Node::new(limits.resolve(width, height, intrinsic_size)) +} + +/// Computes the resulting [`Node`] that fits the [`Limits`] given +/// some width and height requirements and a closure that produces +/// the content [`Node`] inside the given [`Limits`]. +pub fn contained( + limits: &Limits, + width: impl Into, + height: impl Into, + f: impl FnOnce(&Limits) -> Node, +) -> Node { + let width = width.into(); + let height = height.into(); + + let limits = limits.width(width).height(height); + let content = f(&limits); + + Node::with_children( + limits.resolve(width, height, content.size()), + vec![content], + ) +} + +/// Computes the [`Node`] that fits the [`Limits`] given some width, height, and +/// [`Padding`] requirements and a closure that produces the content [`Node`] +/// inside the given [`Limits`]. +pub fn padded( + limits: &Limits, + width: impl Into, + height: impl Into, + padding: impl Into, + layout: impl FnOnce(&Limits) -> Node, +) -> Node { + positioned(limits, width, height, padding, layout, |content, _| content) +} + +/// Computes a [`padded`] [`Node`] with a positioning step. +pub fn positioned( + limits: &Limits, + width: impl Into, + height: impl Into, + padding: impl Into, + layout: impl FnOnce(&Limits) -> Node, + position: impl FnOnce(Node, Size) -> Node, +) -> Node { + let width = width.into(); + let height = height.into(); + let padding = padding.into(); + + let limits = limits.width(width).height(height); + let content = layout(&limits.shrink(padding)); + let padding = padding.fit(content.size(), limits.max()); + + let size = limits + .shrink(padding) + .resolve(width, height, content.size()); + + Node::with_children( + size.expand(padding), + vec![position(content.move_to((padding.left, padding.top)), size)], + ) +} diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 2a12d57f..cf3e1340 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -239,9 +239,9 @@ where let (intrinsic_width, intrinsic_height) = axis.pack(main - pad.0, cross); let size = limits.resolve( - Size::new(intrinsic_width, intrinsic_height), width, height, + Size::new(intrinsic_width, intrinsic_height), ); Node::with_children(size.expand(padding), nodes) diff --git a/core/src/layout/limits.rs b/core/src/layout/limits.rs index eef4c4c9..7fbc7b9d 100644 --- a/core/src/layout/limits.rs +++ b/core/src/layout/limits.rs @@ -114,13 +114,14 @@ impl Limits { } } - /// Computes the resulting [`Size`] that fits the [`Limits`] given the - /// intrinsic size of some content. + /// Computes the resulting [`Size`] that fits the [`Limits`] given + /// some width and height requirements and the intrinsic size of + /// some content. pub fn resolve( &self, - intrinsic_size: Size, width: impl Into, height: impl Into, + intrinsic_size: Size, ) -> Size { let width = match width.into() { Length::Fill | Length::FillPortion(_) => self.max.width, diff --git a/core/src/layout/node.rs b/core/src/layout/node.rs index 00087431..40c71436 100644 --- a/core/src/layout/node.rs +++ b/core/src/layout/node.rs @@ -89,19 +89,23 @@ impl Node { } /// Moves the [`Node`] to the given position. - pub fn move_to(mut self, position: Point) -> Self { + pub fn move_to(mut self, position: impl Into) -> Self { self.move_to_mut(position); self } /// Mutable reference version of [`move_to`]. - pub fn move_to_mut(&mut self, position: Point) { + pub fn move_to_mut(&mut self, position: impl Into) { + let position = position.into(); + self.bounds.x = position.x; self.bounds.y = position.y; } /// Translates the [`Node`] by the given translation. - pub fn translate(self, translation: Vector) -> Self { + pub fn translate(self, translation: impl Into) -> Self { + let translation = translation.into(); + Self { bounds: self.bounds + translation, ..self diff --git a/core/src/point.rs b/core/src/point.rs index ef42852f..cea57518 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -36,20 +36,26 @@ impl Point { } } -impl From<[f32; 2]> for Point { - fn from([x, y]: [f32; 2]) -> Self { +impl From<[T; 2]> for Point +where + T: Num, +{ + fn from([x, y]: [T; 2]) -> Self { Point { x, y } } } -impl From<[u16; 2]> for Point { - fn from([x, y]: [u16; 2]) -> Self { - Point::new(x, y) +impl From<(T, T)> for Point +where + T: Num, +{ + fn from((x, y): (T, T)) -> Self { + Self { x, y } } } -impl From for [f32; 2] { - fn from(point: Point) -> [f32; 2] { +impl From> for [T; 2] { + fn from(point: Point) -> [T; 2] { [point.x, point.y] } } diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index fe3b77d3..4cabc7ce 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -206,28 +206,27 @@ pub fn layout( where Renderer: text::Renderer, { - let limits = limits.width(width).height(height); - let bounds = limits.max(); - - let size = size.unwrap_or_else(|| renderer.default_size()); - let font = font.unwrap_or_else(|| renderer.default_font()); - - let State(ref mut paragraph) = state; - - paragraph.update(text::Text { - content, - bounds, - size, - line_height, - font, - horizontal_alignment, - vertical_alignment, - shaping, - }); - - let size = limits.resolve(paragraph.min_bounds(), width, height); - - layout::Node::new(size) + layout::sized(limits, width, height, |limits| { + let bounds = limits.max(); + + let size = size.unwrap_or_else(|| renderer.default_size()); + let font = font.unwrap_or_else(|| renderer.default_font()); + + let State(ref mut paragraph) = state; + + paragraph.update(text::Text { + content, + bounds, + size, + line_height, + font, + horizontal_alignment, + vertical_alignment, + shaping, + }); + + paragraph.min_bounds() + }) } /// Draws text using the same logic as the [`Text`] widget. diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index d6a4c702..5cf9963d 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -29,9 +29,9 @@ mod rainbow { _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let size = limits.resolve(Size::ZERO, Length::Fill, Length::Shrink); + let width = limits.max().width; - layout::Node::new(Size::new(size.width, size.width)) + layout::Node::new(Size::new(width, width)) } fn draw( diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index e80617d0..1b163585 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -257,10 +257,7 @@ where _renderer: &iced::Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits.width(self.size).height(self.size); - let size = limits.resolve(Size::ZERO, self.size, self.size); - - layout::Node::new(size) + layout::atomic(limits, self.size, self.size) } fn on_event( diff --git a/examples/loading_spinners/src/linear.rs b/examples/loading_spinners/src/linear.rs index d205d3f1..d245575c 100644 --- a/examples/loading_spinners/src/linear.rs +++ b/examples/loading_spinners/src/linear.rs @@ -178,10 +178,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - let size = limits.resolve(Size::ZERO, self.width, self.height); - - layout::Node::new(size) + layout::atomic(limits, self.width, self.height) } fn on_event( diff --git a/widget/src/button.rs b/widget/src/button.rs index 1ce4f662..86abee77 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -10,8 +10,8 @@ use crate::core::touch; use crate::core::widget::tree::{self, Tree}; use crate::core::widget::Operation; use crate::core::{ - Background, Clipboard, Color, Element, Layout, Length, Padding, Point, - Rectangle, Shell, Size, Vector, Widget, + Background, Clipboard, Color, Element, Layout, Length, Padding, Rectangle, + Shell, Size, Vector, Widget, }; pub use iced_style::button::{Appearance, StyleSheet}; @@ -430,20 +430,7 @@ pub fn layout( padding: Padding, layout_content: impl FnOnce(&layout::Limits) -> layout::Node, ) -> layout::Node { - let limits = limits.width(width).height(height); - - let content = layout_content(&limits.shrink(padding)); - let padding = padding.fit(content.size(), limits.max()); - - let size = limits - .shrink(padding) - .resolve(content.size(), width, height) - .expand(padding); - - layout::Node::with_children( - size, - vec![content.move_to(Point::new(padding.left, padding.top))], - ) + layout::padded(limits, width, height, padding, layout_content) } /// Returns the [`mouse::Interaction`] of a [`Button`]. diff --git a/widget/src/canvas.rs b/widget/src/canvas.rs index 2bf09eec..4e42a671 100644 --- a/widget/src/canvas.rs +++ b/widget/src/canvas.rs @@ -133,9 +133,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let size = limits.resolve(Size::ZERO, self.width, self.height); - - layout::Node::new(size) + layout::atomic(limits, self.width, self.height) } fn on_event( diff --git a/widget/src/container.rs b/widget/src/container.rs index 93d8daba..c98de41c 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -321,27 +321,19 @@ pub fn layout( vertical_alignment: alignment::Vertical, layout_content: impl FnOnce(&layout::Limits) -> layout::Node, ) -> layout::Node { - let limits = limits - .width(width) - .height(height) - .max_width(max_width) - .max_height(max_height); - - let content = layout_content(&limits.shrink(padding).loose()); - let padding = padding.fit(content.size(), limits.max()); - let size = limits - .shrink(padding) - .resolve(content.size(), width, height); - - layout::Node::with_children( - size.expand(padding), - vec![content - .move_to(Point::new(padding.left, padding.top)) - .align( + layout::positioned( + &limits.max_width(max_width).max_height(max_height), + width, + height, + padding, + |limits| layout_content(&limits.loose()), + |content, size| { + content.align( Alignment::from(horizontal_alignment), Alignment::from(vertical_alignment), size, - )], + ) + }, ) } diff --git a/widget/src/image.rs b/widget/src/image.rs index 6750c1b3..e906ac13 100644 --- a/widget/src/image.rs +++ b/widget/src/image.rs @@ -99,7 +99,7 @@ where }; // The size to be available to the widget prior to `Shrink`ing - let raw_size = limits.resolve(image_size, width, height); + let raw_size = limits.resolve(width, height, image_size); // The uncropped size of the image when fit to the bounds above let full_size = content_fit.fit(image_size, raw_size); diff --git a/widget/src/image/viewer.rs b/widget/src/image/viewer.rs index dc910f1f..98080577 100644 --- a/widget/src/image/viewer.rs +++ b/widget/src/image/viewer.rs @@ -113,9 +113,9 @@ where let Size { width, height } = renderer.dimensions(&self.handle); let mut size = limits.resolve( - Size::new(width as f32, height as f32), self.width, self.height, + Size::new(width as f32, height as f32), ); let expansion_size = if height > width { diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index b9e06de8..f83eebea 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -369,7 +369,7 @@ where * self.options.len() as f32, ); - limits.resolve(intrinsic, Length::Fill, Length::Shrink) + limits.resolve(Length::Fill, Length::Shrink, intrinsic) }; layout::Node::new(size) diff --git a/widget/src/pane_grid.rs b/widget/src/pane_grid.rs index 36c785b7..cf1f0455 100644 --- a/widget/src/pane_grid.rs +++ b/widget/src/pane_grid.rs @@ -489,7 +489,7 @@ pub fn layout( &layout::Limits, ) -> layout::Node, ) -> layout::Node { - let size = limits.resolve(Size::ZERO, width, height); + let size = limits.resolve(width, height, Size::ZERO); let regions = node.pane_regions(spacing, size); let children = contents diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index d83b0624..2576a1e8 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -392,7 +392,6 @@ where { use std::f32; - let limits = limits.width(width).height(Length::Shrink); let font = font.unwrap_or_else(|| renderer.default_font()); let text_size = text_size.unwrap_or_else(|| renderer.default_size()); @@ -451,8 +450,10 @@ where ); limits + .width(width) + .height(Length::Shrink) .shrink(padding) - .resolve(intrinsic, width, Length::Shrink) + .resolve(width, Length::Shrink, intrinsic) .expand(padding) }; diff --git a/widget/src/progress_bar.rs b/widget/src/progress_bar.rs index a05923a2..15f1277b 100644 --- a/widget/src/progress_bar.rs +++ b/widget/src/progress_bar.rs @@ -98,13 +98,11 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let size = limits.resolve( - Size::ZERO, + layout::atomic( + limits, self.width, self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT)), - ); - - layout::Node::new(size) + ) } fn draw( diff --git a/widget/src/rule.rs b/widget/src/rule.rs index 4ab16c40..cded9cb1 100644 --- a/widget/src/rule.rs +++ b/widget/src/rule.rs @@ -75,7 +75,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - layout::Node::new(limits.resolve(Size::ZERO, self.width, self.height)) + layout::atomic(limits, self.width, self.height) } fn draw( diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 5197afde..70db490a 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -469,28 +469,25 @@ pub fn layout( direction: &Direction, layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, ) -> layout::Node { - let limits = limits.width(width).height(height); - - let child_limits = layout::Limits::new( - Size::new(limits.min().width, limits.min().height), - Size::new( - if direction.horizontal().is_some() { - f32::INFINITY - } else { - limits.max().width - }, - if direction.vertical().is_some() { - f32::MAX - } else { - limits.max().height - }, - ), - ); - - let content = layout_content(renderer, &child_limits); - let size = limits.resolve(content.size(), width, height); + layout::contained(limits, width, height, |limits| { + let child_limits = layout::Limits::new( + Size::new(limits.min().width, limits.min().height), + Size::new( + if direction.horizontal().is_some() { + f32::INFINITY + } else { + limits.max().width + }, + if direction.vertical().is_some() { + f32::MAX + } else { + limits.max().height + }, + ), + ); - layout::Node::with_children(size, vec![content]) + layout_content(renderer, &child_limits) + }) } /// Processes an [`Event`] and updates the [`State`] of a [`Scrollable`] diff --git a/widget/src/shader.rs b/widget/src/shader.rs index 82432c6c..16b68c55 100644 --- a/widget/src/shader.rs +++ b/widget/src/shader.rs @@ -83,10 +83,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - let size = limits.resolve(Size::ZERO, self.width, self.height); - - layout::Node::new(size) + layout::atomic(limits, self.width, self.height) } fn on_event( diff --git a/widget/src/slider.rs b/widget/src/slider.rs index 27588852..1bc94661 100644 --- a/widget/src/slider.rs +++ b/widget/src/slider.rs @@ -172,9 +172,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let size = limits.resolve(Size::ZERO, self.width, self.height); - - layout::Node::new(size) + layout::atomic(limits, self.width, self.height) } fn on_event( diff --git a/widget/src/space.rs b/widget/src/space.rs index 9fd4dcb9..eef990d1 100644 --- a/widget/src/space.rs +++ b/widget/src/space.rs @@ -58,7 +58,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - layout::Node::new(limits.resolve(Size::ZERO, self.width, self.height)) + layout::atomic(limits, self.width, self.height) } fn draw( diff --git a/widget/src/svg.rs b/widget/src/svg.rs index 75ab238a..830abb0f 100644 --- a/widget/src/svg.rs +++ b/widget/src/svg.rs @@ -114,7 +114,7 @@ where let image_size = Size::new(width as f32, height as f32); // The size to be available to the widget prior to `Shrink`ing - let raw_size = limits.resolve(image_size, self.width, self.height); + let raw_size = limits.resolve(self.width, self.height, image_size); // The uncropped size of the image when fit to the bounds above let full_size = self.content_fit.fit(image_size, raw_size); diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 7e91105c..d8540658 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -508,8 +508,8 @@ where let padding = padding.fit(Size::ZERO, limits.max()); let height = line_height.to_absolute(text_size); - let limits = limits.width(width).shrink(padding).height(height); - let text_bounds = limits.resolve(Size::ZERO, width, height); + let limits = limits.width(width).shrink(padding); + let text_bounds = limits.resolve(width, height, Size::ZERO); let placeholder_text = Text { font, diff --git a/widget/src/vertical_slider.rs b/widget/src/vertical_slider.rs index 35bc2fe2..a3029d76 100644 --- a/widget/src/vertical_slider.rs +++ b/widget/src/vertical_slider.rs @@ -169,9 +169,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let size = limits.resolve(Size::ZERO, self.width, self.height); - - layout::Node::new(size) + layout::atomic(limits, self.width, self.height) } fn on_event( -- cgit From e710e7694907fe320e0a849e880c51952e6e748f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jan 2024 06:44:15 +0100 Subject: Fix `size_hint` for `keyed_column` --- examples/todos/src/main.rs | 9 +-------- widget/src/helpers.rs | 1 + widget/src/keyed/column.rs | 45 +++++++++++++++++++++++++-------------------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 4dac032c..aad47c20 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -254,13 +254,7 @@ impl Application for Todos { .spacing(20) .max_width(800); - scrollable( - container(content) - .width(Length::Fill) - .padding(40) - .center_x(), - ) - .into() + scrollable(container(content).padding(40).center_x()).into() } } } @@ -472,7 +466,6 @@ fn empty_message(message: &str) -> Element<'_, Message> { .horizontal_alignment(alignment::Horizontal::Center) .style(Color::from([0.7, 0.7, 0.7])), ) - .width(Length::Fill) .height(200) .center_y() .into() diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 6eaf3392..75528a0c 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -80,6 +80,7 @@ pub fn keyed_column<'a, Key, Message, Renderer>( ) -> keyed::Column<'a, Key, Message, Renderer> where Key: Copy + PartialEq, + Renderer: core::Renderer, { keyed::Column::with_children(children) } diff --git a/widget/src/keyed/column.rs b/widget/src/keyed/column.rs index 32320300..7f05a81e 100644 --- a/widget/src/keyed/column.rs +++ b/widget/src/keyed/column.rs @@ -30,26 +30,10 @@ where impl<'a, Key, Message, Renderer> Column<'a, Key, Message, Renderer> where Key: Copy + PartialEq, + Renderer: crate::core::Renderer, { /// Creates an empty [`Column`]. pub fn new() -> Self { - Self::with_children(Vec::new()) - } - - /// Creates a [`Column`] with the given elements. - pub fn with_children( - children: impl IntoIterator)>, - ) -> Self { - let (keys, children) = children.into_iter().fold( - (Vec::new(), Vec::new()), - |(mut keys, mut children), (key, child)| { - keys.push(key); - children.push(child); - - (keys, children) - }, - ); - Column { spacing: 0.0, padding: Padding::ZERO, @@ -57,11 +41,20 @@ where height: Length::Shrink, max_width: f32::INFINITY, align_items: Alignment::Start, - keys, - children, + keys: Vec::new(), + children: Vec::new(), } } + /// Creates a [`Column`] with the given elements. + pub fn with_children( + children: impl IntoIterator)>, + ) -> Self { + children + .into_iter() + .fold(Self::new(), |column, (key, child)| column.push(key, child)) + } + /// Sets the vertical spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -108,8 +101,19 @@ where key: Key, child: impl Into>, ) -> Self { + let child = child.into(); + let size = child.as_widget().size_hint(); + + if size.width.is_fill() { + self.width = Length::Fill; + } + + if size.height.is_fill() { + self.height = Length::Fill; + } + self.keys.push(key); - self.children.push(child.into()); + self.children.push(child); self } } @@ -117,6 +121,7 @@ where impl<'a, Key, Message, Renderer> Default for Column<'a, Key, Message, Renderer> where Key: Copy + PartialEq, + Renderer: crate::core::Renderer, { fn default() -> Self { Self::new() -- cgit From 67277fbf93f4c180eff67bdc4c9dcf84a54d3425 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jan 2024 06:46:49 +0100 Subject: Make `column` and `row` take an `IntoIterator` --- widget/src/column.rs | 4 ++-- widget/src/helpers.rs | 8 ++++---- widget/src/row.rs | 6 ++---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/widget/src/column.rs b/widget/src/column.rs index 9867d97e..d6eea84b 100644 --- a/widget/src/column.rs +++ b/widget/src/column.rs @@ -41,9 +41,9 @@ where /// Creates a [`Column`] with the given elements. pub fn with_children( - children: impl Iterator>, + children: impl IntoIterator>, ) -> Self { - children.fold(Self::new(), |column, element| column.push(element)) + children.into_iter().fold(Self::new(), Self::push) } /// Sets the vertical spacing _between_ elements. diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 75528a0c..4b988ae3 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -34,7 +34,7 @@ macro_rules! column { $crate::Column::new() ); ($($x:expr),+ $(,)?) => ( - $crate::Column::with_children([$($crate::core::Element::from($x)),+].into_iter()) + $crate::Column::with_children([$($crate::core::Element::from($x)),+]) ); } @@ -47,7 +47,7 @@ macro_rules! row { $crate::Row::new() ); ($($x:expr),+ $(,)?) => ( - $crate::Row::with_children([$($crate::core::Element::from($x)),+].into_iter()) + $crate::Row::with_children([$($crate::core::Element::from($x)),+]) ); } @@ -66,7 +66,7 @@ where /// Creates a new [`Column`] with the given children. pub fn column<'a, Message, Renderer>( - children: impl Iterator>, + children: impl IntoIterator>, ) -> Column<'a, Message, Renderer> where Renderer: core::Renderer, @@ -89,7 +89,7 @@ where /// /// [`Row`]: crate::Row pub fn row<'a, Message, Renderer>( - children: impl Iterator>, + children: impl IntoIterator>, ) -> Row<'a, Message, Renderer> where Renderer: core::Renderer, diff --git a/widget/src/row.rs b/widget/src/row.rs index bcbe9267..90fd2926 100644 --- a/widget/src/row.rs +++ b/widget/src/row.rs @@ -39,11 +39,9 @@ where /// Creates a [`Row`] with the given elements. pub fn with_children( - children: impl Iterator>, + children: impl IntoIterator>, ) -> Self { - children - .into_iter() - .fold(Self::new(), |column, element| column.push(element)) + children.into_iter().fold(Self::new(), Self::push) } /// Sets the horizontal spacing _between_ elements. -- cgit From ecf571dfeb033f3768fccfb06bc9380e59281df3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jan 2024 06:47:52 +0100 Subject: Fix unnecessary `into` call in `Container::new` --- widget/src/container.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/widget/src/container.rs b/widget/src/container.rs index c98de41c..ecc5c651 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -67,7 +67,7 @@ where horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, style: Default::default(), - content: content.into(), + content, } } -- cgit From 025064c9e028ea65cc0c6ff236d42e9861efdda9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jan 2024 07:01:11 +0100 Subject: Fix broken doc links in `layout::Node` API --- core/src/layout/node.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/layout/node.rs b/core/src/layout/node.rs index 40c71436..5743a9bd 100644 --- a/core/src/layout/node.rs +++ b/core/src/layout/node.rs @@ -60,7 +60,7 @@ impl Node { self } - /// Mutable reference version of [`align`]. + /// Mutable reference version of [`Self::align`]. pub fn align_mut( &mut self, horizontal_alignment: Alignment, @@ -94,7 +94,7 @@ impl Node { self } - /// Mutable reference version of [`move_to`]. + /// Mutable reference version of [`Self::move_to`]. pub fn move_to_mut(&mut self, position: impl Into) { let position = position.into(); -- cgit From 88f8c343fa7d69203ab98bb7abc85fe002014422 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jan 2024 07:15:57 +0100 Subject: Fix `cross` calculation in `layout::flex` --- core/src/layout/flex.rs | 27 +++++++++++++-------------- examples/pick_list/src/main.rs | 10 +++------- widget/src/pick_list.rs | 1 - 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index cf3e1340..47cd7112 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -79,7 +79,17 @@ where let max_cross = axis.cross(limits.max()); let mut fill_main_sum = 0; - let mut cross = 0.0f32; + let mut cross = match axis { + Axis::Horizontal => match height { + Length::Shrink => 0.0, + _ => max_cross, + }, + Axis::Vertical => match width { + Length::Shrink => 0.0, + _ => max_cross, + }, + }; + let mut available = axis.main(limits.max()) - total_spacing; let mut nodes: Vec = Vec::with_capacity(items.len()); @@ -113,17 +123,6 @@ where } } - let intrinsic_cross = match axis { - Axis::Horizontal => match height { - Length::Shrink => cross, - _ => max_cross, - }, - Axis::Vertical => match width { - Length::Shrink => cross, - _ => max_cross, - }, - }; - for (i, (child, tree)) in items.iter().zip(trees.iter_mut()).enumerate() { let (fill_main_factor, fill_cross_factor) = { let size = child.as_widget().size(); @@ -132,7 +131,7 @@ where }; if fill_main_factor == 0 && fill_cross_factor != 0 { - let (max_width, max_height) = axis.pack(available, intrinsic_cross); + let (max_width, max_height) = axis.pack(available, cross); let child_limits = Limits::new(Size::ZERO, Size::new(max_width, max_height)); @@ -182,7 +181,7 @@ where let max_cross = if fill_cross_factor == 0 { max_cross } else { - intrinsic_cross + cross }; let (min_width, min_height) = diff --git a/examples/pick_list/src/main.rs b/examples/pick_list/src/main.rs index bfd642f5..e4d96dc8 100644 --- a/examples/pick_list/src/main.rs +++ b/examples/pick_list/src/main.rs @@ -1,4 +1,4 @@ -use iced::widget::{column, container, pick_list, scrollable, vertical_space}; +use iced::widget::{column, pick_list, scrollable, vertical_space}; use iced::{Alignment, Element, Length, Sandbox, Settings}; pub fn main() -> iced::Result { @@ -48,15 +48,11 @@ impl Sandbox for Example { pick_list, vertical_space(600), ] + .width(Length::Fill) .align_items(Alignment::Center) .spacing(10); - container(scrollable(content)) - .width(Length::Fill) - .height(Length::Fill) - .center_x() - .center_y() - .into() + scrollable(content).into() } } diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index 2576a1e8..9f6a371a 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -451,7 +451,6 @@ where limits .width(width) - .height(Length::Shrink) .shrink(padding) .resolve(width, Length::Shrink, intrinsic) .expand(padding) -- cgit From a79b2adf5c3e345667341451a4aaaa14fc9bfe80 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Jan 2024 02:16:29 +0100 Subject: Use first-class functions in `layout` example --- examples/layout/src/main.rs | 143 +++++++++++++++++++++++++------------------- 1 file changed, 83 insertions(+), 60 deletions(-) diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index eeaa76b6..d4d81617 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -1,4 +1,5 @@ use iced::executor; +use iced::keyboard; use iced::widget::{column, container, row, text, vertical_rule}; use iced::{ Application, Command, Element, Length, Settings, Subscription, Theme, @@ -10,9 +11,7 @@ pub fn main() -> iced::Result { #[derive(Debug)] struct Layout { - previous: Vec, - current: Example, - next: Vec, + example: Example, } #[derive(Debug, Clone, Copy)] @@ -30,36 +29,23 @@ impl Application for Layout { fn new(_flags: Self::Flags) -> (Self, Command) { ( Self { - previous: vec![], - current: Example::Centered, - next: vec![Example::NestedQuotes], + example: Example::default(), }, Command::none(), ) } fn title(&self) -> String { - String::from("Counter - Iced") + format!("{} - Layout - Iced", self.example.title) } fn update(&mut self, message: Self::Message) -> Command { match message { Message::Next => { - if !self.next.is_empty() { - let previous = std::mem::replace( - &mut self.current, - self.next.remove(0), - ); - - self.previous.push(previous); - } + self.example = self.example.next(); } Message::Previous => { - if let Some(previous) = self.previous.pop() { - let next = std::mem::replace(&mut self.current, previous); - - self.next.insert(0, next); - } + self.example = self.example.previous(); } } @@ -67,57 +53,94 @@ impl Application for Layout { } fn subscription(&self) -> Subscription { - use iced::event::{self, Event}; - use iced::keyboard; - - event::listen_with(|event, status| match event { - Event::Keyboard(keyboard::Event::KeyReleased { - key_code, .. - }) if status == event::Status::Ignored => match key_code { - keyboard::KeyCode::Left => Some(Message::Previous), - keyboard::KeyCode::Right => Some(Message::Next), - _ => None, - }, + keyboard::on_key_release(|key_code, _modifiers| match key_code { + keyboard::KeyCode::Left => Some(Message::Previous), + keyboard::KeyCode::Right => Some(Message::Next), _ => None, }) } fn view(&self) -> Element { - self.current.view() + self.example.view() } } -#[derive(Debug)] -enum Example { - Centered, - NestedQuotes, +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct Example { + title: &'static str, + view: fn() -> Element<'static, Message>, } impl Example { + const LIST: &'static [Self] = &[ + Self { + title: "Centered", + view: centered, + }, + Self { + title: "Nested Quotes", + view: nested_quotes, + }, + ]; + + fn previous(self) -> Self { + let Some(index) = + Self::LIST.iter().position(|&example| example == self) + else { + return self; + }; + + Self::LIST + .get(index.saturating_sub(1)) + .copied() + .unwrap_or(self) + } + + fn next(self) -> Self { + let Some(index) = + Self::LIST.iter().position(|&example| example == self) + else { + return self; + }; + + Self::LIST.get(index + 1).copied().unwrap_or(self) + } + fn view(&self) -> Element { - match self { - Self::Centered => container(text("I am centered!").size(50)) - .width(Length::Fill) - .height(Length::Fill) - .center_x() - .center_y() - .into(), - Self::NestedQuotes => container((1..5).fold( - column![text("Original text")].padding(10), - |quotes, i| { - column![ - row![vertical_rule(2), quotes].height(Length::Shrink), - text(format!("Reply {i}")) - ] - .spacing(10) - .padding(10) - }, - )) - .width(Length::Fill) - .height(Length::Fill) - .center_x() - .center_y() - .into(), - } + (self.view)() + } +} + +impl Default for Example { + fn default() -> Self { + Self::LIST[0] } } + +fn centered<'a>() -> Element<'a, Message> { + container(text("I am centered!").size(50)) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() +} + +fn nested_quotes<'a>() -> Element<'a, Message> { + container((1..5).fold( + column![text("Original text")].padding(10), + |quotes, i| { + column![ + row![vertical_rule(2), quotes].height(Length::Shrink), + text(format!("Reply {i}")) + ] + .spacing(10) + .padding(10) + }, + )) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() +} -- cgit From 81ecc4a67f7982c6300a4d5e8ec4e8aac8cbd881 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Jan 2024 02:58:40 +0100 Subject: Add basic controls to `layout` example --- examples/layout/src/main.rs | 67 +++++++++++++++++++++++++++++++++++---------- style/src/container.rs | 26 +++++++++++++++++- style/src/theme.rs | 6 ++++ 3 files changed, 84 insertions(+), 15 deletions(-) diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index d4d81617..6d02434d 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -1,8 +1,11 @@ use iced::executor; use iced::keyboard; -use iced::widget::{column, container, row, text, vertical_rule}; +use iced::widget::{ + button, column, container, horizontal_space, row, text, vertical_rule, +}; use iced::{ - Application, Command, Element, Length, Settings, Subscription, Theme, + color, Application, Color, Command, Element, Length, Settings, + Subscription, Theme, }; pub fn main() -> iced::Result { @@ -61,7 +64,29 @@ impl Application for Layout { } fn view(&self) -> Element { - self.example.view() + let example = container(self.example.view()).style( + container::Appearance::default().with_border(Color::BLACK, 2.0), + ); + + let controls = row([ + (!self.example.is_first()).then_some( + button("← Previous") + .padding([5, 10]) + .on_press(Message::Previous) + .into(), + ), + Some(horizontal_space(Length::Fill).into()), + (!self.example.is_last()).then_some( + button("Next →") + .padding([5, 10]) + .on_press(Message::Next) + .into(), + ), + ] + .into_iter() + .filter_map(std::convert::identity)); + + column![example, controls].spacing(10).padding(20).into() } } @@ -83,6 +108,14 @@ impl Example { }, ]; + fn is_first(self) -> bool { + Self::LIST.first() == Some(&self) + } + + fn is_last(self) -> bool { + Self::LIST.last() == Some(&self) + } + fn previous(self) -> Self { let Some(index) = Self::LIST.iter().position(|&example| example == self) @@ -127,20 +160,26 @@ fn centered<'a>() -> Element<'a, Message> { } fn nested_quotes<'a>() -> Element<'a, Message> { - container((1..5).fold( - column![text("Original text")].padding(10), - |quotes, i| { + let quotes = + (1..5).fold(column![text("Original text")].padding(10), |quotes, i| { column![ - row![vertical_rule(2), quotes].height(Length::Shrink), + container( + row![vertical_rule(2), quotes].height(Length::Shrink) + ) + .style( + container::Appearance::default() + .with_background(color!(0x000000, 0.05)) + ), text(format!("Reply {i}")) ] .spacing(10) .padding(10) - }, - )) - .width(Length::Fill) - .height(Length::Fill) - .center_x() - .center_y() - .into() + }); + + container(quotes) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() } diff --git a/style/src/container.rs b/style/src/container.rs index ec543ae4..490a9dab 100644 --- a/style/src/container.rs +++ b/style/src/container.rs @@ -1,5 +1,5 @@ //! Change the appearance of a container. -use iced_core::{Background, BorderRadius, Color}; +use crate::core::{Background, BorderRadius, Color, Pixels}; /// The appearance of a container. #[derive(Debug, Clone, Copy)] @@ -16,6 +16,30 @@ pub struct Appearance { pub border_color: Color, } +impl Appearance { + /// Derives a new [`Appearance`] with a border of the given [`Color`] and + /// `width`. + pub fn with_border( + self, + color: impl Into, + width: impl Into, + ) -> Self { + Self { + border_color: color.into(), + border_width: width.into().0, + ..self + } + } + + /// Derives a new [`Appearance`] with the given [`Background`]. + pub fn with_background(self, background: impl Into) -> Self { + Self { + background: Some(background.into()), + ..self + } + } +} + impl std::default::Default for Appearance { fn default() -> Self { Self { diff --git a/style/src/theme.rs b/style/src/theme.rs index 47010728..eafb0b47 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -383,6 +383,12 @@ pub enum Container { Custom(Box>), } +impl From for Container { + fn from(appearance: container::Appearance) -> Self { + Self::Custom(Box::new(move |_: &_| appearance)) + } +} + impl container::Appearance + 'static> From for Container { fn from(f: T) -> Self { Self::Custom(Box::new(f)) -- cgit From 5dbded61dea19f77eb370e08e72acfa20ffd1a86 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Jan 2024 03:07:10 +0100 Subject: Use `flatten` instead of `filter_map` in `layout` example --- examples/layout/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index 6d02434d..448d2995 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -84,7 +84,7 @@ impl Application for Layout { ), ] .into_iter() - .filter_map(std::convert::identity)); + .flatten()); column![example, controls].spacing(10).padding(20).into() } -- cgit From d76705df29f1960124bd06277683448e18f788b0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Jan 2024 03:56:39 +0100 Subject: Add `explain` toggle to `layout` example --- examples/layout/src/main.rs | 64 +++++++++++++++++++++++++++++++++++++-------- style/src/theme/palette.rs | 3 +++ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index 448d2995..e23b2218 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -1,11 +1,12 @@ use iced::executor; use iced::keyboard; use iced::widget::{ - button, column, container, horizontal_space, row, text, vertical_rule, + button, checkbox, column, container, horizontal_space, row, text, + vertical_rule, }; use iced::{ - color, Application, Color, Command, Element, Length, Settings, - Subscription, Theme, + color, Alignment, Application, Color, Command, Element, Font, Length, + Settings, Subscription, Theme, }; pub fn main() -> iced::Result { @@ -15,12 +16,14 @@ pub fn main() -> iced::Result { #[derive(Debug)] struct Layout { example: Example, + explain: bool, } #[derive(Debug, Clone, Copy)] enum Message { Next, Previous, + ExplainToggled(bool), } impl Application for Layout { @@ -33,6 +36,7 @@ impl Application for Layout { ( Self { example: Example::default(), + explain: false, }, Command::none(), ) @@ -50,6 +54,9 @@ impl Application for Layout { Message::Previous => { self.example = self.example.previous(); } + Message::ExplainToggled(explain) => { + self.explain = explain; + } } Command::none() @@ -64,9 +71,24 @@ impl Application for Layout { } fn view(&self) -> Element { - let example = container(self.example.view()).style( - container::Appearance::default().with_border(Color::BLACK, 2.0), - ); + let header = row![ + text(self.example.title).size(20).font(Font::MONOSPACE), + horizontal_space(Length::Fill), + checkbox("Explain", self.explain, Message::ExplainToggled), + ] + .align_items(Alignment::Center); + + let example = container(if self.explain { + self.example.view().explain(color!(0x0000ff)) + } else { + self.example.view() + }) + .style(|theme: &Theme| { + let palette = theme.extended_palette(); + + container::Appearance::default() + .with_border(palette.background.strong.color, 4.0) + }); let controls = row([ (!self.example.is_first()).then_some( @@ -86,7 +108,14 @@ impl Application for Layout { .into_iter() .flatten()); - column![example, controls].spacing(10).padding(20).into() + column![header, example, controls] + .spacing(10) + .padding(20) + .into() + } + + fn theme(&self) -> Theme { + Theme::Dark } } @@ -166,10 +195,23 @@ fn nested_quotes<'a>() -> Element<'a, Message> { container( row![vertical_rule(2), quotes].height(Length::Shrink) ) - .style( - container::Appearance::default() - .with_background(color!(0x000000, 0.05)) - ), + .style(|theme: &Theme| { + let palette = theme.extended_palette(); + + container::Appearance::default().with_background( + if palette.is_dark { + Color { + a: 0.01, + ..Color::WHITE + } + } else { + Color { + a: 0.08, + ..Color::BLACK + } + }, + ) + }), text(format!("Reply {i}")) ] .spacing(10) diff --git a/style/src/theme/palette.rs b/style/src/theme/palette.rs index aaeb799d..76977a29 100644 --- a/style/src/theme/palette.rs +++ b/style/src/theme/palette.rs @@ -82,6 +82,8 @@ pub struct Extended { pub success: Success, /// The set of danger colors. pub danger: Danger, + /// Whether the palette is dark or not. + pub is_dark: bool, } /// The built-in light variant of an [`Extended`] palette. @@ -113,6 +115,7 @@ impl Extended { palette.background, palette.text, ), + is_dark: is_dark(palette.background), } } } -- cgit From 3850a46db6e13f2948f5731f4ceec42764391f5d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Jan 2024 08:15:05 +0100 Subject: Add `Theme` selector to `layout` example --- examples/layout/src/main.rs | 20 ++++++++++++++++---- examples/styling/src/main.rs | 17 ++++++++++------- style/src/theme.rs | 36 ++++++++++++++++++++++++++++++------ widget/src/helpers.rs | 2 +- widget/src/pick_list.rs | 6 +++--- 5 files changed, 60 insertions(+), 21 deletions(-) diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index e23b2218..c1ff3951 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -1,8 +1,8 @@ use iced::executor; use iced::keyboard; use iced::widget::{ - button, checkbox, column, container, horizontal_space, row, text, - vertical_rule, + button, checkbox, column, container, horizontal_space, pick_list, row, + text, vertical_rule, }; use iced::{ color, Alignment, Application, Color, Command, Element, Font, Length, @@ -17,13 +17,15 @@ pub fn main() -> iced::Result { struct Layout { example: Example, explain: bool, + theme: Theme, } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] enum Message { Next, Previous, ExplainToggled(bool), + ThemeSelected(Theme), } impl Application for Layout { @@ -37,6 +39,7 @@ impl Application for Layout { Self { example: Example::default(), explain: false, + theme: Theme::Light, }, Command::none(), ) @@ -57,6 +60,9 @@ impl Application for Layout { Message::ExplainToggled(explain) => { self.explain = explain; } + Message::ThemeSelected(theme) => { + self.theme = theme; + } } Command::none() @@ -75,7 +81,13 @@ impl Application for Layout { text(self.example.title).size(20).font(Font::MONOSPACE), horizontal_space(Length::Fill), checkbox("Explain", self.explain, Message::ExplainToggled), + pick_list( + Theme::ALL, + Some(self.theme.clone()), + Message::ThemeSelected + ), ] + .spacing(20) .align_items(Alignment::Center); let example = container(if self.explain { @@ -115,7 +127,7 @@ impl Application for Layout { } fn theme(&self) -> Theme { - Theme::Dark + self.theme.clone() } } diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index f14f6a8f..10f3c79d 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -53,13 +53,16 @@ impl Sandbox for Styling { self.theme = match theme { ThemeType::Light => Theme::Light, ThemeType::Dark => Theme::Dark, - ThemeType::Custom => Theme::custom(theme::Palette { - background: Color::from_rgb(1.0, 0.9, 1.0), - text: Color::BLACK, - primary: Color::from_rgb(0.5, 0.5, 0.0), - success: Color::from_rgb(0.0, 1.0, 0.0), - danger: Color::from_rgb(1.0, 0.0, 0.0), - }), + ThemeType::Custom => Theme::custom( + String::from("Custom"), + theme::Palette { + background: Color::from_rgb(1.0, 0.9, 1.0), + text: Color::BLACK, + primary: Color::from_rgb(0.5, 0.5, 0.0), + success: Color::from_rgb(0.0, 1.0, 0.0), + danger: Color::from_rgb(1.0, 0.0, 0.0), + }, + ), } } Message::InputChanged(value) => self.input_value = value, diff --git a/style/src/theme.rs b/style/src/theme.rs index eafb0b47..deccf455 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -23,6 +23,7 @@ use crate::toggler; use iced_core::{Background, Color, Vector}; +use std::fmt; use std::rc::Rc; /// A built-in theme. @@ -38,18 +39,22 @@ pub enum Theme { } impl Theme { + /// A list with all the defined themes. + pub const ALL: &'static [Self] = &[Self::Light, Self::Dark]; + /// Creates a new custom [`Theme`] from the given [`Palette`]. - pub fn custom(palette: Palette) -> Self { - Self::custom_with_fn(palette, palette::Extended::generate) + pub fn custom(name: String, palette: Palette) -> Self { + Self::custom_with_fn(name, palette, palette::Extended::generate) } /// Creates a new custom [`Theme`] from the given [`Palette`], with /// a custom generator of a [`palette::Extended`]. pub fn custom_with_fn( + name: String, palette: Palette, generate: impl FnOnce(Palette) -> palette::Extended, ) -> Self { - Self::Custom(Box::new(Custom::with_fn(palette, generate))) + Self::Custom(Box::new(Custom::with_fn(name, palette, generate))) } /// Returns the [`Palette`] of the [`Theme`]. @@ -71,32 +76,51 @@ impl Theme { } } +impl fmt::Display for Theme { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Light => write!(f, "Light"), + Self::Dark => write!(f, "Dark"), + Self::Custom(custom) => custom.fmt(f), + } + } +} + /// A [`Theme`] with a customized [`Palette`]. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct Custom { + name: String, palette: Palette, extended: palette::Extended, } impl Custom { /// Creates a [`Custom`] theme from the given [`Palette`]. - pub fn new(palette: Palette) -> Self { - Self::with_fn(palette, palette::Extended::generate) + pub fn new(name: String, palette: Palette) -> Self { + Self::with_fn(name, palette, palette::Extended::generate) } /// Creates a [`Custom`] theme from the given [`Palette`] with /// a custom generator of a [`palette::Extended`]. pub fn with_fn( + name: String, palette: Palette, generate: impl FnOnce(Palette) -> palette::Extended, ) -> Self { Self { + name, palette, extended: generate(palette), } } } +impl fmt::Display for Custom { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.name) + } +} + /// The style of an application. #[derive(Default)] pub enum Application { diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 4b988ae3..498dd76c 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -271,7 +271,7 @@ pub fn pick_list<'a, Message, Renderer, T>( on_selected: impl Fn(T) -> Message + 'a, ) -> PickList<'a, T, Message, Renderer> where - T: ToString + Eq + 'static, + T: ToString + PartialEq + 'static, [T]: ToOwned>, Renderer: core::text::Renderer, Renderer::Theme: pick_list::StyleSheet diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index 9f6a371a..2e3aab6f 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -45,7 +45,7 @@ where impl<'a, T: 'a, Message, Renderer> PickList<'a, T, Message, Renderer> where - T: ToString + Eq, + T: ToString + PartialEq, [T]: ToOwned>, Renderer: text::Renderer, Renderer::Theme: StyleSheet @@ -145,7 +145,7 @@ where impl<'a, T: 'a, Message, Renderer> Widget for PickList<'a, T, Message, Renderer> where - T: Clone + ToString + Eq + 'static, + T: Clone + ToString + PartialEq + 'static, [T]: ToOwned>, Message: 'a, Renderer: text::Renderer + 'a, @@ -281,7 +281,7 @@ where impl<'a, T: 'a, Message, Renderer> From> for Element<'a, Message, Renderer> where - T: Clone + ToString + Eq + 'static, + T: Clone + ToString + PartialEq + 'static, [T]: ToOwned>, Message: 'a, Renderer: text::Renderer + 'a, -- cgit From a6cbc365037d740ee9bb8d21fffe361cd198477e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Jan 2024 09:01:01 +0100 Subject: Showcase more layouts in `layout` example --- examples/layout/Cargo.toml | 2 +- examples/layout/src/main.rs | 144 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 132 insertions(+), 14 deletions(-) diff --git a/examples/layout/Cargo.toml b/examples/layout/Cargo.toml index c2c3f49b..855f98d0 100644 --- a/examples/layout/Cargo.toml +++ b/examples/layout/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../.." } +iced = { path = "../..", features = ["canvas"] } diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index c1ff3951..3e69e1a8 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -1,12 +1,14 @@ use iced::executor; use iced::keyboard; +use iced::mouse; +use iced::theme; use iced::widget::{ - button, checkbox, column, container, horizontal_space, pick_list, row, - text, vertical_rule, + button, canvas, checkbox, column, container, horizontal_space, pick_list, + row, scrollable, text, vertical_rule, vertical_space, }; use iced::{ color, Alignment, Application, Color, Command, Element, Font, Length, - Settings, Subscription, Theme, + Point, Rectangle, Renderer, Settings, Subscription, Theme, }; pub fn main() -> iced::Result { @@ -100,7 +102,12 @@ impl Application for Layout { container::Appearance::default() .with_border(palette.background.strong.color, 4.0) - }); + }) + .padding(4) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y(); let controls = row([ (!self.example.is_first()).then_some( @@ -143,6 +150,22 @@ impl Example { title: "Centered", view: centered, }, + Self { + title: "Column", + view: column_, + }, + Self { + title: "Row", + view: row_, + }, + Self { + title: "Space", + view: space, + }, + Self { + title: "Application", + view: application, + }, Self { title: "Nested Quotes", view: nested_quotes, @@ -200,9 +223,79 @@ fn centered<'a>() -> Element<'a, Message> { .into() } +fn column_<'a>() -> Element<'a, Message> { + column![ + "A column can be used to", + "lay out widgets vertically.", + square(50), + square(50), + square(50), + "The amount of space between", + "elements can be configured!", + ] + .spacing(40) + .into() +} + +fn row_<'a>() -> Element<'a, Message> { + row![ + "A row works like a column...", + square(50), + square(50), + square(50), + "but lays out widgets horizontally!", + ] + .spacing(40) + .into() +} + +fn space<'a>() -> Element<'a, Message> { + row!["Left!", horizontal_space(Length::Fill), "Right!"].into() +} + +fn application<'a>() -> Element<'a, Message> { + let header = container( + row![ + square(40), + horizontal_space(Length::Fill), + "Header!", + horizontal_space(Length::Fill), + square(40), + ] + .padding(10) + .align_items(Alignment::Center), + ) + .style(|theme: &Theme| { + let palette = theme.extended_palette(); + + container::Appearance::default() + .with_border(palette.background.strong.color, 1) + }); + + let sidebar = container( + column!["Sidebar!", square(50), square(50)] + .spacing(40) + .padding(10) + .width(200) + .align_items(Alignment::Center), + ) + .style(theme::Container::Box) + .height(Length::Fill) + .center_y(); + + let content = container( + scrollable(column!["Content!", vertical_space(2000), "The end"]) + .width(Length::Fill) + .height(Length::Fill), + ) + .padding(10); + + column![header, row![sidebar, content]].into() +} + fn nested_quotes<'a>() -> Element<'a, Message> { - let quotes = - (1..5).fold(column![text("Original text")].padding(10), |quotes, i| { + (1..5) + .fold(column![text("Original text")].padding(10), |quotes, i| { column![ container( row![vertical_rule(2), quotes].height(Length::Shrink) @@ -228,12 +321,37 @@ fn nested_quotes<'a>() -> Element<'a, Message> { ] .spacing(10) .padding(10) - }); - - container(quotes) - .width(Length::Fill) - .height(Length::Fill) - .center_x() - .center_y() + }) .into() } + +fn square<'a>(size: impl Into + Copy) -> Element<'a, Message> { + struct Square; + + impl canvas::Program for Square { + type State = (); + + fn draw( + &self, + _state: &Self::State, + renderer: &Renderer, + theme: &Theme, + bounds: Rectangle, + _cursor: mouse::Cursor, + ) -> Vec { + let mut frame = canvas::Frame::new(renderer, bounds.size()); + + let palette = theme.extended_palette(); + + frame.fill_rectangle( + Point::ORIGIN, + bounds.size(), + palette.background.strong.color, + ); + + vec![frame.into_geometry()] + } + } + + canvas(Square).width(size).height(size).into() +} -- cgit From 226271148e77a4f8966ce84b0c948c268176d92b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Jan 2024 10:08:11 +0100 Subject: Use multiple squares instead of `vertical_space` in `layout` example --- examples/layout/src/main.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index 3e69e1a8..60dabe54 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -4,7 +4,7 @@ use iced::mouse; use iced::theme; use iced::widget::{ button, canvas, checkbox, column, container, horizontal_space, pick_list, - row, scrollable, text, vertical_rule, vertical_space, + row, scrollable, text, vertical_rule, }; use iced::{ color, Alignment, Application, Color, Command, Element, Font, Length, @@ -284,9 +284,19 @@ fn application<'a>() -> Element<'a, Message> { .center_y(); let content = container( - scrollable(column!["Content!", vertical_space(2000), "The end"]) - .width(Length::Fill) - .height(Length::Fill), + scrollable( + column![ + "Content!", + square(400), + square(200), + square(400), + "The end" + ] + .spacing(40) + .align_items(Alignment::Center) + .width(Length::Fill), + ) + .height(Length::Fill), ) .padding(10); -- cgit From fa53d9adbb0efbbe806a749476f83c04f756be75 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 11 Jan 2024 06:11:36 +0100 Subject: Loosen cross axis constraint for main axis fills in `flex` layout --- core/src/layout/flex.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 47cd7112..3358ef3d 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -166,13 +166,10 @@ where }; if fill_main_factor != 0 { - let max_main = if fill_main_factor == 0 { - available.max(0.0) - } else { - remaining * fill_main_factor as f32 / fill_main_sum as f32 - }; + let max_main = + remaining * fill_main_factor as f32 / fill_main_sum as f32; - let min_main = if fill_main_factor == 0 || max_main.is_infinite() { + let min_main = if max_main.is_infinite() { 0.0 } else { max_main @@ -184,9 +181,7 @@ where cross }; - let (min_width, min_height) = - axis.pack(min_main, axis.cross(limits.min())); - + let (min_width, min_height) = axis.pack(min_main, 0.0); let (max_width, max_height) = axis.pack(max_main, max_cross); let child_limits = Limits::new( -- cgit From 03c901d49b7cce901cfd76100f08dcff31420af8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 11 Jan 2024 06:12:19 +0100 Subject: Make `Button` sizing strategy adaptive --- core/src/length.rs | 12 ++++++++++++ widget/src/button.rs | 9 ++++++--- widget/src/container.rs | 12 ++---------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/core/src/length.rs b/core/src/length.rs index 6dc15049..4c139895 100644 --- a/core/src/length.rs +++ b/core/src/length.rs @@ -42,6 +42,18 @@ impl Length { pub fn is_fill(&self) -> bool { self.fill_factor() != 0 } + + /// Returns the "fluid" variant of the [`Length`]. + /// + /// Specifically: + /// - [`Length::Shrink`] if [`Length::Shrink`] or [`Length::Fixed`]. + /// - [`Length::Fill`] otherwise. + pub fn fluid(&self) -> Length { + match self { + Length::Fill | Length::FillPortion(_) => Length::Fill, + Length::Shrink | Length::Fixed(_) => Length::Shrink, + } + } } impl From for Length { diff --git a/widget/src/button.rs b/widget/src/button.rs index 86abee77..0ebb8dcc 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -71,11 +71,14 @@ where { /// Creates a new [`Button`] with the given content. pub fn new(content: impl Into>) -> Self { + let content = content.into(); + let size = content.as_widget().size_hint(); + Button { - content: content.into(), + content, on_press: None, - width: Length::Shrink, - height: Length::Shrink, + width: size.width.fluid(), + height: size.height.fluid(), padding: Padding::new(5.0), style: ::Style::default(), } diff --git a/widget/src/container.rs b/widget/src/container.rs index ecc5c651..cffb0458 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -52,16 +52,8 @@ where Container { id: None, padding: Padding::ZERO, - width: if size.width.is_fill() { - Length::Fill - } else { - Length::Shrink - }, - height: if size.height.is_fill() { - Length::Fill - } else { - Length::Shrink - }, + width: size.width.fluid(), + height: size.height.fluid(), max_width: f32::INFINITY, max_height: f32::INFINITY, horizontal_alignment: alignment::Horizontal::Left, -- cgit From 11474bdc3e1a43e6c167d7b98f22d87933dbd2b6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 11 Jan 2024 06:12:37 +0100 Subject: Fix `websocket` example --- examples/websocket/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index 59488e69..38a6db1e 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -125,7 +125,7 @@ impl Application for WebSocket { let mut button = button( text("Send") - .height(Length::Fill) + .height(40) .vertical_alignment(alignment::Vertical::Center), ) .padding([0, 20]); -- cgit