core: support children and unpadded button
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
AnyView, App, ClickEvent, ElementId, InteractiveElement as _, IntoElement, ParentElement as _,
|
||||
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _, Window, canvas,
|
||||
relative, transparent_black,
|
||||
AnyElement, AnyView, App, ClickEvent, ElementId, InteractiveElement as _, IntoElement,
|
||||
ParentElement, RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _,
|
||||
Window, canvas, relative, transparent_black,
|
||||
};
|
||||
use gpui_native_icons::Icon;
|
||||
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size};
|
||||
@@ -40,10 +40,12 @@ pub struct Button {
|
||||
id: ElementId,
|
||||
label: Option<SharedString>,
|
||||
icon: Option<Icon>,
|
||||
children: Vec<AnyElement>,
|
||||
variant: ButtonVariant,
|
||||
size: Size,
|
||||
disabled: bool,
|
||||
loading: bool,
|
||||
padded: bool,
|
||||
tab_stop: bool,
|
||||
debug_selector: Option<Rc<dyn Fn() -> String>>,
|
||||
a11y_label: Option<SharedString>,
|
||||
@@ -61,10 +63,12 @@ impl Button {
|
||||
id: id.into(),
|
||||
label: None,
|
||||
icon: None,
|
||||
children: Vec::new(),
|
||||
variant: ButtonVariant::default(),
|
||||
size: Size::default(),
|
||||
disabled: false,
|
||||
loading: false,
|
||||
padded: true,
|
||||
tab_stop: true,
|
||||
debug_selector: None,
|
||||
a11y_label: None,
|
||||
@@ -82,7 +86,7 @@ impl Button {
|
||||
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
|
||||
where
|
||||
I: Into<Icon>,
|
||||
@@ -133,6 +137,12 @@ impl Button {
|
||||
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.
|
||||
pub fn accessibility_label<L>(mut self, label: L) -> Self
|
||||
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 {
|
||||
fn apply_size(&mut self, size: Size) {
|
||||
self.size = size;
|
||||
@@ -169,6 +185,7 @@ impl RenderOnce for Button {
|
||||
let show_border = borders.buttons && self.variant != ButtonVariant::Ghost;
|
||||
let on_click = self.on_click;
|
||||
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 {
|
||||
ButtonVariant::Primary => (
|
||||
@@ -218,10 +235,16 @@ impl RenderOnce for Button {
|
||||
.role(Role::Button)
|
||||
.justify_center()
|
||||
.h(metrics.control_height + BORDER_WIDTH * 2.0)
|
||||
.when(is_icon_only, |button| {
|
||||
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))
|
||||
})
|
||||
.line_height(relative(1.0))
|
||||
.gap_2()
|
||||
.rounded(theme.radius.control)
|
||||
@@ -245,6 +268,7 @@ impl RenderOnce for Button {
|
||||
})
|
||||
})
|
||||
.when_some(self.label, |button, label| button.child(label))
|
||||
.children(self.children)
|
||||
.child(
|
||||
canvas(record_anchor_bounds(self.id.clone()), |_, _, _, _| {})
|
||||
.absolute()
|
||||
|
||||
Reference in New Issue
Block a user