From 6dbc12f6af863a55474706b3afdd502ee8aeedd7 Mon Sep 17 00:00:00 2001
From: Danilo Leal <67129314+danilo-leal@users.noreply.github.com>
Date: Fri, 20 Dec 2024 20:44:47 -0300
Subject: [PATCH] Add the `SwitchWithLabel` component (#22314)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Release Notes:
- N/A
---
crates/ui/src/components/toggle.rs | 75 +++++++++++++++++++++++++++
crates/workspace/src/theme_preview.rs | 15 +++---
2 files changed, 83 insertions(+), 7 deletions(-)
diff --git a/crates/ui/src/components/toggle.rs b/crates/ui/src/components/toggle.rs
index 4d434b81ca75654423f47a0b30e03969141503f6..aa7e4b181a3d737809c9c8f53e06fe5f7e3a5e03 100644
--- a/crates/ui/src/components/toggle.rs
+++ b/crates/ui/src/components/toggle.rs
@@ -282,6 +282,52 @@ impl RenderOnce for Switch {
}
}
+/// A [`Switch`] that has a [`Label`].
+#[derive(IntoElement)]
+pub struct SwitchWithLabel {
+ id: ElementId,
+ label: Label,
+ checked: ToggleState,
+ on_click: Arc,
+}
+
+impl SwitchWithLabel {
+ pub fn new(
+ id: impl Into,
+ label: Label,
+ checked: ToggleState,
+ on_click: impl Fn(&ToggleState, &mut WindowContext) + 'static,
+ ) -> Self {
+ Self {
+ id: id.into(),
+ label,
+ checked,
+ on_click: Arc::new(on_click),
+ }
+ }
+}
+
+impl RenderOnce for SwitchWithLabel {
+ fn render(self, cx: &mut WindowContext) -> impl IntoElement {
+ h_flex()
+ .gap(DynamicSpacing::Base08.rems(cx))
+ .child(Switch::new(self.id.clone(), self.checked).on_click({
+ let on_click = self.on_click.clone();
+ move |checked, cx| {
+ (on_click)(checked, cx);
+ }
+ }))
+ .child(
+ div()
+ .id(SharedString::from(format!("{}-label", self.id)))
+ .on_click(move |_event, cx| {
+ (self.on_click)(&self.checked.inverse(), cx);
+ })
+ .child(self.label),
+ )
+ }
+}
+
impl ComponentPreview for Checkbox {
fn description() -> impl Into