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
+14 -5
View File
@@ -4,9 +4,9 @@ use gpui::{
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _, Toggled, Window,
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 handler, fired with the next pressed state.
type ChangeHandler = Box<dyn Fn(bool, &mut Window, &mut App)>;
@@ -17,6 +17,7 @@ pub struct Toggle {
id: ElementId,
label: Option<SharedString>,
pressed: bool,
size: Size,
disabled: bool,
a11y_label: Option<SharedString>,
on_change: Option<ChangeHandler>,
@@ -32,6 +33,7 @@ impl Toggle {
id: id.into(),
label: None,
pressed: false,
size: Size::default(),
disabled: false,
a11y_label: None,
on_change: None,
@@ -78,11 +80,18 @@ impl Toggle {
}
}
impl Sizable for Toggle {
fn apply_size(&mut self, size: Size) {
self.size = size;
}
}
impl RenderOnce for Toggle {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let colors = theme.colors;
let ramps = theme.ramps;
let metrics = theme.sizes.resolve(self.size);
let on_change = self.on_change;
let next = !self.pressed;
let a11y_label = self.a11y_label.or_else(|| self.label.clone());
@@ -90,10 +99,10 @@ impl RenderOnce for Toggle {
h_flex()
.id(self.id)
.justify_center()
.h_7()
.px_3()
.h(metrics.control_height)
.px(metrics.padding_x * 0.75)
.rounded(theme.radius.control)
.text_sm()
.text_size(metrics.text_size)
.text_color(colors.foreground)
.role(Role::Button)
.when_some(a11y_label, |toggle, label| toggle.aria_label(label))