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 --- 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 -- 15 files changed, 47 insertions(+), 81 deletions(-) (limited to 'examples') 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) -- 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 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 examples/layout/Cargo.toml create mode 100644 examples/layout/src/main.rs (limited to 'examples') 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(), + } + } +} -- 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 --- 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 +++----- 8 files changed, 44 insertions(+), 72 deletions(-) (limited to 'examples') 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), ) -- 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` --- 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 ++------ 7 files changed, 29 insertions(+), 42 deletions(-) (limited to 'examples') 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( -- 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(-) (limited to 'examples') 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 --- examples/geometry/src/main.rs | 4 ++-- examples/loading_spinners/src/circular.rs | 5 +---- examples/loading_spinners/src/linear.rs | 5 +---- 3 files changed, 4 insertions(+), 10 deletions(-) (limited to 'examples') 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( -- 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 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'examples') 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() -- 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` --- examples/pick_list/src/main.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'examples') 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() } } -- 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(-) (limited to 'examples') 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 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 14 deletions(-) (limited to 'examples') 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() } -- 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(-) (limited to 'examples') 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 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 11 deletions(-) (limited to 'examples') 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) -- 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 ++++++++++------- 2 files changed, 26 insertions(+), 11 deletions(-) (limited to 'examples') 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, -- 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(-) (limited to 'examples') 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(-) (limited to 'examples') 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 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(-) (limited to 'examples') 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 From 73e7cf16e315cd179bf416e9051a562f7a8b648a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 15 Jan 2024 23:51:46 +0100 Subject: Update `rfd` to `0.13` --- examples/editor/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/editor/Cargo.toml b/examples/editor/Cargo.toml index a3f6ea3b..dc885728 100644 --- a/examples/editor/Cargo.toml +++ b/examples/editor/Cargo.toml @@ -12,4 +12,4 @@ iced.features = ["highlighter", "tokio", "debug"] tokio.workspace = true tokio.features = ["fs"] -rfd = "0.12" +rfd = "0.13" -- cgit