Refine mouse event naming
This commit is contained in:
parent
3e23d1f48d
commit
6e53deb1b2
11 changed files with 353 additions and 393 deletions
|
@ -4,7 +4,7 @@ use collections::HashSet;
|
|||
use gpui::{
|
||||
elements::{MouseEventHandler, Overlay},
|
||||
geometry::vector::Vector2F,
|
||||
scene::DragRegionEvent,
|
||||
scene::MouseDrag,
|
||||
CursorStyle, Element, ElementBox, EventContext, MouseButton, MutableAppContext, RenderContext,
|
||||
View, WeakViewHandle,
|
||||
};
|
||||
|
@ -70,7 +70,7 @@ impl<V: View> DragAndDrop<V> {
|
|||
}
|
||||
|
||||
pub fn dragging<T: Any>(
|
||||
event: DragRegionEvent,
|
||||
event: MouseDrag,
|
||||
payload: Rc<T>,
|
||||
cx: &mut EventContext,
|
||||
render: Rc<impl 'static + Fn(&T, &mut RenderContext<V>) -> ElementBox>,
|
||||
|
|
|
@ -6,9 +6,8 @@ use crate::{
|
|||
},
|
||||
platform::CursorStyle,
|
||||
scene::{
|
||||
ClickRegionEvent, CursorRegion, DownOutRegionEvent, DownRegionEvent, DragRegionEvent,
|
||||
HandlerSet, HoverRegionEvent, MoveRegionEvent, ScrollWheelRegionEvent, UpOutRegionEvent,
|
||||
UpRegionEvent,
|
||||
CursorRegion, HandlerSet, MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseHover,
|
||||
MouseMove, MouseScrollWheel, MouseUp, MouseUpOut,
|
||||
},
|
||||
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MeasurementContext,
|
||||
MouseButton, MouseRegion, MouseState, PaintContext, RenderContext, SizeConstraint, View,
|
||||
|
@ -61,10 +60,7 @@ impl<Tag> MouseEventHandler<Tag> {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn on_move(
|
||||
mut self,
|
||||
handler: impl Fn(MoveRegionEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
pub fn on_move(mut self, handler: impl Fn(MouseMove, &mut EventContext) + 'static) -> Self {
|
||||
self.handlers = self.handlers.on_move(handler);
|
||||
self
|
||||
}
|
||||
|
@ -72,7 +68,7 @@ impl<Tag> MouseEventHandler<Tag> {
|
|||
pub fn on_down(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(DownRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseDown, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_down(button, handler);
|
||||
self
|
||||
|
@ -81,7 +77,7 @@ impl<Tag> MouseEventHandler<Tag> {
|
|||
pub fn on_up(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(UpRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseUp, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_up(button, handler);
|
||||
self
|
||||
|
@ -90,7 +86,7 @@ impl<Tag> MouseEventHandler<Tag> {
|
|||
pub fn on_click(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(ClickRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseClick, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_click(button, handler);
|
||||
self
|
||||
|
@ -99,7 +95,7 @@ impl<Tag> MouseEventHandler<Tag> {
|
|||
pub fn on_down_out(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(DownOutRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseDownOut, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_down_out(button, handler);
|
||||
self
|
||||
|
@ -108,7 +104,7 @@ impl<Tag> MouseEventHandler<Tag> {
|
|||
pub fn on_up_out(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(UpOutRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseUpOut, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_up_out(button, handler);
|
||||
self
|
||||
|
@ -117,23 +113,20 @@ impl<Tag> MouseEventHandler<Tag> {
|
|||
pub fn on_drag(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(DragRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseDrag, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_drag(button, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_hover(
|
||||
mut self,
|
||||
handler: impl Fn(HoverRegionEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
pub fn on_hover(mut self, handler: impl Fn(MouseHover, &mut EventContext) + 'static) -> Self {
|
||||
self.handlers = self.handlers.on_hover(handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_scroll(
|
||||
mut self,
|
||||
handler: impl Fn(ScrollWheelRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseScrollWheel, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_scroll(handler);
|
||||
self
|
||||
|
|
|
@ -4,7 +4,7 @@ use pathfinder_geometry::vector::{vec2f, Vector2F};
|
|||
use serde_json::json;
|
||||
|
||||
use crate::{
|
||||
geometry::rect::RectF, scene::DragRegionEvent, Axis, CursorStyle, Element, ElementBox,
|
||||
geometry::rect::RectF, scene::MouseDrag, Axis, CursorStyle, Element, ElementBox,
|
||||
ElementStateHandle, MouseButton, MouseRegion, RenderContext, View,
|
||||
};
|
||||
|
||||
|
@ -42,7 +42,7 @@ impl Side {
|
|||
}
|
||||
}
|
||||
|
||||
fn compute_delta(&self, e: DragRegionEvent) -> f32 {
|
||||
fn compute_delta(&self, e: MouseDrag) -> f32 {
|
||||
if self.before_content() {
|
||||
self.relevant_component(e.prev_mouse_position) - self.relevant_component(e.position)
|
||||
} else {
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::{
|
|||
},
|
||||
json::{self, json},
|
||||
presenter::MeasurementContext,
|
||||
scene::ScrollWheelRegionEvent,
|
||||
scene::MouseScrollWheel,
|
||||
ElementBox, MouseRegion, RenderContext, ScrollWheelEvent, View,
|
||||
};
|
||||
use json::ToJson;
|
||||
|
@ -292,7 +292,7 @@ impl Element for UniformList {
|
|||
MouseRegion::new::<Self>(self.view_id, 0, visible_bounds).on_scroll({
|
||||
let scroll_max = layout.scroll_max;
|
||||
let state = self.state.clone();
|
||||
move |ScrollWheelRegionEvent {
|
||||
move |MouseScrollWheel {
|
||||
platform_event:
|
||||
ScrollWheelEvent {
|
||||
position,
|
||||
|
|
|
@ -7,9 +7,8 @@ use crate::{
|
|||
keymap::Keystroke,
|
||||
platform::{CursorStyle, Event},
|
||||
scene::{
|
||||
ClickRegionEvent, CursorRegion, DownOutRegionEvent, DownRegionEvent, DragRegionEvent,
|
||||
HoverRegionEvent, MouseRegionEvent, MoveRegionEvent, ScrollWheelRegionEvent,
|
||||
UpOutRegionEvent, UpRegionEvent,
|
||||
CursorRegion, MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseEvent, MouseHover,
|
||||
MouseMove, MouseScrollWheel, MouseUp, MouseUpOut,
|
||||
},
|
||||
text_layout::TextLayoutCache,
|
||||
Action, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle, Appearance,
|
||||
|
@ -230,12 +229,12 @@ impl Presenter {
|
|||
cx: &mut MutableAppContext,
|
||||
) -> bool {
|
||||
if let Some(root_view_id) = cx.root_view_id(self.window_id) {
|
||||
let mut events_to_send = Vec::new();
|
||||
let mut mouse_events = SmallVec::<[_; 2]>::new();
|
||||
let mut notified_views: HashSet<usize> = Default::default();
|
||||
|
||||
// 1. Allocate the correct set of GPUI events generated from the platform events
|
||||
// 1. Map the platform event into mouse events, which interact with mouse regions.
|
||||
// -> These are usually small: [Mouse Down] or [Mouse up, Click] or [Mouse Moved, Mouse Dragged?]
|
||||
// -> Also moves around mouse related state
|
||||
// -> Also updates mouse-related state
|
||||
match &event {
|
||||
Event::MouseDown(e) => {
|
||||
// Click events are weird because they can be fired after a drag event.
|
||||
|
@ -260,28 +259,28 @@ impl Presenter {
|
|||
self.clicked_button = Some(e.button);
|
||||
}
|
||||
|
||||
events_to_send.push(MouseRegionEvent::Down(DownRegionEvent {
|
||||
mouse_events.push(MouseEvent::Down(MouseDown {
|
||||
region: Default::default(),
|
||||
platform_event: e.clone(),
|
||||
}));
|
||||
events_to_send.push(MouseRegionEvent::DownOut(DownOutRegionEvent {
|
||||
mouse_events.push(MouseEvent::DownOut(MouseDownOut {
|
||||
region: Default::default(),
|
||||
platform_event: e.clone(),
|
||||
}));
|
||||
}
|
||||
Event::MouseUp(e) => {
|
||||
// NOTE: The order of event pushes is important! MouseUp events MUST be fired
|
||||
// before click events, and so the UpRegionEvent events need to be pushed before
|
||||
// ClickRegionEvents
|
||||
events_to_send.push(MouseRegionEvent::Up(UpRegionEvent {
|
||||
// before click events, and so the MouseUp events need to be pushed before
|
||||
// MouseClick events.
|
||||
mouse_events.push(MouseEvent::Up(MouseUp {
|
||||
region: Default::default(),
|
||||
platform_event: e.clone(),
|
||||
}));
|
||||
events_to_send.push(MouseRegionEvent::UpOut(UpOutRegionEvent {
|
||||
mouse_events.push(MouseEvent::UpOut(MouseUpOut {
|
||||
region: Default::default(),
|
||||
platform_event: e.clone(),
|
||||
}));
|
||||
events_to_send.push(MouseRegionEvent::Click(ClickRegionEvent {
|
||||
mouse_events.push(MouseEvent::Click(MouseClick {
|
||||
region: Default::default(),
|
||||
platform_event: e.clone(),
|
||||
}));
|
||||
|
@ -304,7 +303,7 @@ impl Presenter {
|
|||
|
||||
if !event_reused {
|
||||
if pressed_button.is_some() {
|
||||
events_to_send.push(MouseRegionEvent::Drag(DragRegionEvent {
|
||||
mouse_events.push(MouseEvent::Drag(MouseDrag {
|
||||
region: Default::default(),
|
||||
prev_mouse_position: self.mouse_position,
|
||||
platform_event: e.clone(),
|
||||
|
@ -312,27 +311,27 @@ impl Presenter {
|
|||
} else if let Some(clicked_button) = self.clicked_button {
|
||||
// Mouse up event happened outside the current window. Simulate mouse up button event
|
||||
let button_event = e.to_button_event(clicked_button);
|
||||
events_to_send.push(MouseRegionEvent::Up(UpRegionEvent {
|
||||
mouse_events.push(MouseEvent::Up(MouseUp {
|
||||
region: Default::default(),
|
||||
platform_event: button_event.clone(),
|
||||
}));
|
||||
events_to_send.push(MouseRegionEvent::UpOut(UpOutRegionEvent {
|
||||
mouse_events.push(MouseEvent::UpOut(MouseUpOut {
|
||||
region: Default::default(),
|
||||
platform_event: button_event.clone(),
|
||||
}));
|
||||
events_to_send.push(MouseRegionEvent::Click(ClickRegionEvent {
|
||||
mouse_events.push(MouseEvent::Click(MouseClick {
|
||||
region: Default::default(),
|
||||
platform_event: button_event.clone(),
|
||||
}));
|
||||
}
|
||||
|
||||
events_to_send.push(MouseRegionEvent::Move(MoveRegionEvent {
|
||||
mouse_events.push(MouseEvent::Move(MouseMove {
|
||||
region: Default::default(),
|
||||
platform_event: e.clone(),
|
||||
}));
|
||||
}
|
||||
|
||||
events_to_send.push(MouseRegionEvent::Hover(HoverRegionEvent {
|
||||
mouse_events.push(MouseEvent::Hover(MouseHover {
|
||||
region: Default::default(),
|
||||
platform_event: e.clone(),
|
||||
started: false,
|
||||
|
@ -341,7 +340,7 @@ impl Presenter {
|
|||
self.last_mouse_moved_event = Some(event.clone());
|
||||
}
|
||||
Event::ScrollWheel(e) => {
|
||||
events_to_send.push(MouseRegionEvent::ScrollWheel(ScrollWheelRegionEvent {
|
||||
mouse_events.push(MouseEvent::ScrollWheel(MouseScrollWheel {
|
||||
region: Default::default(),
|
||||
platform_event: e.clone(),
|
||||
}))
|
||||
|
@ -354,17 +353,16 @@ impl Presenter {
|
|||
self.mouse_position = position;
|
||||
}
|
||||
|
||||
// 2. Dispatch mouse events on regions
|
||||
let mut any_event_handled = false;
|
||||
// 2. Process the raw mouse events into region events
|
||||
for mut region_event in events_to_send {
|
||||
for mut mouse_event in mouse_events {
|
||||
let mut valid_regions = Vec::new();
|
||||
|
||||
// GPUI elements are arranged by depth but sibling elements can register overlapping
|
||||
// mouse regions. As such, hover events are only fired on overlapping elements which
|
||||
// are at the same depth as the topmost element which overlaps with the mouse.
|
||||
|
||||
match ®ion_event {
|
||||
MouseRegionEvent::Hover(_) => {
|
||||
match &mouse_event {
|
||||
MouseEvent::Hover(_) => {
|
||||
let mut top_most_depth = None;
|
||||
let mouse_position = self.mouse_position.clone();
|
||||
for (region, depth) in self.mouse_regions.iter().rev() {
|
||||
|
@ -402,7 +400,7 @@ impl Presenter {
|
|||
}
|
||||
}
|
||||
}
|
||||
MouseRegionEvent::Down(_) | MouseRegionEvent::Up(_) => {
|
||||
MouseEvent::Down(_) | MouseEvent::Up(_) => {
|
||||
for (region, _) in self.mouse_regions.iter().rev() {
|
||||
if region.bounds.contains_point(self.mouse_position) {
|
||||
if region.notify_on_click {
|
||||
|
@ -412,7 +410,7 @@ impl Presenter {
|
|||
}
|
||||
}
|
||||
}
|
||||
MouseRegionEvent::Click(e) => {
|
||||
MouseEvent::Click(e) => {
|
||||
// Only raise click events if the released button is the same as the one stored
|
||||
if self
|
||||
.clicked_button
|
||||
|
@ -434,7 +432,7 @@ impl Presenter {
|
|||
}
|
||||
}
|
||||
}
|
||||
MouseRegionEvent::Drag(_) => {
|
||||
MouseEvent::Drag(_) => {
|
||||
for (mouse_region, _) in self.mouse_regions.iter().rev() {
|
||||
if self.clicked_region_ids.contains(&mouse_region.id()) {
|
||||
valid_regions.push(mouse_region.clone());
|
||||
|
@ -442,7 +440,7 @@ impl Presenter {
|
|||
}
|
||||
}
|
||||
|
||||
MouseRegionEvent::UpOut(_) | MouseRegionEvent::DownOut(_) => {
|
||||
MouseEvent::UpOut(_) | MouseEvent::DownOut(_) => {
|
||||
for (mouse_region, _) in self.mouse_regions.iter().rev() {
|
||||
// NOT contains
|
||||
if !mouse_region.bounds.contains_point(self.mouse_position) {
|
||||
|
@ -465,29 +463,29 @@ impl Presenter {
|
|||
for valid_region in valid_regions.into_iter() {
|
||||
let mut event_cx = self.build_event_context(&mut notified_views, cx);
|
||||
|
||||
region_event.set_region(valid_region.bounds);
|
||||
if let MouseRegionEvent::Hover(e) = &mut region_event {
|
||||
mouse_event.set_region(valid_region.bounds);
|
||||
if let MouseEvent::Hover(e) = &mut mouse_event {
|
||||
e.started = hovered_region_ids.contains(&valid_region.id())
|
||||
}
|
||||
// Handle Down events if the MouseRegion has a Click or Drag handler. This makes the api more intuitive as you would
|
||||
// not expect a MouseRegion to be transparent to Down events if it also has a Click handler.
|
||||
// This behavior can be overridden by adding a Down handler that calls cx.propogate_event
|
||||
if let MouseRegionEvent::Down(e) = ®ion_event {
|
||||
if let MouseEvent::Down(e) = &mouse_event {
|
||||
if valid_region
|
||||
.handlers
|
||||
.contains_handler(MouseRegionEvent::click_disc(), Some(e.button))
|
||||
.contains_handler(MouseEvent::click_disc(), Some(e.button))
|
||||
|| valid_region
|
||||
.handlers
|
||||
.contains_handler(MouseRegionEvent::drag_disc(), Some(e.button))
|
||||
.contains_handler(MouseEvent::drag_disc(), Some(e.button))
|
||||
{
|
||||
event_cx.handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(callback) = valid_region.handlers.get(®ion_event.handler_key()) {
|
||||
if let Some(callback) = valid_region.handlers.get(&mouse_event.handler_key()) {
|
||||
event_cx.handled = true;
|
||||
event_cx.with_current_view(valid_region.id().view_id(), {
|
||||
let region_event = region_event.clone();
|
||||
let region_event = mouse_event.clone();
|
||||
|cx| {
|
||||
callback(region_event, cx);
|
||||
}
|
||||
|
@ -497,7 +495,7 @@ impl Presenter {
|
|||
any_event_handled = any_event_handled || event_cx.handled;
|
||||
// For bubbling events, if the event was handled, don't continue dispatching
|
||||
// This only makes sense for local events.
|
||||
if event_cx.handled && region_event.is_capturable() {
|
||||
if event_cx.handled && mouse_event.is_capturable() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
mod mouse_event;
|
||||
mod mouse_region;
|
||||
mod mouse_region_event;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
use collections::HashSet;
|
||||
|
@ -15,8 +15,8 @@ use crate::{
|
|||
platform::{current::Surface, CursorStyle},
|
||||
ImageData,
|
||||
};
|
||||
pub use mouse_event::*;
|
||||
pub use mouse_region::*;
|
||||
pub use mouse_region_event::*;
|
||||
|
||||
pub struct Scene {
|
||||
scale_factor: f32,
|
||||
|
|
233
crates/gpui/src/scene/mouse_event.rs
Normal file
233
crates/gpui/src/scene/mouse_event.rs
Normal file
|
@ -0,0 +1,233 @@
|
|||
use std::{
|
||||
mem::{discriminant, Discriminant},
|
||||
ops::Deref,
|
||||
};
|
||||
|
||||
use pathfinder_geometry::{rect::RectF, vector::Vector2F};
|
||||
|
||||
use crate::{MouseButton, MouseButtonEvent, MouseMovedEvent, ScrollWheelEvent};
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct MouseMove {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseMovedEvent,
|
||||
}
|
||||
|
||||
impl Deref for MouseMove {
|
||||
type Target = MouseMovedEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct MouseDrag {
|
||||
pub region: RectF,
|
||||
pub prev_mouse_position: Vector2F,
|
||||
pub platform_event: MouseMovedEvent,
|
||||
}
|
||||
|
||||
impl Deref for MouseDrag {
|
||||
type Target = MouseMovedEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct MouseHover {
|
||||
pub region: RectF,
|
||||
pub started: bool,
|
||||
pub platform_event: MouseMovedEvent,
|
||||
}
|
||||
|
||||
impl Deref for MouseHover {
|
||||
type Target = MouseMovedEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct MouseDown {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseButtonEvent,
|
||||
}
|
||||
|
||||
impl Deref for MouseDown {
|
||||
type Target = MouseButtonEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct MouseUp {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseButtonEvent,
|
||||
}
|
||||
|
||||
impl Deref for MouseUp {
|
||||
type Target = MouseButtonEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct MouseClick {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseButtonEvent,
|
||||
}
|
||||
|
||||
impl Deref for MouseClick {
|
||||
type Target = MouseButtonEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct MouseDownOut {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseButtonEvent,
|
||||
}
|
||||
|
||||
impl Deref for MouseDownOut {
|
||||
type Target = MouseButtonEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct MouseUpOut {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseButtonEvent,
|
||||
}
|
||||
|
||||
impl Deref for MouseUpOut {
|
||||
type Target = MouseButtonEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct MouseScrollWheel {
|
||||
pub region: RectF,
|
||||
pub platform_event: ScrollWheelEvent,
|
||||
}
|
||||
|
||||
impl Deref for MouseScrollWheel {
|
||||
type Target = ScrollWheelEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum MouseEvent {
|
||||
Move(MouseMove),
|
||||
Drag(MouseDrag),
|
||||
Hover(MouseHover),
|
||||
Down(MouseDown),
|
||||
Up(MouseUp),
|
||||
Click(MouseClick),
|
||||
DownOut(MouseDownOut),
|
||||
UpOut(MouseUpOut),
|
||||
ScrollWheel(MouseScrollWheel),
|
||||
}
|
||||
|
||||
impl MouseEvent {
|
||||
pub fn set_region(&mut self, region: RectF) {
|
||||
match self {
|
||||
MouseEvent::Move(r) => r.region = region,
|
||||
MouseEvent::Drag(r) => r.region = region,
|
||||
MouseEvent::Hover(r) => r.region = region,
|
||||
MouseEvent::Down(r) => r.region = region,
|
||||
MouseEvent::Up(r) => r.region = region,
|
||||
MouseEvent::Click(r) => r.region = region,
|
||||
MouseEvent::DownOut(r) => r.region = region,
|
||||
MouseEvent::UpOut(r) => r.region = region,
|
||||
MouseEvent::ScrollWheel(r) => r.region = region,
|
||||
}
|
||||
}
|
||||
|
||||
/// When true, mouse event handlers must call cx.propagate_event() to bubble
|
||||
/// the event to handlers they are painted on top of.
|
||||
pub fn is_capturable(&self) -> bool {
|
||||
match self {
|
||||
MouseEvent::Move(_) => true,
|
||||
MouseEvent::Drag(_) => true,
|
||||
MouseEvent::Hover(_) => false,
|
||||
MouseEvent::Down(_) => true,
|
||||
MouseEvent::Up(_) => true,
|
||||
MouseEvent::Click(_) => true,
|
||||
MouseEvent::DownOut(_) => false,
|
||||
MouseEvent::UpOut(_) => false,
|
||||
MouseEvent::ScrollWheel(_) => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MouseEvent {
|
||||
pub fn move_disc() -> Discriminant<MouseEvent> {
|
||||
discriminant(&MouseEvent::Move(Default::default()))
|
||||
}
|
||||
|
||||
pub fn drag_disc() -> Discriminant<MouseEvent> {
|
||||
discriminant(&MouseEvent::Drag(Default::default()))
|
||||
}
|
||||
|
||||
pub fn hover_disc() -> Discriminant<MouseEvent> {
|
||||
discriminant(&MouseEvent::Hover(Default::default()))
|
||||
}
|
||||
|
||||
pub fn down_disc() -> Discriminant<MouseEvent> {
|
||||
discriminant(&MouseEvent::Down(Default::default()))
|
||||
}
|
||||
|
||||
pub fn up_disc() -> Discriminant<MouseEvent> {
|
||||
discriminant(&MouseEvent::Up(Default::default()))
|
||||
}
|
||||
|
||||
pub fn up_out_disc() -> Discriminant<MouseEvent> {
|
||||
discriminant(&MouseEvent::UpOut(Default::default()))
|
||||
}
|
||||
|
||||
pub fn click_disc() -> Discriminant<MouseEvent> {
|
||||
discriminant(&MouseEvent::Click(Default::default()))
|
||||
}
|
||||
|
||||
pub fn down_out_disc() -> Discriminant<MouseEvent> {
|
||||
discriminant(&MouseEvent::DownOut(Default::default()))
|
||||
}
|
||||
|
||||
pub fn scroll_wheel_disc() -> Discriminant<MouseEvent> {
|
||||
discriminant(&MouseEvent::ScrollWheel(Default::default()))
|
||||
}
|
||||
|
||||
pub fn handler_key(&self) -> (Discriminant<MouseEvent>, Option<MouseButton>) {
|
||||
match self {
|
||||
MouseEvent::Move(_) => (Self::move_disc(), None),
|
||||
MouseEvent::Drag(e) => (Self::drag_disc(), e.pressed_button),
|
||||
MouseEvent::Hover(_) => (Self::hover_disc(), None),
|
||||
MouseEvent::Down(e) => (Self::down_disc(), Some(e.button)),
|
||||
MouseEvent::Up(e) => (Self::up_disc(), Some(e.button)),
|
||||
MouseEvent::Click(e) => (Self::click_disc(), Some(e.button)),
|
||||
MouseEvent::UpOut(e) => (Self::up_out_disc(), Some(e.button)),
|
||||
MouseEvent::DownOut(e) => (Self::down_out_disc(), Some(e.button)),
|
||||
MouseEvent::ScrollWheel(_) => (Self::scroll_wheel_disc(), None),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,11 +7,11 @@ use pathfinder_geometry::rect::RectF;
|
|||
use crate::{EventContext, MouseButton};
|
||||
|
||||
use super::{
|
||||
mouse_region_event::{
|
||||
ClickRegionEvent, DownOutRegionEvent, DownRegionEvent, DragRegionEvent, HoverRegionEvent,
|
||||
MouseRegionEvent, MoveRegionEvent, UpOutRegionEvent, UpRegionEvent,
|
||||
mouse_event::{
|
||||
MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseEvent, MouseHover, MouseMove, MouseUp,
|
||||
MouseUpOut,
|
||||
},
|
||||
ScrollWheelRegionEvent,
|
||||
MouseScrollWheel,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
|
@ -62,7 +62,7 @@ impl MouseRegion {
|
|||
pub fn on_down(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(DownRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseDown, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_down(button, handler);
|
||||
self
|
||||
|
@ -71,7 +71,7 @@ impl MouseRegion {
|
|||
pub fn on_up(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(UpRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseUp, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_up(button, handler);
|
||||
self
|
||||
|
@ -80,7 +80,7 @@ impl MouseRegion {
|
|||
pub fn on_click(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(ClickRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseClick, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_click(button, handler);
|
||||
self
|
||||
|
@ -89,7 +89,7 @@ impl MouseRegion {
|
|||
pub fn on_down_out(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(DownOutRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseDownOut, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_down_out(button, handler);
|
||||
self
|
||||
|
@ -98,7 +98,7 @@ impl MouseRegion {
|
|||
pub fn on_up_out(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(UpOutRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseUpOut, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_up_out(button, handler);
|
||||
self
|
||||
|
@ -107,31 +107,25 @@ impl MouseRegion {
|
|||
pub fn on_drag(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(DragRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseDrag, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_drag(button, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_hover(
|
||||
mut self,
|
||||
handler: impl Fn(HoverRegionEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
pub fn on_hover(mut self, handler: impl Fn(MouseHover, &mut EventContext) + 'static) -> Self {
|
||||
self.handlers = self.handlers.on_hover(handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_move(
|
||||
mut self,
|
||||
handler: impl Fn(MoveRegionEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
pub fn on_move(mut self, handler: impl Fn(MouseMove, &mut EventContext) + 'static) -> Self {
|
||||
self.handlers = self.handlers.on_move(handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_scroll(
|
||||
mut self,
|
||||
handler: impl Fn(ScrollWheelRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseScrollWheel, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_scroll(handler);
|
||||
self
|
||||
|
@ -187,8 +181,8 @@ impl MouseRegionId {
|
|||
pub struct HandlerSet {
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub set: HashMap<
|
||||
(Discriminant<MouseRegionEvent>, Option<MouseButton>),
|
||||
Rc<dyn Fn(MouseRegionEvent, &mut EventContext)>,
|
||||
(Discriminant<MouseEvent>, Option<MouseButton>),
|
||||
Rc<dyn Fn(MouseEvent, &mut EventContext)>,
|
||||
>,
|
||||
}
|
||||
|
||||
|
@ -196,68 +190,50 @@ impl HandlerSet {
|
|||
pub fn capture_all() -> Self {
|
||||
#[allow(clippy::type_complexity)]
|
||||
let mut set: HashMap<
|
||||
(Discriminant<MouseRegionEvent>, Option<MouseButton>),
|
||||
Rc<dyn Fn(MouseRegionEvent, &mut EventContext)>,
|
||||
(Discriminant<MouseEvent>, Option<MouseButton>),
|
||||
Rc<dyn Fn(MouseEvent, &mut EventContext)>,
|
||||
> = Default::default();
|
||||
|
||||
set.insert((MouseRegionEvent::move_disc(), None), Rc::new(|_, _| {}));
|
||||
set.insert((MouseRegionEvent::hover_disc(), None), Rc::new(|_, _| {}));
|
||||
set.insert((MouseEvent::move_disc(), None), Rc::new(|_, _| {}));
|
||||
set.insert((MouseEvent::hover_disc(), None), Rc::new(|_, _| {}));
|
||||
for button in MouseButton::all() {
|
||||
set.insert((MouseEvent::drag_disc(), Some(button)), Rc::new(|_, _| {}));
|
||||
set.insert((MouseEvent::down_disc(), Some(button)), Rc::new(|_, _| {}));
|
||||
set.insert((MouseEvent::up_disc(), Some(button)), Rc::new(|_, _| {}));
|
||||
set.insert((MouseEvent::click_disc(), Some(button)), Rc::new(|_, _| {}));
|
||||
set.insert(
|
||||
(MouseRegionEvent::drag_disc(), Some(button)),
|
||||
(MouseEvent::down_out_disc(), Some(button)),
|
||||
Rc::new(|_, _| {}),
|
||||
);
|
||||
set.insert(
|
||||
(MouseRegionEvent::down_disc(), Some(button)),
|
||||
Rc::new(|_, _| {}),
|
||||
);
|
||||
set.insert(
|
||||
(MouseRegionEvent::up_disc(), Some(button)),
|
||||
Rc::new(|_, _| {}),
|
||||
);
|
||||
set.insert(
|
||||
(MouseRegionEvent::click_disc(), Some(button)),
|
||||
Rc::new(|_, _| {}),
|
||||
);
|
||||
set.insert(
|
||||
(MouseRegionEvent::down_out_disc(), Some(button)),
|
||||
Rc::new(|_, _| {}),
|
||||
);
|
||||
set.insert(
|
||||
(MouseRegionEvent::up_out_disc(), Some(button)),
|
||||
(MouseEvent::up_out_disc(), Some(button)),
|
||||
Rc::new(|_, _| {}),
|
||||
);
|
||||
}
|
||||
set.insert(
|
||||
(MouseRegionEvent::scroll_wheel_disc(), None),
|
||||
Rc::new(|_, _| {}),
|
||||
);
|
||||
set.insert((MouseEvent::scroll_wheel_disc(), None), Rc::new(|_, _| {}));
|
||||
|
||||
HandlerSet { set }
|
||||
}
|
||||
|
||||
pub fn get(
|
||||
&self,
|
||||
key: &(Discriminant<MouseRegionEvent>, Option<MouseButton>),
|
||||
) -> Option<Rc<dyn Fn(MouseRegionEvent, &mut EventContext)>> {
|
||||
key: &(Discriminant<MouseEvent>, Option<MouseButton>),
|
||||
) -> Option<Rc<dyn Fn(MouseEvent, &mut EventContext)>> {
|
||||
self.set.get(key).cloned()
|
||||
}
|
||||
|
||||
pub fn contains_handler(
|
||||
&self,
|
||||
event: Discriminant<MouseRegionEvent>,
|
||||
event: Discriminant<MouseEvent>,
|
||||
button: Option<MouseButton>,
|
||||
) -> bool {
|
||||
self.set.contains_key(&(event, button))
|
||||
}
|
||||
|
||||
pub fn on_move(
|
||||
mut self,
|
||||
handler: impl Fn(MoveRegionEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::move_disc(), None),
|
||||
pub fn on_move(mut self, handler: impl Fn(MouseMove, &mut EventContext) + 'static) -> Self {
|
||||
self.set.insert((MouseEvent::move_disc(), None),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::Move(e) = region_event {
|
||||
if let MouseEvent::Move(e) = region_event {
|
||||
handler(e, cx);
|
||||
} else {
|
||||
panic!(
|
||||
|
@ -271,11 +247,11 @@ impl HandlerSet {
|
|||
pub fn on_down(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(DownRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseDown, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::down_disc(), Some(button)),
|
||||
self.set.insert((MouseEvent::down_disc(), Some(button)),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::Down(e) = region_event {
|
||||
if let MouseEvent::Down(e) = region_event {
|
||||
handler(e, cx);
|
||||
} else {
|
||||
panic!(
|
||||
|
@ -289,11 +265,11 @@ impl HandlerSet {
|
|||
pub fn on_up(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(UpRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseUp, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::up_disc(), Some(button)),
|
||||
self.set.insert((MouseEvent::up_disc(), Some(button)),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::Up(e) = region_event {
|
||||
if let MouseEvent::Up(e) = region_event {
|
||||
handler(e, cx);
|
||||
} else {
|
||||
panic!(
|
||||
|
@ -307,11 +283,11 @@ impl HandlerSet {
|
|||
pub fn on_click(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(ClickRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseClick, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::click_disc(), Some(button)),
|
||||
self.set.insert((MouseEvent::click_disc(), Some(button)),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::Click(e) = region_event {
|
||||
if let MouseEvent::Click(e) = region_event {
|
||||
handler(e, cx);
|
||||
} else {
|
||||
panic!(
|
||||
|
@ -325,11 +301,11 @@ impl HandlerSet {
|
|||
pub fn on_down_out(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(DownOutRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseDownOut, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::down_out_disc(), Some(button)),
|
||||
self.set.insert((MouseEvent::down_out_disc(), Some(button)),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::DownOut(e) = region_event {
|
||||
if let MouseEvent::DownOut(e) = region_event {
|
||||
handler(e, cx);
|
||||
} else {
|
||||
panic!(
|
||||
|
@ -343,11 +319,11 @@ impl HandlerSet {
|
|||
pub fn on_up_out(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(UpOutRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseUpOut, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::up_out_disc(), Some(button)),
|
||||
self.set.insert((MouseEvent::up_out_disc(), Some(button)),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::UpOut(e) = region_event {
|
||||
if let MouseEvent::UpOut(e) = region_event {
|
||||
handler(e, cx);
|
||||
} else {
|
||||
panic!(
|
||||
|
@ -361,11 +337,11 @@ impl HandlerSet {
|
|||
pub fn on_drag(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(DragRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseDrag, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::drag_disc(), Some(button)),
|
||||
self.set.insert((MouseEvent::drag_disc(), Some(button)),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::Drag(e) = region_event {
|
||||
if let MouseEvent::Drag(e) = region_event {
|
||||
handler(e, cx);
|
||||
} else {
|
||||
panic!(
|
||||
|
@ -376,13 +352,10 @@ impl HandlerSet {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn on_hover(
|
||||
mut self,
|
||||
handler: impl Fn(HoverRegionEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::hover_disc(), None),
|
||||
pub fn on_hover(mut self, handler: impl Fn(MouseHover, &mut EventContext) + 'static) -> Self {
|
||||
self.set.insert((MouseEvent::hover_disc(), None),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::Hover(e) = region_event {
|
||||
if let MouseEvent::Hover(e) = region_event {
|
||||
handler(e, cx);
|
||||
} else {
|
||||
panic!(
|
||||
|
@ -395,11 +368,11 @@ impl HandlerSet {
|
|||
|
||||
pub fn on_scroll(
|
||||
mut self,
|
||||
handler: impl Fn(ScrollWheelRegionEvent, &mut EventContext) + 'static,
|
||||
handler: impl Fn(MouseScrollWheel, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::scroll_wheel_disc(), None),
|
||||
self.set.insert((MouseEvent::scroll_wheel_disc(), None),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::ScrollWheel(e) = region_event {
|
||||
if let MouseEvent::ScrollWheel(e) = region_event {
|
||||
handler(e, cx);
|
||||
} else {
|
||||
panic!(
|
||||
|
|
|
@ -1,233 +0,0 @@
|
|||
use std::{
|
||||
mem::{discriminant, Discriminant},
|
||||
ops::Deref,
|
||||
};
|
||||
|
||||
use pathfinder_geometry::{rect::RectF, vector::Vector2F};
|
||||
|
||||
use crate::{MouseButton, MouseButtonEvent, MouseMovedEvent, ScrollWheelEvent};
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct MoveRegionEvent {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseMovedEvent,
|
||||
}
|
||||
|
||||
impl Deref for MoveRegionEvent {
|
||||
type Target = MouseMovedEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct DragRegionEvent {
|
||||
pub region: RectF,
|
||||
pub prev_mouse_position: Vector2F,
|
||||
pub platform_event: MouseMovedEvent,
|
||||
}
|
||||
|
||||
impl Deref for DragRegionEvent {
|
||||
type Target = MouseMovedEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct HoverRegionEvent {
|
||||
pub region: RectF,
|
||||
pub started: bool,
|
||||
pub platform_event: MouseMovedEvent,
|
||||
}
|
||||
|
||||
impl Deref for HoverRegionEvent {
|
||||
type Target = MouseMovedEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct DownRegionEvent {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseButtonEvent,
|
||||
}
|
||||
|
||||
impl Deref for DownRegionEvent {
|
||||
type Target = MouseButtonEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct UpRegionEvent {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseButtonEvent,
|
||||
}
|
||||
|
||||
impl Deref for UpRegionEvent {
|
||||
type Target = MouseButtonEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct ClickRegionEvent {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseButtonEvent,
|
||||
}
|
||||
|
||||
impl Deref for ClickRegionEvent {
|
||||
type Target = MouseButtonEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct DownOutRegionEvent {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseButtonEvent,
|
||||
}
|
||||
|
||||
impl Deref for DownOutRegionEvent {
|
||||
type Target = MouseButtonEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct UpOutRegionEvent {
|
||||
pub region: RectF,
|
||||
pub platform_event: MouseButtonEvent,
|
||||
}
|
||||
|
||||
impl Deref for UpOutRegionEvent {
|
||||
type Target = MouseButtonEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct ScrollWheelRegionEvent {
|
||||
pub region: RectF,
|
||||
pub platform_event: ScrollWheelEvent,
|
||||
}
|
||||
|
||||
impl Deref for ScrollWheelRegionEvent {
|
||||
type Target = ScrollWheelEvent;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.platform_event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum MouseRegionEvent {
|
||||
Move(MoveRegionEvent),
|
||||
Drag(DragRegionEvent),
|
||||
Hover(HoverRegionEvent),
|
||||
Down(DownRegionEvent),
|
||||
Up(UpRegionEvent),
|
||||
Click(ClickRegionEvent),
|
||||
DownOut(DownOutRegionEvent),
|
||||
UpOut(UpOutRegionEvent),
|
||||
ScrollWheel(ScrollWheelRegionEvent),
|
||||
}
|
||||
|
||||
impl MouseRegionEvent {
|
||||
pub fn set_region(&mut self, region: RectF) {
|
||||
match self {
|
||||
MouseRegionEvent::Move(r) => r.region = region,
|
||||
MouseRegionEvent::Drag(r) => r.region = region,
|
||||
MouseRegionEvent::Hover(r) => r.region = region,
|
||||
MouseRegionEvent::Down(r) => r.region = region,
|
||||
MouseRegionEvent::Up(r) => r.region = region,
|
||||
MouseRegionEvent::Click(r) => r.region = region,
|
||||
MouseRegionEvent::DownOut(r) => r.region = region,
|
||||
MouseRegionEvent::UpOut(r) => r.region = region,
|
||||
MouseRegionEvent::ScrollWheel(r) => r.region = region,
|
||||
}
|
||||
}
|
||||
|
||||
/// When true, mouse event handlers must call cx.propagate_event() to bubble
|
||||
/// the event to handlers they are painted on top of.
|
||||
pub fn is_capturable(&self) -> bool {
|
||||
match self {
|
||||
MouseRegionEvent::Move(_) => true,
|
||||
MouseRegionEvent::Drag(_) => true,
|
||||
MouseRegionEvent::Hover(_) => false,
|
||||
MouseRegionEvent::Down(_) => true,
|
||||
MouseRegionEvent::Up(_) => true,
|
||||
MouseRegionEvent::Click(_) => true,
|
||||
MouseRegionEvent::DownOut(_) => false,
|
||||
MouseRegionEvent::UpOut(_) => false,
|
||||
MouseRegionEvent::ScrollWheel(_) => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MouseRegionEvent {
|
||||
pub fn move_disc() -> Discriminant<MouseRegionEvent> {
|
||||
discriminant(&MouseRegionEvent::Move(Default::default()))
|
||||
}
|
||||
|
||||
pub fn drag_disc() -> Discriminant<MouseRegionEvent> {
|
||||
discriminant(&MouseRegionEvent::Drag(Default::default()))
|
||||
}
|
||||
|
||||
pub fn hover_disc() -> Discriminant<MouseRegionEvent> {
|
||||
discriminant(&MouseRegionEvent::Hover(Default::default()))
|
||||
}
|
||||
|
||||
pub fn down_disc() -> Discriminant<MouseRegionEvent> {
|
||||
discriminant(&MouseRegionEvent::Down(Default::default()))
|
||||
}
|
||||
|
||||
pub fn up_disc() -> Discriminant<MouseRegionEvent> {
|
||||
discriminant(&MouseRegionEvent::Up(Default::default()))
|
||||
}
|
||||
|
||||
pub fn up_out_disc() -> Discriminant<MouseRegionEvent> {
|
||||
discriminant(&MouseRegionEvent::UpOut(Default::default()))
|
||||
}
|
||||
|
||||
pub fn click_disc() -> Discriminant<MouseRegionEvent> {
|
||||
discriminant(&MouseRegionEvent::Click(Default::default()))
|
||||
}
|
||||
|
||||
pub fn down_out_disc() -> Discriminant<MouseRegionEvent> {
|
||||
discriminant(&MouseRegionEvent::DownOut(Default::default()))
|
||||
}
|
||||
|
||||
pub fn scroll_wheel_disc() -> Discriminant<MouseRegionEvent> {
|
||||
discriminant(&MouseRegionEvent::ScrollWheel(Default::default()))
|
||||
}
|
||||
|
||||
pub fn handler_key(&self) -> (Discriminant<MouseRegionEvent>, Option<MouseButton>) {
|
||||
match self {
|
||||
MouseRegionEvent::Move(_) => (Self::move_disc(), None),
|
||||
MouseRegionEvent::Drag(e) => (Self::drag_disc(), e.pressed_button),
|
||||
MouseRegionEvent::Hover(_) => (Self::hover_disc(), None),
|
||||
MouseRegionEvent::Down(e) => (Self::down_disc(), Some(e.button)),
|
||||
MouseRegionEvent::Up(e) => (Self::up_disc(), Some(e.button)),
|
||||
MouseRegionEvent::Click(e) => (Self::click_disc(), Some(e.button)),
|
||||
MouseRegionEvent::UpOut(e) => (Self::up_out_disc(), Some(e.button)),
|
||||
MouseRegionEvent::DownOut(e) => (Self::down_out_disc(), Some(e.button)),
|
||||
MouseRegionEvent::ScrollWheel(_) => (Self::scroll_wheel_disc(), None),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ use alacritty_terminal::grid::Dimensions;
|
|||
/// with modifications for our circumstances
|
||||
use alacritty_terminal::index::{Column as GridCol, Line as GridLine, Point, Side};
|
||||
use alacritty_terminal::term::TermMode;
|
||||
use gpui::scene::ScrollWheelRegionEvent;
|
||||
use gpui::scene::MouseScrollWheel;
|
||||
use gpui::{geometry::vector::Vector2F, MouseButtonEvent, MouseMovedEvent, ScrollWheelEvent};
|
||||
|
||||
use crate::TerminalSize;
|
||||
|
@ -115,7 +115,7 @@ impl MouseButton {
|
|||
pub fn scroll_report(
|
||||
point: Point,
|
||||
scroll_lines: i32,
|
||||
e: &ScrollWheelRegionEvent,
|
||||
e: &MouseScrollWheel,
|
||||
mode: TermMode,
|
||||
) -> Option<impl Iterator<Item = Vec<u8>>> {
|
||||
if mode.intersects(TermMode::MOUSE_MODE) {
|
||||
|
|
|
@ -53,7 +53,7 @@ use thiserror::Error;
|
|||
use gpui::{
|
||||
geometry::vector::{vec2f, Vector2F},
|
||||
keymap::Keystroke,
|
||||
scene::{DownRegionEvent, DragRegionEvent, ScrollWheelRegionEvent, UpRegionEvent},
|
||||
scene::{MouseDown, MouseDrag, MouseScrollWheel, MouseUp},
|
||||
ClipboardItem, Entity, ModelContext, MouseButton, MouseMovedEvent, MutableAppContext, Task,
|
||||
};
|
||||
|
||||
|
@ -971,7 +971,7 @@ impl Terminal {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn mouse_drag(&mut self, e: DragRegionEvent, origin: Vector2F) {
|
||||
pub fn mouse_drag(&mut self, e: MouseDrag, origin: Vector2F) {
|
||||
let position = e.position.sub(origin);
|
||||
self.last_mouse_position = Some(position);
|
||||
|
||||
|
@ -997,7 +997,7 @@ impl Terminal {
|
|||
}
|
||||
}
|
||||
|
||||
fn drag_line_delta(&mut self, e: DragRegionEvent) -> Option<f32> {
|
||||
fn drag_line_delta(&mut self, e: MouseDrag) -> Option<f32> {
|
||||
//TODO: Why do these need to be doubled? Probably the same problem that the IME has
|
||||
let top = e.region.origin_y() + (self.last_content.size.line_height * 2.);
|
||||
let bottom = e.region.lower_left().y() - (self.last_content.size.line_height * 2.);
|
||||
|
@ -1011,7 +1011,7 @@ impl Terminal {
|
|||
Some(scroll_delta)
|
||||
}
|
||||
|
||||
pub fn mouse_down(&mut self, e: &DownRegionEvent, origin: Vector2F) {
|
||||
pub fn mouse_down(&mut self, e: &MouseDown, origin: Vector2F) {
|
||||
let position = e.position.sub(origin);
|
||||
let point = grid_point(
|
||||
position,
|
||||
|
@ -1029,7 +1029,7 @@ impl Terminal {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn left_click(&mut self, e: &DownRegionEvent, origin: Vector2F) {
|
||||
pub fn left_click(&mut self, e: &MouseDown, origin: Vector2F) {
|
||||
let position = e.position.sub(origin);
|
||||
if !self.mouse_mode(e.shift) {
|
||||
//Hyperlinks
|
||||
|
@ -1071,7 +1071,7 @@ impl Terminal {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn mouse_up(&mut self, e: &UpRegionEvent, origin: Vector2F, cx: &mut ModelContext<Self>) {
|
||||
pub fn mouse_up(&mut self, e: &MouseUp, origin: Vector2F, cx: &mut ModelContext<Self>) {
|
||||
let settings = cx.global::<Settings>();
|
||||
let copy_on_select = settings
|
||||
.terminal_overrides
|
||||
|
@ -1103,7 +1103,7 @@ impl Terminal {
|
|||
}
|
||||
|
||||
///Scroll the terminal
|
||||
pub fn scroll_wheel(&mut self, e: ScrollWheelRegionEvent, origin: Vector2F) {
|
||||
pub fn scroll_wheel(&mut self, e: MouseScrollWheel, origin: Vector2F) {
|
||||
let mouse_mode = self.mouse_mode(e.shift);
|
||||
|
||||
if let Some(scroll_lines) = self.determine_scroll_lines(&e, mouse_mode) {
|
||||
|
@ -1142,11 +1142,7 @@ impl Terminal {
|
|||
self.hyperlink_from_position(self.last_mouse_position);
|
||||
}
|
||||
|
||||
fn determine_scroll_lines(
|
||||
&mut self,
|
||||
e: &ScrollWheelRegionEvent,
|
||||
mouse_mode: bool,
|
||||
) -> Option<i32> {
|
||||
fn determine_scroll_lines(&mut self, e: &MouseScrollWheel, mouse_mode: bool) -> Option<i32> {
|
||||
let scroll_multiplier = if mouse_mode { 1. } else { SCROLL_MULTIPLIER };
|
||||
|
||||
match e.phase {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue