mod content;
mod forwarding;
mod quiet;
mod size;
#[cfg(test)]
mod tests;
pub use quiet::{Paint, paint};
pub use size::{IconButtonSize, Metrics};
pub const PREFERENCE_KEY: &str = "app.icon-button-size";
pub fn compact_glyph(tokens: &Tokens) -> (Rems, Rems) {
let geometry = Geometry::resolve(
ButtonRole::Compact,
tokens.controls.compact_button,
IconButtonSize::Small,
);
(geometry.glyph, geometry.gap)
}
pub fn initialize(cx: &mut App) {
IconButtonSize::from_key(crate::prefs::get(cx, PREFERENCE_KEY).as_deref()).apply(cx);
}
use crate::Tokens;
use crate::tokens::{ButtonRole, Controls, ToolbarLook};
use content::Geometry;
use gpui_kit::component::{
ActiveTheme as _, Icon, Selectable as _, Sizable as _,
button::{Button, ButtonRounded, ButtonVariant, ButtonVariants as _},
};
use gpui_kit::{
AnyElement, App, ClickEvent, ElementId, FocusHandle, InteractiveElement as _, IntoElement,
ParentElement as _, Pixels, Rems, RenderOnce, SharedString, Window,
};
#[derive(IntoElement)]
pub struct IconButton {
button: Button,
icon: Option<Icon>,
label: Option<SharedString>,
loading: bool,
disabled: bool,
focus_on_click: bool,
role: ButtonRole,
variant: ButtonVariant,
outline: bool,
selected: bool,
flat: bool,
explicit_radius: bool,
explicit_name: bool,
children: Vec<AnyElement>,
focus: Option<FocusHandle>,
}
impl IconButton {
pub fn new(id: impl Into<ElementId>) -> Self {
Self::from_button(Button::new(id).small()).ghost()
}
pub fn from_button(button: Button) -> Self {
let selected = button.is_selected();
Self {
button,
icon: None,
label: None,
loading: false,
disabled: false,
focus_on_click: false,
role: ButtonRole::default(),
variant: ButtonVariant::default(),
outline: false,
selected,
flat: false,
explicit_radius: false,
explicit_name: false,
children: Vec::new(),
focus: None,
}
}
pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn loading(mut self, loading: bool) -> Self {
self.loading = loading;
self
}
pub fn compact(self) -> Self {
self.role(ButtonRole::Compact)
}
pub fn role(mut self, role: ButtonRole) -> Self {
self.role = role;
self
}
pub fn flat(mut self) -> Self {
self.flat = true;
self.ghost()
}
pub fn surface(&self, controls: &Controls) -> Surface {
Style {
glass: controls.liquid_glass,
look: controls.toolbar_look,
flat: self.flat,
role: self.role,
variant: self.variant,
outline: self.outline,
has_icon: self.icon.is_some(),
}
.surface()
}
pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
self.focus = Some(focus.clone());
self
}
pub fn focus_on_click(mut self) -> Self {
self.focus_on_click = true;
self
}
pub(crate) fn render_native(mut self, window: &mut Window, cx: &mut App) -> AnyElement {
let focus = self.native_focus(window, cx);
if self.focus_on_click && !self.disabled && !self.loading {
self.button
.interactivity()
.on_click(move |event, window, cx| {
if matches!(event, ClickEvent::Mouse(_)) {
focus.focus(window, cx);
}
});
}
let rem = window.rem_size();
self.native(rem, cx).render(window, cx).into_any_element()
}
fn native_focus(&mut self, window: &mut Window, cx: &mut App) -> FocusHandle {
let id = self
.button
.interactivity()
.element_id
.clone()
.unwrap_or(0.into());
let state = window.use_keyed_state(id, cx, |_, cx| cx.focus_handle());
if let Some(focus) = &self.focus {
state.update(cx, |state, _| *state = focus.clone());
}
state.read(cx).clone()
}
fn native(self, rem: Pixels, cx: &App) -> Button {
let tokens = Tokens::current(cx);
let finish = self.finish(&tokens, cx);
let hover_glyph = match &finish {
Finish::Quiet(paint) if !self.disabled => Some(paint.hover_glyph),
_ => None,
};
let mut button = self.button;
if !self.explicit_name
&& let Some(label) = &self.label
{
button = button.accessibility_label(label.clone());
}
if self.icon.is_none() && self.label.is_none() {
return button.loading(self.loading).children(self.children);
}
let metrics = tokens.controls.button(self.role);
let geometry = Geometry::resolve(self.role, metrics, IconButtonSize::current(cx));
if !self.explicit_radius {
button = button.rounded(ButtonRounded::Size(geometry.radius_at(rem)));
}
content::compose(
finish.dress(button, &tokens, cx),
content::Content {
icon: self.icon,
label: self.label,
loading: self.loading,
children: self.children,
hover_glyph,
},
geometry,
)
}
fn finish(&self, tokens: &Tokens, cx: &App) -> Finish {
match self.surface(&tokens.controls) {
Surface::Quiet(look) => Finish::Quiet(paint(
look,
self.selected,
tokens,
cx.theme().secondary_foreground,
)),
Surface::Glass => Finish::Glass(self.selected),
Surface::Tinted => Finish::Tinted,
Surface::Native => Finish::Native,
}
}
}
enum Finish {
Native,
Quiet(Paint),
Glass(bool),
Tinted,
}
impl Finish {
fn dress(self, button: Button, tokens: &Tokens, cx: &App) -> Button {
match self {
Self::Native => button,
Self::Quiet(paint) => quiet::dress(button, &paint, tokens, cx),
Self::Glass(selected) => crate::glass::control(button, selected, tokens, cx),
Self::Tinted => button.outline(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Surface {
Native,
Quiet(ToolbarLook),
Glass,
Tinted,
}
struct Style {
glass: bool,
look: ToolbarLook,
flat: bool,
role: ButtonRole,
variant: ButtonVariant,
outline: bool,
has_icon: bool,
}
impl Style {
fn surface(&self) -> Surface {
let ghost = self.variant.is_ghost();
let outlined = self.outline && (ghost || self.variant == ButtonVariant::Default);
let toolbar_glyph = self.role == ButtonRole::Toolbar && self.has_icon;
match (self.flat, self.glass, self.variant) {
(true, _, _) => Surface::Quiet(ToolbarLook::FLAT),
(false, true, ButtonVariant::Danger) => Surface::Tinted,
_ if toolbar_glyph && ghost && !outlined => Surface::Quiet(self.look),
_ if toolbar_glyph && outlined && self.glass => Surface::Glass,
_ => Surface::Native,
}
}
}
impl RenderOnce for IconButton {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
self.render_native(window, cx)
}
}