Checkpoint

This commit is contained in:
Antonio Scandurra 2023-10-19 21:57:09 +02:00
parent 38d8ab2285
commit 180ed7da81
2 changed files with 27 additions and 45 deletions

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
BorrowWindow, Bounds, ElementId, FocusHandle, FocusListeners, LayoutId, Pixels, Point, BorrowWindow, Bounds, ElementId, FocusHandle, FocusListeners, LayoutId, Pixels, Point,
ViewContext, StyleRefinement, ViewContext,
}; };
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
pub(crate) use smallvec::SmallVec; pub(crate) use smallvec::SmallVec;
@ -59,22 +59,18 @@ impl ElementIdentity for Anonymous {
} }
pub trait ElementFocusability<V: 'static + Send + Sync>: 'static + Send + Sync { pub trait ElementFocusability<V: 'static + Send + Sync>: 'static + Send + Sync {
fn focus_handle(&self) -> Option<&FocusHandle>; fn as_focusable(&self) -> Option<&Focusable<V>>;
fn focus_listeners(&self) -> Option<&FocusListeners<V>>;
fn initialize<R>( fn initialize<R>(
&self, &self,
cx: &mut ViewContext<V>, cx: &mut ViewContext<V>,
f: impl FnOnce(&mut ViewContext<V>) -> R, f: impl FnOnce(&mut ViewContext<V>) -> R,
) -> R { ) -> R {
if let Some(focus_listeners) = self.focus_listeners() { if let Some(focusable) = self.as_focusable() {
for listener in focus_listeners.iter().cloned() { for listener in focusable.focus_listeners.iter().cloned() {
cx.on_focus_changed(move |view, event, cx| listener(view, event, cx)); cx.on_focus_changed(move |view, event, cx| listener(view, event, cx));
} }
} cx.with_focus(focusable.focus_handle.clone(), |cx| f(cx))
if let Some(focus_handle) = self.focus_handle().cloned() {
cx.with_focus(focus_handle, |cx| f(cx))
} else { } else {
f(cx) f(cx)
} }
@ -84,18 +80,17 @@ pub trait ElementFocusability<V: 'static + Send + Sync>: 'static + Send + Sync {
pub struct Focusable<V: 'static + Send + Sync> { pub struct Focusable<V: 'static + Send + Sync> {
pub focus_handle: FocusHandle, pub focus_handle: FocusHandle,
pub focus_listeners: FocusListeners<V>, pub focus_listeners: FocusListeners<V>,
pub focus_style: StyleRefinement,
pub focus_in_style: StyleRefinement,
pub in_focus_style: StyleRefinement,
} }
impl<V> ElementFocusability<V> for Focusable<V> impl<V> ElementFocusability<V> for Focusable<V>
where where
V: 'static + Send + Sync, V: 'static + Send + Sync,
{ {
fn focus_handle(&self) -> Option<&FocusHandle> { fn as_focusable(&self) -> Option<&Focusable<V>> {
Some(&self.focus_handle) Some(self)
}
fn focus_listeners(&self) -> Option<&FocusListeners<V>> {
Some(&self.focus_listeners)
} }
} }
@ -106,7 +101,10 @@ where
fn from(value: FocusHandle) -> Self { fn from(value: FocusHandle) -> Self {
Self { Self {
focus_handle: value, focus_handle: value,
focus_listeners: Default::default(), focus_listeners: FocusListeners::default(),
focus_style: StyleRefinement::default(),
focus_in_style: StyleRefinement::default(),
in_focus_style: StyleRefinement::default(),
} }
} }
} }
@ -117,11 +115,7 @@ impl<V> ElementFocusability<V> for NonFocusable
where where
V: 'static + Send + Sync, V: 'static + Send + Sync,
{ {
fn focus_handle(&self) -> Option<&FocusHandle> { fn as_focusable(&self) -> Option<&Focusable<V>> {
None
}
fn focus_listeners(&self) -> Option<&FocusListeners<V>> {
None None
} }
} }

View file

@ -75,9 +75,6 @@ pub struct Div<
group_hover: Option<GroupStyle>, group_hover: Option<GroupStyle>,
active_style: StyleRefinement, active_style: StyleRefinement,
group_active: Option<GroupStyle>, group_active: Option<GroupStyle>,
focus_style: StyleRefinement,
focus_in_style: StyleRefinement,
in_focus_style: StyleRefinement,
interactive_state: InteractiveState<V>, interactive_state: InteractiveState<V>,
} }
@ -95,9 +92,6 @@ where
group_hover: None, group_hover: None,
active_style: StyleRefinement::default(), active_style: StyleRefinement::default(),
group_active: None, group_active: None,
focus_style: StyleRefinement::default(),
focus_in_style: StyleRefinement::default(),
in_focus_style: StyleRefinement::default(),
interactive_state: InteractiveState::default(), interactive_state: InteractiveState::default(),
} }
} }
@ -123,9 +117,6 @@ where
group_hover: self.group_hover, group_hover: self.group_hover,
active_style: self.active_style, active_style: self.active_style,
group_active: self.group_active, group_active: self.group_active,
focus_style: self.focus_style,
focus_in_style: self.focus_in_style,
in_focus_style: self.in_focus_style,
interactive_state: self.interactive_state, interactive_state: self.interactive_state,
} }
} }
@ -206,17 +197,17 @@ where
let mut computed_style = Style::default(); let mut computed_style = Style::default();
computed_style.refine(&self.base_style); computed_style.refine(&self.base_style);
if let Some(handle) = self.focusability.focus_handle() { if let Some(focusable) = self.focusability.as_focusable() {
if handle.contains_focused(cx) { if focusable.focus_handle.contains_focused(cx) {
computed_style.refine(&self.focus_in_style); computed_style.refine(&focusable.focus_in_style);
} }
if handle.within_focused(cx) { if focusable.focus_handle.within_focused(cx) {
computed_style.refine(&self.in_focus_style); computed_style.refine(&focusable.in_focus_style);
} }
if handle.is_focused(cx) { if focusable.focus_handle.is_focused(cx) {
computed_style.refine(&self.focus_style); computed_style.refine(&focusable.focus_style);
} }
} }
@ -321,8 +312,8 @@ where
}); });
} }
if let Some(focus_handle) = self.focusability.focus_handle() { if let Some(focusable) = self.focusability.as_focusable() {
let focus_handle = focus_handle.clone(); let focus_handle = focusable.focus_handle.clone();
cx.on_mouse_event(move |_, event: &MouseDownEvent, phase, cx| { cx.on_mouse_event(move |_, event: &MouseDownEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if !cx.default_prevented() { if !cx.default_prevented() {
@ -375,9 +366,6 @@ where
group_hover: self.group_hover, group_hover: self.group_hover,
active_style: self.active_style, active_style: self.active_style,
group_active: self.group_active, group_active: self.group_active,
focus_style: self.focus_style,
focus_in_style: self.focus_in_style,
in_focus_style: self.in_focus_style,
interactive_state: self.interactive_state, interactive_state: self.interactive_state,
} }
} }
@ -397,15 +385,15 @@ where
} }
fn set_focus_style(&mut self, style: StyleRefinement) { fn set_focus_style(&mut self, style: StyleRefinement) {
self.focus_style = style; self.focusability.focus_style = style;
} }
fn set_focus_in_style(&mut self, style: StyleRefinement) { fn set_focus_in_style(&mut self, style: StyleRefinement) {
self.focus_in_style = style; self.focusability.focus_in_style = style;
} }
fn set_in_focus_style(&mut self, style: StyleRefinement) { fn set_in_focus_style(&mut self, style: StyleRefinement) {
self.in_focus_style = style; self.focusability.in_focus_style = style;
} }
} }