add motion to switch

This commit is contained in:
2026-10-30 10:17:29 +07:00
parent 7fdb9567ad
commit cf400ee65c
4 changed files with 31 additions and 19 deletions
+17 -11
View File
@@ -2,9 +2,9 @@
use gpui::prelude::FluentBuilder as _;
use gpui::{
App, BoxShadow, ElementId, InteractiveElement as _, IntoElement, ParentElement as _,
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _, Toggled, Window,
div, px,
AnimationExt as _, App, BoxShadow, ElementId, InteractiveElement as _, IntoElement,
ParentElement as _, RenderOnce, Role, SharedString, SpringAnimation, SpringConfig,
StatefulInteractiveElement as _, Styled as _, Toggled, Window, div, px,
};
use gpui_native_theme::{ActiveTheme as _, Size};
@@ -100,11 +100,15 @@ impl RenderOnce for Switch {
let knob = metrics.icon_size;
let inset = metrics.icon_size * 0.125;
let travel = track_width - knob - inset * 2.;
let next = !self.checked;
let on_change = self.on_change;
let a11y_label = self.a11y_label.or_else(|| self.label.clone());
let anim_config = SpringConfig::new(500., 35., 1.);
let anim = SpringAnimation::new(anim_config).to(if self.checked { travel } else { px(0.) });
h_flex()
.id(self.id)
.gap_1p5()
@@ -128,29 +132,31 @@ impl RenderOnce for Switch {
})
})
.child(
h_flex()
div()
.flex_none()
.relative()
.w(track_width)
.h(track_height)
.p(inset)
.rounded(theme.radius.full)
.when(self.checked, |track| {
track
.bg(ramps.primary.base)
.justify_end()
.hover(|style| style.bg(ramps.primary.hover))
})
.when(!self.checked, |track| {
track.bg(colors.border).justify_start()
})
.when(!self.checked, |track| track.bg(colors.border))
.child(
div()
.absolute()
.top(inset)
.left(inset)
.size(knob)
.flex_none()
.rounded(theme.radius.full)
.bg(colors.primary_foreground)
.border_1()
.border_color(colors.border),
.border_color(colors.border)
.with_spring("switch-thumb", anim, move |thumb, offset| {
thumb.left(inset + offset)
}),
),
)
.when_some(self.label, |switch, label| switch.child(label))