From 8c99ac6bbc9cc6e881020caccc261996ab7fc717 Mon Sep 17 00:00:00 2001 From: Adoo Date: Thu, 14 Nov 2024 17:01:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20=F0=9F=90=9B=20the=20hit=20test=20?= =?UTF-8?q?for=20the=20`TransformWidget`=20on=20wrong=20position?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++- core/src/builtin_widgets/transform_widget.rs | 4 +++- core/src/events/dispatcher.rs | 24 +++++++++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 885267a22..dea1718d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,8 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he ### Fixed -- **core**: The size of the `Root` container is too small, which could lead to potential missed hits. (#pr @M-Adoo) +- **core**: The size of the `Root` container is too small, which could lead to potential missed hits. (#654 @M-Adoo) +- **core**: The hit test for the `TransformWidget` is not applied at the correct position. (#654 @M-Adoo) ## [0.4.0-alpha.15] - 2024-11-13 diff --git a/core/src/builtin_widgets/transform_widget.rs b/core/src/builtin_widgets/transform_widget.rs index fa83d7d02..7a7c4f2ae 100644 --- a/core/src/builtin_widgets/transform_widget.rs +++ b/core/src/builtin_widgets/transform_widget.rs @@ -26,7 +26,9 @@ impl WrapRender for TransformWidget { fn hit_test(&self, host: &dyn Render, ctx: &HitTestCtx, pos: Point) -> HitTest { if let Some(t) = self.transform.inverse() { - let pos = t.transform_point(pos); + let lt = ctx.box_pos().unwrap(); + let pos = (pos - lt).to_point(); + let pos = t.transform_point(pos) + lt.to_vector(); host.hit_test(ctx, pos) } else { HitTest { hit: false, can_hit_child: false } diff --git a/core/src/events/dispatcher.rs b/core/src/events/dispatcher.rs index b982701e9..16f758fb4 100644 --- a/core/src/events/dispatcher.rs +++ b/core/src/events/dispatcher.rs @@ -677,7 +677,7 @@ mod tests { } #[test] - fn fix_align_test() { + fn fix_align_hit_test() { reset_test_env!(); let (expect_hit, w_hit) = split_value(None); let mut wnd = TestWindow::new_with_size( @@ -697,4 +697,26 @@ mod tests { assert!(expect_hit.read().is_some()); assert_eq!(dispatcher.hit_widget(), *expect_hit.read()); } + + #[test] + fn fix_transform_hit() { + reset_test_env!(); + let (expect_hit, w_hit) = split_value(None); + let mut wnd = TestWindow::new_with_size( + fn_widget! { + @MockBox { + anchor: Point::new(50., 50.), + transform: Transform::rotation(Angle::degrees(45.)), + size: Size::new(100., 100.), + on_mounted: move |ctx| *$w_hit.write() = Some(ctx.id), + } + }, + Size::new(500., 500.), + ); + wnd.draw_frame(); + let mut dispatcher = wnd.dispatcher.borrow_mut(); + dispatcher.info.cursor_pos = Point::new(51., 51.); + assert!(expect_hit.read().is_some()); + assert_eq!(dispatcher.hit_widget(), *expect_hit.read()); + } }