Skip to content

Commit 34db9b0

Browse files
committed
[Rust - agx] Scroll bar receives mouse highlight
1 parent 5d00350 commit 34db9b0

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

rust_programs/libgui/src/scroll_view.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ impl Drawable for ExpandingLayer {
660660
pub struct ScrollView {
661661
pub view: Rc<View>,
662662
pub layer: Rc<ExpandingLayer>,
663+
cached_mouse_position: RefCell<Point>,
663664
}
664665

665666
impl ScrollView {
@@ -679,7 +680,11 @@ impl ScrollView {
679680
view.set_border_enabled(false);
680681
//view.set_border_enabled(false);
681682

682-
Rc::new(Self { view, layer })
683+
Rc::new(Self {
684+
view,
685+
layer,
686+
cached_mouse_position: RefCell::new(Point::zero()),
687+
})
683688
}
684689

685690
pub fn new<F: 'static + Fn(&View, Size) -> Rect>(sizer: F) -> Rc<Self> {
@@ -693,6 +698,17 @@ impl ScrollView {
693698
fn scroll_bar_width() -> isize {
694699
42
695700
}
701+
702+
fn record_mouse_position(&self, mouse_point: Point) {
703+
*self.cached_mouse_position.borrow_mut() = mouse_point;
704+
}
705+
706+
fn scroll_bar_region_contains_mouse(&self) -> bool {
707+
let mouse_position = *self.cached_mouse_position.borrow();
708+
// TODO(PT): Check whether we need to relocate this point to our local coordinate space
709+
self.scroll_bar_content_frame().contains(mouse_position)
710+
}
711+
696712
fn scroll_bar_content_frame(&self) -> Rect {
697713
let (frame_of_inner_margin, frame_of_content) = compute_inner_margin_and_content_frames(
698714
self.outer_border_insets(),
@@ -778,9 +794,14 @@ impl Bordered for ScrollView {
778794
);
779795

780796
// Mouse interaction highlight on top of the scroll bar
797+
let scroll_bar_highlight_color = if self.scroll_bar_region_contains_mouse() {
798+
Color::new(200, 200, 200)
799+
} else {
800+
Color::new(160, 160, 160)
801+
};
781802
scroll_bar_onto.fill_rect(
782803
Rect::from_parts(scrollbar_attrs.origin, scrollbar_attrs.size),
783-
Color::new(160, 160, 160),
804+
scroll_bar_highlight_color,
784805
StrokeThickness::Width(1),
785806
);
786807

@@ -840,6 +861,9 @@ impl UIElement for ScrollView {
840861
}
841862

842863
fn handle_mouse_moved(&self, mouse_point: Point) {
864+
// Keep track of the mouse position, so we can detect whether the mouse is
865+
// hovering on the scroll bar
866+
self.record_mouse_position(mouse_point);
843867
self.view.handle_mouse_moved(mouse_point)
844868
}
845869

0 commit comments

Comments
 (0)