From 74373cb086da6097eae7d2e8bd6348aaf7c43857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 25 Mar 2024 22:12:47 +0100 Subject: [PATCH] Make defaults of composite widgets configurable --- widget/src/combo_box.rs | 17 ++++++++++++++--- widget/src/overlay/menu.rs | 26 +++++++++++++++----------- widget/src/pick_list.rs | 7 ++++++- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index df5358d6f9..e4f4a41fb1 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -64,7 +64,8 @@ where on_selected: impl Fn(T) -> Message + 'static, ) -> Self { let text_input = TextInput::new(placeholder, &state.value()) - .on_input(TextInputEvent::TextChanged); + .on_input(TextInputEvent::TextChanged) + .class(Theme::default_input()); let selection = selection.map(T::to_string).unwrap_or_default(); @@ -77,7 +78,7 @@ where on_option_hovered: None, on_input: None, on_close: None, - menu_class: ::default(), + menu_class: ::default_menu(), padding: text_input::DEFAULT_PADDING, size: None, } @@ -752,7 +753,17 @@ where } /// The theme catalog of a [`ComboBox`]. -pub trait Catalog: text_input::Catalog + menu::Catalog {} +pub trait Catalog: text_input::Catalog + menu::Catalog { + /// The default class for the text input of the [`ComboBox`]. + fn default_input<'a>() -> ::Class<'a> { + ::default() + } + + /// The default class for the menu of the [`ComboBox`]. + fn default_menu<'a>() -> ::Class<'a> { + ::default() + } +} impl Catalog for Theme {} diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index 3d9d6910ef..d76caa8a73 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -1,5 +1,4 @@ //! Build and show dropdown menus. -use crate::container::{self, Container}; use crate::core::alignment; use crate::core::event::{self, Event}; use crate::core::layout::{self, Layout}; @@ -165,7 +164,7 @@ where { position: Point, state: &'a mut Tree, - container: Container<'a, Message, Theme, Renderer>, + list: Scrollable<'a, Message, Theme, Renderer>, width: f32, target_height: f32, class: &'a ::Class<'b>, @@ -201,7 +200,7 @@ where class, } = menu; - let container = Container::new(Scrollable::with_direction( + let list = Scrollable::with_direction( List { options, hovered_option, @@ -215,14 +214,14 @@ where class, }, scrollable::Direction::default(), - )); + ); - state.tree.diff(&container as &dyn Widget<_, _, _>); + state.tree.diff(&list as &dyn Widget<_, _, _>); Self { position, state: &mut state.tree, - container, + list, width, target_height, class, @@ -255,7 +254,7 @@ where ) .width(self.width); - let node = self.container.layout(self.state, renderer, &limits); + let node = self.list.layout(self.state, renderer, &limits); let size = node.size(); node.move_to(if space_below > space_above { @@ -276,7 +275,7 @@ where ) -> event::Status { let bounds = layout.bounds(); - self.container.on_event( + self.list.on_event( self.state, event, layout, cursor, renderer, clipboard, shell, &bounds, ) @@ -289,7 +288,7 @@ where viewport: &Rectangle, renderer: &Renderer, ) -> mouse::Interaction { - self.container + self.list .mouse_interaction(self.state, layout, cursor, viewport, renderer) } @@ -314,7 +313,7 @@ where style.background, ); - self.container.draw( + self.list.draw( self.state, renderer, theme, defaults, layout, cursor, &bounds, ); } @@ -579,13 +578,18 @@ pub struct Style { } /// The theme catalog of a [`Menu`]. -pub trait Catalog: scrollable::Catalog + container::Catalog { +pub trait Catalog: scrollable::Catalog { /// The item class of the [`Catalog`]. type Class<'a>; /// The default class produced by the [`Catalog`]. fn default<'a>() -> ::Class<'a>; + /// The default class for the scrollable of the [`Menu`]. + fn default_scrollable<'a>() -> ::Class<'a> { + ::default() + } + /// The [`Style`] of a class with the given status. fn style(&self, class: &::Class<'_>) -> Style; } diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index e9c33d3d6d..801e792bee 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -84,7 +84,7 @@ where font: None, handle: Handle::default(), class: ::default(), - menu_class: ::default(), + menu_class: ::default_menu(), } } @@ -700,6 +700,11 @@ pub trait Catalog: menu::Catalog { /// The default class produced by the [`Catalog`]. fn default<'a>() -> ::Class<'a>; + /// The default class for the menu of the [`PickList`]. + fn default_menu<'a>() -> ::Class<'a> { + ::default() + } + /// The [`Style`] of a class with the given status. fn style( &self,