add sizable trait

This commit is contained in:
2026-10-29 07:56:23 +07:00
parent 3808b1519f
commit 1b29c2c940
19 changed files with 442 additions and 69 deletions
+24 -8
View File
@@ -6,9 +6,9 @@ use gpui::{
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _, Toggled, Window,
div, px,
};
use gpui_native_theme::ActiveTheme as _;
use gpui_native_theme::{ActiveTheme as _, Size};
use crate::common::h_flex;
use crate::common::{Sizable, h_flex};
/// The change reporter, fired with the next value by pointer, Enter, and Space.
type ChangeHandler = Box<dyn Fn(bool, &mut Window, &mut App)>;
@@ -19,6 +19,7 @@ pub struct Switch {
id: ElementId,
label: Option<SharedString>,
checked: bool,
size: Size,
disabled: bool,
a11y_label: Option<SharedString>,
on_change: Option<ChangeHandler>,
@@ -34,6 +35,7 @@ impl Switch {
id: id.into(),
label: None,
checked: false,
size: Size::default(),
disabled: false,
a11y_label: None,
on_change: None,
@@ -80,19 +82,33 @@ impl Switch {
}
}
impl Sizable for Switch {
fn apply_size(&mut self, size: Size) {
self.size = size;
}
}
impl RenderOnce for Switch {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let colors = theme.colors;
let ramps = theme.ramps;
let on_change = self.on_change;
let metrics = theme.sizes.resolve(self.size);
let track_height = metrics.icon_size * 1.25;
let track_width = metrics.icon_size * 2.25;
let knob = metrics.icon_size;
let inset = metrics.icon_size * 0.125;
let next = !self.checked;
let on_change = self.on_change;
let a11y_label = self.a11y_label.or_else(|| self.label.clone());
h_flex()
.id(self.id)
.gap_1p5()
.text_sm()
.text_size(metrics.text_size)
.role(Role::Switch)
.when_some(a11y_label, |switch, label| switch.aria_label(label))
.aria_toggled(Toggled::from(self.checked))
@@ -114,9 +130,9 @@ impl RenderOnce for Switch {
.child(
h_flex()
.flex_none()
.w_9()
.h_5()
.p_0p5()
.w(track_width)
.h(track_height)
.p(inset)
.rounded(theme.radius.full)
.when(self.checked, |track| {
track
@@ -129,7 +145,7 @@ impl RenderOnce for Switch {
})
.child(
div()
.size_4()
.size(knob)
.flex_none()
.rounded(theme.radius.full)
.bg(colors.primary_foreground)