Get basic mouse_down and mouse_up working

This commit is contained in:
Nathan Sobo 2023-08-16 22:21:27 -06:00
parent 187d78011c
commit 812d3f6af6
10 changed files with 175 additions and 82 deletions

View file

@ -8,8 +8,9 @@ use crate::{
MouseButton, MouseMovedEvent, PromptLevel, WindowBounds,
},
scene::{
CursorRegion, MouseClick, MouseClickOut, MouseDown, MouseDownOut, MouseDrag, MouseEvent,
MouseHover, MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut, Scene,
CursorRegion, InteractiveRegion, MouseClick, MouseClickOut, MouseDown, MouseDownOut,
MouseDrag, MouseEvent, MouseHover, MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp,
MouseUpOut, Scene,
},
text_layout::TextLayoutCache,
util::post_inc,
@ -56,6 +57,7 @@ pub struct Window {
appearance: Appearance,
cursor_regions: Vec<CursorRegion>,
mouse_regions: Vec<(MouseRegion, usize)>,
interactive_regions: Vec<InteractiveRegion>,
last_mouse_moved_event: Option<Event>,
pub(crate) hovered_region_ids: Vec<MouseRegionId>,
pub(crate) clicked_region_ids: Vec<MouseRegionId>,
@ -89,6 +91,7 @@ impl Window {
rendered_views: Default::default(),
cursor_regions: Default::default(),
mouse_regions: Default::default(),
interactive_regions: Vec::new(),
text_layout_cache: TextLayoutCache::new(cx.font_system.clone()),
last_mouse_moved_event: None,
hovered_region_ids: Default::default(),
@ -490,6 +493,8 @@ impl<'a> WindowContext<'a> {
}
pub(crate) fn dispatch_event(&mut self, event: Event, event_reused: bool) -> bool {
self.dispatch_to_interactive_regions(&event);
let mut mouse_events = SmallVec::<[_; 2]>::new();
let mut notified_views: HashSet<usize> = Default::default();
let handle = self.window_handle;
@ -867,6 +872,30 @@ impl<'a> WindowContext<'a> {
any_event_handled
}
fn dispatch_to_interactive_regions(&mut self, event: &Event) {
if let Some(mouse_event) = event.mouse_event() {
let mouse_position = event.position().expect("mouse events must have a position");
let interactive_regions = std::mem::take(&mut self.window.interactive_regions);
for region in interactive_regions.iter().rev() {
if region.event_type == mouse_event.type_id() {
if region.bounds.contains_point(mouse_position) {
self.update_any_view(region.view_id, |view, window_cx| {
(region.event_handler)(
view.as_any_mut(),
mouse_event,
window_cx,
region.view_id,
)
});
}
}
}
self.window.interactive_regions = interactive_regions;
}
}
pub(crate) fn dispatch_key_down(&mut self, event: &KeyDownEvent) -> bool {
let handle = self.window_handle;
if let Some(focused_view_id) = self.window.focused_view_id {
@ -1018,9 +1047,10 @@ impl<'a> WindowContext<'a> {
.insert(root_view_id, rendered_root);
self.window.text_layout_cache.finish_frame();
let scene = scene_builder.build();
let mut scene = scene_builder.build();
self.window.cursor_regions = scene.cursor_regions();
self.window.mouse_regions = scene.mouse_regions();
self.window.interactive_regions = scene.take_interactive_regions();
if self.window_is_active() {
if let Some(event) = self.window.last_mouse_moved_event.clone() {