core: add side option to button

This commit is contained in:
2026-11-12 15:28:19 +07:00
parent 6667c81259
commit 268b385de4
+37 -2
View File
@@ -33,6 +33,18 @@ pub enum ButtonVariant {
Destructive, Destructive,
} }
/// Which edge the button's content aligns to.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ButtonSide {
/// Centered content.
#[default]
Center,
/// Content on the left edge, icon leading the label.
Left,
/// Content on the right edge, icon trailing the label.
Right,
}
/// A push button: pointer, Enter, and Space all fire the single `on_click`. /// A push button: pointer, Enter, and Space all fire the single `on_click`.
#[derive(IntoElement)] #[derive(IntoElement)]
#[must_use] #[must_use]
@@ -46,6 +58,7 @@ pub struct Button {
disabled: bool, disabled: bool,
loading: bool, loading: bool,
padded: bool, padded: bool,
side: ButtonSide,
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>,
@@ -69,6 +82,7 @@ impl Button {
disabled: false, disabled: false,
loading: false, loading: false,
padded: true, padded: true,
side: ButtonSide::default(),
tab_stop: true, tab_stop: true,
debug_selector: None, debug_selector: None,
a11y_label: None, a11y_label: None,
@@ -143,6 +157,12 @@ impl Button {
self self
} }
/// Sets the edge the content aligns to, `Right` renders the icon after the label.
pub fn side(mut self, side: ButtonSide) -> Self {
self.side = side;
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
@@ -186,6 +206,7 @@ impl RenderOnce for Button {
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 is_icon_only = self.icon.is_some() && self.label.is_none() && self.children.is_empty();
let icon_trailing = self.side == ButtonSide::Right;
let (background, foreground, hover, pressed) = match self.variant { let (background, foreground, hover, pressed) = match self.variant {
ButtonVariant::Primary => ( ButtonVariant::Primary => (
@@ -233,7 +254,11 @@ impl RenderOnce for Button {
button.debug_selector(move || selector()) button.debug_selector(move || selector())
}) })
.role(Role::Button) .role(Role::Button)
.justify_center() .map(|button| match self.side {
ButtonSide::Center => button.justify_center(),
ButtonSide::Left => button.justify_start(),
ButtonSide::Right => button.justify_end(),
})
.h(metrics.control_height + BORDER_WIDTH * 2.0) .h(metrics.control_height + BORDER_WIDTH * 2.0)
.when(is_icon_only, |button| { .when(is_icon_only, |button| {
button.w(metrics.control_height + BORDER_WIDTH * 2.0) button.w(metrics.control_height + BORDER_WIDTH * 2.0)
@@ -263,12 +288,22 @@ impl RenderOnce for Button {
) )
}) })
.when(!self.loading, |button| { .when(!self.loading, |button| {
button.when_some(self.icon, |button, icon| { if icon_trailing {
button
.when_some(self.label, |button, label| button.child(label))
.children(self.children)
.when_some(self.icon, |button, icon| {
button.child(icon.size(metrics.icon_size).color(foreground)) button.child(icon.size(metrics.icon_size).color(foreground))
}) })
} else {
button
.when_some(self.icon, |button, icon| {
button.child(icon.size(metrics.icon_size).color(foreground))
}) })
.when_some(self.label, |button, label| button.child(label)) .when_some(self.label, |button, label| button.child(label))
.children(self.children) .children(self.children)
}
})
.child( .child(
canvas(record_anchor_bounds(self.id.clone()), |_, _, _, _| {}) canvas(record_anchor_bounds(self.id.clone()), |_, _, _, _| {})
.absolute() .absolute()