use std::hash::Hash;
use crate::{components::theme::SegmentedStyle, CornerRadius, Id, ImageSource};
use super::super::SemanticStatus;
pub struct SegmentOption<T> {
pub(super) value: T,
pub(super) label: String,
pub(super) icon: Option<ImageSource>,
pub(super) enabled: bool,
pub(super) tooltip: Option<String>,
}
impl<T> SegmentOption<T> {
pub fn new(value: T, label: impl Into<String>) -> Self {
Self {
value,
label: label.into(),
icon: None,
enabled: true,
tooltip: None,
}
}
pub fn icon(mut self, icon: impl Into<ImageSource>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn tooltip(mut self, text: impl Into<String>) -> Self {
self.tooltip = Some(text.into());
self
}
pub(super) fn key(&self) -> Id
where
T: Hash,
{
Id::new(&self.value)
}
}
impl<T, L: Into<String>> From<(T, L)> for SegmentOption<T> {
fn from((value, label): (T, L)) -> Self {
Self::new(value, label)
}
}
impl<T, L: Into<String>, I: Into<ImageSource>> From<(T, L, I)> for SegmentOption<T> {
fn from((value, label, icon): (T, L, I)) -> Self {
Self::new(value, label).icon(icon)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SegmentedVariant {
#[default]
Raised,
Outlined,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SegmentedOrientation {
#[default]
Horizontal,
Vertical,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SegmentedSize {
Small,
#[default]
Medium,
Large,
}
impl SegmentedSize {
pub(super) fn factor(self) -> f32 {
match self {
Self::Small => 0.8,
Self::Medium => 1.0,
Self::Large => 1.25,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum SegmentWidth {
#[default]
Equal,
Content,
Fixed(f32),
Fill,
}
pub(super) struct Config {
pub id: Option<Id>,
pub enabled: bool,
pub status: SemanticStatus,
pub orientation: SegmentedOrientation,
pub size: SegmentedSize,
pub variant: SegmentedVariant,
pub width: SegmentWidth,
pub min_width: Option<f32>,
pub icon_only: bool,
pub follow_focus: bool,
pub style: SegmentedStyle,
pub rounding: Option<CornerRadius>,
}
impl Default for Config {
fn default() -> Self {
Self {
id: None,
enabled: true,
status: SemanticStatus::Normal,
orientation: SegmentedOrientation::Horizontal,
size: SegmentedSize::Medium,
variant: SegmentedVariant::Raised,
width: SegmentWidth::Equal,
min_width: None,
icon_only: false,
follow_focus: true,
style: SegmentedStyle::default(),
rounding: None,
}
}
}