Make the labels of the checkboxes on the welcome screen clickable (#7878)

This PR makes the labels of the checkboxes on the welcome screen
clickable.

Release Notes:

- Added support for clicking the labels of the checkboxes on the welcome
screen to toggle the value
([#7794](https://github.com/zed-industries/zed/issues/7794)).
This commit is contained in:
Marshall Bowers 2024-02-15 20:37:31 -05:00 committed by GitHub
parent 32fdff0285
commit 9ef83a2557
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 113 additions and 108 deletions

View file

@ -3,8 +3,6 @@ use gpui::{div, prelude::*, ElementId, IntoElement, Styled, WindowContext};
use crate::prelude::*; use crate::prelude::*;
use crate::{Color, Icon, IconName, Selection}; use crate::{Color, Icon, IconName, Selection};
pub type CheckHandler = Box<dyn Fn(&Selection, &mut WindowContext) + 'static>;
/// # Checkbox /// # Checkbox
/// ///
/// Checkboxes are used for multiple choices, not for mutually exclusive choices. /// Checkboxes are used for multiple choices, not for mutually exclusive choices.
@ -15,7 +13,7 @@ pub struct Checkbox {
id: ElementId, id: ElementId,
checked: Selection, checked: Selection,
disabled: bool, disabled: bool,
on_click: Option<CheckHandler>, on_click: Option<Box<dyn Fn(&Selection, &mut WindowContext) + 'static>>,
} }
impl Checkbox { impl Checkbox {
@ -33,10 +31,7 @@ impl Checkbox {
self self
} }
pub fn on_click( pub fn on_click(mut self, handler: impl Fn(&Selection, &mut WindowContext) + 'static) -> Self {
mut self,
handler: impl 'static + Fn(&Selection, &mut WindowContext) + Send + Sync,
) -> Self {
self.on_click = Some(Box::new(handler)); self.on_click = Some(Box::new(handler));
self self
} }

View file

@ -144,55 +144,37 @@ impl Render for WelcomePage {
.border_1() .border_1()
.border_color(cx.theme().colors().border) .border_color(cx.theme().colors().border)
.rounded_md() .rounded_md()
.child( .child(LabelledCheckbox::new(
h_flex()
.gap_2()
.child(
Checkbox::new(
"enable-vim", "enable-vim",
Label::new("Enable vim mode"),
if VimModeSetting::get_global(cx).0 { if VimModeSetting::get_global(cx).0 {
ui::Selection::Selected ui::Selection::Selected
} else { } else {
ui::Selection::Unselected ui::Selection::Unselected
}, },
)
.on_click(
cx.listener(move |this, selection, cx| { cx.listener(move |this, selection, cx| {
this.telemetry.report_app_event( this.telemetry
"welcome page: toggle vim".to_string(), .report_app_event("welcome page: toggle vim".to_string());
);
this.update_settings::<VimModeSetting>( this.update_settings::<VimModeSetting>(
selection, selection,
cx, cx,
|setting, value| *setting = Some(value), |setting, value| *setting = Some(value),
); );
}), }),
), ))
) .child(LabelledCheckbox::new(
.child(Label::new("Enable vim mode")),
)
.child(
h_flex()
.gap_2()
.child(
Checkbox::new(
"enable-telemetry", "enable-telemetry",
Label::new("Send anonymous usage data"),
if TelemetrySettings::get_global(cx).metrics { if TelemetrySettings::get_global(cx).metrics {
ui::Selection::Selected ui::Selection::Selected
} else { } else {
ui::Selection::Unselected ui::Selection::Unselected
}, },
)
.on_click(
cx.listener(move |this, selection, cx| { cx.listener(move |this, selection, cx| {
this.telemetry.report_app_event( this.telemetry.report_app_event(
"welcome page: toggle metric telemetry" "welcome page: toggle metric telemetry".to_string(),
.to_string(),
); );
this.update_settings::<TelemetrySettings>( this.update_settings::<TelemetrySettings>(selection, cx, {
selection,
cx,
{
let telemetry = this.telemetry.clone(); let telemetry = this.telemetry.clone();
move |settings, value| { move |settings, value| {
@ -203,35 +185,22 @@ impl Render for WelcomePage {
value.to_string(), value.to_string(),
); );
} }
}, });
);
}), }),
), ))
) .child(LabelledCheckbox::new(
.child(Label::new("Send anonymous usage data")),
)
.child(
h_flex()
.gap_2()
.child(
Checkbox::new(
"enable-crash", "enable-crash",
Label::new("Send crash reports"),
if TelemetrySettings::get_global(cx).diagnostics { if TelemetrySettings::get_global(cx).diagnostics {
ui::Selection::Selected ui::Selection::Selected
} else { } else {
ui::Selection::Unselected ui::Selection::Unselected
}, },
)
.on_click(
cx.listener(move |this, selection, cx| { cx.listener(move |this, selection, cx| {
this.telemetry.report_app_event( this.telemetry.report_app_event(
"welcome page: toggle diagnostic telemetry" "welcome page: toggle diagnostic telemetry".to_string(),
.to_string(),
); );
this.update_settings::<TelemetrySettings>( this.update_settings::<TelemetrySettings>(selection, cx, {
selection,
cx,
{
let telemetry = this.telemetry.clone(); let telemetry = this.telemetry.clone();
move |settings, value| { move |settings, value| {
@ -242,13 +211,9 @@ impl Render for WelcomePage {
value.to_string(), value.to_string(),
); );
} }
}, });
);
}), }),
), )),
)
.child(Label::new("Send crash reports")),
),
), ),
) )
} }
@ -343,3 +308,48 @@ impl Item for WelcomePage {
f(*event) f(*event)
} }
} }
#[derive(IntoElement)]
struct LabelledCheckbox {
id: ElementId,
label: Label,
checked: Selection,
on_click: Arc<dyn Fn(&Selection, &mut WindowContext) + 'static>,
}
impl LabelledCheckbox {
pub fn new(
id: impl Into<ElementId>,
label: Label,
checked: Selection,
on_click: impl Fn(&Selection, &mut WindowContext) + 'static,
) -> Self {
Self {
id: id.into(),
label,
checked,
on_click: Arc::new(on_click),
}
}
}
impl RenderOnce for LabelledCheckbox {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
h_flex()
.gap_2()
.child(Checkbox::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),
)
}
}