core: add side option to button
This commit is contained in:
@@ -33,6 +33,18 @@ pub enum ButtonVariant {
|
||||
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`.
|
||||
#[derive(IntoElement)]
|
||||
#[must_use]
|
||||
@@ -46,6 +58,7 @@ pub struct Button {
|
||||
disabled: bool,
|
||||
loading: bool,
|
||||
padded: bool,
|
||||
side: ButtonSide,
|
||||
tab_stop: bool,
|
||||
debug_selector: Option<Rc<dyn Fn() -> String>>,
|
||||
a11y_label: Option<SharedString>,
|
||||
@@ -69,6 +82,7 @@ impl Button {
|
||||
disabled: false,
|
||||
loading: false,
|
||||
padded: true,
|
||||
side: ButtonSide::default(),
|
||||
tab_stop: true,
|
||||
debug_selector: None,
|
||||
a11y_label: None,
|
||||
@@ -143,6 +157,12 @@ impl Button {
|
||||
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.
|
||||
pub fn accessibility_label<L>(mut self, label: L) -> Self
|
||||
where
|
||||
@@ -186,6 +206,7 @@ impl RenderOnce for Button {
|
||||
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 icon_trailing = self.side == ButtonSide::Right;
|
||||
|
||||
let (background, foreground, hover, pressed) = match self.variant {
|
||||
ButtonVariant::Primary => (
|
||||
@@ -233,7 +254,11 @@ impl RenderOnce for Button {
|
||||
button.debug_selector(move || selector())
|
||||
})
|
||||
.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)
|
||||
.when(is_icon_only, |button| {
|
||||
button.w(metrics.control_height + BORDER_WIDTH * 2.0)
|
||||
@@ -263,12 +288,22 @@ impl RenderOnce for 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))
|
||||
})
|
||||
} 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))
|
||||
.children(self.children)
|
||||
}
|
||||
})
|
||||
.child(
|
||||
canvas(record_anchor_bounds(self.id.clone()), |_, _, _, _| {})
|
||||
.absolute()
|
||||
|
||||
Reference in New Issue
Block a user