Button
A clickable control with five variants and three sizes.
Web replica of the native GPUI output
use gpui::{div, prelude::*, App, Window};
use crate::ui::button::{Button, ButtonVariant};
pub fn button_demo(_window: &mut Window, _cx: &mut App) -> impl IntoElement {
div()
.flex()
.gap_2()
.child(Button::new("save", "Save changes").on_click(|_, _, _| println!("saved")))
.child(Button::new("secondary", "Secondary").variant(ButtonVariant::Secondary))
.child(Button::new("outline", "Outline").variant(ButtonVariant::Outline))
.child(Button::new("ghost", "Ghost").variant(ButtonVariant::Ghost))
.child(Button::new("delete", "Delete").variant(ButtonVariant::Destructive))
}Installation
Add the component
$ cargo hyuskui add buttonOr copy it by hand: paste the source below into
src/ui/button.rsand register it insrc/ui/mod.rs.Make sure the theme is installed
Every component reads colors from
crate::theme. If you have not runcargo hyuskui init, follow the installation guide first.
Usage
use crate::ui::button::*;Source
This file is yours after install. Change the styles, add variants, or delete what you do not need.
use gpui::{
div, prelude::*, px, App, ClickEvent, ElementId, FontWeight, SharedString, Window,
};
use crate::theme::ActiveTheme;
#[derive(Clone, Copy, Default, PartialEq)]
pub enum ButtonVariant {
#[default]
Default,
Secondary,
Outline,
Ghost,
Destructive,
}
#[derive(Clone, Copy, Default, PartialEq)]
pub enum ButtonSize {
Sm,
#[default]
Md,
Lg,
}
type ClickHandler = Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>;
#[derive(IntoElement)]
pub struct Button {
id: ElementId,
label: SharedString,
variant: ButtonVariant,
size: ButtonSize,
disabled: bool,
on_click: Option<ClickHandler>,
}
impl Button {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
variant: ButtonVariant::default(),
size: ButtonSize::default(),
disabled: false,
on_click: None,
}
}
pub fn variant(mut self, variant: ButtonVariant) -> Self {
self.variant = variant;
self
}
pub fn size(mut self, size: ButtonSize) -> Self {
self.size = size;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn on_click(
mut self,
handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.on_click = Some(Box::new(handler));
self
}
}
impl RenderOnce for Button {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let disabled = self.disabled;
let (bg, fg, hover_bg, border) = match self.variant {
ButtonVariant::Default => (
theme.primary,
theme.primary_foreground,
theme.primary.opacity(0.9),
None,
),
ButtonVariant::Secondary => (
theme.secondary,
theme.secondary_foreground,
theme.secondary.opacity(0.8),
None,
),
ButtonVariant::Outline => (
theme.background,
theme.foreground,
theme.accent,
Some(theme.input),
),
ButtonVariant::Ghost => (
gpui::transparent_black(),
theme.foreground,
theme.accent,
None,
),
ButtonVariant::Destructive => (
theme.destructive,
theme.destructive_foreground,
theme.destructive.opacity(0.9),
None,
),
};
let (height, pad_x) = match self.size {
ButtonSize::Sm => (px(32.0), px(12.0)),
ButtonSize::Md => (px(36.0), px(16.0)),
ButtonSize::Lg => (px(40.0), px(24.0)),
};
div()
.id(self.id)
.flex()
.items_center()
.justify_center()
.gap_2()
.h(height)
.px(pad_x)
.rounded(theme.radius)
.bg(bg)
.text_color(fg)
.text_sm()
.font_weight(FontWeight::MEDIUM)
.when_some(border, |this, color| this.border_1().border_color(color))
.when(disabled, |this| this.opacity(0.5).cursor_not_allowed())
.when(!disabled, |this| {
this.cursor_pointer().hover(move |style| style.bg(hover_bg))
})
.when_some(self.on_click.filter(|_| !disabled), |this, handler| {
this.on_click(move |event, window, cx| handler(event, window, cx))
})
.child(self.label)
}
}