pub struct Button { /* private fields */ }Expand description
An element that creates a button with a label and optional icons.
Common buttons:
- Label, Icon + Label:
Button(this component) - Icon only:
IconButton - Custom:
ButtonLike
To create a more complex button than what the Button or IconButton components provide, use
ButtonLike directly.
§Examples
A button with a label, is typically used in scenarios such as a form, where the button’s label indicates what action will be performed when the button is clicked.
use ui::prelude::*;
Button::new("button_id", "Click me!")
.on_click(|event, window, cx| {
// Handle click event
});A toggleable button, is typically used in scenarios such as a toolbar, where the button’s state indicates whether a feature is enabled or not, or a trigger for a popover menu, where clicking the button toggles the visibility of the menu.
use ui::prelude::*;
Button::new("button_id", "Click me!")
.start_icon(Icon::new(IconName::Check))
.toggle_state(true)
.on_click(|event, window, cx| {
// Handle click event
});To change the style of the button when it is selected use the selected_style method.
use ui::prelude::*;
use ui::TintColor;
Button::new("button_id", "Click me!")
.toggle_state(true)
.selected_style(ButtonStyle::Tinted(TintColor::Accent))
.on_click(|event, window, cx| {
// Handle click event
});This will create a button with a blue tinted background when selected.
A full-width button, is typically used in scenarios such as the bottom of a modal or form, where it occupies the entire width of its container. The button’s content, including text and icons, is centered by default.
use ui::prelude::*;
let button = Button::new("button_id", "Click me!")
.full_width()
.on_click(|event, window, cx| {
// Handle click event
});Implementations§
Source§impl Button
impl Button
Sourcepub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self
Creates a new Button with a specified identifier and label.
This is the primary constructor for a Button component. It initializes
the button with the provided identifier and label text, setting all other
properties to their default values, which can be customized using the
builder pattern methods provided by this struct.
Sourcepub fn color(self, label_color: impl Into<Option<Color>>) -> Self
pub fn color(self, label_color: impl Into<Option<Color>>) -> Self
Sets the color of the button’s label.
Sourcepub fn label_size(self, label_size: impl Into<Option<LabelSize>>) -> Self
pub fn label_size(self, label_size: impl Into<Option<LabelSize>>) -> Self
Defines the size of the button’s label.
Sourcepub fn selected_label<L: Into<SharedString>>(
self,
label: impl Into<Option<L>>,
) -> Self
pub fn selected_label<L: Into<SharedString>>( self, label: impl Into<Option<L>>, ) -> Self
Sets the label used when the button is in a selected state.
Sourcepub fn selected_label_color(self, color: impl Into<Option<Color>>) -> Self
pub fn selected_label_color(self, color: impl Into<Option<Color>>) -> Self
Sets the label color used when the button is in a selected state.
Sourcepub fn start_icon(self, icon: impl Into<Option<Icon>>) -> Self
pub fn start_icon(self, icon: impl Into<Option<Icon>>) -> Self
Sets an icon to display at the start (left) of the button label.
The icon’s color will be overridden to Color::Disabled when the button is disabled.
Sourcepub fn end_icon(self, icon: impl Into<Option<Icon>>) -> Self
pub fn end_icon(self, icon: impl Into<Option<Icon>>) -> Self
Sets an icon to display at the end (right) of the button label.
The icon’s color will be overridden to Color::Disabled when the button is disabled.
Sourcepub fn key_binding(self, key_binding: impl Into<Option<KeyBinding>>) -> Self
pub fn key_binding(self, key_binding: impl Into<Option<KeyBinding>>) -> Self
Display the keybinding that triggers the button action.
Sourcepub fn key_binding_position(self, position: KeybindingPosition) -> Self
pub fn key_binding_position(self, position: KeybindingPosition) -> Self
Sets the position of the keybinding relative to the button label.
This method allows you to specify where the keybinding should be displayed in relation to the button’s label.
Sourcepub fn truncate(self, truncate: bool) -> Self
pub fn truncate(self, truncate: bool) -> Self
Truncates overflowing labels with an ellipsis (…) if needed.
Buttons with static labels should never be truncated, ensure this is only used when the label is dynamic and may overflow.
Sourcepub fn loading(self, loading: bool) -> Self
pub fn loading(self, loading: bool) -> Self
Displays a rotating loading spinner in place of the start_icon.
When loading is true, any start_icon is ignored. and a rotating
Sourcepub fn primary(self) -> Self
pub fn primary(self) -> Self
Convenience for a Tailwind-style solid “primary” button: solid
palette::primary(600) background with white text.
Equivalent to .style(ButtonStyle::Tinted(TintColor::Accent)).
Sourcepub fn danger(self) -> Self
pub fn danger(self) -> Self
Convenience for a Tailwind-style solid “danger” button: solid
palette::danger(600) background with white text.
Equivalent to .style(ButtonStyle::Tinted(TintColor::Error)).
Sourcepub fn soft(self) -> Self
pub fn soft(self) -> Self
Convenience for a Tailwind-style “soft” button: faint
palette::primary(50) background with palette::primary(700) text.
Implemented as an additive background override on top of the
Tinted(Accent) style, so it does not require a new ButtonStyle
variant.
Sourcepub fn variant(self, variant: ButtonVariant) -> Self
pub fn variant(self, variant: ButtonVariant) -> Self
shadcn/ui variant alias (recommended vocabulary).
Maps to existing ButtonStyle / convenience builders without
renaming legacy .primary() / .danger() / .soft() call sites.
For link-style buttons use [ButtonLink] instead.
Sourcepub fn shadcn_size(self, size: ButtonSizeAlias) -> Self
pub fn shadcn_size(self, size: ButtonSizeAlias) -> Self
shadcn/ui size alias. Icon-only sizing uses IconButton, not this API.
Trait Implementations§
Source§impl ButtonCommon for Button
impl ButtonCommon for Button
Source§fn style(self, style: ButtonStyle) -> Self
fn style(self, style: ButtonStyle) -> Self
Sets the visual style of the button.
Source§fn size(self, size: ButtonSize) -> Self
fn size(self, size: ButtonSize) -> Self
Sets the size of the button.
Source§fn tooltip(
self,
tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static,
) -> Self
fn tooltip( self, tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static, ) -> Self
Sets a tooltip that appears on hover.
§Examples
Add a tooltip to a button:
use ui::{Tooltip, prelude::*};
Button::new("tooltip_button", "Hover Me")
.tooltip(Tooltip::text("This is a tooltip"));fn tab_index(self, tab_index: impl Into<isize>) -> Self
fn layer(self, elevation: ElevationIndex) -> Self
fn track_focus(self, focus_handle: &FocusHandle) -> Self
Source§impl Clickable for Button
impl Clickable for Button
Source§fn on_click(
self,
handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self
fn on_click( self, handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, ) -> Self
Source§fn cursor_style(self, cursor_style: CursorStyle) -> Self
fn cursor_style(self, cursor_style: CursorStyle) -> Self
Source§impl Component for Button
impl Component for Button
Source§fn scope() -> ComponentScope
fn scope() -> ComponentScope
Source§fn sort_name() -> &'static str
fn sort_name() -> &'static str
Source§fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement>
fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement>
Source§fn id() -> ComponentId
fn id() -> ComponentId
Source§fn status() -> ComponentStatus
fn status() -> ComponentStatus
Source§impl Disableable for Button
impl Disableable for Button
Source§fn disabled(self, disabled: bool) -> Self
fn disabled(self, disabled: bool) -> Self
Disables the button, preventing interaction and changing its appearance.
When disabled, the button’s icon and label will use Color::Disabled.
§Examples
Create a disabled button:
use ui::prelude::*;
Button::new("disabled_button", "Can't Click Me")
.disabled(true);Source§impl Documented for Button
impl Documented for Button
Source§const DOCS: &'static str = "An element that creates a button with a label and optional icons.\n\nCommon buttons:\n- Label, Icon + Label: [`Button`] (this component)\n- Icon only: [`IconButton`]\n- Custom: [`ButtonLike`]\n\nTo create a more complex button than what the [`Button`] or [`IconButton`] components provide, use\n[`ButtonLike`] directly.\n\n# Examples\n\n**A button with a label**, is typically used in scenarios such as a form, where the button\'s label\nindicates what action will be performed when the button is clicked.\n\n```\nuse ui::prelude::*;\n\nButton::new(\"button_id\", \"Click me!\")\n.on_click(|event, window, cx| {\n// Handle click event\n});\n```\n\n**A toggleable button**, is typically used in scenarios such as a toolbar,\nwhere the button\'s state indicates whether a feature is enabled or not, or\na trigger for a popover menu, where clicking the button toggles the visibility of the menu.\n\n```\nuse ui::prelude::*;\n\nButton::new(\"button_id\", \"Click me!\")\n.start_icon(Icon::new(IconName::Check))\n.toggle_state(true)\n.on_click(|event, window, cx| {\n// Handle click event\n});\n```\n\nTo change the style of the button when it is selected use the [`selected_style`][Button::selected_style] method.\n\n```\nuse ui::prelude::*;\nuse ui::TintColor;\n\nButton::new(\"button_id\", \"Click me!\")\n.toggle_state(true)\n.selected_style(ButtonStyle::Tinted(TintColor::Accent))\n.on_click(|event, window, cx| {\n// Handle click event\n});\n```\nThis will create a button with a blue tinted background when selected.\n\n**A full-width button**, is typically used in scenarios such as the bottom of a modal or form, where it occupies the entire width of its container.\nThe button\'s content, including text and icons, is centered by default.\n\n```\nuse ui::prelude::*;\n\nlet button = Button::new(\"button_id\", \"Click me!\")\n.full_width()\n.on_click(|event, window, cx| {\n// Handle click event\n});\n```\n"
const DOCS: &'static str = "An element that creates a button with a label and optional icons.\n\nCommon buttons:\n- Label, Icon + Label: [`Button`] (this component)\n- Icon only: [`IconButton`]\n- Custom: [`ButtonLike`]\n\nTo create a more complex button than what the [`Button`] or [`IconButton`] components provide, use\n[`ButtonLike`] directly.\n\n# Examples\n\n**A button with a label**, is typically used in scenarios such as a form, where the button\'s label\nindicates what action will be performed when the button is clicked.\n\n```\nuse ui::prelude::*;\n\nButton::new(\"button_id\", \"Click me!\")\n.on_click(|event, window, cx| {\n// Handle click event\n});\n```\n\n**A toggleable button**, is typically used in scenarios such as a toolbar,\nwhere the button\'s state indicates whether a feature is enabled or not, or\na trigger for a popover menu, where clicking the button toggles the visibility of the menu.\n\n```\nuse ui::prelude::*;\n\nButton::new(\"button_id\", \"Click me!\")\n.start_icon(Icon::new(IconName::Check))\n.toggle_state(true)\n.on_click(|event, window, cx| {\n// Handle click event\n});\n```\n\nTo change the style of the button when it is selected use the [`selected_style`][Button::selected_style] method.\n\n```\nuse ui::prelude::*;\nuse ui::TintColor;\n\nButton::new(\"button_id\", \"Click me!\")\n.toggle_state(true)\n.selected_style(ButtonStyle::Tinted(TintColor::Accent))\n.on_click(|event, window, cx| {\n// Handle click event\n});\n```\nThis will create a button with a blue tinted background when selected.\n\n**A full-width button**, is typically used in scenarios such as the bottom of a modal or form, where it occupies the entire width of its container.\nThe button\'s content, including text and icons, is centered by default.\n\n```\nuse ui::prelude::*;\n\nlet button = Button::new(\"button_id\", \"Click me!\")\n.full_width()\n.on_click(|event, window, cx| {\n// Handle click event\n});\n```\n"
Source§impl FixedWidth for Button
impl FixedWidth for Button
Source§fn width(self, width: impl Into<DefiniteLength>) -> Self
fn width(self, width: impl Into<DefiniteLength>) -> Self
Sets a fixed width for the button.
§Examples
Create a button with a fixed width of 100 pixels:
use ui::prelude::*;
Button::new("fixed_width_button", "Fixed Width")
.width(px(100.0));Source§fn full_width(self) -> Self
fn full_width(self) -> Self
Makes the button take up the full width of its container.
§Examples
Create a button that takes up the full width of its container:
use ui::prelude::*;
Button::new("full_width_button", "Full Width")
.full_width();Source§impl IntoElement for Button
impl IntoElement for Button
Source§type Element = Component<Button>
type Element = Component<Button>
Source§fn into_element(self) -> Self::Element
fn into_element(self) -> Self::Element
Element.Source§fn into_any_element(self) -> AnyElement
fn into_any_element(self) -> AnyElement
AnyElement.Source§impl RenderOnce for Button
impl RenderOnce for Button
Source§fn render(self, _window: &mut Window, cx: &mut App) -> ButtonLike
fn render(self, _window: &mut Window, cx: &mut App) -> ButtonLike
Render::render() method
which takes a mutable reference.Source§impl SelectableButton for Button
impl SelectableButton for Button
Source§fn selected_style(self, style: ButtonStyle) -> Self
fn selected_style(self, style: ButtonStyle) -> Self
Sets the style for the button in a selected state.
§Examples
Customize the selected appearance of a button:
use ui::prelude::*;
use ui::TintColor;
Button::new("styled_button", "Styled Button")
.toggle_state(true)
.selected_style(ButtonStyle::Tinted(TintColor::Accent));Source§impl Toggleable for Button
impl Toggleable for Button
Source§fn toggle_state(self, selected: bool) -> Self
fn toggle_state(self, selected: bool) -> Self
Sets the selected state of the button.
§Examples
Create a toggleable button that changes appearance when selected:
use ui::prelude::*;
use ui::TintColor;
let selected = true;
Button::new("toggle_button", "Toggle Me")
.start_icon(Icon::new(IconName::Check))
.toggle_state(selected)
.selected_style(ButtonStyle::Tinted(TintColor::Accent))
.on_click(|event, window, cx| {
// Toggle the selected state
});Auto Trait Implementations§
impl !RefUnwindSafe for Button
impl !Send for Button
impl !Sync for Button
impl !UnwindSafe for Button
impl Freeze for Button
impl Unpin for Button
impl UnsafeUnpin for Button
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<E> AnimationExt for Ewhere
E: IntoElement + 'static,
impl<E> AnimationExt for Ewhere
E: IntoElement + 'static,
Source§fn with_animation(
self,
id: impl Into<ElementId>,
animation: Animation,
animator: impl Fn(Self, f32) -> Self + 'static,
) -> AnimationElement<Self>where
Self: Sized,
fn with_animation(
self,
id: impl Into<ElementId>,
animation: Animation,
animator: impl Fn(Self, f32) -> Self + 'static,
) -> AnimationElement<Self>where
Self: Sized,
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CommonAnimationExt for Twhere
T: AnimationExt,
impl<T> CommonAnimationExt for Twhere
T: AnimationExt,
Source§fn with_rotate_animation(self, duration: u64) -> AnimationElement<Self>where
Self: Transformable + Sized,
fn with_rotate_animation(self, duration: u64) -> AnimationElement<Self>where
Self: Transformable + Sized,
Source§fn with_keyed_rotate_animation(
self,
id: impl Into<ElementId>,
duration: u64,
) -> AnimationElement<Self>where
Self: Transformable + Sized,
fn with_keyed_rotate_animation(
self,
id: impl Into<ElementId>,
duration: u64,
) -> AnimationElement<Self>where
Self: Transformable + Sized,
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<T> FluentBuilder for Twhere
T: IntoElement,
impl<T> FluentBuilder for Twhere
T: IntoElement,
Source§fn map<U>(self, f: impl FnOnce(Self) -> U) -> Uwhere
Self: Sized,
fn map<U>(self, f: impl FnOnce(Self) -> U) -> Uwhere
Self: Sized,
Source§fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Selfwhere
Self: Sized,
fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Selfwhere
Self: Sized,
Source§fn when_else(
self,
condition: bool,
then: impl FnOnce(Self) -> Self,
else_fn: impl FnOnce(Self) -> Self,
) -> Selfwhere
Self: Sized,
fn when_else(
self,
condition: bool,
then: impl FnOnce(Self) -> Self,
else_fn: impl FnOnce(Self) -> Self,
) -> Selfwhere
Self: Sized,
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> PopoverTrigger for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more