core: support children and unpadded button

This commit is contained in:
2026-11-11 19:57:06 +07:00
parent 3296a4c38a
commit 085dad60cd
+31 -7
View File
@@ -2,9 +2,9 @@
use gpui::prelude::FluentBuilder as _; use gpui::prelude::FluentBuilder as _;
use gpui::{ use gpui::{
AnyView, App, ClickEvent, ElementId, InteractiveElement as _, IntoElement, ParentElement as _, AnyElement, AnyView, App, ClickEvent, ElementId, InteractiveElement as _, IntoElement,
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _, Window, canvas, ParentElement, RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _,
relative, transparent_black, Window, canvas, relative, transparent_black,
}; };
use gpui_native_icons::Icon; use gpui_native_icons::Icon;
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size}; use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size};
@@ -40,10 +40,12 @@ pub struct Button {
id: ElementId, id: ElementId,
label: Option<SharedString>, label: Option<SharedString>,
icon: Option<Icon>, icon: Option<Icon>,
children: Vec<AnyElement>,
variant: ButtonVariant, variant: ButtonVariant,
size: Size, size: Size,
disabled: bool, disabled: bool,
loading: bool, loading: bool,
padded: bool,
tab_stop: bool, tab_stop: bool,
debug_selector: Option<Rc<dyn Fn() -> String>>, debug_selector: Option<Rc<dyn Fn() -> String>>,
a11y_label: Option<SharedString>, a11y_label: Option<SharedString>,
@@ -61,10 +63,12 @@ impl Button {
id: id.into(), id: id.into(),
label: None, label: None,
icon: None, icon: None,
children: Vec::new(),
variant: ButtonVariant::default(), variant: ButtonVariant::default(),
size: Size::default(), size: Size::default(),
disabled: false, disabled: false,
loading: false, loading: false,
padded: true,
tab_stop: true, tab_stop: true,
debug_selector: None, debug_selector: None,
a11y_label: None, a11y_label: None,
@@ -82,7 +86,7 @@ impl Button {
self self
} }
/// Shows an icon beside the label. /// Shows an icon beside the label; without a label the button renders square.
pub fn icon<I>(mut self, icon: I) -> Self pub fn icon<I>(mut self, icon: I) -> Self
where where
I: Into<Icon>, I: Into<Icon>,
@@ -133,6 +137,12 @@ impl Button {
self self
} }
/// Whether the button pads its content. false removes all padding.
pub fn padded(mut self, padded: bool) -> Self {
self.padded = padded;
self
}
/// Overrides the accessibility label, which defaults to the visible label. /// Overrides the accessibility label, which defaults to the visible label.
pub fn accessibility_label<L>(mut self, label: L) -> Self pub fn accessibility_label<L>(mut self, label: L) -> Self
where where
@@ -152,6 +162,12 @@ impl Button {
} }
} }
impl ParentElement for Button {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl Sizable for Button { impl Sizable for Button {
fn apply_size(&mut self, size: Size) { fn apply_size(&mut self, size: Size) {
self.size = size; self.size = size;
@@ -169,6 +185,7 @@ impl RenderOnce for Button {
let show_border = borders.buttons && self.variant != ButtonVariant::Ghost; let show_border = borders.buttons && self.variant != ButtonVariant::Ghost;
let on_click = self.on_click; let on_click = self.on_click;
let a11y_label = self.a11y_label.or_else(|| self.label.clone()); let a11y_label = self.a11y_label.or_else(|| self.label.clone());
let is_icon_only = self.icon.is_some() && self.label.is_none() && self.children.is_empty();
let (background, foreground, hover, pressed) = match self.variant { let (background, foreground, hover, pressed) = match self.variant {
ButtonVariant::Primary => ( ButtonVariant::Primary => (
@@ -218,10 +235,16 @@ impl RenderOnce for Button {
.role(Role::Button) .role(Role::Button)
.justify_center() .justify_center()
.h(metrics.control_height + BORDER_WIDTH * 2.0) .h(metrics.control_height + BORDER_WIDTH * 2.0)
.when(self.icon.is_some(), |button| { .when(is_icon_only, |button| {
button.px(metrics.dense_padding_x) button.w(metrics.control_height + BORDER_WIDTH * 2.0)
})
.when(!is_icon_only && self.padded, |button| {
button
.when(self.icon.is_some(), |button| {
button.px(metrics.dense_padding_x)
})
.when(self.icon.is_none(), |button| button.px(metrics.padding_x))
}) })
.when(self.icon.is_none(), |button| button.px(metrics.padding_x))
.line_height(relative(1.0)) .line_height(relative(1.0))
.gap_2() .gap_2()
.rounded(theme.radius.control) .rounded(theme.radius.control)
@@ -245,6 +268,7 @@ impl RenderOnce for Button {
}) })
}) })
.when_some(self.label, |button, label| button.child(label)) .when_some(self.label, |button, label| button.child(label))
.children(self.children)
.child( .child(
canvas(record_anchor_bounds(self.id.clone()), |_, _, _, _| {}) canvas(record_anchor_bounds(self.id.clone()), |_, _, _, _| {})
.absolute() .absolute()