use crate::{
app::theme::{BorderSize, PaletteColor, RadiusSize, TypographyStyle},
layout::{scrollbar::ScrollBarStyle, text_style::TextStyle},
node::{
BackgroundColor, BoxShadowValue, Node, SpacingValue, TextColor,
border::{Border, BorderRadius, Borders, ThemedBorderRadius},
border_size_value::BorderSizeValue,
outline::Outline,
padding::Padding,
radius_value::RadiusValue,
select_icon::{SelectCheckmarkPosition, SelectIcon},
},
};
#[derive(Clone, Default)]
pub struct SelectPartStyle {
pub(crate) background: Option<BackgroundColor>,
pub(crate) border: Option<Borders>,
pub(crate) border_radius: Option<ThemedBorderRadius>,
pub(crate) box_shadow: Option<BoxShadowValue>,
pub(crate) outline: Option<Outline>,
pub(crate) padding: Option<Padding>,
pub(crate) text: Option<TextStyle>,
pub(crate) typography: Option<TypographyStyle>,
pub(crate) text_color: Option<TextColor>,
pub(crate) opacity: Option<f32>,
pub(crate) min_width: Option<f32>,
pub(crate) min_height: Option<f32>,
}
impl SelectPartStyle {
pub fn new() -> Self {
Self::default()
}
pub fn background(mut self, color: impl Into<BackgroundColor>) -> Self {
self.background = Some(color.into());
self
}
pub fn rounded(mut self, radius: impl Into<RadiusValue>) -> Self {
self.border_radius = Some(ThemedBorderRadius::all(radius));
self
}
pub fn corner_radius_custom(mut self, radius: BorderRadius) -> Self {
self.border_radius = Some(radius.into());
self
}
pub fn border_inside(mut self, width: impl Into<BorderSizeValue>, color: impl Into<BackgroundColor>) -> Self {
self.border = Some(Borders::all(Border::inside(width, color)));
self
}
pub fn border_outside(mut self, width: impl Into<BorderSizeValue>, color: impl Into<BackgroundColor>) -> Self {
self.border = Some(Borders::all(Border::outside(width, color)));
self
}
pub fn border_center(mut self, width: impl Into<BorderSizeValue>, color: impl Into<BackgroundColor>) -> Self {
self.border = Some(Borders::all(Border::center(width, color)));
self
}
pub fn border_custom(mut self, border: Borders) -> Self {
self.border = Some(border);
self
}
pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
self.padding = Some(padding.into());
self
}
pub fn text(mut self, text: TextStyle) -> Self {
self.text = Some(text);
self
}
pub fn typography(mut self, typography: impl Into<TypographyStyle>) -> Self {
self.typography = Some(typography.into());
self
}
pub fn text_color(mut self, color: impl Into<TextColor>) -> Self {
self.text_color = Some(color.into());
self
}
pub fn box_shadow(mut self, shadow: impl Into<BoxShadowValue>) -> Self {
self.box_shadow = Some(shadow.into());
self
}
pub fn outline(mut self, outline: Outline) -> Self {
self.outline = Some(outline);
self
}
pub fn opacity(mut self, opacity: f32) -> Self {
self.opacity = Some(opacity.clamp(0.0, 1.0));
self
}
pub fn min_width(mut self, width: f32) -> Self {
self.min_width = Some(width);
self
}
pub fn min_height(mut self, height: f32) -> Self {
self.min_height = Some(height);
self
}
pub(crate) fn merge_from(&mut self, other: &Self) {
if other.background.is_some() {
self.background = other.background.clone();
}
if other.border.is_some() {
self.border = other.border.clone();
}
if other.border_radius.is_some() {
self.border_radius = other.border_radius;
}
if other.padding.is_some() {
self.padding = other.padding;
}
if other.box_shadow.is_some() {
self.box_shadow = other.box_shadow.clone();
}
if other.outline.is_some() {
self.outline = other.outline.clone();
}
if other.text.is_some() {
self.text = other.text.clone();
}
if other.typography.is_some() {
self.typography = other.typography;
}
if other.text_color.is_some() {
self.text_color = other.text_color.clone();
}
if other.opacity.is_some() {
self.opacity = other.opacity;
}
if other.min_width.is_some() {
self.min_width = other.min_width;
}
if other.min_height.is_some() {
self.min_height = other.min_height;
}
}
pub(crate) fn text_style(&self) -> Option<&TextStyle> {
self.text.as_ref()
}
pub(crate) fn text_node(&self, content: &str) -> Node {
let node = match (self.typography, self.text.as_ref()) {
(Some(typography), _) => Node::text(content).text_variant(typography),
(None, Some(style)) => Node::text_styled(content, style.clone()),
(None, None) => Node::text(content),
};
match &self.text_color {
Some(color) => node.text_color(color.clone()),
None => node,
}
}
}
#[derive(Clone)]
pub struct SelectStyle(Box<SelectStyleParts>);
impl std::ops::Deref for SelectStyle {
type Target = SelectStyleParts;
fn deref(&self) -> &SelectStyleParts {
&self.0
}
}
impl std::ops::DerefMut for SelectStyle {
fn deref_mut(&mut self) -> &mut SelectStyleParts {
&mut self.0
}
}
#[derive(Clone)]
pub struct SelectStyleParts {
pub(crate) trigger: SelectPartStyle,
pub(crate) trigger_hovered: Option<SelectPartStyle>,
pub(crate) trigger_focused: Option<SelectPartStyle>,
pub(crate) trigger_open: Option<SelectPartStyle>,
pub(crate) placeholder_text: Option<TextStyle>,
pub(crate) trigger_gap: SpacingValue,
pub(crate) menu: SelectPartStyle,
pub(crate) menu_scrollbar: Option<ScrollBarStyle>,
pub(crate) option: SelectPartStyle,
pub(crate) option_hovered: Option<SelectPartStyle>,
pub(crate) option_selected: Option<SelectPartStyle>,
pub(crate) option_selected_hovered: Option<SelectPartStyle>,
pub(crate) option_highlighted: Option<SelectPartStyle>,
pub(crate) option_selected_highlighted: Option<SelectPartStyle>,
pub(crate) option_disabled: Option<SelectPartStyle>,
pub(crate) option_detail: SelectPartStyle,
pub(crate) option_hint: SelectPartStyle,
pub(crate) group_label: SelectPartStyle,
pub(crate) separator: SelectPartStyle,
pub(crate) option_adornment_gap: SpacingValue,
pub(crate) chevron: SelectIcon,
pub(crate) chevron_open: Option<SelectIcon>,
pub(crate) chevron_color: Option<TextColor>,
pub(crate) chevron_size: f32,
pub(crate) checkmark: SelectIcon,
pub(crate) checkmark_unchecked: Option<SelectIcon>,
pub(crate) checkmark_color: Option<TextColor>,
pub(crate) checkmark_size: Option<f32>,
pub(crate) checkmark_position: SelectCheckmarkPosition,
pub(crate) checkmark_gap: SpacingValue,
pub(crate) single_checkmark: bool,
pub(crate) min_menu_width: f32,
pub(crate) menu_fits_options: bool,
pub(crate) max_menu_height: f32,
pub(crate) menu_gap: f32,
}
pub(crate) const DEFAULT_CHECKMARK_SLOT: f32 = 16.0;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct SelectOptionState {
pub(crate) hovered: bool,
pub(crate) highlighted: bool,
pub(crate) selected: bool,
pub(crate) disabled: bool,
}
#[allow(deprecated)]
mod legacy {
use crate::app::theme::PaletteColor;
pub(super) const TRIGGER: PaletteColor = PaletteColor::SurfaceInput;
pub(super) const EDGE: PaletteColor = PaletteColor::Border;
pub(super) const FOCUS: PaletteColor = PaletteColor::BorderFocus;
pub(super) const MENU: PaletteColor = PaletteColor::SurfaceRaised;
pub(super) const HOVERED: PaletteColor = PaletteColor::SurfacePanel;
pub(super) const SELECTED: PaletteColor = PaletteColor::Accent;
pub(super) const SELECTED_HOVERED: PaletteColor = PaletteColor::AccentHover;
pub(super) const MUTED: PaletteColor = PaletteColor::TextMuted;
}
fn colors(
trigger: impl Into<BackgroundColor>,
edge: impl Into<BackgroundColor>,
focus: impl Into<BackgroundColor>,
menu: impl Into<BackgroundColor>,
hovered: impl Into<BackgroundColor>,
selected: impl Into<BackgroundColor>,
selected_hovered: impl Into<BackgroundColor>,
muted: impl Into<TextColor>,
) -> crate::ui::Recipe {
use crate::ui::{Recipe, SelectPart::*, SelectState, Selector};
let line = |color: BackgroundColor| Border::inside(BorderSize::Sm, color);
let (edge, focus, muted) = (edge.into(), focus.into(), muted.into());
Recipe::new()
.base(Trigger, |s| s.background(trigger).border(line(edge.clone())))
.hovered(Trigger, |s| s.border(line(focus.clone())))
.focus_visible(Trigger, |s| s.border(line(focus.clone())))
.state(SelectState::Open, Trigger, |s| s.border(line(focus)))
.base(Panel, |s| s.background(menu).border(line(edge.clone())))
.hovered(Item, |s| s.background(hovered))
.state(SelectState::Selected, Item, |s| s.background(selected))
.when(Selector::new().state(SelectState::Selected).hovered(), Item, |s| {
s.background(selected_hovered)
})
.base(Detail, |s| s.foreground(muted.clone()))
.base(Hint, |s| s.foreground(muted.clone()))
.base(Group, |s| s.foreground(muted))
.base(Separator, |s| s.background(edge))
}
impl crate::ui::Themed for SelectStyle {
const NAME: &'static str = "engine_select";
type Renderer = ();
fn recipe() -> crate::ui::Recipe {
use PaletteColor::*;
colors(
Background,
Border,
Ring,
Card,
Canvas,
Primary,
PrimaryHover,
MutedForeground,
)
}
}
impl Default for SelectStyle {
fn default() -> Self {
use crate::ui::{
Interaction::{FocusVisible, Hovered},
Name,
SelectPart::*,
SelectState,
engine_look::EngineLook,
};
let look = EngineLook::of::<Self>(&[]);
let open = EngineLook::of::<Self>(&[Name::from(SelectState::Open)]);
let selected = EngineLook::of::<Self>(&[Name::from(SelectState::Selected)]);
let fill = |look: &EngineLook, part, interaction, or: PaletteColor| {
look.fill(part, interaction).unwrap_or(BackgroundColor::Palette(or))
};
let edge = |look: &EngineLook, part, interaction| {
look
.edge(part, interaction)
.unwrap_or(BackgroundColor::Palette(legacy::EDGE))
};
let focus = |look: &EngineLook, interaction| {
look
.edge(Trigger, interaction)
.unwrap_or(BackgroundColor::Palette(legacy::FOCUS))
};
let muted = |part| look.ink(part).unwrap_or(TextColor::Palette(legacy::MUTED));
Self(Box::new(SelectStyleParts {
trigger: SelectPartStyle::new()
.background(fill(&look, Trigger, None, legacy::TRIGGER))
.border_inside(BorderSize::Sm, edge(&look, Trigger, None))
.rounded(RadiusSize::Md)
.padding(Padding::symmetric(10.0, 8.0))
.min_height(36.0),
trigger_hovered: Some(SelectPartStyle::new().border_inside(BorderSize::Sm, focus(&look, Some(Hovered)))),
trigger_focused: Some(SelectPartStyle::new().border_inside(BorderSize::Sm, focus(&look, Some(FocusVisible)))),
trigger_open: Some(SelectPartStyle::new().border_inside(BorderSize::Sm, focus(&open, None))),
menu: SelectPartStyle::new()
.background(fill(&look, Panel, None, legacy::MENU))
.border_inside(BorderSize::Sm, edge(&look, Panel, None))
.rounded(RadiusSize::Md),
option: SelectPartStyle::new().padding(Padding::symmetric(10.0, 0.0)),
option_hovered: Some(SelectPartStyle::new().background(fill(&look, Item, Some(Hovered), legacy::HOVERED))),
option_selected: Some(SelectPartStyle::new().background(fill(&selected, Item, None, legacy::SELECTED))),
option_selected_hovered: Some(SelectPartStyle::new().background(fill(
&selected,
Item,
Some(Hovered),
legacy::SELECTED_HOVERED,
))),
option_disabled: Some(SelectPartStyle::new().opacity(0.45)),
option_detail: SelectPartStyle::new()
.typography(TypographyStyle::Caption)
.text_color(muted(Detail)),
option_hint: SelectPartStyle::new()
.typography(TypographyStyle::Caption)
.text_color(muted(Hint)),
group_label: SelectPartStyle::new()
.typography(TypographyStyle::Caption)
.text_color(muted(Group))
.padding(Padding::symmetric(10.0, 6.0)),
separator: SelectPartStyle::new()
.background(fill(&look, Separator, None, legacy::EDGE))
.padding(Padding::symmetric(0.0, 4.0)),
..*Self::unstyled().0
}))
}
}
impl SelectStyle {
pub(crate) fn legacy_recipe() -> crate::ui::Recipe {
use legacy::*;
colors(TRIGGER, EDGE, FOCUS, MENU, HOVERED, SELECTED, SELECTED_HOVERED, MUTED)
}
}
impl SelectStyle {
pub fn new() -> Self {
Self::default()
}
pub fn unstyled() -> Self {
Self(Box::new(SelectStyleParts {
trigger: SelectPartStyle::default(),
trigger_hovered: None,
trigger_focused: None,
trigger_open: None,
placeholder_text: None,
trigger_gap: SpacingValue::from(8.0),
menu: SelectPartStyle::default(),
menu_scrollbar: None,
option: SelectPartStyle::default(),
option_hovered: None,
option_selected: None,
option_selected_hovered: None,
option_highlighted: None,
option_selected_highlighted: None,
option_disabled: None,
option_detail: SelectPartStyle::default(),
option_hint: SelectPartStyle::default(),
group_label: SelectPartStyle::default(),
separator: SelectPartStyle::default(),
option_adornment_gap: SpacingValue::from(8.0),
chevron: SelectIcon::default_chevron(),
chevron_open: None,
chevron_color: None,
chevron_size: 10.0,
checkmark: SelectIcon::default_checkmark(),
checkmark_unchecked: None,
checkmark_color: None,
checkmark_size: None,
checkmark_position: SelectCheckmarkPosition::Leading,
checkmark_gap: SpacingValue::from(6.0),
single_checkmark: false,
min_menu_width: 0.0,
menu_fits_options: false,
max_menu_height: 240.0,
menu_gap: 4.0,
}))
}
pub fn trigger(mut self, style: SelectPartStyle) -> Self {
self.trigger = style;
self
}
pub fn trigger_hovered(mut self, style: SelectPartStyle) -> Self {
self.trigger_hovered = Some(style);
self
}
pub fn trigger_focused(mut self, style: SelectPartStyle) -> Self {
self.trigger_focused = Some(style);
self
}
pub fn trigger_open(mut self, style: SelectPartStyle) -> Self {
self.trigger_open = Some(style);
self
}
pub fn placeholder_text(mut self, style: TextStyle) -> Self {
self.placeholder_text = Some(style);
self
}
pub fn trigger_gap(mut self, gap: impl Into<SpacingValue>) -> Self {
self.trigger_gap = gap.into();
self
}
pub fn menu(mut self, style: SelectPartStyle) -> Self {
self.menu = style;
self
}
pub fn menu_scrollbar(mut self, style: ScrollBarStyle) -> Self {
self.menu_scrollbar = Some(style);
self
}
pub fn option(mut self, style: SelectPartStyle) -> Self {
self.option = style;
self
}
pub fn option_hovered(mut self, style: SelectPartStyle) -> Self {
self.option_hovered = Some(style);
self
}
pub fn option_selected(mut self, style: SelectPartStyle) -> Self {
self.option_selected = Some(style);
self
}
pub fn option_selected_hovered(mut self, style: SelectPartStyle) -> Self {
self.option_selected_hovered = Some(style);
self
}
pub fn option_highlighted(mut self, style: SelectPartStyle) -> Self {
self.option_highlighted = Some(style);
self
}
pub fn option_selected_highlighted(mut self, style: SelectPartStyle) -> Self {
self.option_selected_highlighted = Some(style);
self
}
pub fn option_disabled(mut self, style: SelectPartStyle) -> Self {
self.option_disabled = Some(style);
self
}
pub fn option_detail(mut self, style: SelectPartStyle) -> Self {
self.option_detail = style;
self
}
pub fn option_hint(mut self, style: SelectPartStyle) -> Self {
self.option_hint = style;
self
}
pub fn group_label(mut self, style: SelectPartStyle) -> Self {
self.group_label = style;
self
}
pub fn separator(mut self, style: SelectPartStyle) -> Self {
self.separator = style;
self
}
pub fn option_adornment_gap(mut self, gap: impl Into<SpacingValue>) -> Self {
self.option_adornment_gap = gap.into();
self
}
pub fn chevron(mut self, icon: SelectIcon) -> Self {
self.chevron = icon;
self
}
pub fn chevron_open(mut self, icon: SelectIcon) -> Self {
self.chevron_open = Some(icon);
self
}
pub fn chevron_color(mut self, color: impl Into<TextColor>) -> Self {
self.chevron_color = Some(color.into());
self
}
pub fn chevron_size(mut self, size: f32) -> Self {
self.chevron_size = size;
self
}
pub fn checkmark(mut self, icon: SelectIcon) -> Self {
self.checkmark = icon;
self
}
pub fn checkmark_unchecked(mut self, icon: SelectIcon) -> Self {
self.checkmark_unchecked = Some(icon);
self
}
pub fn checkmark_color(mut self, color: impl Into<TextColor>) -> Self {
self.checkmark_color = Some(color.into());
self
}
pub fn checkmark_size(mut self, size: f32) -> Self {
self.checkmark_size = Some(size.max(0.0));
self
}
pub fn checkmark_position(mut self, position: SelectCheckmarkPosition) -> Self {
self.checkmark_position = position;
self
}
pub fn checkmark_gap(mut self, gap: impl Into<SpacingValue>) -> Self {
self.checkmark_gap = gap.into();
self
}
pub fn single_checkmark(mut self, show: bool) -> Self {
self.single_checkmark = show;
self
}
pub fn max_menu_height(mut self, height: f32) -> Self {
self.max_menu_height = height;
self
}
pub fn min_menu_width(mut self, width: f32) -> Self {
self.min_menu_width = width;
self
}
pub fn menu_fits_options(mut self, fits: bool) -> Self {
self.menu_fits_options = fits;
self
}
pub fn menu_gap(mut self, gap: f32) -> Self {
self.menu_gap = gap;
self
}
pub(crate) fn resolved_trigger(&self, hovered: bool, focus_visible: bool, open: bool) -> SelectPartStyle {
let mut style = self.trigger.clone();
if hovered && let Some(part) = &self.trigger_hovered {
style.merge_from(part);
}
if focus_visible && let Some(part) = &self.trigger_focused {
style.merge_from(part);
}
if open && let Some(part) = &self.trigger_open {
style.merge_from(part);
}
style
}
pub(crate) fn resolved_option(&self, state: SelectOptionState) -> SelectPartStyle {
let SelectOptionState {
hovered,
highlighted,
selected,
disabled,
} = state;
let (hovered, highlighted) = (hovered && !disabled, highlighted && !disabled);
let (highlight, selected_highlight) = match &self.option_highlighted {
Some(part) => (Some(part), self.option_selected_highlighted.as_ref()),
None => (self.option_hovered.as_ref(), self.option_selected_hovered.as_ref()),
};
let layers = [
(selected, self.option_selected.as_ref()),
(hovered, self.option_hovered.as_ref()),
(highlighted, highlight),
(selected && hovered, self.option_selected_hovered.as_ref()),
(selected && highlighted, selected_highlight),
(disabled, self.option_disabled.as_ref()),
];
let mut style = self.option.clone();
for part in layers.into_iter().filter_map(|(active, part)| part.filter(|_| active)) {
style.merge_from(part);
}
style
}
pub(crate) fn shows_checkmark(&self, multiple: bool) -> bool {
multiple || self.single_checkmark
}
pub(crate) fn checkmark_slot_width(&self) -> f32 {
self.checkmark_size.unwrap_or(DEFAULT_CHECKMARK_SLOT)
}
}