Checkpoint

Antonio Scandurra created

Change summary

crates/gpui3/src/element.rs      | 35 ++++++++++++++++++++++++++++++++-
crates/gpui3/src/elements/div.rs | 27 +------------------------
2 files changed, 35 insertions(+), 27 deletions(-)

Detailed changes

crates/gpui3/src/element.rs 🔗

@@ -1,8 +1,9 @@
 use crate::{
-    BorrowWindow, Bounds, ElementId, FocusHandle, FocusListeners, LayoutId, Pixels, Point,
-    StyleRefinement, ViewContext,
+    BorrowWindow, Bounds, DispatchPhase, ElementId, FocusHandle, FocusListeners, LayoutId,
+    MouseDownEvent, Pixels, Point, Style, StyleRefinement, ViewContext, WindowContext,
 };
 use derive_more::{Deref, DerefMut};
+use refineable::Refineable;
 pub(crate) use smallvec::SmallVec;
 use std::mem;
 
@@ -75,6 +76,36 @@ pub trait ElementFocusability<V: 'static + Send + Sync>: 'static + Send + Sync {
             f(cx)
         }
     }
+
+    fn refine_style(&self, style: &mut Style, cx: &WindowContext) {
+        if let Some(focusable) = self.as_focusable() {
+            if focusable.focus_handle.contains_focused(cx) {
+                style.refine(&focusable.focus_in_style);
+            }
+
+            if focusable.focus_handle.within_focused(cx) {
+                style.refine(&focusable.in_focus_style);
+            }
+
+            if focusable.focus_handle.is_focused(cx) {
+                style.refine(&focusable.focus_style);
+            }
+        }
+    }
+
+    fn paint(&self, bounds: Bounds<Pixels>, cx: &mut WindowContext) {
+        if let Some(focusable) = self.as_focusable() {
+            let focus_handle = focusable.focus_handle.clone();
+            cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| {
+                if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
+                    if !cx.default_prevented() {
+                        cx.focus(&focus_handle);
+                        cx.prevent_default();
+                    }
+                }
+            })
+        }
+    }
 }
 
 pub struct Focusable<V: 'static + Send + Sync> {

crates/gpui3/src/elements/div.rs 🔗

@@ -197,19 +197,7 @@ where
         let mut computed_style = Style::default();
         computed_style.refine(&self.base_style);
 
-        if let Some(focusable) = self.focusability.as_focusable() {
-            if focusable.focus_handle.contains_focused(cx) {
-                computed_style.refine(&focusable.focus_in_style);
-            }
-
-            if focusable.focus_handle.within_focused(cx) {
-                computed_style.refine(&focusable.in_focus_style);
-            }
-
-            if focusable.focus_handle.is_focused(cx) {
-                computed_style.refine(&focusable.focus_style);
-            }
-        }
+        self.focusability.refine_style(&mut computed_style, cx);
 
         let mouse_position = cx.mouse_position();
 
@@ -312,18 +300,6 @@ where
             });
         }
 
-        if let Some(focusable) = self.focusability.as_focusable() {
-            let focus_handle = focusable.focus_handle.clone();
-            cx.on_mouse_event(move |_, event: &MouseDownEvent, phase, cx| {
-                if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
-                    if !cx.default_prevented() {
-                        cx.focus(&focus_handle);
-                        cx.prevent_default();
-                    }
-                }
-            })
-        }
-
         for listener in mem::take(&mut self.interactive_state.mouse_down) {
             cx.on_mouse_event(move |state, event: &MouseDownEvent, phase, cx| {
                 listener(state, event, &bounds, phase, cx);
@@ -508,6 +484,7 @@ where
                         element_state.active_state.clone(),
                         cx,
                     );
+                    this.focusability.paint(bounds, cx);
                     this.paint_event_listeners(bounds, element_state.pending_click.clone(), cx);
                 });