From 34db9b0fbb74a6a6164ecd18a2dbc61ca3a0fb88 Mon Sep 17 00:00:00 2001 From: Phillip Tennen Date: Sat, 23 Mar 2024 15:14:01 +0000 Subject: [PATCH] [Rust - agx] Scroll bar receives mouse highlight --- rust_programs/libgui/src/scroll_view.rs | 28 +++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/rust_programs/libgui/src/scroll_view.rs b/rust_programs/libgui/src/scroll_view.rs index 35926738..67f97c63 100644 --- a/rust_programs/libgui/src/scroll_view.rs +++ b/rust_programs/libgui/src/scroll_view.rs @@ -660,6 +660,7 @@ impl Drawable for ExpandingLayer { pub struct ScrollView { pub view: Rc, pub layer: Rc, + cached_mouse_position: RefCell, } impl ScrollView { @@ -679,7 +680,11 @@ impl ScrollView { view.set_border_enabled(false); //view.set_border_enabled(false); - Rc::new(Self { view, layer }) + Rc::new(Self { + view, + layer, + cached_mouse_position: RefCell::new(Point::zero()), + }) } pub fn new Rect>(sizer: F) -> Rc { @@ -693,6 +698,17 @@ impl ScrollView { fn scroll_bar_width() -> isize { 42 } + + fn record_mouse_position(&self, mouse_point: Point) { + *self.cached_mouse_position.borrow_mut() = mouse_point; + } + + fn scroll_bar_region_contains_mouse(&self) -> bool { + let mouse_position = *self.cached_mouse_position.borrow(); + // TODO(PT): Check whether we need to relocate this point to our local coordinate space + self.scroll_bar_content_frame().contains(mouse_position) + } + fn scroll_bar_content_frame(&self) -> Rect { let (frame_of_inner_margin, frame_of_content) = compute_inner_margin_and_content_frames( self.outer_border_insets(), @@ -778,9 +794,14 @@ impl Bordered for ScrollView { ); // Mouse interaction highlight on top of the scroll bar + let scroll_bar_highlight_color = if self.scroll_bar_region_contains_mouse() { + Color::new(200, 200, 200) + } else { + Color::new(160, 160, 160) + }; scroll_bar_onto.fill_rect( Rect::from_parts(scrollbar_attrs.origin, scrollbar_attrs.size), - Color::new(160, 160, 160), + scroll_bar_highlight_color, StrokeThickness::Width(1), ); @@ -840,6 +861,9 @@ impl UIElement for ScrollView { } fn handle_mouse_moved(&self, mouse_point: Point) { + // Keep track of the mouse position, so we can detect whether the mouse is + // hovering on the scroll bar + self.record_mouse_position(mouse_point); self.view.handle_mouse_moved(mouse_point) }