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,111 +144,76 @@ 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() "enable-vim",
.gap_2() Label::new("Enable vim mode"),
.child( if VimModeSetting::get_global(cx).0 {
Checkbox::new( ui::Selection::Selected
"enable-vim", } else {
if VimModeSetting::get_global(cx).0 { ui::Selection::Unselected
ui::Selection::Selected },
} else { cx.listener(move |this, selection, cx| {
ui::Selection::Unselected this.telemetry
}, .report_app_event("welcome page: toggle vim".to_string());
) this.update_settings::<VimModeSetting>(
.on_click( selection,
cx.listener(move |this, selection, cx| { cx,
this.telemetry.report_app_event( |setting, value| *setting = Some(value),
"welcome page: toggle vim".to_string(), );
); }),
this.update_settings::<VimModeSetting>( ))
selection, .child(LabelledCheckbox::new(
cx, "enable-telemetry",
|setting, value| *setting = Some(value), Label::new("Send anonymous usage data"),
); if TelemetrySettings::get_global(cx).metrics {
}), ui::Selection::Selected
), } else {
) ui::Selection::Unselected
.child(Label::new("Enable vim mode")), },
) cx.listener(move |this, selection, cx| {
.child( this.telemetry.report_app_event(
h_flex() "welcome page: toggle metric telemetry".to_string(),
.gap_2() );
.child( this.update_settings::<TelemetrySettings>(selection, cx, {
Checkbox::new( let telemetry = this.telemetry.clone();
"enable-telemetry",
if TelemetrySettings::get_global(cx).metrics {
ui::Selection::Selected
} else {
ui::Selection::Unselected
},
)
.on_click(
cx.listener(move |this, selection, cx| {
this.telemetry.report_app_event(
"welcome page: toggle metric telemetry"
.to_string(),
);
this.update_settings::<TelemetrySettings>(
selection,
cx,
{
let telemetry = this.telemetry.clone();
move |settings, value| { move |settings, value| {
settings.metrics = Some(value); settings.metrics = Some(value);
telemetry.report_setting_event( telemetry.report_setting_event(
"metric telemetry", "metric telemetry",
value.to_string(), value.to_string(),
); );
} }
}, });
); }),
}), ))
), .child(LabelledCheckbox::new(
) "enable-crash",
.child(Label::new("Send anonymous usage data")), Label::new("Send crash reports"),
) if TelemetrySettings::get_global(cx).diagnostics {
.child( ui::Selection::Selected
h_flex() } else {
.gap_2() ui::Selection::Unselected
.child( },
Checkbox::new( cx.listener(move |this, selection, cx| {
"enable-crash", this.telemetry.report_app_event(
if TelemetrySettings::get_global(cx).diagnostics { "welcome page: toggle diagnostic telemetry".to_string(),
ui::Selection::Selected );
} else { this.update_settings::<TelemetrySettings>(selection, cx, {
ui::Selection::Unselected let telemetry = this.telemetry.clone();
},
)
.on_click(
cx.listener(move |this, selection, cx| {
this.telemetry.report_app_event(
"welcome page: toggle diagnostic telemetry"
.to_string(),
);
this.update_settings::<TelemetrySettings>(
selection,
cx,
{
let telemetry = this.telemetry.clone();
move |settings, value| { move |settings, value| {
settings.diagnostics = Some(value); settings.diagnostics = Some(value);
telemetry.report_setting_event( telemetry.report_setting_event(
"diagnostic telemetry", "diagnostic telemetry",
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),
)
}
}