Simplify theming for PickList, ComboBox, and Menu widgets
This commit is contained in:
parent
9b2fd64167
commit
597a41cea7
13 changed files with 420 additions and 471 deletions
|
|
@ -17,8 +17,6 @@
|
|||
pub use iced_core as core;
|
||||
|
||||
pub mod application;
|
||||
pub mod menu;
|
||||
pub mod pick_list;
|
||||
pub mod theme;
|
||||
|
||||
pub use theme::Theme;
|
||||
|
|
|
|||
|
|
@ -1,26 +0,0 @@
|
|||
//! Change the appearance of menus.
|
||||
use iced_core::{Background, Border, Color};
|
||||
|
||||
/// The appearance of a menu.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Appearance {
|
||||
/// The text [`Color`] of the menu.
|
||||
pub text_color: Color,
|
||||
/// The [`Background`] of the menu.
|
||||
pub background: Background,
|
||||
/// The [`Border`] of the menu.
|
||||
pub border: Border,
|
||||
/// The text [`Color`] of a selected option in the menu.
|
||||
pub selected_text_color: Color,
|
||||
/// The background [`Color`] of a selected option in the menu.
|
||||
pub selected_background: Background,
|
||||
}
|
||||
|
||||
/// The style sheet of a menu.
|
||||
pub trait StyleSheet {
|
||||
/// The supported style of the [`StyleSheet`].
|
||||
type Style: Default + Clone;
|
||||
|
||||
/// Produces the [`Appearance`] of a menu.
|
||||
fn appearance(&self, style: &Self::Style) -> Appearance;
|
||||
}
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
//! Change the appearance of a pick list.
|
||||
use iced_core::{Background, Border, Color};
|
||||
|
||||
/// The appearance of a pick list.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Appearance {
|
||||
/// The text [`Color`] of the pick list.
|
||||
pub text_color: Color,
|
||||
/// The placeholder [`Color`] of the pick list.
|
||||
pub placeholder_color: Color,
|
||||
/// The handle [`Color`] of the pick list.
|
||||
pub handle_color: Color,
|
||||
/// The [`Background`] of the pick list.
|
||||
pub background: Background,
|
||||
/// The [`Border`] of the pick list.
|
||||
pub border: Border,
|
||||
}
|
||||
|
||||
/// A set of rules that dictate the style of a container.
|
||||
pub trait StyleSheet {
|
||||
/// The supported style of the [`StyleSheet`].
|
||||
type Style: Default + Clone;
|
||||
|
||||
/// Produces the active [`Appearance`] of a pick list.
|
||||
fn active(&self, style: &<Self as StyleSheet>::Style) -> Appearance;
|
||||
|
||||
/// Produces the hovered [`Appearance`] of a pick list.
|
||||
fn hovered(&self, style: &<Self as StyleSheet>::Style) -> Appearance;
|
||||
}
|
||||
|
|
@ -5,13 +5,8 @@ pub use palette::Palette;
|
|||
|
||||
use crate::application;
|
||||
use crate::core::widget::text;
|
||||
use crate::menu;
|
||||
use crate::pick_list;
|
||||
|
||||
use crate::core::Border;
|
||||
|
||||
use std::fmt;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A built-in theme.
|
||||
|
|
@ -271,107 +266,4 @@ impl<T: Fn(&Theme) -> application::Appearance> application::StyleSheet for T {
|
|||
}
|
||||
}
|
||||
|
||||
/// The style of a menu.
|
||||
#[derive(Clone, Default)]
|
||||
pub enum Menu {
|
||||
/// The default style.
|
||||
#[default]
|
||||
Default,
|
||||
/// A custom style.
|
||||
Custom(Rc<dyn menu::StyleSheet<Style = Theme>>),
|
||||
}
|
||||
|
||||
impl menu::StyleSheet for Theme {
|
||||
type Style = Menu;
|
||||
|
||||
fn appearance(&self, style: &Self::Style) -> menu::Appearance {
|
||||
match style {
|
||||
Menu::Default => {
|
||||
let palette = self.extended_palette();
|
||||
|
||||
menu::Appearance {
|
||||
text_color: palette.background.weak.text,
|
||||
background: palette.background.weak.color.into(),
|
||||
border: Border {
|
||||
width: 1.0,
|
||||
radius: 0.0.into(),
|
||||
color: palette.background.strong.color,
|
||||
},
|
||||
selected_text_color: palette.primary.strong.text,
|
||||
selected_background: palette.primary.strong.color.into(),
|
||||
}
|
||||
}
|
||||
Menu::Custom(custom) => custom.appearance(self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PickList> for Menu {
|
||||
fn from(pick_list: PickList) -> Self {
|
||||
match pick_list {
|
||||
PickList::Default => Self::Default,
|
||||
PickList::Custom(_, menu) => Self::Custom(menu),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The style of a pick list.
|
||||
#[derive(Clone, Default)]
|
||||
pub enum PickList {
|
||||
/// The default style.
|
||||
#[default]
|
||||
Default,
|
||||
/// A custom style.
|
||||
Custom(
|
||||
Rc<dyn pick_list::StyleSheet<Style = Theme>>,
|
||||
Rc<dyn menu::StyleSheet<Style = Theme>>,
|
||||
),
|
||||
}
|
||||
|
||||
impl pick_list::StyleSheet for Theme {
|
||||
type Style = PickList;
|
||||
|
||||
fn active(&self, style: &Self::Style) -> pick_list::Appearance {
|
||||
match style {
|
||||
PickList::Default => {
|
||||
let palette = self.extended_palette();
|
||||
|
||||
pick_list::Appearance {
|
||||
text_color: palette.background.weak.text,
|
||||
background: palette.background.weak.color.into(),
|
||||
placeholder_color: palette.background.strong.color,
|
||||
handle_color: palette.background.weak.text,
|
||||
border: Border {
|
||||
radius: 2.0.into(),
|
||||
width: 1.0,
|
||||
color: palette.background.strong.color,
|
||||
},
|
||||
}
|
||||
}
|
||||
PickList::Custom(custom, _) => custom.active(self),
|
||||
}
|
||||
}
|
||||
|
||||
fn hovered(&self, style: &Self::Style) -> pick_list::Appearance {
|
||||
match style {
|
||||
PickList::Default => {
|
||||
let palette = self.extended_palette();
|
||||
|
||||
pick_list::Appearance {
|
||||
text_color: palette.background.weak.text,
|
||||
background: palette.background.weak.color.into(),
|
||||
placeholder_color: palette.background.strong.color,
|
||||
handle_color: palette.background.weak.text,
|
||||
border: Border {
|
||||
radius: 2.0.into(),
|
||||
width: 1.0,
|
||||
color: palette.primary.strong.color,
|
||||
},
|
||||
}
|
||||
}
|
||||
PickList::Custom(custom, _) => custom.hovered(self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl text::StyleSheet for Theme {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue