Checkpoint
This commit is contained in:
+122
-34
@@ -1,10 +1,11 @@
|
||||
use crate::{
|
||||
BorrowWindow, Bounds, DispatchPhase, ElementId, FocusHandle, FocusListeners, KeyDownEvent,
|
||||
KeyListener, KeyMatch, LayoutId, MouseClickEvent, MouseClickListener, MouseDownEvent,
|
||||
MouseDownListener, MouseMoveEvent, MouseMoveListener, MouseUpEvent, MouseUpListener, Pixels,
|
||||
Point, ScrollWheelEvent, ScrollWheelListener, Style, StyleRefinement, ViewContext,
|
||||
WindowContext,
|
||||
AppContext, BorrowWindow, Bounds, DispatchPhase, ElementId, FocusHandle, FocusListeners,
|
||||
KeyDownEvent, KeyListener, KeyMatch, LayoutId, MouseClickEvent, MouseClickListener,
|
||||
MouseDownEvent, MouseDownListener, MouseMoveEvent, MouseMoveListener, MouseUpEvent,
|
||||
MouseUpListener, Pixels, Point, ScrollWheelEvent, ScrollWheelListener, SharedString, Style,
|
||||
StyleRefinement, ViewContext, WindowContext,
|
||||
};
|
||||
use collections::HashMap;
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use parking_lot::Mutex;
|
||||
use refineable::Refineable;
|
||||
@@ -49,6 +50,52 @@ pub trait ElementInteractivity<V: 'static + Send + Sync>: 'static + Send + Sync
|
||||
fn as_stateful(&self) -> Option<&StatefulInteractivity<V>>;
|
||||
fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity<V>>;
|
||||
|
||||
fn initialize<R>(
|
||||
&mut self,
|
||||
cx: &mut ViewContext<V>,
|
||||
f: impl FnOnce(&mut ViewContext<V>) -> R,
|
||||
) -> R {
|
||||
if let Some(stateful) = self.as_stateful_mut() {
|
||||
cx.with_element_id(stateful.id.clone(), |global_id, cx| {
|
||||
stateful.key_listeners.push((
|
||||
TypeId::of::<KeyDownEvent>(),
|
||||
Arc::new(move |_, key_down, context, phase, cx| {
|
||||
if phase == DispatchPhase::Bubble {
|
||||
let key_down = key_down.downcast_ref::<KeyDownEvent>().unwrap();
|
||||
if let KeyMatch::Some(action) =
|
||||
cx.match_keystroke(&global_id, &key_down.keystroke, context)
|
||||
{
|
||||
return Some(action);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}),
|
||||
));
|
||||
let result = stateful.stateless.initialize(cx, f);
|
||||
stateful.key_listeners.pop();
|
||||
result
|
||||
})
|
||||
} else {
|
||||
cx.with_key_listeners(&self.as_stateless().key_listeners, f)
|
||||
}
|
||||
}
|
||||
|
||||
fn refine_style(&self, style: &mut Style, bounds: Bounds<Pixels>, cx: &mut ViewContext<V>) {
|
||||
let mouse_position = cx.mouse_position();
|
||||
let stateless = self.as_stateless();
|
||||
if let Some(group_hover) = stateless.group_hover.as_ref() {
|
||||
if let Some(group_bounds) = group_bounds(&group_hover.group, cx) {
|
||||
if group_bounds.contains_point(&mouse_position) {
|
||||
style.refine(&group_hover.style);
|
||||
}
|
||||
}
|
||||
}
|
||||
if bounds.contains_point(&mouse_position) {
|
||||
style.refine(&stateless.hover_style);
|
||||
}
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
bounds: Bounds<Pixels>,
|
||||
@@ -80,6 +127,19 @@ pub trait ElementInteractivity<V: 'static + Send + Sync>: 'static + Send + Sync
|
||||
})
|
||||
}
|
||||
|
||||
let hover_group_bounds = stateless
|
||||
.group_hover
|
||||
.as_ref()
|
||||
.and_then(|group_hover| GroupBounds::get(&group_hover.group, cx));
|
||||
|
||||
if let Some(group_bounds) = hover_group_bounds {
|
||||
paint_hover_listener(group_bounds, cx);
|
||||
}
|
||||
|
||||
if stateless.hover_style.is_some() {
|
||||
paint_hover_listener(bounds, cx);
|
||||
}
|
||||
|
||||
if let Some(stateful) = self.as_stateful() {
|
||||
let click_listeners = stateful.mouse_click_listeners.clone();
|
||||
|
||||
@@ -107,37 +167,20 @@ pub trait ElementInteractivity<V: 'static + Send + Sync>: 'static + Send + Sync
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn initialize<R>(
|
||||
&mut self,
|
||||
cx: &mut ViewContext<V>,
|
||||
f: impl FnOnce(&mut ViewContext<V>) -> R,
|
||||
) -> R {
|
||||
if let Some(stateful) = self.as_stateful_mut() {
|
||||
cx.with_element_id(stateful.id.clone(), |global_id, cx| {
|
||||
stateful.key_listeners.push((
|
||||
TypeId::of::<KeyDownEvent>(),
|
||||
Arc::new(move |_, key_down, context, phase, cx| {
|
||||
if phase == DispatchPhase::Bubble {
|
||||
let key_down = key_down.downcast_ref::<KeyDownEvent>().unwrap();
|
||||
if let KeyMatch::Some(action) =
|
||||
cx.match_keystroke(&global_id, &key_down.keystroke, context)
|
||||
{
|
||||
return Some(action);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}),
|
||||
));
|
||||
let result = stateful.stateless.initialize(cx, f);
|
||||
stateful.key_listeners.pop();
|
||||
result
|
||||
})
|
||||
} else {
|
||||
cx.with_key_listeners(&self.as_stateless().key_listeners, f)
|
||||
fn paint_hover_listener<V>(bounds: Bounds<Pixels>, cx: &mut ViewContext<V>)
|
||||
where
|
||||
V: 'static + Send + Sync,
|
||||
{
|
||||
let hovered = bounds.contains_point(&cx.mouse_position());
|
||||
cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
|
||||
if phase == DispatchPhase::Capture {
|
||||
if bounds.contains_point(&event.position) != hovered {
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(Deref, DerefMut)]
|
||||
@@ -189,6 +232,49 @@ pub struct StatelessInteractivity<V> {
|
||||
pub mouse_move_listeners: SmallVec<[MouseMoveListener<V>; 2]>,
|
||||
pub scroll_wheel_listeners: SmallVec<[ScrollWheelListener<V>; 2]>,
|
||||
pub key_listeners: SmallVec<[(TypeId, KeyListener<V>); 32]>,
|
||||
pub hover_style: StyleRefinement,
|
||||
pub group_hover: Option<GroupStyle>,
|
||||
}
|
||||
|
||||
pub struct GroupStyle {
|
||||
pub group: SharedString,
|
||||
pub style: StyleRefinement,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct GroupBounds(HashMap<SharedString, SmallVec<[Bounds<Pixels>; 1]>>);
|
||||
|
||||
impl GroupBounds {
|
||||
pub fn get(name: &SharedString, cx: &mut AppContext) -> Option<Bounds<Pixels>> {
|
||||
cx.default_global::<Self>()
|
||||
.0
|
||||
.get(name)
|
||||
.and_then(|bounds_stack| bounds_stack.last())
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub fn push(name: SharedString, bounds: Bounds<Pixels>, cx: &mut AppContext) {
|
||||
cx.default_global::<Self>()
|
||||
.0
|
||||
.entry(name)
|
||||
.or_default()
|
||||
.push(bounds);
|
||||
}
|
||||
|
||||
pub fn pop(name: &SharedString, cx: &mut AppContext) {
|
||||
cx.default_global::<GroupBounds>()
|
||||
.0
|
||||
.get_mut(name)
|
||||
.unwrap()
|
||||
.pop();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn group_bounds(name: &SharedString, cx: &mut AppContext) -> Option<Bounds<Pixels>> {
|
||||
cx.default_global::<GroupBounds>()
|
||||
.0
|
||||
.get(name)
|
||||
.and_then(|bounds_stack| bounds_stack.last().cloned())
|
||||
}
|
||||
|
||||
impl<V> Default for StatelessInteractivity<V> {
|
||||
@@ -199,6 +285,8 @@ impl<V> Default for StatelessInteractivity<V> {
|
||||
mouse_move_listeners: SmallVec::new(),
|
||||
scroll_wheel_listeners: SmallVec::new(),
|
||||
key_listeners: SmallVec::new(),
|
||||
hover_style: StyleRefinement::default(),
|
||||
group_hover: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
use crate::{
|
||||
Active, AnyElement, AppContext, BorrowWindow, Bounds, DispatchPhase, Element,
|
||||
ElementFocusability, ElementId, ElementInteractivity, Focus, FocusHandle, FocusListeners,
|
||||
Focusable, GlobalElementId, Hover, IntoAnyElement, LayoutId, MouseDownEvent, MouseMoveEvent,
|
||||
Active, AnyElement, BorrowWindow, Bounds, DispatchPhase, Element, ElementFocusability,
|
||||
ElementId, ElementInteractivity, Focus, FocusHandle, FocusListeners, Focusable,
|
||||
GlobalElementId, GroupBounds, GroupStyle, Hover, IntoAnyElement, LayoutId, MouseDownEvent,
|
||||
MouseUpEvent, NonFocusable, Overflow, ParentElement, Pixels, Point, SharedString,
|
||||
StatefulInteractivity, StatefullyInteractive, StatelessInteractivity, StatelesslyInteractive,
|
||||
Style, StyleRefinement, Styled, ViewContext,
|
||||
};
|
||||
use collections::HashMap;
|
||||
use parking_lot::Mutex;
|
||||
use refineable::Refineable;
|
||||
use smallvec::SmallVec;
|
||||
@@ -30,16 +29,6 @@ impl ActiveState {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct GroupBounds(HashMap<SharedString, SmallVec<[Bounds<Pixels>; 1]>>);
|
||||
|
||||
pub fn group_bounds(name: &SharedString, cx: &mut AppContext) -> Option<Bounds<Pixels>> {
|
||||
cx.default_global::<GroupBounds>()
|
||||
.0
|
||||
.get(name)
|
||||
.and_then(|bounds_stack| bounds_stack.last().cloned())
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct ScrollState(Arc<Mutex<Point<Pixels>>>);
|
||||
|
||||
@@ -71,8 +60,6 @@ pub struct Div<
|
||||
children: SmallVec<[AnyElement<V>; 2]>,
|
||||
group: Option<SharedString>,
|
||||
base_style: StyleRefinement,
|
||||
hover_style: StyleRefinement,
|
||||
group_hover: Option<GroupStyle>,
|
||||
active_style: StyleRefinement,
|
||||
group_active: Option<GroupStyle>,
|
||||
}
|
||||
@@ -87,18 +74,11 @@ where
|
||||
children: SmallVec::new(),
|
||||
group: None,
|
||||
base_style: StyleRefinement::default(),
|
||||
hover_style: StyleRefinement::default(),
|
||||
group_hover: None,
|
||||
active_style: StyleRefinement::default(),
|
||||
group_active: None,
|
||||
}
|
||||
}
|
||||
|
||||
struct GroupStyle {
|
||||
group: SharedString,
|
||||
style: StyleRefinement,
|
||||
}
|
||||
|
||||
impl<V, F> Div<V, StatelessInteractivity<V>, F>
|
||||
where
|
||||
F: ElementFocusability<V>,
|
||||
@@ -111,8 +91,6 @@ where
|
||||
children: self.children,
|
||||
group: self.group,
|
||||
base_style: self.base_style,
|
||||
hover_style: self.hover_style,
|
||||
group_hover: self.group_hover,
|
||||
active_style: self.active_style,
|
||||
group_active: self.group_active,
|
||||
}
|
||||
@@ -195,19 +173,8 @@ where
|
||||
computed_style.refine(&self.base_style);
|
||||
|
||||
self.focusability.refine_style(&mut computed_style, cx);
|
||||
|
||||
let mouse_position = cx.mouse_position();
|
||||
|
||||
if let Some(group_hover) = self.group_hover.as_ref() {
|
||||
if let Some(group_bounds) = group_bounds(&group_hover.group, cx) {
|
||||
if group_bounds.contains_point(&mouse_position) {
|
||||
computed_style.refine(&group_hover.style);
|
||||
}
|
||||
}
|
||||
}
|
||||
if bounds.contains_point(&mouse_position) {
|
||||
computed_style.refine(&self.hover_style);
|
||||
}
|
||||
self.interactivity
|
||||
.refine_style(&mut computed_style, bounds, cx);
|
||||
|
||||
let active_state = *state.active_state.lock();
|
||||
if active_state.group {
|
||||
@@ -222,21 +189,6 @@ where
|
||||
computed_style
|
||||
}
|
||||
|
||||
fn paint_hover_listeners(
|
||||
&self,
|
||||
bounds: Bounds<Pixels>,
|
||||
group_bounds: Option<Bounds<Pixels>>,
|
||||
cx: &mut ViewContext<V>,
|
||||
) {
|
||||
if let Some(group_bounds) = group_bounds {
|
||||
paint_hover_listener(group_bounds, cx);
|
||||
}
|
||||
|
||||
if self.hover_style.is_some() {
|
||||
paint_hover_listener(bounds, cx);
|
||||
}
|
||||
}
|
||||
|
||||
fn paint_active_listener(
|
||||
&self,
|
||||
bounds: Bounds<Pixels>,
|
||||
@@ -279,8 +231,6 @@ where
|
||||
children: self.children,
|
||||
group: self.group,
|
||||
base_style: self.base_style,
|
||||
hover_style: self.hover_style,
|
||||
group_hover: self.group_hover,
|
||||
active_style: self.active_style,
|
||||
group_active: self.group_active,
|
||||
}
|
||||
@@ -372,21 +322,13 @@ where
|
||||
) {
|
||||
self.with_element_id(cx, |this, _global_id, cx| {
|
||||
if let Some(group) = this.group.clone() {
|
||||
cx.default_global::<GroupBounds>()
|
||||
.0
|
||||
.entry(group)
|
||||
.or_default()
|
||||
.push(bounds);
|
||||
GroupBounds::push(group, bounds, cx);
|
||||
}
|
||||
|
||||
let hover_group_bounds = this
|
||||
.group_hover
|
||||
.as_ref()
|
||||
.and_then(|group_hover| group_bounds(&group_hover.group, cx));
|
||||
let active_group_bounds = this
|
||||
.group_active
|
||||
.as_ref()
|
||||
.and_then(|group_active| group_bounds(&group_active.group, cx));
|
||||
.and_then(|group_active| GroupBounds::get(&group_active.group, cx));
|
||||
let style = this.compute_style(bounds, element_state, cx);
|
||||
let z_index = style.z_index.unwrap_or(0);
|
||||
|
||||
@@ -394,7 +336,6 @@ where
|
||||
cx.stack(z_index, |cx| {
|
||||
cx.stack(0, |cx| {
|
||||
style.paint(bounds, cx);
|
||||
this.paint_hover_listeners(bounds, hover_group_bounds, cx);
|
||||
this.paint_active_listener(
|
||||
bounds,
|
||||
active_group_bounds,
|
||||
@@ -418,11 +359,7 @@ where
|
||||
});
|
||||
|
||||
if let Some(group) = this.group.as_ref() {
|
||||
cx.default_global::<GroupBounds>()
|
||||
.0
|
||||
.get_mut(group)
|
||||
.unwrap()
|
||||
.pop();
|
||||
GroupBounds::pop(group, cx);
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -479,10 +416,11 @@ where
|
||||
V: 'static + Send + Sync,
|
||||
{
|
||||
fn set_hover_style(&mut self, group: Option<SharedString>, style: StyleRefinement) {
|
||||
let stateless = self.interactivity.as_stateless_mut();
|
||||
if let Some(group) = group {
|
||||
self.group_hover = Some(GroupStyle { group, style });
|
||||
stateless.group_hover = Some(GroupStyle { group, style });
|
||||
} else {
|
||||
self.hover_style = style;
|
||||
stateless.hover_style = style;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -510,17 +448,3 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn paint_hover_listener<V>(bounds: Bounds<Pixels>, cx: &mut ViewContext<V>)
|
||||
where
|
||||
V: 'static + Send + Sync,
|
||||
{
|
||||
let hovered = bounds.contains_point(&cx.mouse_position());
|
||||
cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
|
||||
if phase == DispatchPhase::Capture {
|
||||
if bounds.contains_point(&event.position) != hovered {
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user