From 58ec30a094952918bab72b11b114a0a70cae6b27 Mon Sep 17 00:00:00 2001 From: Ben Frankel Date: Sat, 17 Aug 2024 16:26:10 -0700 Subject: [PATCH] Skip cursor tracking when unnecessary --- src/lib.rs | 64 +++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9f128a2..81c4c19 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,41 +120,41 @@ fn update_tooltip_context( let _old_state = ctx.state; // Detect cursor movement. - for (camera, camera_gt) in &camera_query { - let RenderTarget::Window(window) = camera.target else { - continue; - }; - let window = match window { - WindowRef::Primary => cq!(primary_window_query.get_single()), - WindowRef::Entity(id) => id, - }; - let window = c!(window_query.get(window)); - cq!(window.focused); - let cursor_pos = cq!(window - .cursor_position() - .and_then(|cursor| camera.viewport_to_world_2d(camera_gt, cursor))); - - // Reset activation delay on cursor move. - if ctx.cursor_pos != cursor_pos - && matches!(ctx.state, TooltipState::Delayed) - && ctx.activation.reset_delay_on_cursor_move - { - ctx.timer = ctx.activation.delay; - } + if (matches!(ctx.state, TooltipState::Delayed) && ctx.activation.reset_delay_on_cursor_move) + || (matches!(ctx.state, TooltipState::Active) && ctx.activation.radius.is_finite()) + { + for (camera, camera_gt) in &camera_query { + let RenderTarget::Window(window) = camera.target else { + continue; + }; + let window = match window { + WindowRef::Primary => cq!(primary_window_query.get_single()), + WindowRef::Entity(id) => id, + }; + let window = c!(window_query.get(window)); + cq!(window.focused); + let cursor_pos = cq!(window + .cursor_position() + .and_then(|cursor| camera.viewport_to_world_2d(camera_gt, cursor))); + + // Reset activation delay on cursor move. + if matches!(ctx.state, TooltipState::Delayed) + && ctx.activation.reset_delay_on_cursor_move + && ctx.cursor_pos != cursor_pos + { + ctx.cursor_pos = cursor_pos; + ctx.timer = ctx.activation.delay; + } - // Dismiss tooltip if cursor has left the activation radius. - if matches!(ctx.state, TooltipState::Active) - && ctx.cursor_pos.distance_squared(cursor_pos) > ctx.activation.radius - { - ctx.state = TooltipState::Dismissed; - } + // Dismiss tooltip if cursor has left the activation radius. + if matches!(ctx.state, TooltipState::Active) + && ctx.cursor_pos.distance_squared(cursor_pos) > ctx.activation.radius + { + ctx.state = TooltipState::Dismissed; + } - // Update cursor position. - if !matches!(ctx.state, TooltipState::Active) { - ctx.cursor_pos = cursor_pos; + break; } - - break; } // Tick timer for transfer timeout / activation delay.