Checkpoint

This commit is contained in:
Antonio Scandurra 2023-10-18 16:13:36 +02:00
parent 1270bcc6ed
commit d98c347902
3 changed files with 15 additions and 18 deletions

View file

@ -61,6 +61,12 @@ pub trait ElementFocusability: 'static + Send + Sync {
pub struct Focusable(FocusHandle); pub struct Focusable(FocusHandle);
impl AsRef<FocusHandle> for Focusable {
fn as_ref(&self) -> &FocusHandle {
&self.0
}
}
impl ElementFocusability for Focusable { impl ElementFocusability for Focusable {
fn focus_handle(&self) -> Option<&FocusHandle> { fn focus_handle(&self) -> Option<&FocusHandle> {
Some(&self.0) Some(&self.0)

View file

@ -1,9 +1,9 @@
use crate::{ use crate::{
Active, Anonymous, AnyElement, AppContext, BorrowWindow, Bounds, Click, DispatchPhase, Element, Active, Anonymous, AnyElement, AppContext, BorrowWindow, Bounds, Click, DispatchPhase, Element,
ElementFocusability, ElementId, ElementIdentity, EventListeners, FocusHandle, Focusable, Hover, ElementFocusability, ElementId, ElementIdentity, EventListeners, Focus, FocusHandle, Focusable,
Identified, Interactive, IntoAnyElement, KeyDownEvent, LayoutId, MouseClickEvent, Hover, Identified, Interactive, IntoAnyElement, LayoutId, MouseClickEvent, MouseDownEvent,
MouseDownEvent, MouseMoveEvent, MouseUpEvent, NonFocusable, Overflow, ParentElement, Pixels, MouseMoveEvent, MouseUpEvent, NonFocusable, Overflow, ParentElement, Pixels, Point,
Point, ScrollWheelEvent, SharedString, Style, StyleRefinement, Styled, ViewContext, ScrollWheelEvent, SharedString, Style, StyleRefinement, Styled, ViewContext,
}; };
use collections::HashMap; use collections::HashMap;
use parking_lot::Mutex; use parking_lot::Mutex;
@ -339,20 +339,13 @@ where
} }
} }
impl<I, V> Div<I, Focusable, V> impl<I, V> Focus for Div<I, Focusable, V>
where where
I: ElementIdentity, I: ElementIdentity,
V: 'static + Send + Sync, V: 'static + Send + Sync,
{ {
pub fn on_key_down<F>( fn handle(&self) -> &FocusHandle {
mut self, self.focusability.as_ref()
listener: impl Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>)
+ Send
+ Sync
+ 'static,
) -> Self {
self.listeners.key_down.push(Box::new(listener));
self
} }
} }

View file

@ -1,11 +1,9 @@
use crate::{ use crate::{
DispatchPhase, Element, EventListeners, FocusEvent, FocusHandle, KeyDownEvent, KeyUpEvent, DispatchPhase, FocusEvent, FocusHandle, Interactive, KeyDownEvent, KeyUpEvent, ViewContext,
ViewContext,
}; };
pub trait Focus: Element { pub trait Focus: Interactive {
fn handle(&self) -> &FocusHandle; fn handle(&self) -> &FocusHandle;
fn listeners(&mut self) -> &mut EventListeners<Self::ViewState>;
fn on_focus( fn on_focus(
mut self, mut self,