utils: add shared StyledExt styling helper

This commit is contained in:
2026-11-16 14:02:33 +07:00
parent d7e4047070
commit 745a01d7af
29 changed files with 332 additions and 68 deletions
+11 -2
View File
@@ -1,12 +1,12 @@
use gpui::prelude::FluentBuilder as _;
use gpui::{
App, ElementId, InteractiveElement as _, IntoElement, ParentElement as _, RenderOnce, Role,
SharedString, StatefulInteractiveElement as _, Styled as _, Toggled, Window,
SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Toggled, Window,
};
use gpui_native_theme::{ActiveTheme as _, Size};
use gpui_native_utils::focus_ring;
use crate::common::{Sizable, h_flex};
use crate::common::{Sizable, StyledExt as _, h_flex};
/// The change handler, fired with the next pressed state.
type ChangeHandler = Box<dyn Fn(bool, &mut Window, &mut App)>;
@@ -22,6 +22,7 @@ pub struct Toggle {
disabled: bool,
a11y_label: Option<SharedString>,
on_change: Option<ChangeHandler>,
style: StyleRefinement,
}
impl Toggle {
@@ -38,6 +39,7 @@ impl Toggle {
disabled: false,
a11y_label: None,
on_change: None,
style: StyleRefinement::default(),
}
}
@@ -81,6 +83,12 @@ impl Toggle {
}
}
impl gpui::Styled for Toggle {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl Sizable for Toggle {
fn apply_size(&mut self, size: Size) {
self.size = size;
@@ -118,6 +126,7 @@ impl RenderOnce for Toggle {
.hover(|style| style.bg(colors.muted))
.active(|style| style.bg(ramps.secondary.hover))
})
.refine_style(&self.style)
.when(self.disabled, |toggle| toggle.opacity(0.5))
.when(!self.disabled, |toggle| {
toggle