Checkpoint

This commit is contained in:
Antonio Scandurra
2023-10-20 12:59:02 +02:00
parent 847a1cb068
commit fd94f2a5b5
3 changed files with 25 additions and 17 deletions
+8 -8
View File
@@ -236,14 +236,14 @@ where
element_state: Option<Self::ElementState>, element_state: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>, cx: &mut ViewContext<Self::ViewState>,
) -> Self::ElementState { ) -> Self::ElementState {
self.interaction.initialize(cx, |cx| { self.focus.initialize(cx, |focus_handle, cx| {
self.focus.initialize(cx, |cx| { self.interaction
for child in &mut self.children { .initialize(element_state, focus_handle, cx, |cx| {
child.initialize(view_state, cx); for child in &mut self.children {
} child.initialize(view_state, cx);
}); }
}); })
element_state.unwrap_or_default() })
} }
fn layout( fn layout(
+4 -3
View File
@@ -146,15 +146,16 @@ pub trait ElementFocus<V: 'static + Send + Sync>: 'static + Send + Sync {
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(Option<FocusHandle>, &mut ViewContext<V>) -> R,
) -> R { ) -> R {
if let Some(focusable) = self.as_focusable() { if let Some(focusable) = self.as_focusable() {
for listener in focusable.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)) let focus_handle = focusable.focus_handle.clone();
cx.with_focus(focus_handle.clone(), |cx| f(Some(focus_handle), cx))
} else { } else {
f(cx) f(None, cx)
} }
} }
+13 -6
View File
@@ -305,11 +305,13 @@ pub trait ElementInteraction<V: 'static + Send + Sync>: 'static + Send + Sync {
fn as_stateful(&self) -> Option<&StatefulInteraction<V>>; fn as_stateful(&self) -> Option<&StatefulInteraction<V>>;
fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteraction<V>>; fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteraction<V>>;
fn initialize<R>( fn initialize(
&mut self, &mut self,
element_state: Option<InteractiveElementState>,
focus_handle: Option<FocusHandle>,
cx: &mut ViewContext<V>, cx: &mut ViewContext<V>,
f: impl FnOnce(&mut ViewContext<V>) -> R, f: impl FnOnce(&mut ViewContext<V>),
) -> R { ) -> InteractiveElementState {
if let Some(stateful) = self.as_stateful_mut() { if let Some(stateful) = self.as_stateful_mut() {
cx.with_element_id(stateful.id.clone(), |global_id, cx| { cx.with_element_id(stateful.id.clone(), |global_id, cx| {
stateful.key_listeners.push(( stateful.key_listeners.push((
@@ -327,15 +329,19 @@ pub trait ElementInteraction<V: 'static + Send + Sync>: 'static + Send + Sync {
None None
}), }),
)); ));
let result = stateful.stateless.initialize(cx, f); let mut element_state = stateful.stateless.initialize(element_state, None, cx, f);
element_state.focus_handle = focus_handle
.or(element_state.focus_handle.take())
.or_else(|| cx.focused());
stateful.key_listeners.pop(); stateful.key_listeners.pop();
result element_state
}) })
} else { } else {
let stateless = self.as_stateless(); let stateless = self.as_stateless();
cx.with_key_dispatch_context(stateless.dispatch_context.clone(), |cx| { cx.with_key_dispatch_context(stateless.dispatch_context.clone(), |cx| {
cx.with_key_listeners(&stateless.key_listeners, f) cx.with_key_listeners(&stateless.key_listeners, f)
}) });
element_state.unwrap_or_default()
} }
} }
@@ -607,6 +613,7 @@ impl ActiveState {
#[derive(Default)] #[derive(Default)]
pub struct InteractiveElementState { pub struct InteractiveElementState {
focus_handle: Option<FocusHandle>,
active_state: Arc<Mutex<ActiveState>>, active_state: Arc<Mutex<ActiveState>>,
pending_click: Arc<Mutex<Option<MouseDownEvent>>>, pending_click: Arc<Mutex<Option<MouseDownEvent>>>,
} }