Skip to content

Commit

Permalink
Fix hover/click of scrolled elements/viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
cfraz89 committed Sep 6, 2024
1 parent f54bcc4 commit 8b198c8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions examples/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ fn root() -> Element {
align-items: center;
color: white;
}
.gap:hover {
background: red;
}
.not-scrollable {
background-color: yellow;
Expand All @@ -35,6 +39,7 @@ fn root() -> Element {
}
div {
class: "gap",
onclick: |_| println!("Gap clicked!"),
"gap"
}
div {
Expand Down
5 changes: 3 additions & 2 deletions packages/dioxus-blitz/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ impl<Doc: DocumentLike> View<Doc> {
}

pub fn mouse_move(&mut self, x: f32, y: f32) -> bool {
let dom_x = x / self.viewport.zoom();
let dom_y = y / self.viewport.zoom();
let viewport_scroll = self.dom.as_ref().viewport_scroll();
let dom_x = x + viewport_scroll.x as f32 / self.viewport.zoom();
let dom_y = y + viewport_scroll.y as f32 / self.viewport.zoom();

// println!("Mouse move: ({}, {})", x, y);
// println!("Unscaled: ({}, {})",);
Expand Down
10 changes: 7 additions & 3 deletions packages/dom/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,11 +782,15 @@ impl Node {
/// TODO: z-index
/// (If multiple children are positioned at the position then a random one will be recursed into)
pub fn hit(&self, x: f32, y: f32) -> Option<HitResult> {
let x = x - self.final_layout.location.x;
let y = y - self.final_layout.location.y;
let x = x - self.final_layout.location.x + self.scroll_offset.x as f32;
let y = y - self.final_layout.location.y + self.scroll_offset.y as f32;

let size = self.final_layout.size;
if x < 0.0 || x > size.width || y < 0.0 || y > size.height {
if x < 0.0
|| x > size.width + self.scroll_offset.x as f32
|| y < 0.0
|| y > size.height + self.scroll_offset.y as f32
{
return None;
}

Expand Down

0 comments on commit 8b198c8

Please sign in to comment.