use super::form::{self, rem};
use crate::{
icons::{CarbonIcon, Icon},
motion::{self, Curve, Motion, Speed},
theme::color,
tokens::{self, ColorRole, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
App, CursorStyle, ElementId, Hsla, InteractiveElement as _, IntoElement, MouseButton,
ParentElement, RenderOnce, SharedString, Styled, Window, div, prelude::FluentBuilder as _,
};
use std::rc::Rc;
pub const SWITCH_COLOR_MOTION: Motion = Motion::new(Curve::ProductiveExit, Speed::Fast01);
pub const SWITCH_KNOB_MOTION: Motion =
Motion::new(Curve::ProductiveExit, Speed::Fast01).spatial(true);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum SwitchSize {
#[default]
Md,
Sm,
}
impl SwitchSize {
pub const ALL: [Self; 2] = [Self::Md, Self::Sm];
pub const fn name(self) -> &'static str {
match self {
Self::Md => "md",
Self::Sm => "sm",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SwitchGeometry {
pub width: f32,
pub height: f32,
pub knob: f32,
pub inset: f32,
pub travel: f32,
}
impl SwitchGeometry {
pub const fn of(size: SwitchSize, readonly: bool) -> Self {
let inset = if readonly { 2. } else { 3. };
match size {
SwitchSize::Md => Self {
width: 48.,
height: 24.,
knob: 18.,
inset,
travel: 24.,
},
SwitchSize::Sm => Self {
width: 32.,
height: 16.,
knob: 10.,
inset,
travel: 16.,
},
}
}
}
impl SwitchGeometry {
pub const TEXT_GAP: f32 = 8.;
}
pub const fn box_width(size: SwitchSize, hide_label: bool) -> f32 {
let track = SwitchGeometry::of(size, false).width;
if hide_label {
track
} else {
track + SwitchGeometry::TEXT_GAP
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SwitchState {
pub checked: bool,
pub disabled: bool,
pub readonly: bool,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SwitchLook {
pub track: Hsla,
pub track_border: Option<Hsla>,
pub knob: Hsla,
pub check: Hsla,
pub text: Hsla,
pub label: Hsla,
}
impl SwitchLook {
pub fn of(state: SwitchState) -> Self {
if state.disabled {
return Self {
track: color(ColorRole::ButtonDisabled),
track_border: None,
knob: color(ColorRole::IconOnColorDisabled),
check: color(ColorRole::ButtonDisabled),
text: color(ColorRole::TextDisabled),
label: color(ColorRole::TextDisabled),
};
}
if state.readonly {
return Self {
track: Hsla::transparent_black(),
track_border: Some(color(ColorRole::IconDisabled)),
knob: color(ColorRole::IconPrimary),
check: color(ColorRole::Background),
text: color(ColorRole::TextPrimary),
label: color(ColorRole::TextSecondary),
};
}
Self {
track: color(if state.checked {
ColorRole::SupportSuccess
} else {
ColorRole::ToggleOff
}),
track_border: None,
knob: color(ColorRole::IconOnColor),
check: color(ColorRole::SupportSuccess),
text: color(ColorRole::TextPrimary),
label: color(ColorRole::TextSecondary),
}
}
}
pub const DEFAULT_LABEL_ON: &str = "On";
pub const DEFAULT_LABEL_OFF: &str = "Off";
pub fn state_text(
checked: bool,
has_label: bool,
hide_label: bool,
label_on: Option<SharedString>,
label_off: Option<SharedString>,
) -> Option<SharedString> {
if hide_label {
return None;
}
let (text, default) = if checked {
(label_on, DEFAULT_LABEL_ON)
} else {
(label_off, DEFAULT_LABEL_OFF)
};
text.or_else(|| has_label.then(|| default.into()))
}
type ChangeHandler = Rc<dyn Fn(bool, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct Switch {
id: ElementId,
label: Option<SharedString>,
hide_label: bool,
checked: Option<bool>,
default_checked: bool,
label_on: Option<SharedString>,
label_off: Option<SharedString>,
size: SwitchSize,
disabled: bool,
readonly: bool,
on_checked_change: Option<ChangeHandler>,
}
impl Switch {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
label: None,
hide_label: false,
checked: None,
default_checked: false,
label_on: None,
label_off: None,
size: SwitchSize::Md,
disabled: false,
readonly: false,
on_checked_change: None,
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn hide_label(mut self, hide: bool) -> Self {
self.hide_label = hide;
self
}
pub fn checked(mut self, checked: bool) -> Self {
self.checked = Some(checked);
self
}
pub fn default_checked(mut self, checked: bool) -> Self {
self.default_checked = checked;
self
}
pub fn label_on(mut self, text: impl Into<SharedString>) -> Self {
self.label_on = Some(text.into());
self
}
pub fn label_off(mut self, text: impl Into<SharedString>) -> Self {
self.label_off = Some(text.into());
self
}
pub fn size(mut self, size: SwitchSize) -> Self {
self.size = size;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn readonly(mut self, readonly: bool) -> Self {
self.readonly = readonly;
self
}
pub fn on_checked_change(
mut self,
handler: impl Fn(bool, &mut Window, &mut App) + 'static,
) -> Self {
self.on_checked_change = Some(Rc::new(handler));
self
}
}
impl RenderOnce for Switch {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let (checked, store) = form::controlled(
(id.clone(), "checked"),
self.checked,
self.default_checked,
window,
cx,
);
let focus = window
.use_keyed_state((id.clone(), "focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let pressed = window.use_keyed_state((id.clone(), "pressed"), cx, |_, _| false);
let state = SwitchState {
checked,
disabled: self.disabled,
readonly: self.readonly,
};
let look = SwitchLook::of(state);
let geometry = SwitchGeometry::of(self.size, self.readonly);
let track = motion::animate_color(
(id.clone(), "track"),
look.track,
SWITCH_COLOR_MOTION,
window,
cx,
);
let knob_x = motion::animate(
(id.clone(), "knob"),
if checked { geometry.travel } else { 0. },
SWITCH_KNOB_MOTION,
window,
cx,
);
let ring = !self.disabled && (focus.is_focused(window) || *pressed.read(cx));
let inert = self.disabled || self.readonly;
let has_label = self.label.as_ref().is_some_and(|l| !l.is_empty());
let text = state_text(
checked,
has_label,
self.hide_label,
self.label_on.clone(),
self.label_off.clone(),
);
let on_change = self.on_checked_change.clone();
let knob = div()
.absolute()
.top(rem(geometry.inset))
.left(rem(geometry.inset + knob_x))
.size(rem(geometry.knob))
.rounded_full()
.bg(look.knob)
.when(self.size == SwitchSize::Sm && checked, |this| {
this.flex().items_center().justify_center().child(
Icon::carbon(CarbonIcon::Checkmark)
.size_length(rem(8.))
.color(look.check),
)
});
let track_el = div()
.relative()
.flex_shrink_0()
.w(rem(geometry.width))
.h(rem(geometry.height))
.rounded(rem(geometry.height / 2.))
.bg(track)
.when_some(look.track_border, |this, border| {
this.border_1().border_color(border)
})
.child(knob)
.when(ring, |this| {
this.child(
div()
.absolute()
.top(rem(-3.))
.left(rem(-3.))
.right(rem(-3.))
.bottom(rem(-3.))
.rounded(tokens::BORDER_RADIUS_16)
.border_2()
.border_color(color(ColorRole::Focus)),
)
});
let keep_gap = text.is_none() && !self.hide_label;
let appearance = div()
.flex()
.flex_row()
.items_center()
.gap(tokens::SPACING_03)
.when(keep_gap, |this| this.pr(rem(SwitchGeometry::TEXT_GAP)))
.child(track_el)
.when_some(text, |this, text| {
this.child(
div()
.type_style(TypeStyle::Body01)
.text_color(look.text)
.whitespace_nowrap()
.child(text),
)
});
let switch = gpui_kit::base::Switch::new(id.clone())
.track_focus(&focus)
.checked(checked)
.disabled(inert)
.when_some(self.label.clone(), |this, label| {
this.accessibility_label(label)
})
.flex()
.flex_col()
.items_start()
.cursor(if self.disabled {
CursorStyle::OperationNotAllowed
} else if self.readonly {
CursorStyle::Arrow
} else {
CursorStyle::PointingHand
})
.on_mouse_down(MouseButton::Left, {
let pressed = pressed.clone();
move |_, _, cx| {
pressed.update(cx, |p, cx| {
*p = true;
cx.notify();
})
}
})
.on_mouse_up(MouseButton::Left, {
let pressed = pressed.clone();
move |_, _, cx| {
pressed.update(cx, |p, cx| {
*p = false;
cx.notify();
})
}
})
.on_mouse_up_out(MouseButton::Left, move |_, _, cx| {
pressed.update(cx, |p, cx| {
if *p {
*p = false;
cx.notify();
}
})
})
.on_change(move |next, _, window, cx| {
store.update(cx, |c, cx| {
*c = next;
cx.notify();
});
if let Some(handler) = on_change.as_ref() {
handler(next, window, cx);
}
})
.when_some(
self.label
.clone()
.filter(|l| !l.is_empty() && !self.hide_label),
|this, label| {
this.child(
div()
.type_style(TypeStyle::Label01)
.text_color(look.label)
.mb(tokens::SPACING_05)
.child(label),
)
},
)
.child(appearance);
div().flex_shrink_0().child(switch)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
fn hex(c: Hsla) -> String {
let c = c.to_rgb();
let b = |v: f32| (v * 255.).round() as u8;
format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
}
#[test]
fn colors_follow_toggle_css() {
crate::theme::tests_set(CarbonTheme::White);
let off = SwitchLook::of(SwitchState::default());
assert_eq!(hex(off.track), "#8d8d8d");
assert_eq!(hex(off.knob), "#ffffff");
let on = SwitchLook::of(SwitchState {
checked: true,
..Default::default()
});
assert_eq!(hex(on.track), "#24a148");
let disabled = SwitchLook::of(SwitchState {
disabled: true,
..Default::default()
});
assert_eq!(hex(disabled.track), "#c6c6c6");
assert_eq!(hex(disabled.knob), "#8d8d8d");
let readonly = SwitchLook::of(SwitchState {
readonly: true,
..Default::default()
});
assert_eq!(readonly.track.a, 0.);
assert_eq!(hex(readonly.knob), "#161616");
}
#[test]
fn state_text_follows_reference_toggle() {
let s = |t: &str| Some(SharedString::from(t.to_string()));
assert_eq!(state_text(true, true, false, None, None), s("On"));
assert_eq!(state_text(false, true, false, None, None), s("Off"));
assert_eq!(state_text(true, true, false, s("Yes"), s("No")), s("Yes"));
assert_eq!(state_text(true, false, false, None, None), None);
assert_eq!(state_text(false, false, false, None, s("Dark")), s("Dark"));
assert_eq!(state_text(true, true, true, s("Yes"), None), None);
}
#[test]
fn geometry_follows_toggle_css() {
let md = SwitchGeometry::of(SwitchSize::Md, false);
assert_eq!(
(md.width, md.height, md.knob, md.inset, md.travel),
(48., 24., 18., 3., 24.)
);
let sm = SwitchGeometry::of(SwitchSize::Sm, false);
assert_eq!(
(sm.width, sm.height, sm.knob, sm.travel),
(32., 16., 10., 16.)
);
assert_eq!(md.inset + md.travel + md.knob, md.width - 3.);
assert_eq!(SwitchGeometry::of(SwitchSize::Md, true).inset, 2.);
assert_eq!(box_width(SwitchSize::Md, false), 56.);
assert_eq!(box_width(SwitchSize::Sm, false), 40.);
assert_eq!(box_width(SwitchSize::Md, true), 48.);
assert_eq!(box_width(SwitchSize::Sm, true), 32.);
}
}