Skip to content

Commit

Permalink
Merge pull request #764 from zed-industries/refine-event-handlers-beh…
Browse files Browse the repository at this point in the history
…avior

Calculate hit-box based on visible bounds in `{Mouse}EventHandler`
  • Loading branch information
nathansobo authored Apr 7, 2022
2 parents ae415ee + b396909 commit 7a151ff
Show file tree
Hide file tree
Showing 23 changed files with 44 additions and 18 deletions.
1 change: 1 addition & 0 deletions crates/editor/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,7 @@ impl Element for EditorElement {
&mut self,
event: &Event,
_: RectF,
_: RectF,
layout: &mut LayoutState,
paint: &mut PaintState,
cx: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl gpui::Element for TextElement {
&mut self,
_: &gpui::Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
_: &mut gpui::EventContext,
Expand Down
10 changes: 9 additions & 1 deletion crates/gpui/src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub trait Element {
&mut self,
event: &Event,
bounds: RectF,
visible_bounds: RectF,
layout: &mut Self::LayoutState,
paint: &mut Self::PaintState,
cx: &mut EventContext,
Expand Down Expand Up @@ -169,6 +170,7 @@ pub enum Lifecycle<T: Element> {
element: T,
constraint: SizeConstraint,
bounds: RectF,
visible_bounds: RectF,
layout: T::LayoutState,
paint: T::PaintState,
},
Expand Down Expand Up @@ -222,6 +224,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
element,
constraint,
bounds,
visible_bounds,
layout,
paint,
}
Expand All @@ -242,6 +245,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
element,
constraint,
bounds,
visible_bounds,
layout,
paint,
}
Expand All @@ -254,12 +258,13 @@ impl<T: Element> AnyElement for Lifecycle<T> {
if let Lifecycle::PostPaint {
element,
bounds,
visible_bounds,
layout,
paint,
..
} = self
{
element.dispatch_event(event, *bounds, layout, paint, cx)
element.dispatch_event(event, *bounds, *visible_bounds, layout, paint, cx)
} else {
panic!("invalid element lifecycle state");
}
Expand Down Expand Up @@ -288,6 +293,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
element,
constraint,
bounds,
visible_bounds,
layout,
paint,
} => {
Expand All @@ -299,6 +305,8 @@ impl<T: Element> AnyElement for Lifecycle<T> {
new_map.insert("type".into(), typ);
}
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);
json::Value::Object(new_map)
} else {
Expand Down
3 changes: 2 additions & 1 deletion crates/gpui/src/elements/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ impl Element for Align {
fn dispatch_event(
&mut self,
event: &Event,
_: pathfinder_geometry::rect::RectF,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ where
&mut self,
_: &crate::Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
_: &mut crate::EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/constrained_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl Element for ConstrainedBox {
&mut self,
event: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ impl Element for Container {
&mut self,
event: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl Element for Empty {
&mut self,
_: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
_: &mut EventContext,
Expand Down
11 changes: 6 additions & 5 deletions crates/gpui/src/elements/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ impl Element for EventHandler {
fn dispatch_event(
&mut self,
event: &Event,
bounds: RectF,
_: RectF,
visible_bounds: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
) -> bool {
if let Some(capture) = self.capture.as_mut() {
if capture(event, bounds, cx) {
if capture(event, visible_bounds, cx) {
return true;
}
}
Expand All @@ -101,15 +102,15 @@ impl Element for EventHandler {
match event {
Event::LeftMouseDown { position, .. } => {
if let Some(callback) = self.mouse_down.as_mut() {
if bounds.contains_point(*position) {
if visible_bounds.contains_point(*position) {
return callback(cx);
}
}
false
}
Event::RightMouseDown { position, .. } => {
if let Some(callback) = self.right_mouse_down.as_mut() {
if bounds.contains_point(*position) {
if visible_bounds.contains_point(*position) {
return callback(cx);
}
}
Expand All @@ -121,7 +122,7 @@ impl Element for EventHandler {
..
} => {
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);
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl Element for Expanded {
&mut self,
event: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
Expand Down
8 changes: 2 additions & 6 deletions crates/gpui/src/elements/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,11 @@ impl Element for Flex {
&mut self,
event: &Event,
bounds: RectF,
_: RectF,
remaining_space: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
) -> bool {
if let Some(position) = event.position() {
if !bounds.contains_point(position) {
return false;
}
}

let mut handled = false;
for child in &mut self.children {
handled = child.dispatch_event(event, cx) || handled;
Expand Down Expand Up @@ -391,6 +386,7 @@ impl Element for FlexItem {
&mut self,
event: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl Element for Hook {
&mut self,
event: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl Element for Image {
&mut self,
_: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
_: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl Element for Label {
&mut self,
_: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
_: &mut EventContext,
Expand Down
2 changes: 2 additions & 0 deletions crates/gpui/src/elements/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ impl Element for List {
&mut self,
event: &Event,
bounds: RectF,
_: RectF,
scroll_top: &mut ListOffset,
_: &mut (),
cx: &mut EventContext,
Expand Down Expand Up @@ -872,6 +873,7 @@ mod tests {
&mut self,
_: &Event,
_: RectF,
_: RectF,
_: &mut (),
_: &mut (),
_: &mut EventContext,
Expand Down
7 changes: 4 additions & 3 deletions crates/gpui/src/elements/mouse_event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ impl Element for MouseEventHandler {
fn dispatch_event(
&mut self,
event: &Event,
bounds: RectF,
_: RectF,
visible_bounds: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
Expand All @@ -112,8 +113,8 @@ impl Element for MouseEventHandler {
let handled_in_child = self.child.dispatch_event(event, cx);

let hit_bounds = RectF::from_points(
bounds.origin() - vec2f(self.padding.left, self.padding.top),
bounds.lower_right() + vec2f(self.padding.right, self.padding.bottom),
visible_bounds.origin() - vec2f(self.padding.left, self.padding.top),
visible_bounds.lower_right() + vec2f(self.padding.right, self.padding.bottom),
)
.round_out();

Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Element for Overlay {
&mut self,
event: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl Element for Stack {
&mut self,
event: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl Element for Svg {
&mut self,
_: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
_: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl Element for Text {
&mut self,
_: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
_: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/elements/uniform_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ where
&mut self,
event: &Event,
bounds: RectF,
_: RectF,
layout: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/presenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ impl Element for ChildView {
&mut self,
event: &Event,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
cx: &mut EventContext,
Expand Down
5 changes: 3 additions & 2 deletions crates/workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use gpui::{
action,
color::Color,
elements::*,
geometry::{vector::vec2f, PathBuilder},
geometry::{rect::RectF, vector::vec2f, PathBuilder},
json::{self, to_string_pretty, ToJson},
keymap::Binding,
platform::{CursorStyle, WindowOptions},
Expand Down Expand Up @@ -2068,7 +2068,8 @@ impl Element for AvatarRibbon {
fn dispatch_event(
&mut self,
_: &gpui::Event,
_: gpui::geometry::rect::RectF,
_: RectF,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
_: &mut gpui::EventContext,
Expand Down

0 comments on commit 7a151ff

Please sign in to comment.