mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-04 10:12:47 +00:00
Add right-click support to MouseEventHandler
This commit is contained in:
parent
307eb1726c
commit
1d7fc12229
6 changed files with 77 additions and 1 deletions
|
@ -462,6 +462,7 @@ impl TestAppContext {
|
|||
titlebar_height: 0.,
|
||||
hovered_region_ids: Default::default(),
|
||||
clicked_region_id: None,
|
||||
right_clicked_region_id: None,
|
||||
refreshing: false,
|
||||
};
|
||||
f(view, &mut render_cx)
|
||||
|
@ -1082,6 +1083,7 @@ impl MutableAppContext {
|
|||
titlebar_height,
|
||||
hovered_region_ids: Default::default(),
|
||||
clicked_region_id: None,
|
||||
right_clicked_region_id: None,
|
||||
refreshing: false,
|
||||
})
|
||||
.unwrap(),
|
||||
|
@ -3410,6 +3412,7 @@ pub struct RenderParams {
|
|||
pub titlebar_height: f32,
|
||||
pub hovered_region_ids: HashSet<MouseRegionId>,
|
||||
pub clicked_region_id: Option<MouseRegionId>,
|
||||
pub right_clicked_region_id: Option<MouseRegionId>,
|
||||
pub refreshing: bool,
|
||||
}
|
||||
|
||||
|
@ -3419,6 +3422,7 @@ pub struct RenderContext<'a, T: View> {
|
|||
pub(crate) view_type: PhantomData<T>,
|
||||
pub(crate) hovered_region_ids: HashSet<MouseRegionId>,
|
||||
pub(crate) clicked_region_id: Option<MouseRegionId>,
|
||||
pub(crate) right_clicked_region_id: Option<MouseRegionId>,
|
||||
pub app: &'a mut MutableAppContext,
|
||||
pub titlebar_height: f32,
|
||||
pub refreshing: bool,
|
||||
|
@ -3428,6 +3432,7 @@ pub struct RenderContext<'a, T: View> {
|
|||
pub struct MouseState {
|
||||
pub hovered: bool,
|
||||
pub clicked: bool,
|
||||
pub right_clicked: bool,
|
||||
}
|
||||
|
||||
impl<'a, V: View> RenderContext<'a, V> {
|
||||
|
@ -3440,6 +3445,7 @@ impl<'a, V: View> RenderContext<'a, V> {
|
|||
titlebar_height: params.titlebar_height,
|
||||
hovered_region_ids: params.hovered_region_ids.clone(),
|
||||
clicked_region_id: params.clicked_region_id,
|
||||
right_clicked_region_id: params.right_clicked_region_id,
|
||||
refreshing: params.refreshing,
|
||||
}
|
||||
}
|
||||
|
@ -3461,6 +3467,7 @@ impl<'a, V: View> RenderContext<'a, V> {
|
|||
MouseState {
|
||||
hovered: self.hovered_region_ids.contains(®ion_id),
|
||||
clicked: self.clicked_region_id == Some(region_id),
|
||||
right_clicked: self.right_clicked_region_id == Some(region_id),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,8 @@ pub struct MouseEventHandler {
|
|||
cursor_style: Option<CursorStyle>,
|
||||
mouse_down_handler: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
click_handler: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
|
||||
right_mouse_down_handler: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
right_click_handler: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
|
||||
drag_handler: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
padding: Padding,
|
||||
}
|
||||
|
@ -38,6 +40,8 @@ impl MouseEventHandler {
|
|||
cursor_style: None,
|
||||
mouse_down_handler: None,
|
||||
click_handler: None,
|
||||
right_mouse_down_handler: None,
|
||||
right_click_handler: None,
|
||||
drag_handler: None,
|
||||
padding: Default::default(),
|
||||
}
|
||||
|
@ -64,6 +68,22 @@ impl MouseEventHandler {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn on_right_mouse_down(
|
||||
mut self,
|
||||
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.right_mouse_down_handler = Some(Rc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_right_click(
|
||||
mut self,
|
||||
handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.right_click_handler = Some(Rc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_drag(mut self, handler: impl Fn(Vector2F, &mut EventContext) + 'static) -> Self {
|
||||
self.drag_handler = Some(Rc::new(handler));
|
||||
self
|
||||
|
@ -117,6 +137,8 @@ impl Element for MouseEventHandler {
|
|||
hover: None,
|
||||
click: self.click_handler.clone(),
|
||||
mouse_down: self.mouse_down_handler.clone(),
|
||||
right_click: self.right_click_handler.clone(),
|
||||
right_mouse_down: self.right_mouse_down_handler.clone(),
|
||||
drag: self.drag_handler.clone(),
|
||||
});
|
||||
|
||||
|
|
|
@ -43,6 +43,7 @@ pub enum Event {
|
|||
},
|
||||
RightMouseUp {
|
||||
position: Vector2F,
|
||||
click_count: usize,
|
||||
},
|
||||
NavigateMouseDown {
|
||||
position: Vector2F,
|
||||
|
@ -72,7 +73,7 @@ impl Event {
|
|||
| Event::LeftMouseUp { position, .. }
|
||||
| Event::LeftMouseDragged { position }
|
||||
| Event::RightMouseDown { position, .. }
|
||||
| Event::RightMouseUp { position }
|
||||
| Event::RightMouseUp { position, .. }
|
||||
| Event::NavigateMouseDown { position, .. }
|
||||
| Event::NavigateMouseUp { position, .. }
|
||||
| Event::MouseMoved { position, .. } => Some(*position),
|
||||
|
|
|
@ -178,6 +178,7 @@ impl Event {
|
|||
native_event.locationInWindow().x as f32,
|
||||
window_height - native_event.locationInWindow().y as f32,
|
||||
),
|
||||
click_count: native_event.clickCount() as usize,
|
||||
}),
|
||||
NSEventType::NSOtherMouseDown => {
|
||||
let direction = match native_event.buttonNumber() {
|
||||
|
|
|
@ -33,6 +33,7 @@ pub struct Presenter {
|
|||
last_mouse_moved_event: Option<Event>,
|
||||
hovered_region_ids: HashSet<MouseRegionId>,
|
||||
clicked_region: Option<MouseRegion>,
|
||||
right_clicked_region: Option<MouseRegion>,
|
||||
prev_drag_position: Option<Vector2F>,
|
||||
titlebar_height: f32,
|
||||
}
|
||||
|
@ -58,6 +59,7 @@ impl Presenter {
|
|||
last_mouse_moved_event: None,
|
||||
hovered_region_ids: Default::default(),
|
||||
clicked_region: None,
|
||||
right_clicked_region: None,
|
||||
prev_drag_position: None,
|
||||
titlebar_height,
|
||||
}
|
||||
|
@ -100,6 +102,10 @@ impl Presenter {
|
|||
titlebar_height: self.titlebar_height,
|
||||
hovered_region_ids: self.hovered_region_ids.clone(),
|
||||
clicked_region_id: self.clicked_region.as_ref().map(MouseRegion::id),
|
||||
right_clicked_region_id: self
|
||||
.right_clicked_region
|
||||
.as_ref()
|
||||
.map(MouseRegion::id),
|
||||
refreshing: false,
|
||||
})
|
||||
.unwrap(),
|
||||
|
@ -118,6 +124,10 @@ impl Presenter {
|
|||
titlebar_height: self.titlebar_height,
|
||||
hovered_region_ids: self.hovered_region_ids.clone(),
|
||||
clicked_region_id: self.clicked_region.as_ref().map(MouseRegion::id),
|
||||
right_clicked_region_id: self
|
||||
.right_clicked_region
|
||||
.as_ref()
|
||||
.map(MouseRegion::id),
|
||||
refreshing: true,
|
||||
})
|
||||
.unwrap();
|
||||
|
@ -181,6 +191,7 @@ impl Presenter {
|
|||
refreshing,
|
||||
hovered_region_ids: self.hovered_region_ids.clone(),
|
||||
clicked_region_id: self.clicked_region.as_ref().map(MouseRegion::id),
|
||||
right_clicked_region_id: self.right_clicked_region.as_ref().map(MouseRegion::id),
|
||||
titlebar_height: self.titlebar_height,
|
||||
app: cx,
|
||||
}
|
||||
|
@ -207,6 +218,7 @@ impl Presenter {
|
|||
let mut hovered_regions = Vec::new();
|
||||
let mut unhovered_regions = Vec::new();
|
||||
let mut clicked_region = None;
|
||||
let mut right_clicked_region = None;
|
||||
let mut dragged_region = None;
|
||||
|
||||
match event {
|
||||
|
@ -233,6 +245,27 @@ impl Presenter {
|
|||
}
|
||||
}
|
||||
}
|
||||
Event::RightMouseDown { position, .. } => {
|
||||
for region in self.mouse_regions.iter().rev() {
|
||||
if region.bounds.contains_point(position) {
|
||||
invalidated_views.push(region.view_id);
|
||||
self.right_clicked_region = Some(region.clone());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::RightMouseUp {
|
||||
position,
|
||||
click_count,
|
||||
..
|
||||
} => {
|
||||
if let Some(region) = self.right_clicked_region.take() {
|
||||
invalidated_views.push(region.view_id);
|
||||
if region.bounds.contains_point(position) {
|
||||
right_clicked_region = Some((region, position, click_count));
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::MouseMoved {
|
||||
position,
|
||||
left_mouse_down,
|
||||
|
@ -311,6 +344,14 @@ impl Presenter {
|
|||
}
|
||||
}
|
||||
|
||||
if let Some((right_clicked_region, position, click_count)) = right_clicked_region {
|
||||
if let Some(right_click_callback) = right_clicked_region.right_click {
|
||||
event_cx.with_current_view(right_clicked_region.view_id, |event_cx| {
|
||||
right_click_callback(position, click_count, event_cx);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((dragged_region, delta)) = dragged_region {
|
||||
if let Some(drag_callback) = dragged_region.drag {
|
||||
event_cx.with_current_view(dragged_region.view_id, |event_cx| {
|
||||
|
@ -389,6 +430,7 @@ pub struct LayoutContext<'a> {
|
|||
titlebar_height: f32,
|
||||
hovered_region_ids: HashSet<MouseRegionId>,
|
||||
clicked_region_id: Option<MouseRegionId>,
|
||||
right_clicked_region_id: Option<MouseRegionId>,
|
||||
}
|
||||
|
||||
impl<'a> LayoutContext<'a> {
|
||||
|
@ -418,6 +460,7 @@ impl<'a> LayoutContext<'a> {
|
|||
titlebar_height: self.titlebar_height,
|
||||
hovered_region_ids: self.hovered_region_ids.clone(),
|
||||
clicked_region_id: self.clicked_region_id,
|
||||
right_clicked_region_id: self.right_clicked_region_id,
|
||||
refreshing: self.refreshing,
|
||||
};
|
||||
f(view, &mut render_cx)
|
||||
|
|
|
@ -52,6 +52,8 @@ pub struct MouseRegion {
|
|||
pub hover: Option<Rc<dyn Fn(bool, &mut EventContext)>>,
|
||||
pub mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
pub click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
|
||||
pub right_mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
pub right_click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
|
||||
pub drag: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue