Make clicking ListHeader labels toggle the disclosure (#4140)

This PR makes clicking the label inside of a `ListHeader` with a
disclosure also toggle the disclosure.

Release Notes:

- Added support for clicking the "Online", "Offline", and "Requests"
headers in the contact list to toggle their expansion.
This commit is contained in:
Marshall Bowers 2024-01-18 18:21:53 -05:00 committed by GitHub
parent f3a76c8636
commit 0691ad480b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 8 deletions

View file

@ -1,3 +1,5 @@
use std::sync::Arc;
use gpui::ClickEvent; use gpui::ClickEvent;
use crate::{prelude::*, Color, IconButton, IconName, IconSize}; use crate::{prelude::*, Color, IconButton, IconName, IconSize};
@ -6,7 +8,7 @@ use crate::{prelude::*, Color, IconButton, IconName, IconSize};
pub struct Disclosure { pub struct Disclosure {
id: ElementId, id: ElementId,
is_open: bool, is_open: bool,
on_toggle: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>, on_toggle: Option<Arc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
} }
impl Disclosure { impl Disclosure {
@ -20,7 +22,7 @@ impl Disclosure {
pub fn on_toggle( pub fn on_toggle(
mut self, mut self,
handler: impl Into<Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>>, handler: impl Into<Option<Arc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>>,
) -> Self { ) -> Self {
self.on_toggle = handler.into(); self.on_toggle = handler.into();
self self

View file

@ -1,3 +1,5 @@
use std::sync::Arc;
use crate::{h_flex, prelude::*, Disclosure, Label}; use crate::{h_flex, prelude::*, Disclosure, Label};
use gpui::{AnyElement, ClickEvent}; use gpui::{AnyElement, ClickEvent};
@ -14,7 +16,7 @@ pub struct ListHeader {
/// It will obscure the `end_slot` when visible. /// It will obscure the `end_slot` when visible.
end_hover_slot: Option<AnyElement>, end_hover_slot: Option<AnyElement>,
toggle: Option<bool>, toggle: Option<bool>,
on_toggle: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>, on_toggle: Option<Arc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
inset: bool, inset: bool,
selected: bool, selected: bool,
} }
@ -42,7 +44,7 @@ impl ListHeader {
mut self, mut self,
on_toggle: impl Fn(&ClickEvent, &mut WindowContext) + 'static, on_toggle: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
) -> Self { ) -> Self {
self.on_toggle = Some(Box::new(on_toggle)); self.on_toggle = Some(Arc::new(on_toggle));
self self
} }
@ -98,15 +100,19 @@ impl RenderOnce for ListHeader {
h_flex() h_flex()
.gap_1() .gap_1()
.children(self.toggle.map(|is_open| { .children(self.toggle.map(|is_open| {
Disclosure::new("toggle", is_open).on_toggle(self.on_toggle) Disclosure::new("toggle", is_open).on_toggle(self.on_toggle.clone())
})) }))
.child( .child(
div() div()
.id("label_container")
.flex() .flex()
.gap_1() .gap_1()
.items_center() .items_center()
.children(self.start_slot) .children(self.start_slot)
.child(Label::new(self.label.clone()).color(Color::Muted)), .child(Label::new(self.label.clone()).color(Color::Muted))
.when_some(self.on_toggle, |this, on_toggle| {
this.on_click(move |event, cx| on_toggle(event, cx))
}),
), ),
) )
.child(h_flex().children(self.end_slot)) .child(h_flex().children(self.end_slot))

View file

@ -1,3 +1,5 @@
use std::sync::Arc;
use gpui::{px, AnyElement, AnyView, ClickEvent, MouseButton, MouseDownEvent, Pixels}; use gpui::{px, AnyElement, AnyView, ClickEvent, MouseButton, MouseDownEvent, Pixels};
use smallvec::SmallVec; use smallvec::SmallVec;
@ -29,7 +31,7 @@ pub struct ListItem {
toggle: Option<bool>, toggle: Option<bool>,
inset: bool, inset: bool,
on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>, on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
on_toggle: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>, on_toggle: Option<Arc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
tooltip: Option<Box<dyn Fn(&mut WindowContext) -> AnyView + 'static>>, tooltip: Option<Box<dyn Fn(&mut WindowContext) -> AnyView + 'static>>,
on_secondary_mouse_down: Option<Box<dyn Fn(&MouseDownEvent, &mut WindowContext) + 'static>>, on_secondary_mouse_down: Option<Box<dyn Fn(&MouseDownEvent, &mut WindowContext) + 'static>>,
children: SmallVec<[AnyElement; 2]>, children: SmallVec<[AnyElement; 2]>,
@ -104,7 +106,7 @@ impl ListItem {
mut self, mut self,
on_toggle: impl Fn(&ClickEvent, &mut WindowContext) + 'static, on_toggle: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
) -> Self { ) -> Self {
self.on_toggle = Some(Box::new(on_toggle)); self.on_toggle = Some(Arc::new(on_toggle));
self self
} }