feat: unify theme system (#7)
Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
@@ -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)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user