From 6985b708592b1147a4477b06150a5ea7f5870727 Mon Sep 17 00:00:00 2001 From: Mikayla Date: Mon, 20 Nov 2023 11:55:27 -0800 Subject: [PATCH] fix gpui2 --- crates/gpui2/src/elements/div.rs | 25 +++++------ crates/gpui2/src/interactive.rs | 2 +- crates/gpui2_macros/src/derive_render_once.rs | 45 ++----------------- 3 files changed, 17 insertions(+), 55 deletions(-) diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs index 0590372694de230300ab926cb049c921c74fc407..630b368b9518aa8c8b46708f4f65e4c45bf01928 100644 --- a/crates/gpui2/src/elements/div.rs +++ b/crates/gpui2/src/elements/div.rs @@ -266,7 +266,7 @@ pub trait InteractiveElement: Sized + Element { .key_down_listeners .push(Box::new(move |event, phase, cx| { if phase == DispatchPhase::Capture { - (listener.callback)(event, cx) + listener(event, cx) } })); self @@ -277,7 +277,7 @@ pub trait InteractiveElement: Sized + Element { .key_up_listeners .push(Box::new(move |event, phase, cx| { if phase == DispatchPhase::Bubble { - (listener.callback)(event, cx) + listener(event, cx) } })); self @@ -291,7 +291,7 @@ pub trait InteractiveElement: Sized + Element { .key_up_listeners .push(Box::new(move |event, phase, cx| { if phase == DispatchPhase::Capture { - (listener.callback)(event, cx) + listener(event, cx) } })); self @@ -326,7 +326,7 @@ pub trait InteractiveElement: Sized + Element { self.interactivity().drop_listeners.push(( TypeId::of::(), Box::new(move |dragged_view, cx| { - (listener.callback)(&dragged_view.downcast().unwrap(), cx); + listener(&dragged_view.downcast().unwrap(), cx); }), )); self @@ -384,7 +384,7 @@ pub trait StatefulInteractiveElement: InteractiveElement { { self.interactivity() .click_listeners - .push(Box::new(move |event, cx| (listener.callback)(event, cx))); + .push(Box::new(move |event, cx| listener(event, cx))); self } @@ -398,7 +398,7 @@ pub trait StatefulInteractiveElement: InteractiveElement { "calling on_drag more than once on the same element is not supported" ); self.interactivity().drag_listener = Some(Box::new(move |cursor_offset, cx| AnyDrag { - view: (listener.callback)(cx).into(), + view: listener(cx).into(), cursor_offset, })); self @@ -424,8 +424,7 @@ pub trait StatefulInteractiveElement: InteractiveElement { self.interactivity().tooltip_builder.is_none(), "calling tooltip more than once on the same element is not supported" ); - self.interactivity().tooltip_builder = - Some(Rc::new(move |cx| (build_tooltip.callback)(cx))); + self.interactivity().tooltip_builder = Some(Rc::new(build_tooltip)); self } @@ -456,7 +455,7 @@ pub trait FocusableElement: InteractiveElement { .focus_listeners .push(Box::new(move |focus_handle, event, cx| { if event.focused.as_ref() == Some(focus_handle) { - (listener.callback)(event, cx) + listener(event, cx) } })); self @@ -470,7 +469,7 @@ pub trait FocusableElement: InteractiveElement { .focus_listeners .push(Box::new(move |focus_handle, event, cx| { if event.blurred.as_ref() == Some(focus_handle) { - (listener.callback)(event, cx) + listener(event, cx) } })); self @@ -493,7 +492,7 @@ pub trait FocusableElement: InteractiveElement { .map_or(false, |focused| focus_handle.contains(focused, cx)); if !descendant_blurred && descendant_focused { - (listener.callback)(event, cx) + listener(event, cx) } })); self @@ -515,7 +514,7 @@ pub trait FocusableElement: InteractiveElement { .as_ref() .map_or(false, |focused| focus_handle.contains(focused, cx)); if descendant_blurred && !descendant_focused { - (listener.callback)(event, cx) + listener(event, cx) } })); self @@ -907,7 +906,7 @@ impl Interactivity { *was_hovered = is_hovered; drop(was_hovered); - (hover_listener.callback)(&is_hovered, cx); + hover_listener(&is_hovered, cx); } }); } diff --git a/crates/gpui2/src/interactive.rs b/crates/gpui2/src/interactive.rs index be5ebf7cff87e96c9d210e163153fce4069de48b..3dfe0c3b436dda921b8fe10727fdcd548a114e3e 100644 --- a/crates/gpui2/src/interactive.rs +++ b/crates/gpui2/src/interactive.rs @@ -307,7 +307,7 @@ mod test { .key_context("parent") .on_key_down(cx.listener(|this, _, _| this.saw_key_down = true)) .on_action( - cx.callback(|this: &mut TestView, _: &TestAction, _| { + cx.listener(|this: &mut TestView, _: &TestAction, _| { this.saw_action = true }), ) diff --git a/crates/gpui2_macros/src/derive_render_once.rs b/crates/gpui2_macros/src/derive_render_once.rs index 732f2df21f10ebf409935ed04ce17ea5553d9321..efe6aab0bb9c43776a4f9da26d084c2577ca367f 100644 --- a/crates/gpui2_macros/src/derive_render_once.rs +++ b/crates/gpui2_macros/src/derive_render_once.rs @@ -1,37 +1,17 @@ use proc_macro::TokenStream; use quote::quote; -use syn::{parse_macro_input, parse_quote, DeriveInput}; +use syn::{parse_macro_input, DeriveInput}; pub fn derive_render_once(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); let type_name = &ast.ident; - - let mut trait_generics = ast.generics.clone(); - let view_type = if let Some(view_type) = specified_view_type(&ast) { - quote! { #view_type } - } else { - if let Some(first_type_param) = ast.generics.params.iter().find_map(|param| { - if let syn::GenericParam::Type(type_param) = param { - Some(type_param.ident.clone()) - } else { - None - } - }) { - quote! { #first_type_param } - } else { - trait_generics.params.push(parse_quote! { V: 'static }); - quote! { V } - } - }; - - let (impl_generics, _, where_clause) = trait_generics.split_for_impl(); - let (_, type_generics, _) = ast.generics.split_for_impl(); + let (impl_generics, type_generics, where_clause) = ast.generics.split_for_impl(); let gen = quote! { - impl #impl_generics gpui::RenderOnce<#view_type> for #type_name #type_generics + impl #impl_generics gpui::RenderOnce for #type_name #type_generics #where_clause { - type Element = gpui::CompositeElement<#view_type, Self>; + type Element = gpui::CompositeElement; fn element_id(&self) -> Option { None @@ -45,20 +25,3 @@ pub fn derive_render_once(input: TokenStream) -> TokenStream { gen.into() } - -fn specified_view_type(ast: &DeriveInput) -> Option { - ast.attrs.iter().find_map(|attr| { - if attr.path.is_ident("view") { - if let Ok(syn::Meta::NameValue(meta_name_value)) = attr.parse_meta() { - if let syn::Lit::Str(lit_str) = meta_name_value.lit { - return Some( - lit_str - .parse::() - .expect("Failed to parse view_type"), - ); - } - } - } - None - }) -}