use gpui::{AnyElement, App, ElementId, Entity, FocusHandle, SharedString, Window};
use gpui_material_foundation::InteractionSource;
use gpui_material_theme::{CornerRole, TypeRole};
use crate::SemanticTone;
use crate::action::{Action, ActionControl, ActionLayout, ActionStyle};
use crate::control_style::{ButtonStyle, ControlSize};
#[derive(Clone, Copy)]
pub(super) struct Metrics {
pub(super) height: f32,
pub(super) icon: f32,
pub(super) gap: f32,
pub(super) padding: f32,
pub(super) outline: f32,
pub(super) square: CornerRole,
pub(super) pressed: CornerRole,
pub(super) label: TypeRole,
}
impl ControlSize {
pub(super) fn metrics(self) -> Metrics {
use CornerRole::*;
use TypeRole::*;
let (height, icon, gap, padding, outline, square, pressed, label) = match self {
Self::ExtraSmall => (32., 20., 8., 16., 1., Medium, Small, LabelLarge),
Self::Small => (40., 20., 8., 16., 1., Medium, Small, LabelLarge),
Self::Medium => (56., 24., 8., 24., 1., Large, Medium, TitleMedium),
Self::Large => (96., 32., 12., 48., 2., ExtraLarge, Large, HeadlineSmall),
Self::ExtraLarge => (136., 40., 16., 64., 3., ExtraLarge, Large, HeadlineLarge),
};
Metrics {
height,
icon,
gap,
padding,
outline,
square,
pressed,
label,
}
}
pub(super) fn icon_metrics(self, width: IconButtonWidth) -> (Metrics, f32) {
let mut metrics = self.metrics();
let (icon, narrow, uniform, wide) = match self {
Self::ExtraSmall => (20., 4., 6., 10.),
Self::Small => (24., 4., 8., 14.),
Self::Medium => (24., 12., 16., 24.),
Self::Large => (32., 16., 32., 48.),
Self::ExtraLarge => (40., 32., 48., 72.),
};
metrics.icon = icon;
let space = match width {
IconButtonWidth::Narrow => narrow,
IconButtonWidth::Uniform => uniform,
IconButtonWidth::Wide => wide,
};
(metrics, icon + 2. * space)
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ButtonShape {
#[default]
Round,
Square,
}
impl ButtonShape {
pub(super) fn corner(self, square: CornerRole, invert: bool) -> CornerRole {
if (self == Self::Round) != invert {
CornerRole::Full
} else {
square
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ButtonVariant {
Elevated,
#[default]
Filled,
FilledTonal,
Outlined,
Text,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ToggleButtonVariant {
Elevated,
#[default]
Filled,
FilledTonal,
Outlined,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum IconButtonVariant {
#[default]
Standard,
Filled,
FilledTonal,
Outlined,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum IconButtonWidth {
Narrow,
#[default]
Uniform,
Wide,
}
pub(super) struct Common {
pub(super) id: ElementId,
pub(super) label: SharedString,
pub(super) disabled: bool,
pub(super) loading: bool,
pub(super) focus: Option<FocusHandle>,
pub(super) interaction_source: Option<Entity<InteractionSource>>,
pub(super) appearance: ButtonStyle,
pub(super) size: ControlSize,
pub(super) shape: ButtonShape,
pub(super) tone: SemanticTone,
}
impl Common {
pub(super) fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
disabled: false,
loading: false,
focus: None,
interaction_source: None,
appearance: ButtonStyle::default(),
size: ControlSize::default(),
shape: ButtonShape::default(),
tone: SemanticTone::default(),
}
}
pub(super) fn render(
self,
content: AnyElement,
style: ActionStyle,
action: Action,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
self.render_scaled(content, style, action, 1., window, cx)
}
pub(super) fn render_scaled(
self,
content: AnyElement,
style: ActionStyle,
action: Action,
scale: f32,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
ActionControl {
id: self.id,
label: self.label,
focus: self.focus,
disabled: self.disabled,
action,
style,
content,
}
.render_layout(
scale,
ActionLayout {
tab_stop: true,
loading: self.loading,
appearance: self.appearance,
..Default::default()
},
self.interaction_source.as_ref(),
window,
cx,
)
}
}