From 6551a0b2ab6c831dd1d3646ecf55180339275e22 Mon Sep 17 00:00:00 2001 From: Bingus Date: Thu, 11 May 2023 09:12:06 -0700 Subject: Added support for gradients as background variants + other optimizations. --- examples/tour/Cargo.toml | 2 +- examples/tour/src/main.rs | 58 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 9 deletions(-) (limited to 'examples/tour') diff --git a/examples/tour/Cargo.toml b/examples/tour/Cargo.toml index 39e83671..48471f2d 100644 --- a/examples/tour/Cargo.toml +++ b/examples/tour/Cargo.toml @@ -7,4 +7,4 @@ publish = false [dependencies] iced = { path = "../..", features = ["image", "debug"] } -env_logger = "0.8" +env_logger = "0.10.0" diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 9c38ad0e..630b6359 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -1,11 +1,15 @@ -use iced::alignment; use iced::theme; +use iced::theme::Palette; use iced::widget::{ checkbox, column, container, horizontal_space, image, radio, row, scrollable, slider, text, text_input, toggler, vertical_space, }; use iced::widget::{Button, Column, Container, Slider}; -use iced::{Color, Element, Font, Length, Renderer, Sandbox, Settings}; +use iced::{alignment, widget, Theme}; +use iced::{ + Color, Degrees, Element, Font, Gradient, Length, Radians, Renderer, + Sandbox, Settings, +}; pub fn main() -> iced::Result { env_logger::init(); @@ -53,9 +57,11 @@ impl Sandbox for Tour { if steps.has_previous() { controls = controls.push( - button("Back") - .on_press(Message::BackPressed) - .style(theme::Button::Secondary), + button("Back").on_press(Message::BackPressed).style( + theme::Button::Custom(Box::new( + CustomButtonStyle::Secondary, + )), + ), ); } @@ -63,9 +69,9 @@ impl Sandbox for Tour { if steps.can_continue() { controls = controls.push( - button("Next") - .on_press(Message::NextPressed) - .style(theme::Button::Primary), + button("Next").on_press(Message::NextPressed).style( + theme::Button::Custom(Box::new(CustomButtonStyle::Primary)), + ), ); } @@ -716,3 +722,39 @@ pub enum Layout { Row, Column, } + +enum CustomButtonStyle { + Primary, + Secondary, +} + +impl widget::button::StyleSheet for CustomButtonStyle { + type Style = Theme; + + fn active(&self, _style: &Self::Style) -> widget::button::Appearance { + match self { + CustomButtonStyle::Primary => widget::button::Appearance { + background: Gradient::linear(Degrees(270.0)) + .add_stop(0.0, Palette::LIGHT.primary) + .add_stop(1.0, Color::from_rgb8(54, 80, 168)) + .build() + .into(), + text_color: Color::WHITE, + border_radius: 5.0, + ..Default::default() + }, + CustomButtonStyle::Secondary => widget::button::Appearance { + background: Gradient::linear(Radians( + 3.0 * std::f32::consts::PI / 2.0, + )) + .add_stop(0.0, Color::from_rgb8(194, 194, 194)) + .add_stop(1.0, Color::from_rgb8(126, 126, 126)) + .build() + .into(), + text_color: Color::WHITE, + border_radius: 5.0, + ..Default::default() + }, + } + } +} -- cgit From 4c1a082f0468a59099bbf8aa8991420a41234948 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 19 May 2023 03:32:21 +0200 Subject: Remove `Builder` abstractions for gradients --- examples/tour/src/main.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'examples/tour') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 630b6359..a40f0f33 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -1,3 +1,4 @@ +use iced::gradient; use iced::theme; use iced::theme::Palette; use iced::widget::{ @@ -7,8 +8,7 @@ use iced::widget::{ use iced::widget::{Button, Column, Container, Slider}; use iced::{alignment, widget, Theme}; use iced::{ - Color, Degrees, Element, Font, Gradient, Length, Radians, Renderer, - Sandbox, Settings, + Color, Degrees, Element, Font, Length, Radians, Renderer, Sandbox, Settings, }; pub fn main() -> iced::Result { @@ -734,23 +734,25 @@ impl widget::button::StyleSheet for CustomButtonStyle { fn active(&self, _style: &Self::Style) -> widget::button::Appearance { match self { CustomButtonStyle::Primary => widget::button::Appearance { - background: Gradient::linear(Degrees(270.0)) - .add_stop(0.0, Palette::LIGHT.primary) - .add_stop(1.0, Color::from_rgb8(54, 80, 168)) - .build() - .into(), + background: Some( + gradient::Linear::new(Degrees(270.0)) + .add_stop(0.0, Palette::LIGHT.primary) + .add_stop(1.0, Color::from_rgb8(54, 80, 168)) + .into(), + ), text_color: Color::WHITE, border_radius: 5.0, ..Default::default() }, CustomButtonStyle::Secondary => widget::button::Appearance { - background: Gradient::linear(Radians( - 3.0 * std::f32::consts::PI / 2.0, - )) - .add_stop(0.0, Color::from_rgb8(194, 194, 194)) - .add_stop(1.0, Color::from_rgb8(126, 126, 126)) - .build() - .into(), + background: Some( + gradient::Linear::new(Radians( + 3.0 * std::f32::consts::PI / 2.0, + )) + .add_stop(0.0, Color::from_rgb8(194, 194, 194)) + .add_stop(1.0, Color::from_rgb8(126, 126, 126)) + .into(), + ), text_color: Color::WHITE, border_radius: 5.0, ..Default::default() -- cgit From 96aa0379d58ff799493097e3bd0572f9a87da453 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 19 May 2023 03:43:11 +0200 Subject: Implement `custom` helper for `theme::Button` --- examples/tour/src/main.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'examples/tour') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index a40f0f33..257a4def 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -57,11 +57,9 @@ impl Sandbox for Tour { if steps.has_previous() { controls = controls.push( - button("Back").on_press(Message::BackPressed).style( - theme::Button::Custom(Box::new( - CustomButtonStyle::Secondary, - )), - ), + button("Back") + .on_press(Message::BackPressed) + .style(theme::Button::custom(CustomButtonStyle::Secondary)), ); } @@ -69,9 +67,9 @@ impl Sandbox for Tour { if steps.can_continue() { controls = controls.push( - button("Next").on_press(Message::NextPressed).style( - theme::Button::Custom(Box::new(CustomButtonStyle::Primary)), - ), + button("Next") + .on_press(Message::NextPressed) + .style(theme::Button::custom(CustomButtonStyle::Primary)), ); } -- cgit From 10a8c599e62e46dad4da0e53e184861047ad34c0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 19 May 2023 03:47:00 +0200 Subject: Keep `tour` buttons solid The gradients feel a bit out of place currently. --- examples/tour/src/main.rs | 55 +++++------------------------------------------ 1 file changed, 5 insertions(+), 50 deletions(-) (limited to 'examples/tour') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 257a4def..13bcd5ff 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -1,15 +1,11 @@ -use iced::gradient; +use iced::alignment; use iced::theme; -use iced::theme::Palette; use iced::widget::{ checkbox, column, container, horizontal_space, image, radio, row, scrollable, slider, text, text_input, toggler, vertical_space, }; use iced::widget::{Button, Column, Container, Slider}; -use iced::{alignment, widget, Theme}; -use iced::{ - Color, Degrees, Element, Font, Length, Radians, Renderer, Sandbox, Settings, -}; +use iced::{Color, Element, Font, Length, Renderer, Sandbox, Settings}; pub fn main() -> iced::Result { env_logger::init(); @@ -59,18 +55,15 @@ impl Sandbox for Tour { controls = controls.push( button("Back") .on_press(Message::BackPressed) - .style(theme::Button::custom(CustomButtonStyle::Secondary)), + .style(theme::Button::Secondary), ); } controls = controls.push(horizontal_space(Length::Fill)); if steps.can_continue() { - controls = controls.push( - button("Next") - .on_press(Message::NextPressed) - .style(theme::Button::custom(CustomButtonStyle::Primary)), - ); + controls = + controls.push(button("Next").on_press(Message::NextPressed)); } let content: Element<_> = column![ @@ -720,41 +713,3 @@ pub enum Layout { Row, Column, } - -enum CustomButtonStyle { - Primary, - Secondary, -} - -impl widget::button::StyleSheet for CustomButtonStyle { - type Style = Theme; - - fn active(&self, _style: &Self::Style) -> widget::button::Appearance { - match self { - CustomButtonStyle::Primary => widget::button::Appearance { - background: Some( - gradient::Linear::new(Degrees(270.0)) - .add_stop(0.0, Palette::LIGHT.primary) - .add_stop(1.0, Color::from_rgb8(54, 80, 168)) - .into(), - ), - text_color: Color::WHITE, - border_radius: 5.0, - ..Default::default() - }, - CustomButtonStyle::Secondary => widget::button::Appearance { - background: Some( - gradient::Linear::new(Radians( - 3.0 * std::f32::consts::PI / 2.0, - )) - .add_stop(0.0, Color::from_rgb8(194, 194, 194)) - .add_stop(1.0, Color::from_rgb8(126, 126, 126)) - .into(), - ), - text_color: Color::WHITE, - border_radius: 5.0, - ..Default::default() - }, - } - } -} -- cgit