Add xsmall controls for close affordances
- Add the xsmall size class and Sizable::xsmall(). - Use xsmall close buttons in tabs, dialogs, and alerts. - Assert xsmall and medium button heights.
This commit is contained in:
@@ -183,7 +183,7 @@ impl RenderOnce for Alert {
|
|||||||
Button::new((self.id.clone(), "alert-close"))
|
Button::new((self.id.clone(), "alert-close"))
|
||||||
.icon(IconRole::Close)
|
.icon(IconRole::Close)
|
||||||
.variant(ButtonVariant::Ghost)
|
.variant(ButtonVariant::Ghost)
|
||||||
.small()
|
.xsmall()
|
||||||
.accessibility_label("Dismiss")
|
.accessibility_label("Dismiss")
|
||||||
.on_click(move |_, _window, cx| on_dismiss(cx))
|
.on_click(move |_, _window, cx| on_dismiss(cx))
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -24,6 +24,14 @@ pub trait Sizable: Sized {
|
|||||||
/// Applies the size to the component's fields.
|
/// Applies the size to the component's fields.
|
||||||
fn apply_size(&mut self, size: Size);
|
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`.
|
/// Sets `Size::Small`.
|
||||||
fn small(self) -> Self
|
fn small(self) -> Self
|
||||||
where
|
where
|
||||||
|
|||||||
@@ -245,7 +245,7 @@ impl RenderOnce for Dialog {
|
|||||||
Button::new("dialog-close")
|
Button::new("dialog-close")
|
||||||
.icon(IconRole::Close)
|
.icon(IconRole::Close)
|
||||||
.variant(ButtonVariant::Ghost)
|
.variant(ButtonVariant::Ghost)
|
||||||
.small()
|
.xsmall()
|
||||||
.debug_selector(|| "dialog-close".to_string())
|
.debug_selector(|| "dialog-close".to_string())
|
||||||
.accessibility_label("Close")
|
.accessibility_label("Close")
|
||||||
.on_click({
|
.on_click({
|
||||||
|
|||||||
@@ -5,28 +5,44 @@ use gpui::{
|
|||||||
VisualTestContext, Window, div, px, size,
|
VisualTestContext, Window, div, px, size,
|
||||||
};
|
};
|
||||||
use gpui_native_core::button::Button;
|
use gpui_native_core::button::Button;
|
||||||
|
use gpui_native_core::common::Sizable as _;
|
||||||
use gpui_native_theme::Theme;
|
use gpui_native_theme::Theme;
|
||||||
|
|
||||||
struct Harness;
|
struct Harness;
|
||||||
|
|
||||||
impl Render for Harness {
|
impl Render for Harness {
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||||
div().size_full().child(
|
div()
|
||||||
Button::new("height-probe")
|
.size_full()
|
||||||
.label("Probe")
|
.flex()
|
||||||
.debug_selector(|| "height-probe".to_string()),
|
.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()),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
fn medium_button_renders_at_the_token_height(cx: &mut TestAppContext) {
|
fn buttons_render_at_their_token_heights(cx: &mut TestAppContext) {
|
||||||
cx.update(Theme::init);
|
cx.update(Theme::init);
|
||||||
let window = cx.open_window(size(px(400.), px(400.)), |_, _| Harness);
|
let window = cx.open_window(size(px(400.), px(400.)), |_, _| Harness);
|
||||||
let mut cx = VisualTestContext::from_window(window.into(), cx);
|
let mut cx = VisualTestContext::from_window(window.into(), cx);
|
||||||
cx.run_until_parked();
|
cx.run_until_parked();
|
||||||
|
|
||||||
let bounds = cx.debug_bounds("height-probe").expect("button laid out");
|
let medium = cx.debug_bounds("height-probe").expect("button laid out");
|
||||||
|
let xsmall = cx
|
||||||
|
.debug_bounds("xsmall-height-probe")
|
||||||
|
.expect("xsmall button laid out");
|
||||||
|
|
||||||
assert_eq!(bounds.size.height, px(32.0));
|
assert_eq!(medium.size.height, px(32.0));
|
||||||
|
assert_eq!(xsmall.size.height, px(24.0));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1201,7 +1201,7 @@ impl Tab {
|
|||||||
Button::new((self.key.clone(), "tab-close"))
|
Button::new((self.key.clone(), "tab-close"))
|
||||||
.icon(IconRole::Close)
|
.icon(IconRole::Close)
|
||||||
.variant(ButtonVariant::Ghost)
|
.variant(ButtonVariant::Ghost)
|
||||||
.small()
|
.xsmall()
|
||||||
.tab_stop(false)
|
.tab_stop(false)
|
||||||
.debug_selector({
|
.debug_selector({
|
||||||
let tabs_id = tabs_id.clone();
|
let tabs_id = tabs_id.clone();
|
||||||
|
|||||||
@@ -169,6 +169,7 @@ impl ThemeVariant {
|
|||||||
/// A named control size class.
|
/// A named control size class.
|
||||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||||
pub enum Size {
|
pub enum Size {
|
||||||
|
XSmall,
|
||||||
Small,
|
Small,
|
||||||
/// The default.
|
/// The default.
|
||||||
#[default]
|
#[default]
|
||||||
@@ -496,6 +497,7 @@ pub struct ControlMetrics {
|
|||||||
/// The control metrics for every size class.
|
/// The control metrics for every size class.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
pub struct SizeTable {
|
pub struct SizeTable {
|
||||||
|
pub xsmall: ControlMetrics,
|
||||||
pub small: ControlMetrics,
|
pub small: ControlMetrics,
|
||||||
pub medium: ControlMetrics,
|
pub medium: ControlMetrics,
|
||||||
pub large: ControlMetrics,
|
pub large: ControlMetrics,
|
||||||
@@ -522,6 +524,25 @@ impl SizeTable {
|
|||||||
switch_knob: f32,
|
switch_knob: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let xsmall = ClassFactors {
|
||||||
|
control_height: 1.5,
|
||||||
|
input_height: 1.5,
|
||||||
|
padding_x: 0.375,
|
||||||
|
text_size: 0.75,
|
||||||
|
icon_size: 0.75,
|
||||||
|
dense_icon_size: 0.625,
|
||||||
|
indicator_size: 0.75,
|
||||||
|
avatar_size: 1.25,
|
||||||
|
caption_size: 0.625,
|
||||||
|
dense_padding_x: 0.375,
|
||||||
|
plate_inset: 0.0625,
|
||||||
|
strip_height: 1.5,
|
||||||
|
menu_item_height: 1.5,
|
||||||
|
switch_height: 1.0,
|
||||||
|
switch_width: 1.75,
|
||||||
|
switch_knob: 0.625,
|
||||||
|
};
|
||||||
|
|
||||||
let small = ClassFactors {
|
let small = ClassFactors {
|
||||||
control_height: 1.75,
|
control_height: 1.75,
|
||||||
input_height: 1.75,
|
input_height: 1.75,
|
||||||
@@ -599,6 +620,7 @@ impl SizeTable {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
xsmall: metrics(&xsmall),
|
||||||
small: metrics(&small),
|
small: metrics(&small),
|
||||||
medium: metrics(&medium),
|
medium: metrics(&medium),
|
||||||
large: metrics(&large),
|
large: metrics(&large),
|
||||||
@@ -608,6 +630,7 @@ impl SizeTable {
|
|||||||
/// The metrics for `size`.
|
/// The metrics for `size`.
|
||||||
pub fn resolve(&self, size: Size) -> ControlMetrics {
|
pub fn resolve(&self, size: Size) -> ControlMetrics {
|
||||||
match size {
|
match size {
|
||||||
|
Size::XSmall => self.xsmall,
|
||||||
Size::Small => self.small,
|
Size::Small => self.small,
|
||||||
Size::Medium => self.medium,
|
Size::Medium => self.medium,
|
||||||
Size::Large => self.large,
|
Size::Large => self.large,
|
||||||
|
|||||||
@@ -293,6 +293,19 @@ mod tests {
|
|||||||
assert_eq!(resolved.sizes.medium.control_height, px(32.));
|
assert_eq!(resolved.sizes.medium.control_height, px(32.));
|
||||||
assert_eq!(resolved.sizes.medium.input_height, px(32.));
|
assert_eq!(resolved.sizes.medium.input_height, px(32.));
|
||||||
assert_eq!(resolved.sizes.medium.text_size, px(14.));
|
assert_eq!(resolved.sizes.medium.text_size, px(14.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.control_height, px(24.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.input_height, px(24.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.text_size, px(12.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.icon_size, px(12.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.indicator_size, px(12.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.avatar_size, px(20.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.caption_size, px(10.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.dense_padding_x, px(6.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.plate_inset, px(1.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.menu_item_height, px(24.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.switch_height, px(16.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.switch_width, px(28.));
|
||||||
|
assert_eq!(resolved.sizes.xsmall.switch_knob, px(10.));
|
||||||
assert_eq!(resolved.sizes.small.control_height, px(28.));
|
assert_eq!(resolved.sizes.small.control_height, px(28.));
|
||||||
assert_eq!(resolved.sizes.small.text_size, px(12.));
|
assert_eq!(resolved.sizes.small.text_size, px(12.));
|
||||||
assert_eq!(resolved.sizes.small.avatar_size, px(24.));
|
assert_eq!(resolved.sizes.small.avatar_size, px(24.));
|
||||||
|
|||||||
Reference in New Issue
Block a user