Skip to content

Commit

Permalink
Firefox: try to recover hidden keydown#esc
Browse files Browse the repository at this point in the history
Firefox's `permissions.default.shortcuts=2 (disallow)` will hide
  `Escape` keydown events,
so Vimium C has to recover it from keyup ones.

See philc/vimium#3699 (comment)
  • Loading branch information
gdh1995 committed Feb 13, 2024
1 parent 7127339 commit c3d04f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
8 changes: 7 additions & 1 deletion content/key_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let currentKeys: string
let curKeyTimestamp: number
let nextKeys: KeyFSM | ReadonlyChildKeyFSM & SafeObject | null

let maybeEscIsHidden_ff: kKeyCode | 0 = kKeyCode.esc
let isWaitingAccessKey = false
let isCmdTriggered: kKeyCode = kKeyCode.None
let noopEventHandler: EventListenerObject["handleEvent"] & (() => void) = Object.is as any
Expand All @@ -43,7 +44,7 @@ set_esc(function<T extends Exclude<HandlerResult, HandlerResult.ExitNormalMode>>

export {
passKeys, keyFSM, mappedKeys, mapKeyTypes, currentKeys, isWaitingAccessKey, isCmdTriggered, anyClickHandler,
onPassKey, isPassKeysReversed, noopEventHandler as noopHandler,
onPassKey, isPassKeysReversed, noopEventHandler as noopHandler, maybeEscIsHidden_ff
}
export function set_isCmdTriggered (_newTriggerred: kKeyCode): kKeyCode { return isCmdTriggered = _newTriggerred }
export function set_passKeys (_newPassKeys: typeof passKeys): void { passKeys = _newPassKeys }
Expand All @@ -54,6 +55,7 @@ export function set_keyFSM (_newKeyFSM: KeyFSM): KeyFSM { return keyFSM = _newKe
export function set_mapKeyTypes (_newMapKeyTypes: kMapKey): void { mapKeyTypes = _newMapKeyTypes }
export function set_mappedKeys (_newMappedKeys: typeof mappedKeys): void { mappedKeys = _newMappedKeys }
export function set_currentKeys (_newCurrentKeys: string): void { currentKeys = _newCurrentKeys }
export function set_maybeEscIsHidden_ff (_isEsc: 0): void { maybeEscIsHidden_ff = _isEsc }

export const inheritKeyMappings = (newState: ReturnType<VApiTy["y"]>): void => {
if (newState.m[3]) {
Expand Down Expand Up @@ -232,6 +234,7 @@ export const onKeydown = (event: KeyboardEventToPrevent): void => {
return;
}
OnChrome && isWaitingAccessKey && /*#__NOINLINE__*/ resetAnyClickHandler_cr()
OnFirefox && key === kKeyCode.esc && event.type[3] < kChar.e && (maybeEscIsHidden_ff = 0)
OnFirefox && raw_insert_lock && insert_Lock_()
let action = HandlerResult.Nothing, keyStr: string;
let handler_ind = handler_stack.length
Expand Down Expand Up @@ -331,6 +334,9 @@ export const onKeyup = (event: KeyboardEventToPrevent): void => {
: event.isTrusted === false) // skip checks of `instanceof KeyboardEvent` if checking `!.keyCode`
&& (event as UserTrustedKeyboardEvent).z !== fgCache
) { return; }
if (OnFirefox && key === maybeEscIsHidden_ff && key && !keydownEvents_[key] && event.key === "Escape") {
onKeydown(event)
}
if (scroll_keyIsDown && (key === isCmdTriggered || isCmdTriggered < kKeyCode.True + 1)) {
scrollTick(0);
}
Expand Down
13 changes: 10 additions & 3 deletions content/mode_find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import { hudHide, hud_box, hudTip, hud_opacity, toggleOpacity as hud_toggleOpaci
import { post_, send_, runFallbackKey, runtimeConnect, runtime_port } from "./port"
import { insert_Lock_, raw_insert_lock, setupSuppress } from "./insert"
import { lastHovered_, set_lastHovered_, set_lastBubbledHovered_, select_ } from "./async_dispatcher"
import { checkKey, checkKeyOnTop, currentKeys, noopHandler, set_isCmdTriggered } from "./key_handler"
import {
checkKey, checkKeyOnTop, currentKeys, noopHandler, set_isCmdTriggered, maybeEscIsHidden_ff, set_maybeEscIsHidden_ff
} from "./key_handler"

export declare const enum FindAction {
PassDirectly = -1,
Expand Down Expand Up @@ -554,15 +556,19 @@ const onPaste_not_cr = !OnChrome ? (event: ClipboardEvent & ToPrevent): void =>
} : 0 as never as null

const onIFrameKeydown = (event: KeyboardEventToPrevent): void => {
const n = event.keyCode, isUp = event.type === "keyup" && !set_isCmdTriggered(0)
Stop_(event)
if ((!OnChrome || Build.MinCVer >= BrowserVer.Min$Event$$IsTrusted
? !event.isTrusted : event.isTrusted === false) && (event as UserTrustedKeyboardEvent).z !== fgCache) { return }
const n = event.keyCode
const eventWrapper: HandlerNS.Event = {c: kChar.INVALID, e: event, i: n, v: ""}
if (!n || n === kKeyCode.ime || scroll_keyIsDown && onScrolls(eventWrapper) || isUp) {
let isUp: boolean | 0 = event.type[3] > kChar.e && !set_isCmdTriggered(0)
const isEscDownUp = OnFirefox && n === maybeEscIsHidden_ff && isUp && n && !keydownEvents_[n]
&& event.key === "Escape"
if (!n || n === kKeyCode.ime || scroll_keyIsDown && onScrolls(eventWrapper) || isUp && !isEscDownUp) {
if (isUp && keydownEvents_[n]) { keydownEvents_[n] = 0; prevent_(event) }
return
}
OnFirefox && n === kKeyCode.esc && !isUp && set_maybeEscIsHidden_ff(0)
const consumedByHost = currentKeys && checkKeyOnTop(eventWrapper),
key = getMappedKey(eventWrapper, kModeId.Find), keybody = keybody_(key);
const i: FindAction | KeyStat = consumedByHost ? FindAction.ConsumedByHost
Expand Down Expand Up @@ -627,6 +633,7 @@ const onIFrameKeydown = (event: KeyboardEventToPrevent): void => {
} else {
i < FindAction.MinNotExit && deactivate(i)
}
if (OnFirefox && isEscDownUp && keydownEvents_[n]) { keydownEvents_[n] = 0; prevent_(event) }
}

const onHostKeydown = (event: HandlerNS.Event): HandlerResult => {
Expand Down
19 changes: 15 additions & 4 deletions front/vomnibar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,8 @@ var VCID_: string | undefined = VCID_ || "", VHost_: string | undefined = VHost_
};
input.addEventListener("compositionstart", func);
input.addEventListener("compositionend", func);
} else {
listen("keyup", a.HandleKeyup_ff_!, true);
}
a.styleEl_ && document.head!.appendChild(a.styleEl_);
a.darkBtn_ = document.querySelector("#toggle-dark") as HTMLElement | null;
Expand Down Expand Up @@ -1495,13 +1497,19 @@ var VCID_: string | undefined = VCID_ || "", VHost_: string | undefined = VHost_
if (Build.MinCVer >= BrowserVer.Min$Event$$IsTrusted || !(Build.BTypes & BrowserType.Chrome) ? !event.isTrusted
: event.isTrusted !== true && !(event.isTrusted == null && event instanceof KeyboardEvent)) { return; }
Vomnibar_.keyResult_ = SimpleKeyResult.Prevent as SimpleKeyResult;
let stop: 0 | 1 | 2 | 3 = 3, now = 0
let keyCode = event.keyCode, stop: 0 | 1 | 2 | 3 = 3, now = 0
if (Build.BTypes & BrowserType.Firefox && (Build.BTypes === BrowserType.Firefox as number
|| Vomnibar_.browser_ & BrowserType.Firefox) && keyCode === kKeyCode.esc
&& !!Vomnibar_.HandleKeyup_ff_ && event.type[3] < kChar.e && event.key === "Escape") {
removeEventListener("keyup", Vomnibar_.HandleKeyup_ff_!, true)
Vomnibar_.HandleKeyup_ff_ = null
}
if (Vomnibar_.last_scrolling_key_) {
const keyCode = event.keyCode, hasChar = keyCode > kKeyCode.maxAcsKeys || keyCode < kKeyCode.minAcsKeys,
const hasChar = keyCode > kKeyCode.maxAcsKeys || keyCode < kKeyCode.minAcsKeys,
isSameChar = keyCode === Math.abs(Vomnibar_.last_scrolling_key_)
stop = event.repeat || hasChar && isSameChar && event.type === "keydown" ? 0 : 1
stop = event.repeat || hasChar && isSameChar && event.type[3] < kChar.e ? 0 : 1
if (hasChar && !isSameChar) { stop = 3 }
else if (Vomnibar_.last_scrolling_key_ > 0) { stop = event.type === "keyup" ? 2 : 0 }
else if (Vomnibar_.last_scrolling_key_ > 0) { stop = event.type[3] < kChar.e ? 0 : 2 }
else if (stop || (now = event.timeStamp) - Vomnibar_.lastScrolling_ > 40 || now < Vomnibar_.lastScrolling_) {
VPort_.postToOwner_({ N: stop ? VomnibarNS.kFReq.stopScroll : VomnibarNS.kFReq.scrollGoing })
Vomnibar_.lastScrolling_ = now;
Expand All @@ -1517,6 +1525,9 @@ var VCID_: string | undefined = VCID_ || "", VHost_: string | undefined = VHost_
if (Vomnibar_.keyResult_ === SimpleKeyResult.Nothing) { return; }
VUtils_.Stop_(event, Vomnibar_.keyResult_ === SimpleKeyResult.Prevent);
},
HandleKeyup_ff_: Build.BTypes & BrowserType.Firefox ? (event: KeyboardEventToPrevent): void => {
event.keyCode === kKeyCode.esc && event.key === "Escape" && Vomnibar_.HandleKeydown_(event)
} : 0 as never as null,
onAltUp_ (event: KeyboardEvent): void {
const listened = Vomnibar_._listenedAltDown
if (!listened || (typeof listened === "string" ? Vomnibar_.getMappedKey_(event).key : event.keyCode) === listened) {
Expand Down

0 comments on commit c3d04f4

Please sign in to comment.