Skip to content

Commit

Permalink
Skip cursor tracking when unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Aug 17, 2024
1 parent ec6c004 commit 58ec30a
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 58ec30a

Please sign in to comment.