Merge pull request #764 from zed-industries/refine-event-handlers-behavior

Calculate hit-box based on visible bounds in `{Mouse}EventHandler`
This commit is contained in:
Nathan Sobo 2022-04-07 08:16:14 -06:00 committed by GitHub
commit 7a151ffc75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 44 additions and 18 deletions

View file

@ -1147,6 +1147,7 @@ impl Element for EditorElement {
&mut self, &mut self,
event: &Event, event: &Event,
_: RectF, _: RectF,
_: RectF,
layout: &mut LayoutState, layout: &mut LayoutState,
paint: &mut PaintState, paint: &mut PaintState,
cx: &mut EventContext, cx: &mut EventContext,

View file

@ -104,6 +104,7 @@ impl gpui::Element for TextElement {
&mut self, &mut self,
_: &gpui::Event, _: &gpui::Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
_: &mut gpui::EventContext, _: &mut gpui::EventContext,

View file

@ -74,6 +74,7 @@ pub trait Element {
&mut self, &mut self,
event: &Event, event: &Event,
bounds: RectF, bounds: RectF,
visible_bounds: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
paint: &mut Self::PaintState, paint: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,
@ -169,6 +170,7 @@ pub enum Lifecycle<T: Element> {
element: T, element: T,
constraint: SizeConstraint, constraint: SizeConstraint,
bounds: RectF, bounds: RectF,
visible_bounds: RectF,
layout: T::LayoutState, layout: T::LayoutState,
paint: T::PaintState, paint: T::PaintState,
}, },
@ -222,6 +224,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
element, element,
constraint, constraint,
bounds, bounds,
visible_bounds,
layout, layout,
paint, paint,
} }
@ -242,6 +245,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
element, element,
constraint, constraint,
bounds, bounds,
visible_bounds,
layout, layout,
paint, paint,
} }
@ -254,12 +258,13 @@ impl<T: Element> AnyElement for Lifecycle<T> {
if let Lifecycle::PostPaint { if let Lifecycle::PostPaint {
element, element,
bounds, bounds,
visible_bounds,
layout, layout,
paint, paint,
.. ..
} = self } = self
{ {
element.dispatch_event(event, *bounds, layout, paint, cx) element.dispatch_event(event, *bounds, *visible_bounds, layout, paint, cx)
} else { } else {
panic!("invalid element lifecycle state"); panic!("invalid element lifecycle state");
} }
@ -288,6 +293,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
element, element,
constraint, constraint,
bounds, bounds,
visible_bounds,
layout, layout,
paint, paint,
} => { } => {
@ -299,6 +305,8 @@ impl<T: Element> AnyElement for Lifecycle<T> {
new_map.insert("type".into(), typ); new_map.insert("type".into(), typ);
} }
new_map.insert("constraint".into(), constraint.to_json()); new_map.insert("constraint".into(), constraint.to_json());
new_map.insert("bounds".into(), bounds.to_json());
new_map.insert("visible_bounds".into(), visible_bounds.to_json());
new_map.append(map); new_map.append(map);
json::Value::Object(new_map) json::Value::Object(new_map)
} else { } else {

View file

@ -85,7 +85,8 @@ impl Element for Align {
fn dispatch_event( fn dispatch_event(
&mut self, &mut self,
event: &Event, event: &Event,
_: pathfinder_geometry::rect::RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,

View file

@ -59,6 +59,7 @@ where
&mut self, &mut self,
_: &crate::Event, _: &crate::Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
_: &mut crate::EventContext, _: &mut crate::EventContext,

View file

@ -81,6 +81,7 @@ impl Element for ConstrainedBox {
&mut self, &mut self,
event: &Event, event: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,

View file

@ -247,6 +247,7 @@ impl Element for Container {
&mut self, &mut self,
event: &Event, event: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,

View file

@ -52,6 +52,7 @@ impl Element for Empty {
&mut self, &mut self,
_: &Event, _: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
_: &mut EventContext, _: &mut EventContext,

View file

@ -84,13 +84,14 @@ impl Element for EventHandler {
fn dispatch_event( fn dispatch_event(
&mut self, &mut self,
event: &Event, event: &Event,
bounds: RectF, _: RectF,
visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
if let Some(capture) = self.capture.as_mut() { if let Some(capture) = self.capture.as_mut() {
if capture(event, bounds, cx) { if capture(event, visible_bounds, cx) {
return true; return true;
} }
} }
@ -101,7 +102,7 @@ impl Element for EventHandler {
match event { match event {
Event::LeftMouseDown { position, .. } => { Event::LeftMouseDown { position, .. } => {
if let Some(callback) = self.mouse_down.as_mut() { if let Some(callback) = self.mouse_down.as_mut() {
if bounds.contains_point(*position) { if visible_bounds.contains_point(*position) {
return callback(cx); return callback(cx);
} }
} }
@ -109,7 +110,7 @@ impl Element for EventHandler {
} }
Event::RightMouseDown { position, .. } => { Event::RightMouseDown { position, .. } => {
if let Some(callback) = self.right_mouse_down.as_mut() { if let Some(callback) = self.right_mouse_down.as_mut() {
if bounds.contains_point(*position) { if visible_bounds.contains_point(*position) {
return callback(cx); return callback(cx);
} }
} }
@ -121,7 +122,7 @@ impl Element for EventHandler {
.. ..
} => { } => {
if let Some(callback) = self.navigate_mouse_down.as_mut() { if let Some(callback) = self.navigate_mouse_down.as_mut() {
if bounds.contains_point(*position) { if visible_bounds.contains_point(*position) {
return callback(*direction, cx); return callback(*direction, cx);
} }
} }

View file

@ -66,6 +66,7 @@ impl Element for Expanded {
&mut self, &mut self,
event: &Event, event: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,

View file

@ -266,16 +266,11 @@ impl Element for Flex {
&mut self, &mut self,
event: &Event, event: &Event,
bounds: RectF, bounds: RectF,
_: RectF,
remaining_space: &mut Self::LayoutState, remaining_space: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
if let Some(position) = event.position() {
if !bounds.contains_point(position) {
return false;
}
}
let mut handled = false; let mut handled = false;
for child in &mut self.children { for child in &mut self.children {
handled = child.dispatch_event(event, cx) || handled; handled = child.dispatch_event(event, cx) || handled;
@ -391,6 +386,7 @@ impl Element for FlexItem {
&mut self, &mut self,
event: &Event, event: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,

View file

@ -57,6 +57,7 @@ impl Element for Hook {
&mut self, &mut self,
event: &Event, event: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,

View file

@ -81,6 +81,7 @@ impl Element for Image {
&mut self, &mut self,
_: &Event, _: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
_: &mut EventContext, _: &mut EventContext,

View file

@ -166,6 +166,7 @@ impl Element for Label {
&mut self, &mut self,
_: &Event, _: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
_: &mut EventContext, _: &mut EventContext,

View file

@ -253,6 +253,7 @@ impl Element for List {
&mut self, &mut self,
event: &Event, event: &Event,
bounds: RectF, bounds: RectF,
_: RectF,
scroll_top: &mut ListOffset, scroll_top: &mut ListOffset,
_: &mut (), _: &mut (),
cx: &mut EventContext, cx: &mut EventContext,
@ -872,6 +873,7 @@ mod tests {
&mut self, &mut self,
_: &Event, _: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut (), _: &mut (),
_: &mut (), _: &mut (),
_: &mut EventContext, _: &mut EventContext,

View file

@ -99,7 +99,8 @@ impl Element for MouseEventHandler {
fn dispatch_event( fn dispatch_event(
&mut self, &mut self,
event: &Event, event: &Event,
bounds: RectF, _: RectF,
visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,
@ -112,8 +113,8 @@ impl Element for MouseEventHandler {
let handled_in_child = self.child.dispatch_event(event, cx); let handled_in_child = self.child.dispatch_event(event, cx);
let hit_bounds = RectF::from_points( let hit_bounds = RectF::from_points(
bounds.origin() - vec2f(self.padding.left, self.padding.top), visible_bounds.origin() - vec2f(self.padding.left, self.padding.top),
bounds.lower_right() + vec2f(self.padding.right, self.padding.bottom), visible_bounds.lower_right() + vec2f(self.padding.right, self.padding.bottom),
) )
.round_out(); .round_out();

View file

@ -44,6 +44,7 @@ impl Element for Overlay {
&mut self, &mut self,
event: &Event, event: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,

View file

@ -51,6 +51,7 @@ impl Element for Stack {
&mut self, &mut self,
event: &Event, event: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,

View file

@ -76,6 +76,7 @@ impl Element for Svg {
&mut self, &mut self,
_: &Event, _: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
_: &mut EventContext, _: &mut EventContext,

View file

@ -172,6 +172,7 @@ impl Element for Text {
&mut self, &mut self,
_: &Event, _: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
_: &mut EventContext, _: &mut EventContext,

View file

@ -281,6 +281,7 @@ where
&mut self, &mut self,
event: &Event, event: &Event,
bounds: RectF, bounds: RectF,
_: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,

View file

@ -535,6 +535,7 @@ impl Element for ChildView {
&mut self, &mut self,
event: &Event, event: &Event,
_: RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,

View file

@ -16,7 +16,7 @@ use gpui::{
action, action,
color::Color, color::Color,
elements::*, elements::*,
geometry::{vector::vec2f, PathBuilder}, geometry::{rect::RectF, vector::vec2f, PathBuilder},
json::{self, to_string_pretty, ToJson}, json::{self, to_string_pretty, ToJson},
keymap::Binding, keymap::Binding,
platform::{CursorStyle, WindowOptions}, platform::{CursorStyle, WindowOptions},
@ -2068,7 +2068,8 @@ impl Element for AvatarRibbon {
fn dispatch_event( fn dispatch_event(
&mut self, &mut self,
_: &gpui::Event, _: &gpui::Event,
_: gpui::geometry::rect::RectF, _: RectF,
_: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
_: &mut gpui::EventContext, _: &mut gpui::EventContext,