feat: unify theme system (#7)

Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
2026-11-16 12:58:32 +00:00
parent 745a01d7af
commit aed3601c89
47 changed files with 888 additions and 1875 deletions
+17 -13
View File
@@ -8,7 +8,10 @@ use gpui::{
ease_out_quint,
};
use gpui_native_icons::{Icon, IconRole};
use gpui_native_theme::{ActiveTheme as _, ENTER_SLIDE_DISTANCE, Size, TextRole, Typeset as _};
use gpui_native_theme::{
ALERT_EDGE_WIDTH, ALERT_STACK_WIDTH, ActiveTheme as _, ENTER_SLIDE_DISTANCE, Size, TextRole,
Typeset as _,
};
use gpui_native_utils::ResultLogErr as _;
use crate::button::{Button, ButtonVariant};
@@ -150,7 +153,6 @@ impl RenderOnce for Alert {
let metrics = theme.sizes.resolve(Size::Medium);
let icon_size = metrics.icon_size;
let dense_icon_size = metrics.dense_icon_size;
let caption_size = metrics.caption_size;
let content = v_flex()
.flex_1()
@@ -171,7 +173,7 @@ impl RenderOnce for Alert {
};
alert.child(
div()
.text_size(caption_size)
.text_size(metrics.text_size)
.text_color(color)
.child(message),
)
@@ -181,7 +183,7 @@ impl RenderOnce for Alert {
Button::new((self.id.clone(), "alert-close"))
.icon(IconRole::Close)
.variant(ButtonVariant::Ghost)
.small()
.xsmall()
.accessibility_label("Dismiss")
.on_click(move |_, _window, cx| on_dismiss(cx))
});
@@ -189,11 +191,7 @@ impl RenderOnce for Alert {
match self.presentation {
AlertStyle::Overlay => {
let surface = theme.colors.surface;
let border = if theme.borders.overlays {
theme.colors.border
} else {
surface
};
let border = theme.colors.border;
div()
.id(self.id.clone())
@@ -231,7 +229,13 @@ impl RenderOnce for Alert {
.overflow_hidden()
.bg(ramp.subtle)
.refine_style(&self.style)
.child(div().flex_none().w_1p5().self_stretch().bg(ramp.base))
.child(
div()
.flex_none()
.w(ALERT_EDGE_WIDTH)
.self_stretch()
.bg(ramp.base),
)
.child(
div()
.flex_1()
@@ -239,7 +243,7 @@ impl RenderOnce for Alert {
.items_start()
.gap_3()
.px_3()
.py_2p5()
.py_2()
.when_some(self.icon.clone(), |alert, icon_glyph| {
alert.child(
div()
@@ -247,7 +251,7 @@ impl RenderOnce for Alert {
.flex()
.items_center()
.justify_center()
.size_6()
.size(theme.sizes.small.avatar_size)
.rounded(theme.radius.full)
.bg(ramp.base)
.child(icon_glyph.size(dense_icon_size).color(ramp.foreground)),
@@ -375,7 +379,7 @@ impl RenderOnce for AlertsView {
.absolute()
.top_4()
.right_4()
.w_80()
.w(ALERT_STACK_WIDTH)
.flex()
.flex_col()
.items_end()
+7 -1
View File
@@ -92,7 +92,13 @@ impl gpui::Styled for Avatar {
impl RenderOnce for Avatar {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let side = theme.sizes.resolve(self.size).avatar_size;
// Avatars sit one class below the shared control scale.
let size = match self.size {
Size::XSmall | Size::Small => Size::XSmall,
Size::Medium => Size::Small,
Size::Large => Size::Medium,
};
let side = theme.sizes.resolve(size).avatar_size;
let radius = if self.fully_rounded {
theme.radius.full
+26 -28
View File
@@ -7,7 +7,7 @@ use gpui::{
StyleRefinement, Styled as _, Window, canvas, relative, transparent_black,
};
use gpui_native_icons::Icon;
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size};
use gpui_native_theme::{ActiveTheme as _, DISABLED_OPACITY, Size};
use gpui_native_utils::{focus_ring, record_anchor_bounds};
use std::rc::Rc;
@@ -207,10 +207,8 @@ impl RenderOnce for Button {
let theme = cx.theme();
let colors = theme.colors;
let ramps = theme.ramps;
let borders = theme.borders;
let metrics = theme.sizes.resolve(self.size);
let show_border = borders.buttons && self.variant != ButtonVariant::Ghost;
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();
@@ -224,10 +222,10 @@ impl RenderOnce for Button {
ramps.primary.pressed,
),
ButtonVariant::Secondary => (
Some(colors.secondary),
colors.secondary_foreground,
ramps.secondary.hover,
ramps.secondary.pressed,
Some(colors.surface),
colors.foreground,
colors.muted,
colors.muted_hover,
),
ButtonVariant::Ghost => (
None,
@@ -243,17 +241,23 @@ impl RenderOnce for Button {
),
};
let (border, hover_border, pressed_border) = if show_border {
(colors.border, colors.border, colors.border)
} else {
match background {
Some(fill) => (fill, hover, pressed),
None => (
transparent_black(),
transparent_black(),
transparent_black(),
),
}
let (border, hover_border, pressed_border) = match self.variant {
ButtonVariant::Primary => (
ramps.primary.base,
ramps.primary.hover,
ramps.primary.pressed,
),
ButtonVariant::Secondary => (colors.border, colors.border, colors.border),
ButtonVariant::Ghost => (
transparent_black(),
transparent_black(),
transparent_black(),
),
ButtonVariant::Destructive => (
ramps.destructive.base,
ramps.destructive.hover,
ramps.destructive.pressed,
),
};
h_flex()
@@ -267,16 +271,10 @@ impl RenderOnce for Button {
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)
})
.h(metrics.control_height)
.when(is_icon_only, |button| button.w(metrics.control_height))
.when(!is_icon_only && self.padded, |button| {
button
.when(self.icon.is_some(), |button| {
button.px(metrics.dense_padding_x)
})
.when(self.icon.is_none(), |button| button.px(metrics.padding_x))
button.px(metrics.padding_x)
})
.line_height(relative(1.0))
.rounded(theme.radius.control)
@@ -324,7 +322,7 @@ impl RenderOnce for Button {
.absolute()
.inset_0(),
)
.when(self.disabled, |button| button.opacity(0.5))
.when(self.disabled, |button| button.opacity(DISABLED_OPACITY))
.when_some(self.tooltip, |button, tooltip| {
button.tooltip(move |window, cx| tooltip(window, cx))
})
+8 -4
View File
@@ -8,7 +8,7 @@ use gpui::{
SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Toggled, Window,
};
use gpui_native_icons::{IconRole, icon};
use gpui_native_theme::{ActiveTheme as _, Size};
use gpui_native_theme::{ActiveTheme as _, DISABLED_OPACITY, Size};
use gpui_native_utils::focus_ring;
use crate::common::{Sizable, StyledExt as _, h_flex};
@@ -123,7 +123,7 @@ impl RenderOnce for Checkbox {
h_flex()
.id(self.id)
.gap_1p5()
.gap_2()
.text_size(metrics.text_size)
.refine_style(&self.style)
.when_some(self.debug_selector, |checkbox, selector| {
@@ -132,7 +132,7 @@ impl RenderOnce for Checkbox {
.role(Role::CheckBox)
.when_some(a11y_label, |checkbox, label| checkbox.aria_label(label))
.aria_toggled(Toggled::from(self.checked))
.when(self.disabled, |checkbox| checkbox.opacity(0.5))
.when(self.disabled, |checkbox| checkbox.opacity(DISABLED_OPACITY))
.when(!self.disabled, |checkbox| {
checkbox
.cursor_pointer()
@@ -154,7 +154,11 @@ impl RenderOnce for Checkbox {
indicator
.bg(ramps.primary.base)
.border_color(ramps.primary.base)
.hover(|style| style.bg(ramps.primary.hover))
.hover(|style| {
style
.bg(ramps.primary.hover)
.border_color(ramps.primary.hover)
})
.child(
icon(IconRole::Check)
.size(metrics.dense_icon_size)
+7 -11
View File
@@ -17,7 +17,7 @@ use gpui::{
};
use gpui_native_icons::{IconRole, icon};
use gpui_native_theme::ActiveTheme as _;
use gpui_native_theme::{BORDER_WIDTH, POPUP_MAX_HEIGHT, Size};
use gpui_native_theme::{COMBOBOX_MIN_WIDTH, COMBOBOX_WIDTH, POPUP_MAX_HEIGHT, Size};
use gpui_native_utils::ResultLogErr as _;
use crate::common::{OVERLAY_LAYER, Sizable, StyledExt as _, h_flex, v_flex};
@@ -479,16 +479,11 @@ where
let theme = cx.theme();
let metrics = theme.sizes.resolve(self.size);
let panel_border = if theme.borders.overlays {
theme.colors.border
} else {
theme.colors.surface
};
(
metrics,
theme.colors,
theme.radius,
panel_border,
theme.colors.border,
theme.spacing.unit,
theme.elevations.popover.shadows(),
)
@@ -536,8 +531,8 @@ where
.id(id.clone())
.key_context(COMBOBOX_CONTEXT)
.relative()
.w(window.rem_size() * 12.0)
.min_w(window.rem_size() * 6.0)
.w(COMBOBOX_WIDTH)
.min_w(COMBOBOX_MIN_WIDTH)
.refine_style(&self.style)
.role(Role::ComboBox)
.aria_expanded(open)
@@ -587,7 +582,7 @@ where
h_flex()
.absolute()
.right(metrics.dense_padding_x)
.top((metrics.input_height + BORDER_WIDTH * 2.0 - icon_size) / 2.)
.top((metrics.input_height - icon_size) / 2.)
.child(chevron),
)
.when(open, |wrapper| {
@@ -618,6 +613,7 @@ where
.gap_2()
.px_2()
.py_1()
.min_h(metrics.control_height)
.rounded(radius.sm)
.when(highlighted, |row| row.bg(colors.muted))
.when(!highlighted, |row| row.hover(|row| row.bg(colors.muted)))
@@ -686,7 +682,7 @@ where
let popup_width = wrapper_bounds
.borrow()
.map(|bounds| bounds.size.width)
.unwrap_or_else(|| window.rem_size() * 12.0);
.unwrap_or(COMBOBOX_WIDTH);
let panel = v_flex()
.id((id.clone(), "panel"))
+7 -7
View File
@@ -331,18 +331,15 @@ impl Render for CommandPaletteState {
return div().into_any_element();
}
let (colors, radius, metrics, panel_border, shadows) = {
let (colors, radius, metrics, panel_border, shadows, titlebar_height) = {
let theme = cx.theme();
(
theme.colors,
theme.radius,
theme.sizes.resolve(Size::default()),
if theme.borders.overlays {
theme.colors.border
} else {
theme.colors.surface
},
theme.colors.border,
theme.elevations.dialog.shadows(),
theme.titlebar_height,
)
};
@@ -370,6 +367,9 @@ impl Render for CommandPaletteState {
.gap_2()
.px_2()
.py_1()
.min_h(metrics.control_height)
.text_size(metrics.text_size)
.line_height(relative(1.0))
.rounded(radius.sm)
.when(highlighted, |row| row.bg(colors.muted))
.when(!highlighted, |row| row.hover(|row| row.bg(colors.muted)))
@@ -519,7 +519,7 @@ impl Render for CommandPaletteState {
.flex()
.justify_center()
.items_start()
.pt_10()
.pt(titlebar_height)
.child(scrim)
.child(panel),
)
+8
View File
@@ -24,6 +24,14 @@ pub trait Sizable: Sized {
/// Applies the size to the component's fields.
fn apply_size(&mut self, size: Size);
/// Sets `Size::XSmall`.
fn xsmall(self) -> Self
where
Self: Sized,
{
self.with_size(Size::XSmall)
}
/// Sets `Size::Small`.
fn small(self) -> Self
where
+8 -10
View File
@@ -8,7 +8,7 @@ use gpui::{
Styled as _, Window, canvas, deferred, div, ease_out_quint, relative,
};
use gpui_native_icons::IconRole;
use gpui_native_theme::{ActiveTheme as _, TextRole, Typeset as _};
use gpui_native_theme::{ActiveTheme as _, DIALOG_MIN_WIDTH, TextRole, Typeset as _};
use crate::button::{Button, ButtonVariant};
use crate::common::{DIALOG_LAYER, Sizable, StyledExt as _, v_flex};
@@ -160,11 +160,7 @@ impl RenderOnce for Dialog {
let theme = cx.theme();
(
theme.colors.overlay,
if theme.borders.overlays {
theme.colors.border
} else {
theme.colors.surface
},
theme.colors.border,
theme.colors.foreground,
theme.colors.surface,
theme.radius.overlay,
@@ -207,11 +203,13 @@ impl RenderOnce for Dialog {
.role(role)
.aria_label(label)
.when_some(self.min_width, |panel, width| panel.min_w(width))
.when(self.min_width.is_none(), |panel| panel.min_w_80())
.when(self.min_width.is_none(), |panel| {
panel.min_w(DIALOG_MIN_WIDTH)
})
.max_w(relative(0.9))
.when_some(height, |panel, height| panel.h(height))
.p_4()
.gap_3()
.gap_4()
.bg(surface)
.text_color(foreground)
.rounded(radius)
@@ -247,7 +245,7 @@ impl RenderOnce for Dialog {
Button::new("dialog-close")
.icon(IconRole::Close)
.variant(ButtonVariant::Ghost)
.small()
.xsmall()
.debug_selector(|| "dialog-close".to_string())
.accessibility_label("Close")
.on_click({
@@ -278,7 +276,7 @@ impl RenderOnce for Dialog {
})
})
.when_some(actions, |panel, actions| {
panel.child(div().flex().justify_end().gap_2().children(actions))
panel.child(div().flex().justify_end().gap_3().children(actions))
})
.child(
canvas(|_, _, _| {}, {
+30 -21
View File
@@ -11,7 +11,7 @@ use gpui::{
div, relative,
};
use gpui_native_icons::{Icon, IconRole, icon};
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size};
use gpui_native_theme::{ActiveTheme as _, DISABLED_OPACITY, Size};
use gpui_native_utils::{focus_handle, focus_ring};
use crate::button::ButtonVariant;
@@ -80,7 +80,7 @@ impl DropdownMenu {
self
}
/// Shows an icon beside the label.
/// Shows an icon. Omitting the label renders an icon-only trigger.
pub fn icon<I>(mut self, icon: I) -> Self
where
I: Into<Icon>,
@@ -186,7 +186,6 @@ impl RenderOnce for DropdownMenu {
let radius = theme.radius.control;
let ring = colors.ring;
let show_border = theme.borders.buttons && self.variant != ButtonVariant::Ghost;
let (panel_chrome, enter_duration) = MenuChrome::read(cx);
let (background, foreground, hover, pressed) = match self.variant {
@@ -197,10 +196,10 @@ impl RenderOnce for DropdownMenu {
ramps.primary.pressed,
),
ButtonVariant::Secondary => (
Some(colors.secondary),
colors.secondary_foreground,
ramps.secondary.hover,
ramps.secondary.pressed,
Some(colors.surface),
colors.foreground,
colors.muted,
colors.muted_hover,
),
ButtonVariant::Ghost => (
None,
@@ -216,13 +215,15 @@ impl RenderOnce for DropdownMenu {
),
};
let border = if show_border {
colors.border
} else {
background.unwrap_or_else(gpui::transparent_black)
let border = match self.variant {
ButtonVariant::Primary => ramps.primary.base,
ButtonVariant::Secondary => colors.border,
ButtonVariant::Ghost => gpui::transparent_black(),
ButtonVariant::Destructive => ramps.destructive.base,
};
let split = self.on_click.is_some();
let icon_only = self.icon.is_some() && self.label.is_none() && !split;
let inert = self.disabled || self.loading;
let label = self
@@ -262,6 +263,7 @@ impl RenderOnce for DropdownMenu {
})
.justify_center()
.h_full()
.when(icon_only, |zone| zone.w_full())
.gap_2()
.line_height(relative(1.0))
.text_size(metrics.text_size)
@@ -269,20 +271,22 @@ impl RenderOnce for DropdownMenu {
.role(Role::Button)
.when_some(a11y_label, |zone, label| zone.aria_label(label))
.aria_expanded(self.open)
.px(if split && self.icon.is_none() {
metrics.padding_x
.px(if icon_only {
gpui::Pixels::ZERO
} else {
metrics.dense_padding_x
metrics.padding_x
})
.when(split, |zone| zone.rounded_l(radius))
.when(!split, |zone| {
zone.rounded(radius)
.when(self.open, |zone| zone.bg(pressed))
.child(
icon(IconRole::ChevronDown)
.size(metrics.dense_icon_size)
.color(foreground),
)
})
.when(!icon_only && !split, |zone| {
zone.child(
icon(IconRole::ChevronDown)
.size(metrics.dense_icon_size)
.color(foreground),
)
})
.child(
h_flex()
@@ -375,12 +379,17 @@ impl RenderOnce for DropdownMenu {
);
let trigger = h_flex()
.h(metrics.control_height + BORDER_WIDTH * 2.0)
.debug_selector({
let id = self.id.clone();
move || format!("{id}-trigger")
})
.h(metrics.control_height)
.when(icon_only, |trigger| trigger.w(metrics.control_height))
.rounded(radius)
.border_1()
.border_color(border)
.when_some(background, |trigger, background| trigger.bg(background))
.when(self.disabled, |trigger| trigger.opacity(0.5))
.when(self.disabled, |trigger| trigger.opacity(DISABLED_OPACITY))
.when(!inert, |trigger| {
trigger
.track_focus(&focus)
+7 -2
View File
@@ -5,7 +5,7 @@ use gpui::{
AnyElement, App, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce,
Role, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled, Window, div,
};
use gpui_native_theme::ActiveTheme as _;
use gpui_native_theme::{ActiveTheme as _, TextRole, Typeset as _};
use crate::common::{StyledExt, h_flex, v_flex};
@@ -99,7 +99,12 @@ impl RenderOnce for Field {
let id = id.clone();
move || format!("{id}-label")
})
.text_size(metrics.text_size)
.when(self.description.is_some(), |title| {
title.typeset(TextRole::Headline, cx)
})
.when(self.description.is_none(), |title| {
title.typeset(TextRole::Callout, cx)
})
.child(self.label.clone());
let control = h_flex()
+7 -17
View File
@@ -16,10 +16,9 @@ use gpui::{
};
use gpui_native_icons::IconRole;
use gpui_native_theme::{
ActiveTheme as _, BORDER_WIDTH, CARET_WIDTH, ColorTokens, Size, UNDERLINE_WIDTH,
ActiveTheme as _, CARET_WIDTH, ColorTokens, DISABLED_OPACITY, Size, UNDERLINE_WIDTH,
WHEEL_LINE_HEIGHT,
};
use gpui_native_utils::focus_ring;
use crate::button::{Button, ButtonVariant};
use crate::common::{Sizable, StyledExt as _, h_flex};
@@ -1991,7 +1990,7 @@ impl RenderOnce for Input {
let recording = focused && !self.disabled && !self.state.read(cx).is_read_only();
let small = theme.sizes.resolve(Size::Small);
let docked_height = small.control_height + BORDER_WIDTH * 2.0;
let docked_height = small.control_height;
let edge_inset = ((metrics.input_height - docked_height) / 2.0).max(Pixels::ZERO);
let vertical_inset = metrics.dense_padding_x * 0.5;
@@ -2014,7 +2013,7 @@ impl RenderOnce for Input {
let field_height = line_height.map_or(metrics.input_height, |line_height| {
line_height * self.rows as f32 + vertical_inset * 2.0
}) + BORDER_WIDTH * 2.0;
});
let fallback_label = self
.a11y_label
@@ -2148,28 +2147,19 @@ impl RenderOnce for Input {
} else {
metrics.dense_padding_x
})
.gap_1p5()
.gap_2()
.rounded(radius.control)
.bg(colors.input)
.text_size(metrics.text_size)
.text_color(colors.foreground)
.border_1()
.border_color(if theme.borders.fields {
colors.border
} else {
colors.input
})
.border_color(colors.border)
.refine_style(&self.style)
.when(self.disabled, |input| input.opacity(0.5))
.when(self.disabled, |input| input.opacity(DISABLED_OPACITY))
.when(!self.disabled, |input| {
input.track_focus(&focus_handle).tab_stop(true)
})
.when(focused && !theme.borders.focus_ring, |input| {
input.border_color(colors.ring)
})
.when(focused && theme.borders.focus_ring, |input| {
input.shadow(focus_ring(colors.ring))
})
.when(focused, |input| input.border_color(colors.ring))
.when_some(self.prefix, |input, prefix| {
input.child(edge_zone("input-prefix", prefix))
})
+3 -3
View File
@@ -4,7 +4,7 @@ use gpui::{
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _,
Window,
};
use gpui_native_theme::{ActiveTheme as _, Size};
use gpui_native_theme::{ActiveTheme as _, DISABLED_OPACITY, Size};
use gpui_native_utils::focus_ring;
use crate::common::{Sizable, StyledExt as _, h_flex};
@@ -113,12 +113,12 @@ impl RenderOnce for Link {
h_flex()
.id(self.id)
.text_size(metrics.text_size)
.text_color(colors.accent)
.text_color(ramps.primary.base)
.underline()
.refine_style(&self.style)
.role(Role::Link)
.when_some(a11y_label, |link, label| link.aria_label(label))
.when(self.disabled, |link| link.opacity(0.5))
.when(self.disabled, |link| link.opacity(DISABLED_OPACITY))
.when(!self.disabled, |link| {
link.cursor_pointer()
.focusable()
+6 -15
View File
@@ -12,7 +12,7 @@ use gpui::{
Window, anchored, canvas, deferred, div, ease_out_quint, point, px, relative,
};
use gpui_native_icons::{Icon, IconRole, icon as icon_glyph};
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, POPUP_MAX_HEIGHT};
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, MENU_MIN_WIDTH, POPUP_MAX_HEIGHT};
use gpui_native_utils::focus_handle;
use crate::common::{OVERLAY_LAYER, StyledExt as _, v_flex};
@@ -171,7 +171,6 @@ impl FluentBuilder for MenuItem {}
pub(crate) struct MenuChrome {
surface: gpui::Hsla,
border: gpui::Hsla,
panel_border: gpui::Hsla,
foreground: gpui::Hsla,
muted_foreground: gpui::Hsla,
muted_hover: gpui::Hsla,
@@ -214,11 +213,6 @@ impl MenuChrome {
Self {
surface: theme.colors.surface,
border: theme.colors.border,
panel_border: if theme.borders.overlays {
theme.colors.border
} else {
theme.colors.surface
},
foreground: theme.colors.foreground,
muted_foreground: theme.colors.muted_foreground,
muted_hover: theme.colors.muted_hover,
@@ -691,18 +685,15 @@ fn menu_panel(
.role(Role::Menu)
.aria_label(params.label)
.when_some(params.min_width, |panel, width| panel.min_w(width))
.when(params.min_width.is_none(), |panel| panel.min_w_56())
.when(params.min_width.is_none(), |panel| {
panel.min_w(MENU_MIN_WIDTH)
})
.p(params.chrome.spacing_unit)
.bg(params.chrome.surface)
.text_color(params.chrome.foreground)
.rounded(params.chrome.radius_overlay)
.border_1()
// A flyout overlaps its parent, so it always wears a visible border.
.border_color(if params.is_flyout {
params.chrome.border
} else {
params.chrome.panel_border
})
.border_color(params.chrome.border)
.shadow(params.chrome.shadow.clone())
.overflow_hidden()
.on_mouse_down_out({
@@ -963,7 +954,7 @@ fn row_shell(
.items_center()
.gap_2()
.min_h(chrome.menu_item_height)
.px(chrome.spacing_unit * 2.0)
.px(chrome.spacing_unit * 4.0)
.text_size(chrome.text_size)
.line_height(relative(1.0))
.rounded(chrome.radius_sm)
+3 -7
View File
@@ -8,7 +8,7 @@ use gpui::{
ParentElement as _, Pixels, RenderOnce, Role, SharedString, StatefulInteractiveElement as _,
StyleRefinement, Styled as _, Window, anchored, canvas, deferred, div, ease_out_quint, point,
};
use gpui_native_theme::ActiveTheme as _;
use gpui_native_theme::{ActiveTheme as _, POPOVER_MIN_WIDTH};
use crate::common::{OVERLAY_LAYER, StyledExt as _, v_flex};
@@ -107,11 +107,7 @@ impl RenderOnce for Popover {
theme.radius,
theme.elevations.popover.shadows(),
theme.motion.overlay,
if theme.borders.overlays {
theme.colors.border
} else {
theme.colors.surface
},
theme.colors.border,
theme.spacing.unit,
)
};
@@ -185,7 +181,7 @@ impl RenderOnce for Popover {
.track_focus(&focus)
.role(Role::Dialog)
.aria_label(label)
.min_w_32()
.min_w(POPOVER_MIN_WIDTH)
.bg(colors.surface)
.text_color(colors.surface_foreground)
.rounded(radius.overlay)
+8 -4
View File
@@ -4,7 +4,7 @@ use gpui::{
SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Toggled, Window,
div,
};
use gpui_native_theme::{ActiveTheme as _, Size};
use gpui_native_theme::{ActiveTheme as _, DISABLED_OPACITY, Size};
use gpui_native_utils::focus_ring;
use crate::common::{Sizable, StyledExt as _, h_flex, v_flex};
@@ -189,11 +189,11 @@ where
h_flex()
.id(radio.id)
.gap_1p5()
.gap_2()
.role(Role::RadioButton)
.when_some(a11y_label, |row, label| row.aria_label(label))
.aria_toggled(Toggled::from(is_selected))
.when(disabled, |row| row.opacity(0.5))
.when(disabled, |row| row.opacity(DISABLED_OPACITY))
.when(!disabled, move |row| {
let value = radio.value.clone();
row.cursor_pointer()
@@ -217,7 +217,11 @@ where
indicator
.bg(ramps.primary.base)
.border_color(ramps.primary.base)
.hover(|style| style.bg(ramps.primary.hover))
.hover(|style| {
style
.bg(ramps.primary.hover)
.border_color(ramps.primary.hover)
})
.child(
div()
.size(metrics.indicator_size / 2.0)
+2 -2
View File
@@ -164,7 +164,7 @@ where
let track_height = metrics.strip_height;
let track_inset = metrics.plate_inset;
let horizontal_padding = metrics.dense_padding_x;
let text_size = metrics.caption_size;
let text_size = metrics.text_size;
let icon_size = metrics.icon_size;
let selected_index = self
@@ -218,7 +218,7 @@ where
h_flex()
.id((ElementId::from("segment"), segment.label.clone()))
.gap_1p5()
.gap_2()
.justify_center()
.cursor_pointer()
.h_full()
+9 -23
View File
@@ -7,8 +7,8 @@ use gpui::{
Window, canvas,
};
use gpui_native_icons::{Icon, IconRole, icon};
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size};
use gpui_native_utils::{focus_handle, focus_ring};
use gpui_native_theme::{ActiveTheme as _, Size};
use gpui_native_utils::focus_handle;
use crate::common::{Sizable, StyledExt, h_flex};
use crate::menu::{Menu, MenuItem};
@@ -182,8 +182,6 @@ where
let muted_foreground = theme.colors.muted_foreground;
let radius = theme.radius.control;
let icon_size = metrics.dense_icon_size;
let fields_bordered = theme.borders.fields;
let focus_ring_on = theme.borders.focus_ring;
let label = self
.a11y_label
@@ -209,18 +207,8 @@ where
let is_open = *open.read(cx);
let open_for_items = open.clone();
let (rest_border, hover_border) = if fields_bordered {
(
if is_open && !focus_ring_on {
ring
} else {
border
},
border,
)
} else {
(input, muted)
};
let rest_border = if is_open { ring } else { border };
let hover_border = rest_border;
let selected_index = self.selected.as_ref().and_then(|value| {
self.options
@@ -252,7 +240,7 @@ where
.aria_expanded(is_open)
.track_focus(&focus)
.tab_stop(true)
.focus_visible(move |style| style.shadow(focus_ring(ring)))
.focus_visible(move |style| style.border_color(ring))
.on_key_down({
let open = open.clone();
move |event: &KeyDownEvent, _window, cx| {
@@ -266,10 +254,11 @@ where
})
.cursor_pointer()
.justify_between()
.gap_2()
.gap_3()
.w_full()
.h(metrics.input_height + BORDER_WIDTH * 2.0)
.px(metrics.dense_padding_x)
.h(metrics.input_height)
.pl(metrics.dense_padding_x)
.pr(metrics.plate_inset * 2.0)
.rounded(radius)
.border_1()
.border_color(rest_border)
@@ -280,9 +269,6 @@ where
} else {
muted_foreground
})
.when(is_open && focus_ring_on, |trigger| {
trigger.shadow(focus_ring(ring))
})
.hover(|style| style.bg(muted).border_color(hover_border))
.child(trigger_text.clone())
.child(
+4 -4
View File
@@ -8,7 +8,7 @@ use gpui::{
SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Window, canvas,
div, relative,
};
use gpui_native_theme::{ActiveTheme as _, Size};
use gpui_native_theme::{ActiveTheme as _, DISABLED_OPACITY, Size};
use gpui_native_utils::focus_ring;
use crate::common::{Sizable, StyledExt as _, h_flex};
@@ -133,9 +133,9 @@ impl RenderOnce for Slider {
let radius = theme.radius;
let metrics = theme.sizes.resolve(self.size);
let row_height = metrics.icon_size;
let row_height = metrics.icon_size * 2.5;
let track_height = metrics.icon_size / 4.0;
let knob = metrics.icon_size * 1.25;
let knob = metrics.indicator_size;
let value = self.value.clamp(self.min, self.max);
let fraction = if self.max > self.min {
@@ -186,7 +186,7 @@ impl RenderOnce for Slider {
.aria_numeric_value(f64::from(value))
.aria_min_numeric_value(f64::from(self.min))
.aria_max_numeric_value(f64::from(self.max))
.when(self.disabled, |slider| slider.opacity(0.5))
.when(self.disabled, |slider| slider.opacity(DISABLED_OPACITY))
.when(!self.disabled, |slider| {
slider
.track_focus(&focus_handle)
+4 -4
View File
@@ -19,7 +19,7 @@ use gpui::{
StyleRefinement, Styled, Subscription, Task, WeakEntity, Window, div, relative,
};
use gpui_native_icons::{IconRole, icon};
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size};
use gpui_native_theme::{ActiveTheme as _, DISABLED_OPACITY, SPEECH_WAVEFORM_WIDTH, Size};
use gpui_native_utils::focus_ring;
use crate::common::{Sizable, StyledExt as _, h_flex};
@@ -743,14 +743,14 @@ impl RenderOnce for SpeechButton {
.group(group.clone())
.role(Role::Button)
.justify_center()
.h(metrics.control_height + BORDER_WIDTH * 2.0)
.h(metrics.control_height)
.px(metrics.dense_padding_x)
.gap_0p5()
.rounded(theme.radius.control)
.text_color(foreground)
.when(capturing, |button| button.bg(colors.muted))
.refine_style(&self.style)
.when(disabled, |button| button.opacity(0.5))
.when(disabled, |button| button.opacity(DISABLED_OPACITY))
.aria_label(label.clone())
.tooltip(Tooltip::text(label))
.child(if stopping {
@@ -877,7 +877,7 @@ impl RenderOnce for SpeechWaveform {
.justify_end()
.gap_0p5()
.h(metrics.icon_size)
.w_24()
.w(SPEECH_WAVEFORM_WIDTH)
.overflow_hidden()
.children(levels.iter().map(|level| {
div()
+16 -7
View File
@@ -6,7 +6,7 @@ use gpui::{
RenderOnce, Role, SharedString, SpringAnimation, StatefulInteractiveElement as _,
StyleRefinement, Styled as _, Toggled, Window, div, px,
};
use gpui_native_theme::{ActiveTheme as _, CONTROL_SPRING, Size};
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, CONTROL_SPRING, DISABLED_OPACITY, Size};
use gpui_native_utils::focus_ring;
use crate::common::{Sizable, StyledExt as _, h_flex};
@@ -109,8 +109,9 @@ impl RenderOnce for Switch {
let track_width = metrics.switch_width;
let knob = metrics.switch_knob;
let inset = (track_height - knob) / 2.;
let travel = track_width - knob - inset * 2.;
// Absolute children sit inside the track's border, so remove it from the inset.
let inset = (track_height - knob) / 2. - BORDER_WIDTH;
let travel = track_width - BORDER_WIDTH * 2. - knob - inset * 2.;
let next = !self.checked;
let on_change = self.on_change;
@@ -122,13 +123,13 @@ impl RenderOnce for Switch {
h_flex()
.id(self.id)
.gap_1p5()
.gap_2()
.text_size(metrics.text_size)
.refine_style(&self.style)
.role(Role::Switch)
.when_some(a11y_label, |switch, label| switch.aria_label(label))
.aria_toggled(Toggled::from(self.checked))
.when(self.disabled, |switch| switch.opacity(0.5))
.when(self.disabled, |switch| switch.opacity(DISABLED_OPACITY))
.when(!self.disabled, |switch| {
switch
.cursor_pointer()
@@ -147,12 +148,20 @@ impl RenderOnce for Switch {
.w(track_width)
.h(track_height)
.rounded(theme.radius.full)
.border_1()
.when(self.checked, |track| {
track
.bg(ramps.primary.base)
.hover(|style| style.bg(ramps.primary.hover))
.border_color(ramps.primary.base)
.hover(|style| {
style
.bg(ramps.primary.hover)
.border_color(ramps.primary.hover)
})
})
.when(!self.checked, |track| {
track.bg(colors.input).border_color(colors.border)
})
.when(!self.checked, |track| track.bg(colors.border))
.child(
div()
.absolute()
+7 -10
View File
@@ -274,16 +274,12 @@ impl ToastEntry {
fn pill(&self, toasts: &Entity<Toasts>, cx: &App) -> gpui::Stateful<gpui::Div> {
let theme = cx.theme();
let surface = theme.colors.surface;
let border = if theme.borders.overlays {
theme.colors.border
} else {
surface
};
let border = theme.colors.border;
let foreground = theme.colors.foreground;
let radius = theme.radius.full;
let shadows = theme.elevations.popover.shadows();
let icon_size = theme.sizes.small.icon_size;
let text_size = theme.sizes.small.text_size;
let icon_size = theme.sizes.medium.icon_size;
let text_size = theme.sizes.medium.text_size;
let toast = &self.toast;
let toasts = toasts.clone();
let id = self.id;
@@ -296,6 +292,7 @@ impl ToastEntry {
.max_w(TOAST_MAX_WIDTH)
.flex()
.items_center()
.gap_2()
.py_1p5()
.px_3()
.bg(surface)
@@ -308,7 +305,6 @@ impl ToastEntry {
pill.child(
div()
.flex_none()
.mr_2()
.flex()
.items_center()
.child(icon.size(icon_size).color(foreground)),
@@ -316,6 +312,7 @@ impl ToastEntry {
})
.child(
div()
.flex_1()
.min_w(px(0.))
.truncate()
.text_size(text_size)
@@ -325,7 +322,7 @@ impl ToastEntry {
.when_some(toast.action.clone(), |pill, action| {
let toasts = toasts.clone();
pill.child(
div().flex_none().ml_2p5().child(
div().flex_none().child(
Button::new(("toast-action", id))
.label(action.label.clone())
.variant(ButtonVariant::Primary)
@@ -340,7 +337,7 @@ impl ToastEntry {
)
})
.child(
div().flex_none().ml_4().child(
div().flex_none().child(
Button::new(("toast-close", id))
.icon(IconRole::Close)
.variant(ButtonVariant::Ghost)
+27 -4
View File
@@ -3,7 +3,8 @@ use gpui::{
App, ElementId, InteractiveElement as _, IntoElement, ParentElement as _, RenderOnce, Role,
SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Toggled, Window,
};
use gpui_native_theme::{ActiveTheme as _, Size};
use gpui_native_icons::Icon;
use gpui_native_theme::{ActiveTheme as _, DISABLED_OPACITY, Size};
use gpui_native_utils::focus_ring;
use crate::common::{Sizable, StyledExt as _, h_flex};
@@ -17,6 +18,7 @@ type ChangeHandler = Box<dyn Fn(bool, &mut Window, &mut App)>;
pub struct Toggle {
id: ElementId,
label: Option<SharedString>,
icon: Option<Icon>,
pressed: bool,
size: Size,
disabled: bool,
@@ -34,6 +36,7 @@ impl Toggle {
Self {
id: id.into(),
label: None,
icon: None,
pressed: false,
size: Size::default(),
disabled: false,
@@ -52,6 +55,15 @@ impl Toggle {
self
}
/// Shows an icon. Omitting the label renders an icon-only toggle.
pub fn icon<I>(mut self, icon: I) -> Self
where
I: Into<Icon>,
{
self.icon = Some(icon.into());
self
}
/// Sets the current pressed state; the toggle never owns it.
pub fn pressed(mut self, pressed: bool) -> Self {
self.pressed = pressed;
@@ -104,12 +116,16 @@ impl RenderOnce for Toggle {
let on_change = self.on_change;
let next = !self.pressed;
let a11y_label = self.a11y_label.or_else(|| self.label.clone());
let icon_only = self.icon.is_some() && self.label.is_none();
h_flex()
.id(self.id)
.justify_center()
.h(metrics.control_height)
.px(metrics.dense_padding_x)
.when(icon_only, |toggle| {
toggle.w(metrics.control_height).px(gpui::Pixels::ZERO)
})
.when(!icon_only, |toggle| toggle.px(metrics.dense_padding_x))
.rounded(theme.radius.control)
.text_size(metrics.text_size)
.text_color(colors.foreground)
@@ -127,7 +143,7 @@ impl RenderOnce for Toggle {
.active(|style| style.bg(ramps.secondary.hover))
})
.refine_style(&self.style)
.when(self.disabled, |toggle| toggle.opacity(0.5))
.when(self.disabled, |toggle| toggle.opacity(DISABLED_OPACITY))
.when(!self.disabled, |toggle| {
toggle
.cursor_pointer()
@@ -138,6 +154,13 @@ impl RenderOnce for Toggle {
toggle.on_click(move |_, window, cx| on_change(next, window, cx))
})
})
.when_some(self.label, |toggle, label| toggle.child(label))
.child(
h_flex()
.gap_2()
.when_some(self.icon, |content, icon| {
content.child(icon.size(metrics.icon_size))
})
.when_some(self.label, |content, label| content.child(label)),
)
}
}
+5 -7
View File
@@ -64,18 +64,15 @@ impl Render for Tooltip {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
// Copied out: the mutable reads below need `cx` free of this borrow.
let theme = cx.theme();
let text_size = theme.sizes.small.text_size;
let text_size = theme.sizes.medium.text_size;
let line_height = theme.typography.callout.line_height;
let spacing_unit = theme.spacing.unit;
let surface = theme.colors.surface;
let surface_foreground = theme.colors.surface_foreground;
let radius_sm = theme.radius.sm;
let shadows = theme.elevations.popover.shadows();
let border = if theme.borders.overlays {
theme.colors.border
} else {
theme.colors.surface
};
let border = theme.colors.border;
let build_panel = || {
div()
@@ -91,6 +88,7 @@ impl Render for Tooltip {
.rounded(radius_sm)
.shadow(shadows.clone())
.text_size(text_size)
.line_height(line_height)
.text_color(surface_foreground)
.child(self.text.clone())
};
@@ -106,7 +104,7 @@ impl Render for Tooltip {
cx,
)
.map(|bounds| bounds.size.height)
.unwrap_or_else(|| text_size * 1.5 + window.rem_size() * 0.5 + BORDER_WIDTH * 2.0);
.unwrap_or_else(|| line_height + spacing_unit * 2.0 + BORDER_WIDTH * 2.0);
let below = viewport_height - spacing_unit - parent.bottom();
let above = parent.top() - spacing_unit;
+61
View File
@@ -0,0 +1,61 @@
#![cfg(all(feature = "button", feature = "dropdown_menu"))]
use gpui::{
Context, IntoElement, ParentElement as _, Render, Styled as _, TestAppContext,
VisualTestContext, Window, div, px, size,
};
use gpui_native_core::button::Button;
use gpui_native_core::common::Sizable as _;
use gpui_native_core::dropdown_menu::DropdownMenu;
use gpui_native_core::menu::MenuItem;
use gpui_native_icons::IconRole;
use gpui_native_theme::Theme;
struct Harness;
impl Render for Harness {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div()
.size_full()
.flex()
.flex_col()
.gap_2()
.child(
Button::new("height-probe")
.label("Probe")
.debug_selector(|| "height-probe".to_string()),
)
.child(
Button::new("xsmall-height-probe")
.label("Probe")
.xsmall()
.debug_selector(|| "xsmall-height-probe".to_string()),
)
.child(
DropdownMenu::new("icon-only-dropdown")
.icon(IconRole::More)
.accessibility_label("More actions")
.items(|_, _| vec![MenuItem::new("One")]),
)
}
}
#[gpui::test]
fn buttons_render_at_their_token_heights(cx: &mut TestAppContext) {
cx.update(Theme::init);
let window = cx.open_window(size(px(400.), px(400.)), |_, _| Harness);
let mut cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
let medium = cx.debug_bounds("height-probe").expect("button laid out");
let xsmall = cx
.debug_bounds("xsmall-height-probe")
.expect("xsmall button laid out");
let icon_only = cx
.debug_bounds("icon-only-dropdown-trigger")
.expect("icon-only dropdown laid out");
assert_eq!(medium.size.height, px(32.0));
assert_eq!(xsmall.size.height, px(20.0));
assert_eq!(icon_only.size, size(px(32.0), px(32.0)));
}