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
+21 -7
View File
@@ -4,12 +4,12 @@ use gpui::prelude::FluentBuilder as _;
use gpui::{
App, BoxShadow, ClickEvent, ElementId, InteractiveElement as _, IntoElement,
ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _,
Styled as _, Window, px,
Styled as _, Window, px, relative,
};
use gpui_native_icons::{IconRole, icon};
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 single activation handler, shared by pointer, Enter, and Space.
type ClickHandler = Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>;
@@ -35,6 +35,7 @@ pub struct Button {
label: Option<SharedString>,
icon: Option<IconRole>,
variant: ButtonVariant,
size: Size,
disabled: bool,
a11y_label: Option<SharedString>,
on_click: Option<ClickHandler>,
@@ -51,6 +52,7 @@ impl Button {
label: None,
icon: None,
variant: ButtonVariant::default(),
size: Size::default(),
disabled: false,
a11y_label: None,
on_click: None,
@@ -103,11 +105,18 @@ impl Button {
}
}
impl Sizable for Button {
fn apply_size(&mut self, size: Size) {
self.size = size;
}
}
impl RenderOnce for Button {
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_click = self.on_click;
let a11y_label = self.a11y_label.or_else(|| self.label.clone());
@@ -143,11 +152,12 @@ impl RenderOnce for Button {
.id(self.id)
.role(Role::Button)
.justify_center()
.h_7()
.px_4()
.h(metrics.control_height)
.px(metrics.padding_x)
.line_height(relative(1.0))
.gap_1p5()
.rounded(theme.radius.control)
.text_sm()
.text_size(metrics.text_size)
.when(self.variant == ButtonVariant::Secondary, |button| {
button.border_1().border_color(colors.border)
})
@@ -155,7 +165,11 @@ impl RenderOnce for Button {
.text_color(foreground)
.when_some(a11y_label, |button, label| button.aria_label(label))
.when_some(self.icon, |button, role| {
button.child(icon(role).color(foreground))
button.child(
icon(role)
.size(f32::from(metrics.icon_size))
.color(foreground),
)
})
.when_some(self.label, |button, label| button.child(label))
.when(self.disabled, |button| button.opacity(0.5))