Add icon-only and icon-label toggles
CI / check (pull_request) Waiting to run

- Add an icon builder to Toggle.
- Render a square icon-only toggle when no label is set.
- Render icon and label together otherwise.
- Demonstrate both in the gallery.
This commit is contained in:
2026-11-16 19:48:56 +07:00
parent 40beda76dc
commit 7c1827b41d
2 changed files with 70 additions and 8 deletions
+25 -2
View File
@@ -3,6 +3,7 @@ use gpui::{
App, ElementId, InteractiveElement as _, IntoElement, ParentElement as _, RenderOnce, Role, App, ElementId, InteractiveElement as _, IntoElement, ParentElement as _, RenderOnce, Role,
SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Toggled, Window, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Toggled, Window,
}; };
use gpui_native_icons::Icon;
use gpui_native_theme::{ActiveTheme as _, DISABLED_OPACITY, Size}; use gpui_native_theme::{ActiveTheme as _, DISABLED_OPACITY, Size};
use gpui_native_utils::focus_ring; use gpui_native_utils::focus_ring;
@@ -17,6 +18,7 @@ type ChangeHandler = Box<dyn Fn(bool, &mut Window, &mut App)>;
pub struct Toggle { pub struct Toggle {
id: ElementId, id: ElementId,
label: Option<SharedString>, label: Option<SharedString>,
icon: Option<Icon>,
pressed: bool, pressed: bool,
size: Size, size: Size,
disabled: bool, disabled: bool,
@@ -34,6 +36,7 @@ impl Toggle {
Self { Self {
id: id.into(), id: id.into(),
label: None, label: None,
icon: None,
pressed: false, pressed: false,
size: Size::default(), size: Size::default(),
disabled: false, disabled: false,
@@ -52,6 +55,15 @@ impl Toggle {
self 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. /// Sets the current pressed state; the toggle never owns it.
pub fn pressed(mut self, pressed: bool) -> Self { pub fn pressed(mut self, pressed: bool) -> Self {
self.pressed = pressed; self.pressed = pressed;
@@ -104,12 +116,16 @@ impl RenderOnce for Toggle {
let on_change = self.on_change; let on_change = self.on_change;
let next = !self.pressed; let next = !self.pressed;
let a11y_label = self.a11y_label.or_else(|| self.label.clone()); let a11y_label = self.a11y_label.or_else(|| self.label.clone());
let icon_only = self.icon.is_some() && self.label.is_none();
h_flex() h_flex()
.id(self.id) .id(self.id)
.justify_center() .justify_center()
.h(metrics.control_height) .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) .rounded(theme.radius.control)
.text_size(metrics.text_size) .text_size(metrics.text_size)
.text_color(colors.foreground) .text_color(colors.foreground)
@@ -138,6 +154,13 @@ impl RenderOnce for Toggle {
toggle.on_click(move |_, window, cx| on_change(next, window, cx)) 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)),
)
} }
} }
+41 -2
View File
@@ -500,7 +500,11 @@ impl Gallery {
plan: SharedString::from("starter"), plan: SharedString::from("starter"),
})), })),
"switch" => Arc::new(cx.new(|_| SwitchDemo { checked: true })), "switch" => Arc::new(cx.new(|_| SwitchDemo { checked: true })),
"toggle" => Arc::new(cx.new(|_| ToggleDemo { pressed: false })), "toggle" => Arc::new(cx.new(|_| ToggleDemo {
pressed: false,
icon_pressed: false,
icon_label_pressed: false,
})),
"slider" => Arc::new(cx.new(|_| SliderDemo { volume: 40.0 })), "slider" => Arc::new(cx.new(|_| SliderDemo { volume: 40.0 })),
"segmented" => Arc::new(cx.new(|_| SegmentedDemo { "segmented" => Arc::new(cx.new(|_| SegmentedDemo {
filter: ExploreFilter::default(), filter: ExploreFilter::default(),
@@ -1585,6 +1589,8 @@ impl Render for SwitchDemo {
struct ToggleDemo { struct ToggleDemo {
pressed: bool, pressed: bool,
icon_pressed: bool,
icon_label_pressed: bool,
} }
impl Panel for ToggleDemo { impl Panel for ToggleDemo {
@@ -1609,11 +1615,14 @@ impl Render for ToggleDemo {
Toggle::new("toggle-demo-bold") Toggle::new("toggle-demo-bold")
.label("Bold") .label("Bold")
.pressed(self.pressed) .pressed(self.pressed)
.on_change(move |pressed, _, cx| { .on_change({
let view = view.clone();
move |pressed, _, cx| {
view.update(cx, |demo, cx| { view.update(cx, |demo, cx| {
demo.pressed = pressed; demo.pressed = pressed;
cx.notify(); cx.notify();
}); });
}
}), }),
) )
.child( .child(
@@ -1621,6 +1630,36 @@ impl Render for ToggleDemo {
.label("Disabled") .label("Disabled")
.pressed(true) .pressed(true)
.disabled(true), .disabled(true),
)
.child(
Toggle::new("toggle-demo-icon")
.icon(IconRole::Star)
.accessibility_label("Favorite")
.pressed(self.icon_pressed)
.on_change({
let view = view.clone();
move |pressed, _, cx| {
view.update(cx, |demo, cx| {
demo.icon_pressed = pressed;
cx.notify();
});
}
}),
)
.child(
Toggle::new("toggle-demo-icon-label")
.icon(IconRole::Star)
.label("Favorite")
.pressed(self.icon_label_pressed)
.on_change({
let view = view.clone();
move |pressed, _, cx| {
view.update(cx, |demo, cx| {
demo.icon_label_pressed = pressed;
cx.notify();
});
}
}),
), ),
)) ))
} }