From 396735b682433928f52ba777891e14f2fbc703c7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 7 Jun 2022 04:51:44 +0200 Subject: Implement theme styling for `PickList` and `Menu` --- examples/game_of_life/src/main.rs | 4 +- examples/game_of_life/src/style.rs | 56 ---------------------------- examples/pure/game_of_life/src/main.rs | 4 +- examples/pure/game_of_life/src/style.rs | 56 ---------------------------- graphics/src/overlay/menu.rs | 2 +- native/src/overlay/menu.rs | 63 +++++++++++++++++++++----------- native/src/widget/pick_list.rs | 51 ++++++++++++++++---------- pure/src/helpers.rs | 1 + pure/src/widget/pick_list.rs | 32 ++++++++-------- src/pure/widget.rs | 3 +- src/widget.rs | 3 +- style/src/menu.rs | 17 +++------ style/src/pick_list.rs | 65 ++++++--------------------------- style/src/theme.rs | 58 ++++++++++++++++++++++++++++- 14 files changed, 168 insertions(+), 247 deletions(-) delete mode 100644 examples/game_of_life/src/style.rs delete mode 100644 examples/pure/game_of_life/src/style.rs diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index fa97583f..b2ae6953 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -1,7 +1,6 @@ //! This example showcases an interactive version of the Game of Life, invented //! by John Conway. It leverages a `Canvas` together with other widgets. mod preset; -mod style; use grid::Grid; use iced::button::{self, Button}; @@ -877,8 +876,7 @@ impl Controls { Message::PresetPicked, ) .padding(8) - .text_size(16) - .style(style::PickList), + .text_size(16), ) .push( Button::new(&mut self.clear_button, Text::new("Clear")) diff --git a/examples/game_of_life/src/style.rs b/examples/game_of_life/src/style.rs deleted file mode 100644 index 69889889..00000000 --- a/examples/game_of_life/src/style.rs +++ /dev/null @@ -1,56 +0,0 @@ -use iced::{pick_list, Color}; - -const BACKGROUND: Color = Color::from_rgb( - 0x2F as f32 / 255.0, - 0x31 as f32 / 255.0, - 0x36 as f32 / 255.0, -); - -pub struct PickList; - -impl pick_list::StyleSheet for PickList { - fn menu(&self) -> pick_list::Menu { - pick_list::Menu { - text_color: Color::WHITE, - background: BACKGROUND.into(), - border_width: 1.0, - border_color: Color { - a: 0.7, - ..Color::BLACK - }, - selected_background: Color { - a: 0.5, - ..Color::BLACK - } - .into(), - selected_text_color: Color::WHITE, - } - } - - fn active(&self) -> pick_list::Style { - pick_list::Style { - text_color: Color::WHITE, - background: BACKGROUND.into(), - border_width: 1.0, - border_color: Color { - a: 0.6, - ..Color::BLACK - }, - border_radius: 2.0, - icon_size: 0.5, - ..pick_list::Style::default() - } - } - - fn hovered(&self) -> pick_list::Style { - let active = self.active(); - - pick_list::Style { - border_color: Color { - a: 0.9, - ..Color::BLACK - }, - ..active - } - } -} diff --git a/examples/pure/game_of_life/src/main.rs b/examples/pure/game_of_life/src/main.rs index 4db9fbc7..58528b96 100644 --- a/examples/pure/game_of_life/src/main.rs +++ b/examples/pure/game_of_life/src/main.rs @@ -1,7 +1,6 @@ //! This example showcases an interactive version of the Game of Life, invented //! by John Conway. It leverages a `Canvas` together with other widgets. mod preset; -mod style; use grid::Grid; use iced::executor; @@ -195,8 +194,7 @@ fn view_controls<'a>( .push( pick_list(preset::ALL, Some(preset), Message::PresetPicked) .padding(8) - .text_size(16) - .style(style::PickList), + .text_size(16), ) .push( button("Clear") diff --git a/examples/pure/game_of_life/src/style.rs b/examples/pure/game_of_life/src/style.rs deleted file mode 100644 index d1ca5c9b..00000000 --- a/examples/pure/game_of_life/src/style.rs +++ /dev/null @@ -1,56 +0,0 @@ -use iced::{pick_list, Color}; - -pub const BACKGROUND: Color = Color::from_rgb( - 0x2F as f32 / 255.0, - 0x31 as f32 / 255.0, - 0x36 as f32 / 255.0, -); - -pub struct PickList; - -impl pick_list::StyleSheet for PickList { - fn menu(&self) -> pick_list::Menu { - pick_list::Menu { - text_color: Color::WHITE, - background: BACKGROUND.into(), - border_width: 1.0, - border_color: Color { - a: 0.7, - ..Color::BLACK - }, - selected_background: Color { - a: 0.5, - ..Color::BLACK - } - .into(), - selected_text_color: Color::WHITE, - } - } - - fn active(&self) -> pick_list::Style { - pick_list::Style { - text_color: Color::WHITE, - background: BACKGROUND.into(), - border_width: 1.0, - border_color: Color { - a: 0.6, - ..Color::BLACK - }, - border_radius: 2.0, - icon_size: 0.5, - ..pick_list::Style::default() - } - } - - fn hovered(&self) -> pick_list::Style { - let active = self.active(); - - pick_list::Style { - border_color: Color { - a: 0.9, - ..Color::BLACK - }, - ..active - } - } -} diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs index c5ff093d..8b489e5e 100644 --- a/graphics/src/overlay/menu.rs +++ b/graphics/src/overlay/menu.rs @@ -1,3 +1,3 @@ //! Build and show dropdown menus. -pub use iced_style::menu::Style; +pub use iced_style::menu::{Appearance, StyleSheet}; diff --git a/native/src/overlay/menu.rs b/native/src/overlay/menu.rs index fdb68247..979a13c3 100644 --- a/native/src/overlay/menu.rs +++ b/native/src/overlay/menu.rs @@ -14,11 +14,15 @@ use crate::{ Shell, Size, Vector, Widget, }; -pub use iced_style::menu::Style; +pub use iced_style::menu::{Appearance, StyleSheet}; /// A list of selectable options. #[allow(missing_debug_implementations)] -pub struct Menu<'a, T, Renderer: text::Renderer> { +pub struct Menu<'a, T, Renderer> +where + Renderer: text::Renderer, + Renderer::Theme: StyleSheet, +{ state: &'a mut State, options: &'a [T], hovered_option: &'a mut Option, @@ -27,14 +31,15 @@ pub struct Menu<'a, T, Renderer: text::Renderer> { padding: Padding, text_size: Option, font: Renderer::Font, - style: Style, + style: ::Style, } impl<'a, T, Renderer> Menu<'a, T, Renderer> where T: ToString + Clone, Renderer: text::Renderer + 'a, - Renderer::Theme: container::StyleSheet + scrollable::StyleSheet, + Renderer::Theme: + StyleSheet + container::StyleSheet + scrollable::StyleSheet, { /// Creates a new [`Menu`] with the given [`State`], a list of options, and /// the message to produced when an option is selected. @@ -82,7 +87,10 @@ where } /// Sets the style of the [`Menu`]. - pub fn style(mut self, style: impl Into