Checkpoint
This commit is contained in:
parent
38d8ab2285
commit
180ed7da81
2 changed files with 27 additions and 45 deletions
|
@ -1,6 +1,6 @@
|
|||
use crate::{
|
||||
BorrowWindow, Bounds, ElementId, FocusHandle, FocusListeners, LayoutId, Pixels, Point,
|
||||
ViewContext,
|
||||
StyleRefinement, ViewContext,
|
||||
};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
pub(crate) use smallvec::SmallVec;
|
||||
|
@ -59,22 +59,18 @@ impl ElementIdentity for Anonymous {
|
|||
}
|
||||
|
||||
pub trait ElementFocusability<V: 'static + Send + Sync>: 'static + Send + Sync {
|
||||
fn focus_handle(&self) -> Option<&FocusHandle>;
|
||||
fn focus_listeners(&self) -> Option<&FocusListeners<V>>;
|
||||
fn as_focusable(&self) -> Option<&Focusable<V>>;
|
||||
|
||||
fn initialize<R>(
|
||||
&self,
|
||||
cx: &mut ViewContext<V>,
|
||||
f: impl FnOnce(&mut ViewContext<V>) -> R,
|
||||
) -> R {
|
||||
if let Some(focus_listeners) = self.focus_listeners() {
|
||||
for listener in focus_listeners.iter().cloned() {
|
||||
if let Some(focusable) = self.as_focusable() {
|
||||
for listener in focusable.focus_listeners.iter().cloned() {
|
||||
cx.on_focus_changed(move |view, event, cx| listener(view, event, cx));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(focus_handle) = self.focus_handle().cloned() {
|
||||
cx.with_focus(focus_handle, |cx| f(cx))
|
||||
cx.with_focus(focusable.focus_handle.clone(), |cx| f(cx))
|
||||
} else {
|
||||
f(cx)
|
||||
}
|
||||
|
@ -84,18 +80,17 @@ pub trait ElementFocusability<V: 'static + Send + Sync>: 'static + Send + Sync {
|
|||
pub struct Focusable<V: 'static + Send + Sync> {
|
||||
pub focus_handle: FocusHandle,
|
||||
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>
|
||||
where
|
||||
V: 'static + Send + Sync,
|
||||
{
|
||||
fn focus_handle(&self) -> Option<&FocusHandle> {
|
||||
Some(&self.focus_handle)
|
||||
}
|
||||
|
||||
fn focus_listeners(&self) -> Option<&FocusListeners<V>> {
|
||||
Some(&self.focus_listeners)
|
||||
fn as_focusable(&self) -> Option<&Focusable<V>> {
|
||||
Some(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,7 +101,10 @@ where
|
|||
fn from(value: FocusHandle) -> Self {
|
||||
Self {
|
||||
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
|
||||
V: 'static + Send + Sync,
|
||||
{
|
||||
fn focus_handle(&self) -> Option<&FocusHandle> {
|
||||
None
|
||||
}
|
||||
|
||||
fn focus_listeners(&self) -> Option<&FocusListeners<V>> {
|
||||
fn as_focusable(&self) -> Option<&Focusable<V>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,9 +75,6 @@ pub struct Div<
|
|||
group_hover: Option<GroupStyle>,
|
||||
active_style: StyleRefinement,
|
||||
group_active: Option<GroupStyle>,
|
||||
focus_style: StyleRefinement,
|
||||
focus_in_style: StyleRefinement,
|
||||
in_focus_style: StyleRefinement,
|
||||
interactive_state: InteractiveState<V>,
|
||||
}
|
||||
|
||||
|
@ -95,9 +92,6 @@ where
|
|||
group_hover: None,
|
||||
active_style: StyleRefinement::default(),
|
||||
group_active: None,
|
||||
focus_style: StyleRefinement::default(),
|
||||
focus_in_style: StyleRefinement::default(),
|
||||
in_focus_style: StyleRefinement::default(),
|
||||
interactive_state: InteractiveState::default(),
|
||||
}
|
||||
}
|
||||
|
@ -123,9 +117,6 @@ where
|
|||
group_hover: self.group_hover,
|
||||
active_style: self.active_style,
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
@ -206,17 +197,17 @@ where
|
|||
let mut computed_style = Style::default();
|
||||
computed_style.refine(&self.base_style);
|
||||
|
||||
if let Some(handle) = self.focusability.focus_handle() {
|
||||
if handle.contains_focused(cx) {
|
||||
computed_style.refine(&self.focus_in_style);
|
||||
if let Some(focusable) = self.focusability.as_focusable() {
|
||||
if focusable.focus_handle.contains_focused(cx) {
|
||||
computed_style.refine(&focusable.focus_in_style);
|
||||
}
|
||||
|
||||
if handle.within_focused(cx) {
|
||||
computed_style.refine(&self.in_focus_style);
|
||||
if focusable.focus_handle.within_focused(cx) {
|
||||
computed_style.refine(&focusable.in_focus_style);
|
||||
}
|
||||
|
||||
if handle.is_focused(cx) {
|
||||
computed_style.refine(&self.focus_style);
|
||||
if focusable.focus_handle.is_focused(cx) {
|
||||
computed_style.refine(&focusable.focus_style);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -321,8 +312,8 @@ where
|
|||
});
|
||||
}
|
||||
|
||||
if let Some(focus_handle) = self.focusability.focus_handle() {
|
||||
let focus_handle = focus_handle.clone();
|
||||
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() {
|
||||
|
@ -375,9 +366,6 @@ where
|
|||
group_hover: self.group_hover,
|
||||
active_style: self.active_style,
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
@ -397,15 +385,15 @@ where
|
|||
}
|
||||
|
||||
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) {
|
||||
self.focus_in_style = style;
|
||||
self.focusability.focus_in_style = style;
|
||||
}
|
||||
|
||||
fn set_in_focus_style(&mut self, style: StyleRefinement) {
|
||||
self.in_focus_style = style;
|
||||
self.focusability.in_focus_style = style;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue