Checkpoint

Antonio Scandurra created

Change summary

crates/gpui3/src/elements/img.rs | 81 +++++++++++++++++++++++++--------
1 file changed, 60 insertions(+), 21 deletions(-)

Detailed changes

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

@@ -1,18 +1,19 @@
 use crate::{
     div, Active, Anonymous, AnyElement, BorrowWindow, Bounds, Click, Div, DivState, Element,
-    ElementId, ElementIdentity, EventListeners, Hover, Identified, Interactive, IntoAnyElement,
-    LayoutId, NonFocusable, Pixels, SharedString, StyleRefinement, Styled, ViewContext,
+    ElementFocusability, ElementId, ElementIdentity, EventListeners, Focus, Focusable, Hover,
+    Identified, Interactive, IntoAnyElement, LayoutId, NonFocusable, Pixels, SharedString,
+    StyleRefinement, Styled, ViewContext,
 };
 use futures::FutureExt;
 use util::ResultExt;
 
-pub struct Img<V: 'static + Send + Sync, K: ElementIdentity = Anonymous> {
-    base: Div<V, K, NonFocusable>,
+pub struct Img<V: 'static + Send + Sync, I: ElementIdentity, F: ElementFocusability> {
+    base: Div<V, I, F>,
     uri: Option<SharedString>,
     grayscale: bool,
 }
 
-pub fn img<V>() -> Img<V, Anonymous>
+pub fn img<V>() -> Img<V, Anonymous, NonFocusable>
 where
     V: 'static + Send + Sync,
 {
@@ -23,10 +24,11 @@ where
     }
 }
 
-impl<V, K> Img<V, K>
+impl<V, I, F> Img<V, I, F>
 where
     V: 'static + Send + Sync,
-    K: ElementIdentity,
+    I: ElementIdentity,
+    F: ElementFocusability,
 {
     pub fn uri(mut self, uri: impl Into<SharedString>) -> Self {
         self.uri = Some(uri.into());
@@ -39,8 +41,12 @@ where
     }
 }
 
-impl<V: 'static + Send + Sync> Img<V, Anonymous> {
-    pub fn id(self, id: impl Into<ElementId>) -> Img<V, Identified> {
+impl<V, F> Img<V, Anonymous, F>
+where
+    V: 'static + Send + Sync,
+    F: ElementFocusability,
+{
+    pub fn id(self, id: impl Into<ElementId>) -> Img<V, Identified, F> {
         Img {
             base: self.base.id(id),
             uri: self.uri,
@@ -49,20 +55,22 @@ impl<V: 'static + Send + Sync> Img<V, Anonymous> {
     }
 }
 
-impl<V, K> IntoAnyElement<V> for Img<V, K>
+impl<V, I, F> IntoAnyElement<V> for Img<V, I, F>
 where
     V: 'static + Send + Sync,
-    K: ElementIdentity,
+    I: ElementIdentity,
+    F: ElementFocusability,
 {
     fn into_any(self) -> AnyElement<V> {
         AnyElement::new(self)
     }
 }
 
-impl<V, K> Element for Img<V, K>
+impl<V, I, F> Element for Img<V, I, F>
 where
     V: Send + Sync + 'static,
-    K: ElementIdentity,
+    I: ElementIdentity,
+    F: ElementFocusability,
 {
     type ViewState = V;
     type ElementState = DivState;
@@ -127,43 +135,74 @@ where
     }
 }
 
-impl<V, K> Styled for Img<V, K>
+impl<V, I, F> Styled for Img<V, I, F>
 where
     V: 'static + Send + Sync,
-    K: ElementIdentity,
+    I: ElementIdentity,
+    F: ElementFocusability,
 {
     fn style(&mut self) -> &mut StyleRefinement {
         self.base.style()
     }
 }
 
-impl<V, K> Interactive for Img<V, K>
+impl<V, I, F> Interactive for Img<V, I, F>
 where
     V: 'static + Send + Sync,
-    K: ElementIdentity,
+    I: ElementIdentity,
+    F: ElementFocusability,
 {
     fn listeners(&mut self) -> &mut EventListeners<V> {
         self.base.listeners()
     }
 }
 
-impl<V, K> Hover for Img<V, K>
+impl<V, I, F> Hover for Img<V, I, F>
 where
     V: 'static + Send + Sync,
-    K: ElementIdentity,
+    I: ElementIdentity,
+    F: ElementFocusability,
 {
     fn set_hover_style(&mut self, group: Option<SharedString>, style: StyleRefinement) {
         self.base.set_hover_style(group, style);
     }
 }
 
-impl<V> Click for Img<V, Identified> where V: 'static + Send + Sync {}
+impl<V, F> Click for Img<V, Identified, F>
+where
+    V: 'static + Send + Sync,
+    F: ElementFocusability,
+{
+}
 
-impl<V> Active for Img<V, Identified>
+impl<V, F> Active for Img<V, Identified, F>
 where
     V: 'static + Send + Sync,
+    F: ElementFocusability,
 {
     fn set_active_style(&mut self, group: Option<SharedString>, style: StyleRefinement) {
         self.base.set_active_style(group, style)
     }
 }
+
+impl<V, I> Focus for Img<V, I, Focusable>
+where
+    V: 'static + Send + Sync,
+    I: ElementIdentity,
+{
+    fn set_focus_style(&mut self, style: StyleRefinement) {
+        self.base.set_focus_style(style)
+    }
+
+    fn set_focus_in_style(&mut self, style: StyleRefinement) {
+        self.base.set_focus_in_style(style)
+    }
+
+    fn set_in_focus_style(&mut self, style: StyleRefinement) {
+        self.base.set_in_focus_style(style)
+    }
+
+    fn handle(&self) -> &crate::FocusHandle {
+        self.base.handle()
+    }
+}