Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(chrome-ext): allow users to hover over suggestions to open them (#4839)

Elijah Potterba0cdd03ca39
diff --git a/packages/chrome-plugin/src/ProtocolClient.ts b/packages/chrome-plugin/src/ProtocolClient.ts
index b0b5adcb7..d18f0b541 100644
--- a/packages/chrome-plugin/src/ProtocolClient.ts
+++ b/packages/chrome-plugin/src/ProtocolClient.ts
@@ -78,6 +78,16 @@ export default class ProtocolClient {
await chrome.runtime.sendMessage({ kind: 'setIsolateEnglish', isolateEnglish });
}
+ /** Read whether pointer hover should open suggestion popups. */
+ public static async getOpenSuggestionsOnHover(): Promise<boolean> {
+ return (await chrome.runtime.sendMessage({ kind: 'getOpenSuggestionsOnHover' })).enabled;
+ }
+
+ /** Persist the optional hover behavior for all extension tabs. */
+ public static async setOpenSuggestionsOnHover(enabled: boolean): Promise<void> {
+ await chrome.runtime.sendMessage({ kind: 'setOpenSuggestionsOnHover', enabled });
+ }
+
public static async getDelay(): Promise<number> {
return (await chrome.runtime.sendMessage({ kind: 'getDelay' })).delay;
}
diff --git a/packages/chrome-plugin/src/background/index.ts b/packages/chrome-plugin/src/background/index.ts
index 3f0161501..d78cf6e18 100644
--- a/packages/chrome-plugin/src/background/index.ts
+++ b/packages/chrome-plugin/src/background/index.ts
@@ -242,6 +242,15 @@ function handleRequest(message: Request, sender?: chrome.runtime.MessageSender):
return handleGetIsolateEnglish();
case 'setIsolateEnglish':
return handleSetIsolateEnglish(message);
+ case 'getOpenSuggestionsOnHover':
+ return chrome.storage.local.get({ openSuggestionsOnHover: false }).then((stored) => ({
+ kind: 'getOpenSuggestionsOnHover',
+ enabled: stored.openSuggestionsOnHover === true,
+ }));
+ case 'setOpenSuggestionsOnHover':
+ return chrome.storage.local
+ .set({ openSuggestionsOnHover: message.enabled === true })
+ .then(createUnitResponse);
case 'getDelay':
return handleGetDelay(message);
case 'setDelay':
diff --git a/packages/chrome-plugin/src/contentScript/index.ts b/packages/chrome-plugin/src/contentScript/index.ts
index 209b7ed63..8cbfef51a 100644
--- a/packages/chrome-plugin/src/contentScript/index.ts
+++ b/packages/chrome-plugin/src/contentScript/index.ts
@@ -37,6 +37,11 @@ const fw = new LintFramework(
},
);
+// Read once at startup; changes to this preference require a page reload.
+ProtocolClient.getOpenSuggestionsOnHover()
+ .then((enabled) => fw.setOpenSuggestionsOnHover(enabled))
+ .catch((error) => console.error('Failed to load suggestion hover setting:', error));
+
const syncGoogleDocsBridge = createGoogleDocsBridgeSync(fw);
function padWithContext(source: string, start: number, end: number, contextLength: number): string {
diff --git a/packages/chrome-plugin/src/options/Options.svelte b/packages/chrome-plugin/src/options/Options.svelte
index 78970c1a5..07d069fd5 100644
--- a/packages/chrome-plugin/src/options/Options.svelte
+++ b/packages/chrome-plugin/src/options/Options.svelte
@@ -47,6 +47,7 @@ let searchQueryLower = $derived(searchQuery.toLowerCase());
let expandedGroups: Record<string, boolean> = $state({});
let dialect = $state(Dialect.American);
let isolateEnglish = $state(false);
+let openSuggestionsOnHover = $state(false);
let delay = $state(0);
let delayLoaded = $state(false);
let defaultEnabled = $state(false);
@@ -96,6 +97,11 @@ $effect(() => {
ProtocolClient.setUserDictionary(stringToDict(userDict));
});
+$effect(() => {
+ if (!loaded) return;
+ ProtocolClient.setOpenSuggestionsOnHover(openSuggestionsOnHover);
+});
+
// Load all initial state before displaying settings to prevent flash of defaults
Promise.all([
ProtocolClient.getLintConfig(),
@@ -104,6 +110,7 @@ Promise.all([
ProtocolClient.getDialect(),
ProtocolClient.getIsolateEnglish(),
ProtocolClient.getDelay(),
+ ProtocolClient.getOpenSuggestionsOnHover(),
ProtocolClient.getDefaultEnabled(),
ProtocolClient.getActivationKey(),
ProtocolClient.getHotkey(),
@@ -117,6 +124,7 @@ Promise.all([
nextDialect,
nextIsolateEnglish,
nextDelay,
+ nextOpenSuggestionsOnHover,
nextDefaultEnabled,
nextActivationKey,
nextHotkey,
@@ -129,6 +137,7 @@ Promise.all([
dialect = nextDialect;
isolateEnglish = nextIsolateEnglish;
delay = nextDelay;
+ openSuggestionsOnHover = nextOpenSuggestionsOnHover;
delayLoaded = true;
defaultEnabled = nextDefaultEnabled;
activationKey = nextActivationKey;
@@ -623,6 +632,46 @@ async function removeWeirpack(id: string) {
<Button size="sm" class="h-9 w-48 shrink-0 cursor-pointer text-xs font-semibold bg-primary! text-black! hover:bg-primary/85! outline-none! focus:outline-none! focus:ring-0! focus-visible:ring-2! focus-visible:ring-primary!" aria-describedby="export-desc" on:click={exportEnabledDomainsCSV}>Export JSON</Button>
</div>
+ <!-- Suggestion Hover Activation -->
+ <div class="flex items-center justify-between gap-4 py-3.5">
+ <div class="flex flex-col">
+ <h3 id="hover-label" class="text-base font-semibold">Open suggestions on hover</h3>
+ <p id="hover-desc" class="text-xs text-gray-600 dark:text-[#d1d5db]!">Open suggestions after hovering over an underlined error for 200 ms. Reload open pages after changing this setting. Click and keyboard activation remain available.</p>
+ </div>
+
+ <div class="h-9 w-48 shrink-0 rounded-lg border border-gray-200 p-1 dark:border-slate-700 dark:bg-slate-900">
+ <div class="relative flex h-full w-full" role="radiogroup" aria-labelledby="hover-label" aria-describedby="hover-desc">
+ <div
+ class="absolute inset-y-0 left-0 w-1/2 rounded-md bg-primary transition-transform duration-300 ease-out motion-reduce:transition-none"
+ class:translate-x-0={openSuggestionsOnHover}
+ class:translate-x-full={!openSuggestionsOnHover}
+ ></div>
+ <button
+ type="button"
+ role="radio"
+ aria-checked={openSuggestionsOnHover}
+ tabindex={openSuggestionsOnHover ? 0 : -1}
+ onkeydown={(e) => handleRadioKeydown(e, (v) => (openSuggestionsOnHover = v))}
+ class="relative z-10 flex-1 cursor-pointer rounded-md py-1 text-center text-xs font-semibold transition-colors duration-200 motion-reduce:transition-none {openSuggestionsOnHover ? 'text-black' : 'text-gray-500 hover:text-gray-900 dark:text-[#d1d5db]! dark:hover:text-white!'}"
+ onclick={() => (openSuggestionsOnHover = true)}
+ >
+ On
+ </button>
+ <button
+ type="button"
+ role="radio"
+ aria-checked={!openSuggestionsOnHover}
+ tabindex={!openSuggestionsOnHover ? 0 : -1}
+ onkeydown={(e) => handleRadioKeydown(e, (v) => (openSuggestionsOnHover = v))}
+ class="relative z-10 flex-1 cursor-pointer rounded-md py-1 text-center text-xs font-semibold transition-colors duration-200 motion-reduce:transition-none {!openSuggestionsOnHover ? 'text-black' : 'text-gray-500 hover:text-gray-900 dark:text-[#d1d5db]! dark:hover:text-white!'}"
+ onclick={() => (openSuggestionsOnHover = false)}
+ >
+ Off
+ </button>
+ </div>
+ </div>
+ </div>
+
<!-- Activation Key -->
<div class="flex items-center justify-between gap-4 py-3.5">
<div class="flex flex-col">
diff --git a/packages/chrome-plugin/src/protocol.ts b/packages/chrome-plugin/src/protocol.ts
index 17d2dee27..e43aded89 100644
--- a/packages/chrome-plugin/src/protocol.ts
+++ b/packages/chrome-plugin/src/protocol.ts
@@ -11,6 +11,8 @@ export type Request =
| GetDialectRequest
| GetIsolateEnglishRequest
| SetIsolateEnglishRequest
+ | GetOpenSuggestionsOnHoverRequest
+ | SetOpenSuggestionsOnHoverRequest
| GetDelayRequest
| SetDelayRequest
| SetDomainStatusRequest
@@ -46,6 +48,7 @@ export type Response =
| GetLintDescriptionsResponse
| GetDialectResponse
| GetIsolateEnglishResponse
+ | GetOpenSuggestionsOnHoverResponse
| GetDelayResponse
| GetDomainStatusResponse
| GetDefaultStatusResponse
@@ -98,6 +101,20 @@ export type SetDialectRequest = {
dialect: Dialect;
};
+export type GetOpenSuggestionsOnHoverRequest = {
+ kind: 'getOpenSuggestionsOnHover';
+};
+
+export type SetOpenSuggestionsOnHoverRequest = {
+ kind: 'setOpenSuggestionsOnHover';
+ enabled: boolean;
+};
+
+export type GetOpenSuggestionsOnHoverResponse = {
+ kind: 'getOpenSuggestionsOnHover';
+ enabled: boolean;
+};
+
export type GetDelayRequest = {
kind: 'getDelay';
};
diff --git a/packages/lint-framework/src/lint/LintFramework.ts b/packages/lint-framework/src/lint/LintFramework.ts
index 15045d1b5..6574a707a 100644
--- a/packages/lint-framework/src/lint/LintFramework.ts
+++ b/packages/lint-framework/src/lint/LintFramework.ts
@@ -106,6 +106,11 @@ export default class LintFramework {
this.attachWindowListeners();
}
+ /** Enable optional hover activation without changing click or keyboard activation. */
+ public setOpenSuggestionsOnHover(enabled: boolean): void {
+ this.popupHandler.setOpenSuggestionsOnHover(enabled);
+ }
+
/** Returns the currents targets that are visible on-screen. */
onScreenTargets(): Node[] {
const onScreen = [] as Node[];
diff --git a/packages/lint-framework/src/lint/PopupHandler.ts b/packages/lint-framework/src/lint/PopupHandler.ts
index b9f183af2..f9962f49c 100644
--- a/packages/lint-framework/src/lint/PopupHandler.ts
+++ b/packages/lint-framework/src/lint/PopupHandler.ts
@@ -10,6 +10,8 @@ import hintsData from '../assets/hints.json';
import RenderBox from './RenderBox';
import SuggestionBox from './SuggestionBox';
+const HOVER_ACTIVATION_DELAY_MS = 200;
+
type ActivationHandler = () => void;
function monitorActivationKey(
@@ -33,6 +35,15 @@ export default class PopupHandler {
private currentLintBoxes: IgnorableLintBox[];
private popupLint: number | undefined;
private selectedSuggestion = 0;
+ private openSuggestionsOnHover = false;
+ /** One pending dwell, identified by source, lint span, and the latest pointer position. */
+ private pendingHover:
+ | {
+ box: IgnorableLintBox;
+ point: [number, number];
+ timer: ReturnType<typeof setTimeout>;
+ }
+ | undefined;
/** Only a keyboard activation moves focus into the popup; a click leaves it in the editor. */
private autofocusSuggestion = false;
private currentHint: string | null | undefined;
@@ -88,6 +99,7 @@ export default class PopupHandler {
/** Open and render the row nearest the editor caret immediately on activation. */
private openClosestToCaret() {
+ this.cancelPendingHover();
const host = this.renderBox.getShadowHost();
const popupFocused = document.activeElement === host;
const caretPosition = popupFocused ? null : getCaretPosition();
@@ -114,7 +126,86 @@ export default class PopupHandler {
row?.scrollIntoView({ block: 'nearest' });
}
+ /** Update source listeners immediately; hover is off by default for all hosts. */
+ public setOpenSuggestionsOnHover(enabled: boolean): void {
+ if (this.openSuggestionsOnHover === enabled) return;
+ this.openSuggestionsOnHover = enabled;
+ this.cancelPendingHover();
+ for (const { source } of this.currentLintBoxes) {
+ if (enabled) {
+ source.addEventListener('pointermove', this.onPointerMove as EventListener);
+ source.addEventListener('pointerleave', this.cancelPendingHover);
+ } else {
+ source.removeEventListener('pointermove', this.onPointerMove as EventListener);
+ source.removeEventListener('pointerleave', this.cancelPendingHover);
+ }
+ }
+ }
+
+ /** Cancel a pending dwell and remove its temporary interruption listeners. */
+ private cancelPendingHover = (): void => {
+ if (!this.pendingHover) return;
+ clearTimeout(this.pendingHover.timer);
+ this.pendingHover = undefined;
+ document.removeEventListener('pointerdown', this.cancelPendingHover, true);
+ window.removeEventListener('blur', this.cancelPendingHover);
+ window.removeEventListener('scroll', this.cancelPendingHover, true);
+ };
+
+ /** Resolve the pending lint against fresh boxes instead of retaining a stale row index. */
+ private pendingHoverIndex(): number {
+ const pending = this.pendingHover;
+ if (!pending) return -1;
+ return this.currentLintBoxes.findIndex(
+ (box) =>
+ box.source === pending.box.source &&
+ box.lint.span.start === pending.box.lint.span.start &&
+ box.lint.span.end === pending.box.lint.span.end &&
+ box.lint.source === pending.box.lint.source &&
+ isPointInBox(pending.point, box),
+ );
+ }
+
+ /** Require a 200 ms dwell on one error without moving focus or interrupting dragging. */
+ private onPointerMove = (event: PointerEvent): void => {
+ if (!this.openSuggestionsOnHover || event.buttons !== 0 || event.pointerType === 'touch') {
+ this.cancelPendingHover();
+ return;
+ }
+ const path = event.composedPath();
+ if (path.includes(this.renderBox.getShadowHost())) {
+ this.cancelPendingHover();
+ return;
+ }
+ const point: [number, number] = [event.x, event.y];
+ const index = this.currentLintBoxes.findIndex(
+ (box) => path.includes(box.source) && isPointInBox(point, box),
+ );
+ if (this.pendingHover) this.pendingHover.point = point;
+ if (index >= 0 && index !== this.popupLint && index === this.pendingHoverIndex()) return;
+ this.cancelPendingHover();
+ // Leaving the underline cancels activation, but does not close an open popup.
+ if (index < 0 || index === this.popupLint) return;
+ this.pendingHover = {
+ box: this.currentLintBoxes[index],
+ point,
+ timer: setTimeout(() => {
+ const nextIndex = this.pendingHoverIndex();
+ this.cancelPendingHover();
+ if (!this.openSuggestionsOnHover || nextIndex < 0) return;
+ this.popupLint = nextIndex;
+ this.selectedSuggestion = 0;
+ this.autofocusSuggestion = false;
+ this.render();
+ }, HOVER_ACTIVATION_DELAY_MS),
+ };
+ document.addEventListener('pointerdown', this.cancelPendingHover, true);
+ window.addEventListener('blur', this.cancelPendingHover);
+ window.addEventListener('scroll', this.cancelPendingHover, true);
+ };
+
private onPointerDown(e: PointerEvent) {
+ this.cancelPendingHover();
for (let i = 0; i < this.currentLintBoxes.length; i++) {
const box = this.currentLintBoxes[i];
@@ -132,6 +223,7 @@ export default class PopupHandler {
}
private close = () => {
+ this.cancelPendingHover();
this.popupLint = undefined;
this.selectedSuggestion = 0;
this.autofocusSuggestion = false;
@@ -214,6 +306,8 @@ export default class PopupHandler {
const popupFocused = document.activeElement === host;
this.currentLintBoxes.forEach((b) => {
b.source.removeEventListener('pointerdown', this.pointerDownCallback as EventListener);
+ b.source.removeEventListener('pointermove', this.onPointerMove as EventListener);
+ b.source.removeEventListener('pointerleave', this.cancelPendingHover);
});
const previous = this.popupLint == null ? undefined : this.currentLintBoxes[this.popupLint];
@@ -255,8 +349,13 @@ export default class PopupHandler {
if (this.popupLint != null) this.currentHintFor = this.popupLint;
this.currentLintBoxes = boxes;
+ if (this.pendingHoverIndex() < 0) this.cancelPendingHover();
this.currentLintBoxes.forEach((b) => {
b.source.addEventListener('pointerdown', this.pointerDownCallback as EventListener);
+ if (this.openSuggestionsOnHover) {
+ b.source.addEventListener('pointermove', this.onPointerMove as EventListener);
+ b.source.addEventListener('pointerleave', this.cancelPendingHover);
+ }
});
this.render();