Use Box instead of Rc for List event handlers

Max Brunsfeld created

Change summary

crates/ui2/src/components/disclosure.rs       | 10 +++-------
crates/ui2/src/components/list/list_header.rs | 10 +++-------
crates/ui2/src/components/list/list_item.rs   | 18 +++++++-----------
3 files changed, 13 insertions(+), 25 deletions(-)

Detailed changes

crates/ui2/src/components/disclosure.rs 🔗

@@ -1,14 +1,10 @@
-use std::rc::Rc;
-
+use crate::{prelude::*, Color, Icon, IconButton, IconSize};
 use gpui::ClickEvent;
 
-use crate::prelude::*;
-use crate::{Color, Icon, IconButton, IconSize};
-
 #[derive(IntoElement)]
 pub struct Disclosure {
     is_open: bool,
-    on_toggle: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
+    on_toggle: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
 }
 
 impl Disclosure {
@@ -21,7 +17,7 @@ impl Disclosure {
 
     pub fn on_toggle(
         mut self,
-        handler: impl Into<Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>>,
+        handler: impl Into<Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>>,
     ) -> Self {
         self.on_toggle = handler.into();
         self

crates/ui2/src/components/list/list_header.rs 🔗

@@ -1,18 +1,14 @@
-use std::rc::Rc;
-
+use crate::{h_stack, prelude::*, Disclosure, Icon, IconElement, IconSize, Label};
 use gpui::{AnyElement, ClickEvent, Div};
 use smallvec::SmallVec;
 
-use crate::prelude::*;
-use crate::{h_stack, Disclosure, Icon, IconElement, IconSize, Label};
-
 #[derive(IntoElement)]
 pub struct ListHeader {
     label: SharedString,
     left_icon: Option<Icon>,
     meta: SmallVec<[AnyElement; 2]>,
     toggle: Option<bool>,
-    on_toggle: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
+    on_toggle: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
     inset: bool,
     selected: bool,
 }
@@ -39,7 +35,7 @@ impl ListHeader {
         mut self,
         on_toggle: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
     ) -> Self {
-        self.on_toggle = Some(Rc::new(on_toggle));
+        self.on_toggle = Some(Box::new(on_toggle));
         self
     }
 

crates/ui2/src/components/list/list_item.rs 🔗

@@ -1,14 +1,10 @@
-use std::rc::Rc;
-
+use crate::{prelude::*, Avatar, Disclosure, Icon, IconElement, IconSize};
 use gpui::{
     px, AnyElement, AnyView, ClickEvent, Div, ImageSource, MouseButton, MouseDownEvent, Pixels,
     Stateful,
 };
 use smallvec::SmallVec;
 
-use crate::prelude::*;
-use crate::{Avatar, Disclosure, Icon, IconElement, IconSize};
-
 #[derive(IntoElement)]
 pub struct ListItem {
     id: ElementId,
@@ -20,10 +16,10 @@ pub struct ListItem {
     left_slot: Option<AnyElement>,
     toggle: Option<bool>,
     inset: bool,
-    on_click: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
-    on_toggle: Option<Rc<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>>,
     tooltip: Option<Box<dyn Fn(&mut WindowContext) -> AnyView + 'static>>,
-    on_secondary_mouse_down: Option<Rc<dyn Fn(&MouseDownEvent, &mut WindowContext) + 'static>>,
+    on_secondary_mouse_down: Option<Box<dyn Fn(&MouseDownEvent, &mut WindowContext) + 'static>>,
     children: SmallVec<[AnyElement; 2]>,
 }
 
@@ -46,7 +42,7 @@ impl ListItem {
     }
 
     pub fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self {
-        self.on_click = Some(Rc::new(handler));
+        self.on_click = Some(Box::new(handler));
         self
     }
 
@@ -54,7 +50,7 @@ impl ListItem {
         mut self,
         handler: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static,
     ) -> Self {
-        self.on_secondary_mouse_down = Some(Rc::new(handler));
+        self.on_secondary_mouse_down = Some(Box::new(handler));
         self
     }
 
@@ -87,7 +83,7 @@ impl ListItem {
         mut self,
         on_toggle: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
     ) -> Self {
-        self.on_toggle = Some(Rc::new(on_toggle));
+        self.on_toggle = Some(Box::new(on_toggle));
         self
     }