Skip to content

Commit

Permalink
feat(ui): improved tooltip behavior
Browse files Browse the repository at this point in the history
Close when mouse leaves map
Close when mouse is far from system
Shorter debounce before opening
  • Loading branch information
MichaelMakesGames committed Feb 27, 2024
1 parent 9c00a9c commit 925a90d
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/renderer/src/lib/map/MapContainer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,14 @@
map.$destroy();
});
const TOOLTIP_MAX_DISTANCE = 32;
const TOOLTIP_AUTOCLOSE_DISTANCE = TOOLTIP_MAX_DISTANCE * 2;
let tooltip: {
x: number;
y: number;
system: GalacticObject;
} | null = null;
function onMouseMove(e: MouseEvent) {
function onMouseMoveInner(e: MouseEvent) {
if (dataOrNull != null && !resizing && !zooming) {
let viewBoxWidth = 1000;
let viewBoxHeight = 1000;
Expand Down Expand Up @@ -299,7 +301,10 @@
if (settings.terraIncognita && !processedSystem.systemIsKnown) {
tooltip = null;
} else if (Math.hypot(tooltipPoint[0] - e.offsetX, tooltipPoint[1] - e.offsetY) > 32) {
} else if (
Math.hypot(tooltipPoint[0] - e.offsetX, tooltipPoint[1] - e.offsetY) >
TOOLTIP_MAX_DISTANCE
) {
tooltip = null;
} else {
tooltip = {
Expand All @@ -312,6 +317,16 @@
}
}
}
const onMouseMoveInnerDebounced = debounce(onMouseMoveInner, 50);
function onMouseMove(e: MouseEvent) {
if (
tooltip &&
Math.hypot(tooltip.x - e.offsetX, tooltip.y - e.offsetY) > TOOLTIP_AUTOCLOSE_DISTANCE
) {
tooltip = null;
}
onMouseMoveInnerDebounced(e);
}
</script>

<div class="relative h-full w-full" style:background={bg} bind:this={container}>
Expand Down Expand Up @@ -370,13 +385,17 @@
</div>
</div>
{/await}
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
<svg
bind:this={svg}
width={outputWidth}
height={outputHeight}
viewBox="0 0 {outputWidth} {outputHeight}"
role="presentation"
on:mousemove={debounce(onMouseMove, 100)}
on:mousemove={onMouseMove}
on:mouseout={() => {
tooltip = null;
}}
class="h-full w-full"
>
<g bind:this={g}>
Expand Down

0 comments on commit 925a90d

Please sign in to comment.