Merge branch 'main' into ime-support-2
This commit is contained in:
commit
7b009c8bbe
209 changed files with 1859 additions and 9740 deletions
|
@ -5581,7 +5581,7 @@ impl RefCounts {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{actions, elements::*, impl_actions, MouseButton, MouseEvent};
|
||||
use crate::{actions, elements::*, impl_actions, MouseButton, MouseButtonEvent};
|
||||
use serde::Deserialize;
|
||||
use smol::future::poll_once;
|
||||
use std::{
|
||||
|
@ -5934,7 +5934,7 @@ mod tests {
|
|||
let presenter = cx.presenters_and_platform_windows[&window_id].0.clone();
|
||||
// Ensure window's root element is in a valid lifecycle state.
|
||||
presenter.borrow_mut().dispatch_event(
|
||||
Event::MouseDown(MouseEvent {
|
||||
Event::MouseDown(MouseButtonEvent {
|
||||
position: Default::default(),
|
||||
button: MouseButton::Left,
|
||||
ctrl: false,
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
use crate::{
|
||||
geometry::vector::Vector2F, presenter::MeasurementContext, CursorRegion, DebugContext, Element,
|
||||
ElementBox, Event, EventContext, LayoutContext, MouseButton, MouseEvent, MouseRegion,
|
||||
ElementBox, Event, EventContext, LayoutContext, MouseButton, MouseButtonEvent, MouseRegion,
|
||||
NavigationDirection, PaintContext, SizeConstraint,
|
||||
};
|
||||
use pathfinder_geometry::rect::RectF;
|
||||
use serde_json::json;
|
||||
use std::{any::TypeId, ops::Range, rc::Rc};
|
||||
use std::{any::TypeId, ops::Range};
|
||||
|
||||
pub struct EventHandler {
|
||||
child: ElementBox,
|
||||
|
@ -82,19 +82,11 @@ impl Element for EventHandler {
|
|||
bounds: visible_bounds,
|
||||
style: Default::default(),
|
||||
});
|
||||
cx.scene.push_mouse_region(MouseRegion {
|
||||
view_id: cx.current_view_id(),
|
||||
discriminant: Some(discriminant),
|
||||
bounds: visible_bounds,
|
||||
hover: Some(Rc::new(|_, _, _| {})),
|
||||
mouse_down: Some(Rc::new(|_, _| {})),
|
||||
click: Some(Rc::new(|_, _, _| {})),
|
||||
right_mouse_down: Some(Rc::new(|_, _| {})),
|
||||
right_click: Some(Rc::new(|_, _, _| {})),
|
||||
drag: Some(Rc::new(|_, _, _| {})),
|
||||
mouse_down_out: Some(Rc::new(|_, _| {})),
|
||||
right_mouse_down_out: Some(Rc::new(|_, _| {})),
|
||||
});
|
||||
cx.scene.push_mouse_region(MouseRegion::handle_all(
|
||||
cx.current_view_id(),
|
||||
Some(discriminant),
|
||||
visible_bounds,
|
||||
));
|
||||
cx.scene.pop_stacking_context();
|
||||
}
|
||||
self.child.paint(bounds.origin(), visible_bounds, cx);
|
||||
|
@ -117,7 +109,7 @@ impl Element for EventHandler {
|
|||
true
|
||||
} else {
|
||||
match event {
|
||||
Event::MouseDown(MouseEvent {
|
||||
Event::MouseDown(MouseButtonEvent {
|
||||
button: MouseButton::Left,
|
||||
position,
|
||||
..
|
||||
|
@ -129,7 +121,7 @@ impl Element for EventHandler {
|
|||
}
|
||||
false
|
||||
}
|
||||
Event::MouseDown(MouseEvent {
|
||||
Event::MouseDown(MouseButtonEvent {
|
||||
button: MouseButton::Right,
|
||||
position,
|
||||
..
|
||||
|
@ -141,7 +133,7 @@ impl Element for EventHandler {
|
|||
}
|
||||
false
|
||||
}
|
||||
Event::MouseDown(MouseEvent {
|
||||
Event::MouseDown(MouseButtonEvent {
|
||||
button: MouseButton::Navigate(direction),
|
||||
position,
|
||||
..
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
use std::{any::TypeId, ops::Range, rc::Rc};
|
||||
|
||||
use super::Padding;
|
||||
use crate::{
|
||||
geometry::{
|
||||
|
@ -7,25 +5,19 @@ use crate::{
|
|||
vector::{vec2f, Vector2F},
|
||||
},
|
||||
platform::CursorStyle,
|
||||
scene::CursorRegion,
|
||||
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MouseRegion,
|
||||
MouseState, PaintContext, RenderContext, SizeConstraint, View, presenter::MeasurementContext,
|
||||
scene::{CursorRegion, HandlerSet},
|
||||
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, MeasurementContext,
|
||||
MouseButton, MouseButtonEvent, MouseMovedEvent, MouseRegion, MouseState, PaintContext,
|
||||
RenderContext, SizeConstraint, View,
|
||||
};
|
||||
use serde_json::json;
|
||||
use std::{any::TypeId, ops::Range};
|
||||
|
||||
pub struct MouseEventHandler {
|
||||
child: ElementBox,
|
||||
tag: TypeId,
|
||||
id: usize,
|
||||
discriminant: (TypeId, usize),
|
||||
cursor_style: Option<CursorStyle>,
|
||||
mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
|
||||
right_mouse_down: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
right_click: Option<Rc<dyn Fn(Vector2F, usize, &mut EventContext)>>,
|
||||
mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
right_mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
drag: Option<Rc<dyn Fn(Vector2F, Vector2F, &mut EventContext)>>,
|
||||
hover: Option<Rc<dyn Fn(Vector2F, bool, &mut EventContext)>>,
|
||||
handlers: HandlerSet,
|
||||
padding: Padding,
|
||||
}
|
||||
|
||||
|
@ -37,18 +29,10 @@ impl MouseEventHandler {
|
|||
F: FnOnce(MouseState, &mut RenderContext<V>) -> ElementBox,
|
||||
{
|
||||
Self {
|
||||
id,
|
||||
tag: TypeId::of::<Tag>(),
|
||||
child: render_child(cx.mouse_state::<Tag>(id), cx),
|
||||
cursor_style: None,
|
||||
mouse_down: None,
|
||||
click: None,
|
||||
right_mouse_down: None,
|
||||
right_click: None,
|
||||
mouse_down_out: None,
|
||||
right_mouse_down_out: None,
|
||||
drag: None,
|
||||
hover: None,
|
||||
discriminant: (TypeId::of::<Tag>(), id),
|
||||
handlers: Default::default(),
|
||||
padding: Default::default(),
|
||||
}
|
||||
}
|
||||
|
@ -58,67 +42,47 @@ impl MouseEventHandler {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn on_mouse_down(
|
||||
pub fn on_down(
|
||||
mut self,
|
||||
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.mouse_down = Some(Rc::new(handler));
|
||||
self.handlers = self.handlers.on_down(button, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_click(
|
||||
mut self,
|
||||
handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.click = Some(Rc::new(handler));
|
||||
self.handlers = self.handlers.on_click(button, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_right_mouse_down(
|
||||
pub fn on_down_out(
|
||||
mut self,
|
||||
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.right_mouse_down = Some(Rc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_right_click(
|
||||
mut self,
|
||||
handler: impl Fn(Vector2F, usize, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.right_click = Some(Rc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_mouse_down_out(
|
||||
mut self,
|
||||
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.mouse_down_out = Some(Rc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_right_mouse_down_out(
|
||||
mut self,
|
||||
handler: impl Fn(Vector2F, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.right_mouse_down_out = Some(Rc::new(handler));
|
||||
self.handlers = self.handlers.on_down_out(button, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_drag(
|
||||
mut self,
|
||||
handler: impl Fn(Vector2F, Vector2F, &mut EventContext) + 'static,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(Vector2F, MouseMovedEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.drag = Some(Rc::new(handler));
|
||||
self.handlers = self.handlers.on_drag(button, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_hover(
|
||||
mut self,
|
||||
handler: impl Fn(Vector2F, bool, &mut EventContext) + 'static,
|
||||
handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.hover = Some(Rc::new(handler));
|
||||
self.handlers = self.handlers.on_hover(handler);
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -163,19 +127,12 @@ impl Element for MouseEventHandler {
|
|||
});
|
||||
}
|
||||
|
||||
cx.scene.push_mouse_region(MouseRegion {
|
||||
view_id: cx.current_view_id(),
|
||||
discriminant: Some((self.tag, self.id)),
|
||||
bounds: hit_bounds,
|
||||
hover: self.hover.clone(),
|
||||
click: self.click.clone(),
|
||||
mouse_down: self.mouse_down.clone(),
|
||||
right_click: self.right_click.clone(),
|
||||
right_mouse_down: self.right_mouse_down.clone(),
|
||||
mouse_down_out: self.mouse_down_out.clone(),
|
||||
right_mouse_down_out: self.right_mouse_down_out.clone(),
|
||||
drag: self.drag.clone(),
|
||||
});
|
||||
cx.scene.push_mouse_region(MouseRegion::from_handlers(
|
||||
cx.current_view_id(),
|
||||
Some(self.discriminant.clone()),
|
||||
hit_bounds,
|
||||
self.handlers.clone(),
|
||||
));
|
||||
|
||||
self.child.paint(bounds.origin(), visible_bounds, cx);
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ use crate::{
|
|||
geometry::{rect::RectF, vector::Vector2F},
|
||||
json::json,
|
||||
presenter::MeasurementContext,
|
||||
Action, Axis, ElementStateHandle, LayoutContext, PaintContext, RenderContext, SizeConstraint,
|
||||
Task, View,
|
||||
Action, Axis, ElementStateHandle, LayoutContext, MouseMovedEvent, PaintContext, RenderContext,
|
||||
SizeConstraint, Task, View,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
|
@ -93,7 +93,7 @@ impl Tooltip {
|
|||
};
|
||||
let child =
|
||||
MouseEventHandler::new::<MouseEventHandlerState<Tag>, _, _>(id, cx, |_, _| child)
|
||||
.on_hover(move |position, hover, cx| {
|
||||
.on_hover(move |hover, MouseMovedEvent { position, .. }, cx| {
|
||||
let window_id = cx.window_id();
|
||||
if let Some(view_id) = cx.view_id() {
|
||||
if hover {
|
||||
|
|
|
@ -19,20 +19,26 @@ pub struct ModifiersChangedEvent {
|
|||
pub cmd: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct ScrollWheelEvent {
|
||||
pub position: Vector2F,
|
||||
pub delta: Vector2F,
|
||||
pub precise: bool,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
|
||||
pub enum NavigationDirection {
|
||||
Back,
|
||||
Forward,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
impl Default for NavigationDirection {
|
||||
fn default() -> Self {
|
||||
Self::Back
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
|
||||
pub enum MouseButton {
|
||||
Left,
|
||||
Right,
|
||||
|
@ -40,8 +46,26 @@ pub enum MouseButton {
|
|||
Navigate(NavigationDirection),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct MouseEvent {
|
||||
impl MouseButton {
|
||||
pub fn all() -> Vec<Self> {
|
||||
vec![
|
||||
MouseButton::Left,
|
||||
MouseButton::Right,
|
||||
MouseButton::Middle,
|
||||
MouseButton::Navigate(NavigationDirection::Back),
|
||||
MouseButton::Navigate(NavigationDirection::Forward),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for MouseButton {
|
||||
fn default() -> Self {
|
||||
Self::Left
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct MouseButtonEvent {
|
||||
pub button: MouseButton,
|
||||
pub position: Vector2F,
|
||||
pub ctrl: bool,
|
||||
|
@ -51,7 +75,7 @@ pub struct MouseEvent {
|
|||
pub click_count: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct MouseMovedEvent {
|
||||
pub position: Vector2F,
|
||||
pub pressed_button: Option<MouseButton>,
|
||||
|
@ -66,8 +90,8 @@ pub enum Event {
|
|||
KeyDown(KeyDownEvent),
|
||||
KeyUp(KeyUpEvent),
|
||||
ModifiersChanged(ModifiersChangedEvent),
|
||||
MouseDown(MouseEvent),
|
||||
MouseUp(MouseEvent),
|
||||
MouseDown(MouseButtonEvent),
|
||||
MouseUp(MouseButtonEvent),
|
||||
MouseMoved(MouseMovedEvent),
|
||||
ScrollWheel(ScrollWheelEvent),
|
||||
}
|
||||
|
|
|
@ -2,8 +2,8 @@ use crate::{
|
|||
geometry::vector::vec2f,
|
||||
keymap::Keystroke,
|
||||
platform::{Event, NavigationDirection},
|
||||
KeyDownEvent, KeyUpEvent, ModifiersChangedEvent, MouseButton, MouseEvent, MouseMovedEvent,
|
||||
ScrollWheelEvent,
|
||||
KeyDownEvent, KeyUpEvent, ModifiersChangedEvent, MouseButton, MouseButtonEvent,
|
||||
MouseMovedEvent, ScrollWheelEvent,
|
||||
};
|
||||
use cocoa::{
|
||||
appkit::{NSEvent, NSEventModifierFlags, NSEventType},
|
||||
|
@ -104,7 +104,7 @@ impl Event {
|
|||
let modifiers = native_event.modifierFlags();
|
||||
|
||||
window_height.map(|window_height| {
|
||||
Self::MouseDown(MouseEvent {
|
||||
Self::MouseDown(MouseButtonEvent {
|
||||
button,
|
||||
position: vec2f(
|
||||
native_event.locationInWindow().x as f32,
|
||||
|
@ -133,7 +133,7 @@ impl Event {
|
|||
|
||||
window_height.map(|window_height| {
|
||||
let modifiers = native_event.modifierFlags();
|
||||
Self::MouseUp(MouseEvent {
|
||||
Self::MouseUp(MouseButtonEvent {
|
||||
button,
|
||||
position: vec2f(
|
||||
native_event.locationInWindow().x as f32,
|
||||
|
|
|
@ -7,8 +7,8 @@ use crate::{
|
|||
},
|
||||
keymap::Keystroke,
|
||||
platform::{self, Event, WindowBounds, WindowContext},
|
||||
InputHandler, KeyDownEvent, ModifiersChangedEvent, MouseButton, MouseEvent, MouseMovedEvent,
|
||||
Scene,
|
||||
InputHandler, KeyDownEvent, ModifiersChangedEvent, MouseButton, MouseButtonEvent,
|
||||
MouseMovedEvent, Scene,
|
||||
};
|
||||
use block::ConcreteBlock;
|
||||
use cocoa::{
|
||||
|
@ -768,7 +768,7 @@ extern "C" fn handle_view_event(this: &Object, _: Sel, native_event: id) {
|
|||
))
|
||||
.detach();
|
||||
}
|
||||
Event::MouseUp(MouseEvent {
|
||||
Event::MouseUp(MouseButtonEvent {
|
||||
button: MouseButton::Left,
|
||||
..
|
||||
}) => {
|
||||
|
|
|
@ -6,10 +6,10 @@ use crate::{
|
|||
json::{self, ToJson},
|
||||
keymap::Keystroke,
|
||||
platform::{CursorStyle, Event},
|
||||
scene::CursorRegion,
|
||||
scene::{CursorRegion, MouseRegionEvent},
|
||||
text_layout::TextLayoutCache,
|
||||
Action, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AssetCache, ElementBox, Entity,
|
||||
FontSystem, ModelHandle, MouseButton, MouseEvent, MouseMovedEvent, MouseRegion, MouseRegionId,
|
||||
FontSystem, ModelHandle, MouseButtonEvent, MouseMovedEvent, MouseRegion, MouseRegionId,
|
||||
ReadModel, ReadView, RenderContext, RenderParams, Scene, UpgradeModelHandle, UpgradeViewHandle,
|
||||
View, ViewHandle, WeakModelHandle, WeakViewHandle,
|
||||
};
|
||||
|
@ -241,105 +241,57 @@ impl Presenter {
|
|||
let mut mouse_down_out_handlers = Vec::new();
|
||||
let mut mouse_down_region = None;
|
||||
let mut clicked_region = None;
|
||||
let mut right_mouse_down_region = None;
|
||||
let mut right_clicked_region = None;
|
||||
let mut dragged_region = None;
|
||||
|
||||
match event {
|
||||
Event::MouseDown(MouseEvent {
|
||||
position,
|
||||
button: MouseButton::Left,
|
||||
..
|
||||
}) => {
|
||||
match &event {
|
||||
Event::MouseDown(
|
||||
e @ MouseButtonEvent {
|
||||
position, button, ..
|
||||
},
|
||||
) => {
|
||||
let mut hit = false;
|
||||
for (region, _) in self.mouse_regions.iter().rev() {
|
||||
if region.bounds.contains_point(position) {
|
||||
if region.bounds.contains_point(*position) {
|
||||
if !hit {
|
||||
hit = true;
|
||||
invalidated_views.push(region.view_id);
|
||||
mouse_down_region = Some((region.clone(), position));
|
||||
mouse_down_region =
|
||||
Some((region.clone(), MouseRegionEvent::Down(e.clone())));
|
||||
self.clicked_region = Some(region.clone());
|
||||
self.prev_drag_position = Some(position);
|
||||
self.prev_drag_position = Some(*position);
|
||||
}
|
||||
} else if let Some(handler) = region.mouse_down_out.clone() {
|
||||
mouse_down_out_handlers.push((handler, region.view_id, position));
|
||||
} else if let Some(handler) = region
|
||||
.handlers
|
||||
.get(&(MouseRegionEvent::down_out_disc(), Some(*button)))
|
||||
{
|
||||
mouse_down_out_handlers.push((
|
||||
handler,
|
||||
region.view_id,
|
||||
MouseRegionEvent::DownOut(e.clone()),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::MouseUp(MouseEvent {
|
||||
position,
|
||||
click_count,
|
||||
button: MouseButton::Left,
|
||||
..
|
||||
}) => {
|
||||
Event::MouseUp(e @ MouseButtonEvent { position, .. }) => {
|
||||
self.prev_drag_position.take();
|
||||
if let Some(region) = self.clicked_region.take() {
|
||||
invalidated_views.push(region.view_id);
|
||||
if region.bounds.contains_point(position) {
|
||||
clicked_region = Some((region, position, click_count));
|
||||
if region.bounds.contains_point(*position) {
|
||||
clicked_region = Some((region, MouseRegionEvent::Click(e.clone())));
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::MouseDown(MouseEvent {
|
||||
position,
|
||||
button: MouseButton::Right,
|
||||
..
|
||||
}) => {
|
||||
let mut hit = false;
|
||||
for (region, _) in self.mouse_regions.iter().rev() {
|
||||
if region.bounds.contains_point(position) {
|
||||
if !hit {
|
||||
hit = true;
|
||||
invalidated_views.push(region.view_id);
|
||||
right_mouse_down_region = Some((region.clone(), position));
|
||||
self.right_clicked_region = Some(region.clone());
|
||||
}
|
||||
} else if let Some(handler) = region.right_mouse_down_out.clone() {
|
||||
mouse_down_out_handlers.push((handler, region.view_id, position));
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::MouseUp(MouseEvent {
|
||||
position,
|
||||
click_count,
|
||||
button: MouseButton::Right,
|
||||
..
|
||||
}) => {
|
||||
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(MouseMovedEvent {
|
||||
pressed_button,
|
||||
position,
|
||||
shift,
|
||||
ctrl,
|
||||
alt,
|
||||
cmd,
|
||||
..
|
||||
}) => {
|
||||
if let Some(MouseButton::Left) = pressed_button {
|
||||
if let Some((clicked_region, prev_drag_position)) = self
|
||||
.clicked_region
|
||||
.as_ref()
|
||||
.zip(self.prev_drag_position.as_mut())
|
||||
{
|
||||
dragged_region =
|
||||
Some((clicked_region.clone(), *prev_drag_position, position));
|
||||
*prev_drag_position = position;
|
||||
}
|
||||
|
||||
self.last_mouse_moved_event = Some(Event::MouseMoved(MouseMovedEvent {
|
||||
position,
|
||||
pressed_button: Some(MouseButton::Left),
|
||||
shift,
|
||||
ctrl,
|
||||
alt,
|
||||
cmd,
|
||||
}));
|
||||
Event::MouseMoved(e @ MouseMovedEvent { position, .. }) => {
|
||||
if let Some((clicked_region, prev_drag_position)) = self
|
||||
.clicked_region
|
||||
.as_ref()
|
||||
.zip(self.prev_drag_position.as_mut())
|
||||
{
|
||||
dragged_region = Some((
|
||||
clicked_region.clone(),
|
||||
MouseRegionEvent::Drag(*prev_drag_position, e.clone()),
|
||||
));
|
||||
*prev_drag_position = *position;
|
||||
}
|
||||
|
||||
self.last_mouse_moved_event = Some(event.clone());
|
||||
|
@ -350,51 +302,39 @@ impl Presenter {
|
|||
let (mut handled, mut event_cx) =
|
||||
self.handle_hover_events(&event, &mut invalidated_views, cx);
|
||||
|
||||
for (handler, view_id, position) in mouse_down_out_handlers {
|
||||
event_cx.with_current_view(view_id, |event_cx| handler(position, event_cx))
|
||||
for (handler, view_id, region_event) in mouse_down_out_handlers {
|
||||
event_cx.with_current_view(view_id, |event_cx| handler(region_event, event_cx))
|
||||
}
|
||||
|
||||
if let Some((mouse_down_region, position)) = mouse_down_region {
|
||||
if let Some((mouse_down_region, region_event)) = mouse_down_region {
|
||||
handled = true;
|
||||
if let Some(mouse_down_callback) = mouse_down_region.mouse_down {
|
||||
if let Some(mouse_down_callback) =
|
||||
mouse_down_region.handlers.get(®ion_event.handler_key())
|
||||
{
|
||||
event_cx.with_current_view(mouse_down_region.view_id, |event_cx| {
|
||||
mouse_down_callback(position, event_cx);
|
||||
mouse_down_callback(region_event, event_cx);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((clicked_region, position, click_count)) = clicked_region {
|
||||
if let Some((clicked_region, region_event)) = clicked_region {
|
||||
handled = true;
|
||||
if let Some(click_callback) = clicked_region.click {
|
||||
if let Some(click_callback) =
|
||||
clicked_region.handlers.get(®ion_event.handler_key())
|
||||
{
|
||||
event_cx.with_current_view(clicked_region.view_id, |event_cx| {
|
||||
click_callback(position, click_count, event_cx);
|
||||
click_callback(region_event, event_cx);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((right_mouse_down_region, position)) = right_mouse_down_region {
|
||||
if let Some((dragged_region, region_event)) = dragged_region {
|
||||
handled = true;
|
||||
if let Some(right_mouse_down_callback) = right_mouse_down_region.right_mouse_down {
|
||||
event_cx.with_current_view(right_mouse_down_region.view_id, |event_cx| {
|
||||
right_mouse_down_callback(position, event_cx);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((right_clicked_region, position, click_count)) = right_clicked_region {
|
||||
handled = true;
|
||||
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, prev_position, position)) = dragged_region {
|
||||
handled = true;
|
||||
if let Some(drag_callback) = dragged_region.drag {
|
||||
if let Some(drag_callback) =
|
||||
dragged_region.handlers.get(®ion_event.handler_key())
|
||||
{
|
||||
event_cx.with_current_view(dragged_region.view_id, |event_cx| {
|
||||
drag_callback(prev_position, position, event_cx);
|
||||
drag_callback(region_event, event_cx);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -431,14 +371,17 @@ impl Presenter {
|
|||
invalidated_views: &mut Vec<usize>,
|
||||
cx: &'a mut MutableAppContext,
|
||||
) -> (bool, EventContext<'a>) {
|
||||
let mut unhovered_regions = Vec::new();
|
||||
let mut hovered_regions = Vec::new();
|
||||
let mut hover_regions = Vec::new();
|
||||
// let mut unhovered_regions = Vec::new();
|
||||
// let mut hovered_regions = Vec::new();
|
||||
|
||||
if let Event::MouseMoved(MouseMovedEvent {
|
||||
position,
|
||||
pressed_button,
|
||||
..
|
||||
}) = event
|
||||
if let Event::MouseMoved(
|
||||
e @ MouseMovedEvent {
|
||||
position,
|
||||
pressed_button,
|
||||
..
|
||||
},
|
||||
) = event
|
||||
{
|
||||
if let None = pressed_button {
|
||||
let mut style_to_assign = CursorStyle::Arrow;
|
||||
|
@ -459,7 +402,10 @@ impl Presenter {
|
|||
if let Some(region_id) = region.id() {
|
||||
if !self.hovered_region_ids.contains(®ion_id) {
|
||||
invalidated_views.push(region.view_id);
|
||||
hovered_regions.push((region.clone(), position));
|
||||
hover_regions.push((
|
||||
region.clone(),
|
||||
MouseRegionEvent::Hover(true, e.clone()),
|
||||
));
|
||||
self.hovered_region_ids.insert(region_id);
|
||||
}
|
||||
}
|
||||
|
@ -467,7 +413,10 @@ impl Presenter {
|
|||
if let Some(region_id) = region.id() {
|
||||
if self.hovered_region_ids.contains(®ion_id) {
|
||||
invalidated_views.push(region.view_id);
|
||||
unhovered_regions.push((region.clone(), position));
|
||||
hover_regions.push((
|
||||
region.clone(),
|
||||
MouseRegionEvent::Hover(false, e.clone()),
|
||||
));
|
||||
self.hovered_region_ids.remove(®ion_id);
|
||||
}
|
||||
}
|
||||
|
@ -479,20 +428,11 @@ impl Presenter {
|
|||
let mut event_cx = self.build_event_context(cx);
|
||||
let mut handled = false;
|
||||
|
||||
for (unhovered_region, position) in unhovered_regions {
|
||||
for (hover_region, region_event) in hover_regions {
|
||||
handled = true;
|
||||
if let Some(hover_callback) = unhovered_region.hover {
|
||||
event_cx.with_current_view(unhovered_region.view_id, |event_cx| {
|
||||
hover_callback(*position, false, event_cx);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for (hovered_region, position) in hovered_regions {
|
||||
handled = true;
|
||||
if let Some(hover_callback) = hovered_region.hover {
|
||||
event_cx.with_current_view(hovered_region.view_id, |event_cx| {
|
||||
hover_callback(*position, true, event_cx);
|
||||
if let Some(hover_callback) = hover_region.handlers.get(®ion_event.handler_key()) {
|
||||
event_cx.with_current_view(hover_region.view_id, |event_cx| {
|
||||
hover_callback(region_event, event_cx);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
mod mouse_region;
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use std::{any::TypeId, borrow::Cow, rc::Rc, sync::Arc};
|
||||
use std::{borrow::Cow, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
color::Color,
|
||||
|
@ -8,8 +10,9 @@ use crate::{
|
|||
geometry::{rect::RectF, vector::Vector2F},
|
||||
json::ToJson,
|
||||
platform::CursorStyle,
|
||||
EventContext, ImageData, MouseEvent, MouseMovedEvent, ScrollWheelEvent,
|
||||
ImageData,
|
||||
};
|
||||
pub use mouse_region::*;
|
||||
|
||||
pub struct Scene {
|
||||
scale_factor: f32,
|
||||
|
@ -44,38 +47,6 @@ pub struct CursorRegion {
|
|||
pub style: CursorStyle,
|
||||
}
|
||||
|
||||
pub enum MouseRegionEvent {
|
||||
Moved(MouseMovedEvent),
|
||||
Hover(MouseEvent),
|
||||
Down(MouseEvent),
|
||||
Up(MouseEvent),
|
||||
Click(MouseEvent),
|
||||
DownOut(MouseEvent),
|
||||
ScrollWheel(ScrollWheelEvent),
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct MouseRegion {
|
||||
pub view_id: usize,
|
||||
pub discriminant: Option<(TypeId, usize)>,
|
||||
pub bounds: RectF,
|
||||
|
||||
pub hover: Option<Rc<dyn Fn(Vector2F, 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, Vector2F, &mut EventContext)>>,
|
||||
pub mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
pub right_mouse_down_out: Option<Rc<dyn Fn(Vector2F, &mut EventContext)>>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
||||
pub struct MouseRegionId {
|
||||
pub view_id: usize,
|
||||
pub discriminant: (TypeId, usize),
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct Quad {
|
||||
pub bounds: RectF,
|
||||
|
|
336
crates/gpui/src/scene/mouse_region.rs
Normal file
336
crates/gpui/src/scene/mouse_region.rs
Normal file
|
@ -0,0 +1,336 @@
|
|||
use std::{any::TypeId, mem::Discriminant, rc::Rc};
|
||||
|
||||
use collections::HashMap;
|
||||
use pathfinder_geometry::{rect::RectF, vector::Vector2F};
|
||||
|
||||
use crate::{EventContext, MouseButton, MouseButtonEvent, MouseMovedEvent, ScrollWheelEvent};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct MouseRegion {
|
||||
pub view_id: usize,
|
||||
pub discriminant: Option<(TypeId, usize)>,
|
||||
pub bounds: RectF,
|
||||
pub handlers: HandlerSet,
|
||||
}
|
||||
|
||||
impl MouseRegion {
|
||||
pub fn new(view_id: usize, discriminant: Option<(TypeId, usize)>, bounds: RectF) -> Self {
|
||||
Self::from_handlers(view_id, discriminant, bounds, Default::default())
|
||||
}
|
||||
|
||||
pub fn from_handlers(
|
||||
view_id: usize,
|
||||
discriminant: Option<(TypeId, usize)>,
|
||||
bounds: RectF,
|
||||
handlers: HandlerSet,
|
||||
) -> Self {
|
||||
Self {
|
||||
view_id,
|
||||
discriminant,
|
||||
bounds,
|
||||
handlers,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_all(
|
||||
view_id: usize,
|
||||
discriminant: Option<(TypeId, usize)>,
|
||||
bounds: RectF,
|
||||
) -> Self {
|
||||
Self {
|
||||
view_id,
|
||||
discriminant,
|
||||
bounds,
|
||||
handlers: HandlerSet::handle_all(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_down(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_down(button, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_up(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_up(button, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_click(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_click(button, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_down_out(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_down_out(button, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_drag(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(Vector2F, MouseMovedEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_drag(button, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_hover(
|
||||
mut self,
|
||||
handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.handlers = self.handlers.on_hover(handler);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
||||
pub struct MouseRegionId {
|
||||
pub view_id: usize,
|
||||
pub discriminant: (TypeId, usize),
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct HandlerSet {
|
||||
pub set: HashMap<
|
||||
(Discriminant<MouseRegionEvent>, Option<MouseButton>),
|
||||
Rc<dyn Fn(MouseRegionEvent, &mut EventContext)>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl HandlerSet {
|
||||
pub fn handle_all() -> Self {
|
||||
let mut set: HashMap<
|
||||
(Discriminant<MouseRegionEvent>, Option<MouseButton>),
|
||||
Rc<dyn Fn(MouseRegionEvent, &mut EventContext)>,
|
||||
> = Default::default();
|
||||
|
||||
set.insert((MouseRegionEvent::move_disc(), None), Rc::new(|_, _| {}));
|
||||
set.insert((MouseRegionEvent::hover_disc(), None), Rc::new(|_, _| {}));
|
||||
for button in MouseButton::all() {
|
||||
set.insert(
|
||||
(MouseRegionEvent::drag_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::scroll_wheel_disc(), None),
|
||||
Rc::new(|_, _| {}),
|
||||
);
|
||||
|
||||
HandlerSet { set }
|
||||
}
|
||||
|
||||
pub fn get(
|
||||
&self,
|
||||
key: &(Discriminant<MouseRegionEvent>, Option<MouseButton>),
|
||||
) -> Option<Rc<dyn Fn(MouseRegionEvent, &mut EventContext)>> {
|
||||
self.set.get(key).cloned()
|
||||
}
|
||||
|
||||
pub fn on_down(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::down_disc(), Some(button)),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::Down(mouse_button_event) = region_event {
|
||||
handler(mouse_button_event, cx);
|
||||
} else {
|
||||
panic!(
|
||||
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Down, found {:?}",
|
||||
region_event);
|
||||
}
|
||||
}));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_up(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::up_disc(), Some(button)),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::Up(mouse_button_event) = region_event {
|
||||
handler(mouse_button_event, cx);
|
||||
} else {
|
||||
panic!(
|
||||
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Up, found {:?}",
|
||||
region_event);
|
||||
}
|
||||
}));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_click(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::click_disc(), Some(button)),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::Click(mouse_button_event) = region_event {
|
||||
handler(mouse_button_event, cx);
|
||||
} else {
|
||||
panic!(
|
||||
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Click, found {:?}",
|
||||
region_event);
|
||||
}
|
||||
}));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_down_out(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(MouseButtonEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::down_out_disc(), Some(button)),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::DownOut(mouse_button_event) = region_event {
|
||||
handler(mouse_button_event, cx);
|
||||
} else {
|
||||
panic!(
|
||||
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::DownOut, found {:?}",
|
||||
region_event);
|
||||
}
|
||||
}));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_drag(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
handler: impl Fn(Vector2F, MouseMovedEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::drag_disc(), Some(button)),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::Drag(prev_drag_position, mouse_moved_event) = region_event {
|
||||
handler(prev_drag_position, mouse_moved_event, cx);
|
||||
} else {
|
||||
panic!(
|
||||
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Drag, found {:?}",
|
||||
region_event);
|
||||
}
|
||||
}));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_hover(
|
||||
mut self,
|
||||
handler: impl Fn(bool, MouseMovedEvent, &mut EventContext) + 'static,
|
||||
) -> Self {
|
||||
self.set.insert((MouseRegionEvent::hover_disc(), None),
|
||||
Rc::new(move |region_event, cx| {
|
||||
if let MouseRegionEvent::Hover(hover, mouse_moved_event) = region_event {
|
||||
handler(hover, mouse_moved_event, cx);
|
||||
} else {
|
||||
panic!(
|
||||
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::Hover, found {:?}",
|
||||
region_event);
|
||||
}
|
||||
}));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum MouseRegionEvent {
|
||||
Move(MouseMovedEvent),
|
||||
Drag(Vector2F, MouseMovedEvent),
|
||||
Hover(bool, MouseMovedEvent),
|
||||
Down(MouseButtonEvent),
|
||||
Up(MouseButtonEvent),
|
||||
Click(MouseButtonEvent),
|
||||
DownOut(MouseButtonEvent),
|
||||
ScrollWheel(ScrollWheelEvent),
|
||||
}
|
||||
|
||||
impl MouseRegionEvent {
|
||||
pub fn move_disc() -> Discriminant<MouseRegionEvent> {
|
||||
std::mem::discriminant(&MouseRegionEvent::Move(Default::default()))
|
||||
}
|
||||
pub fn drag_disc() -> Discriminant<MouseRegionEvent> {
|
||||
std::mem::discriminant(&MouseRegionEvent::Drag(
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
))
|
||||
}
|
||||
pub fn hover_disc() -> Discriminant<MouseRegionEvent> {
|
||||
std::mem::discriminant(&MouseRegionEvent::Hover(
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
))
|
||||
}
|
||||
pub fn down_disc() -> Discriminant<MouseRegionEvent> {
|
||||
std::mem::discriminant(&MouseRegionEvent::Down(Default::default()))
|
||||
}
|
||||
pub fn up_disc() -> Discriminant<MouseRegionEvent> {
|
||||
std::mem::discriminant(&MouseRegionEvent::Up(Default::default()))
|
||||
}
|
||||
pub fn click_disc() -> Discriminant<MouseRegionEvent> {
|
||||
std::mem::discriminant(&MouseRegionEvent::Click(Default::default()))
|
||||
}
|
||||
pub fn down_out_disc() -> Discriminant<MouseRegionEvent> {
|
||||
std::mem::discriminant(&MouseRegionEvent::DownOut(Default::default()))
|
||||
}
|
||||
pub fn scroll_wheel_disc() -> Discriminant<MouseRegionEvent> {
|
||||
std::mem::discriminant(&MouseRegionEvent::ScrollWheel(Default::default()))
|
||||
}
|
||||
|
||||
pub fn handler_key(&self) -> (Discriminant<MouseRegionEvent>, Option<MouseButton>) {
|
||||
match self {
|
||||
MouseRegionEvent::Move(_) => (Self::move_disc(), None),
|
||||
MouseRegionEvent::Drag(_, MouseMovedEvent { pressed_button, .. }) => {
|
||||
(Self::drag_disc(), *pressed_button)
|
||||
}
|
||||
MouseRegionEvent::Hover(_, _) => (Self::hover_disc(), None),
|
||||
MouseRegionEvent::Down(MouseButtonEvent { button, .. }) => {
|
||||
(Self::down_disc(), Some(*button))
|
||||
}
|
||||
MouseRegionEvent::Up(MouseButtonEvent { button, .. }) => {
|
||||
(Self::up_disc(), Some(*button))
|
||||
}
|
||||
MouseRegionEvent::Click(MouseButtonEvent { button, .. }) => {
|
||||
(Self::click_disc(), Some(*button))
|
||||
}
|
||||
MouseRegionEvent::DownOut(MouseButtonEvent { button, .. }) => {
|
||||
(Self::down_out_disc(), Some(*button))
|
||||
}
|
||||
MouseRegionEvent::ScrollWheel(_) => (Self::scroll_wheel_disc(), None),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -238,6 +238,8 @@ impl Line {
|
|||
None
|
||||
}
|
||||
|
||||
// If round_to_closest, find the closest index to the given x position
|
||||
// If !round_to_closest, find the largest index before the given x position
|
||||
pub fn index_for_x(&self, x: f32) -> Option<usize> {
|
||||
if x >= self.layout.width {
|
||||
None
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
actions, elements::*, impl_actions, AppContext, Entity, MutableAppContext, RenderContext, View,
|
||||
ViewContext, WeakViewHandle,
|
||||
actions, elements::*, impl_actions, AppContext, Entity, MouseButton, MutableAppContext,
|
||||
RenderContext, View, ViewContext, WeakViewHandle,
|
||||
};
|
||||
|
||||
pub struct Select {
|
||||
|
@ -119,7 +119,9 @@ impl View for Select {
|
|||
.with_style(style.header)
|
||||
.boxed()
|
||||
})
|
||||
.on_click(move |_, _, cx| cx.dispatch_action(ToggleSelect))
|
||||
.on_click(MouseButton::Left, move |_, cx| {
|
||||
cx.dispatch_action(ToggleSelect)
|
||||
})
|
||||
.boxed(),
|
||||
);
|
||||
if self.is_open {
|
||||
|
@ -151,7 +153,7 @@ impl View for Select {
|
|||
)
|
||||
},
|
||||
)
|
||||
.on_click(move |_, _, cx| {
|
||||
.on_click(MouseButton::Left, move |_, cx| {
|
||||
cx.dispatch_action(SelectItem(ix))
|
||||
})
|
||||
.boxed()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue