fix(overlay): keep Hachidori attention from pausing playback

Hachidori's hachidori-popup-shown/hidden pair means "the reader needs
mouse events", which includes a left press anywhere on the overlay that
may start a selection. SubMiner treated it as popup visibility, so a
click paused and resumed mpv, and holding the button kept it paused.
The host element only exists after the first lookup, so the first
attempt (gate on an open popup pane when a host exists) still paused
on clicks before any lookup.

Once a Hachidori event has been seen, popup auto-pause now requires an
unhidden popup pane in the host's shadow root, and rechecks after each
successful lookup so drag-select popups still pause.
This commit is contained in:
2026-09-22 21:06:19 -07:00
parent 2958591ec9
commit c39dbb5a8d
4 changed files with 274 additions and 12 deletions
+40 -4
View File
@@ -10,17 +10,39 @@ export const YOMITAN_POPUP_MOUSE_LEAVE_EVENT = 'yomitan-popup-mouse-leave';
export const YOMITAN_POPUP_COMMAND_EVENT = 'subminer-yomitan-popup-command';
export const YOMITAN_LOOKUP_EVENT = 'subminer-yomitan-lookup';
export const PRIMARY_SUB_VISIBLE_ON_YOMITAN_POPUP_CLASS = 'primary-sub-visible-on-yomitan-popup';
// Hachidori's shown/hidden pair means "the reader needs mouse events", which
// also covers a left press on subtitle text that may start a selection. Its
// popup panes live in the host's open shadow root.
export const HACHIDORI_POPUP_SHOWN_EVENT = 'hachidori-popup-shown';
export const HACHIDORI_POPUP_HIDDEN_EVENT = 'hachidori-popup-hidden';
export const HACHIDORI_HOST_SELECTOR = 'hachidori-host';
export const HACHIDORI_POPUP_SELECTOR = '.gsm-hoshidicts-popup';
export type DictionaryReader = 'yomitan' | 'hachidori';
// Only the active backend injects a reader. Consume its native attention events.
export function registerDictionaryPopupVisibilityListener(
state: 'shown' | 'hidden',
listener: () => void,
listener: (reader: DictionaryReader) => void,
target: EventTarget = window,
): () => void {
const events = [`yomitan-popup-${state}`, `hachidori-popup-${state}`];
for (const event of events) target.addEventListener(event, listener);
const events: Array<[string, DictionaryReader]> =
state === 'shown'
? [
[YOMITAN_POPUP_SHOWN_EVENT, 'yomitan'],
[HACHIDORI_POPUP_SHOWN_EVENT, 'hachidori'],
]
: [
[YOMITAN_POPUP_HIDDEN_EVENT, 'yomitan'],
[HACHIDORI_POPUP_HIDDEN_EVENT, 'hachidori'],
];
const wrapped = events.map(([event, reader]) => {
const handler = (): void => listener(reader);
target.addEventListener(event, handler);
return [event, handler] as const;
});
return () => {
for (const event of events) target.removeEventListener(event, listener);
for (const [event, handler] of wrapped) target.removeEventListener(event, handler);
};
}
@@ -86,6 +108,20 @@ function queryPopupElements<T extends Element>(
return [];
}
/**
* Whether a Hachidori popup pane is on screen. Auto-pause reads this because
* the host's visible marker only tracks Hachidori's attention signal. Hachidori
* attaches its host on the first lookup, so no host means no popup.
*/
export function isHachidoriPopupOpen(root: ParentNode | null | undefined = document): boolean {
const hosts = queryPopupElements<HTMLElement>(root, HACHIDORI_HOST_SELECTOR);
return hosts.some((host) =>
queryPopupElements<HTMLElement>(host.shadowRoot, HACHIDORI_POPUP_SELECTOR).some(
(pane) => !pane.hidden,
),
);
}
export function isYomitanPopupVisible(root: ParentNode | null | undefined = document): boolean {
const visiblePopupHosts = queryPopupElements<HTMLElement>(
root,