-
-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: trigger contextual men on control-click or long press in the ma…
…thfield
- Loading branch information
Showing
9 changed files
with
234 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { distance } from 'ui/geometry/utils'; | ||
import { eventLocation } from './utils'; | ||
|
||
// We use a class to encapsulate the state that needs to be tracked and, | ||
// more importantly, to avoid memory leaks by using the `handleEvent()` hook | ||
// to ensure proper disposal of event handlers | ||
export class LongPressDetector { | ||
static DELAY = 300; // In ms | ||
static DISTANCE = 10; // In pixels | ||
|
||
private readonly startPoint?: { x: number; y: number }; | ||
private lastPoint?: { x: number; y: number }; | ||
|
||
private timer = 0; | ||
|
||
constructor(triggerEvent: Event, onLongPress: () => void) { | ||
const location = eventLocation(triggerEvent); | ||
if (!location) return; | ||
|
||
this.startPoint = location; | ||
this.lastPoint = location; | ||
|
||
this.timer = setTimeout(() => { | ||
this.dispose(); | ||
const delta = distance(this.lastPoint!, this.startPoint!); | ||
if (delta < LongPressDetector.DISTANCE) onLongPress(); | ||
}, LongPressDetector.DELAY); | ||
for (const evt of ['pointermove', 'pointerup', 'pointercancel']) | ||
window.addEventListener(evt, this, { passive: true }); | ||
} | ||
|
||
dispose(): void { | ||
clearTimeout(this.timer); | ||
this.timer = 0; | ||
|
||
for (const evt of ['pointermove', 'pointerup', 'pointercancel']) | ||
window.removeEventListener(evt, this); | ||
} | ||
|
||
handleEvent(event: Event): void { | ||
if (event.type === 'pointerup' || event.type === 'pointercancel') { | ||
this.dispose(); | ||
event.stopPropagation(); | ||
} else if (event.type === 'pointermove') { | ||
const location = eventLocation(event); | ||
if (location) { | ||
this.lastPoint = location; | ||
event.stopPropagation(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function onLongPress( | ||
triggerEvent: Event, | ||
onLongPress: () => void | ||
): LongPressDetector { | ||
return new LongPressDetector(triggerEvent, onLongPress); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { eventLocation, keyboardModifiersFromEvent } from 'ui/events/utils'; | ||
import { Menu } from './menu'; | ||
import { onLongPress } from 'ui/events/longpress'; | ||
|
||
export function onContextMenu( | ||
event: Event, | ||
target: Element, | ||
menu: Menu, | ||
onTrigger?: () => void | ||
): boolean { | ||
// | ||
// The context menu gesture (right-click, control-click, etc..) | ||
// was triggered | ||
// | ||
if (event.type === 'contextmenu') { | ||
const evt = event as MouseEvent; | ||
onTrigger?.(); | ||
menu.show({ | ||
parent: target, | ||
location: { x: Math.round(evt.clientX), y: Math.round(evt.clientY) }, | ||
keyboardModifiers: keyboardModifiersFromEvent(evt), | ||
}); | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
return true; | ||
} | ||
|
||
// | ||
// The context menu keyboard shortcut (shift+F10) was triggered | ||
// | ||
if (event.type === 'keydown') { | ||
const evt = event as KeyboardEvent; | ||
if (evt.code === 'ContextMenu' || (evt.code === 'F10' && evt.shiftKey)) { | ||
// Shift+F10 = contextual menu | ||
// Get the center of the parent | ||
const bounds = target?.getBoundingClientRect(); | ||
if (bounds) { | ||
onTrigger?.(); | ||
menu.show({ | ||
parent: target, | ||
location: { | ||
x: Math.round(bounds.left + bounds.width / 2), | ||
y: Math.round(bounds.top + bounds.height / 2), | ||
}, | ||
keyboardModifiers: keyboardModifiersFromEvent(evt), | ||
}); | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
// | ||
// This might be a long press... | ||
// | ||
if (event.type === 'pointerdown') { | ||
// Are we inside the target element? | ||
let eventTarget = event.target as HTMLElement; | ||
while (eventTarget && target !== eventTarget) | ||
eventTarget = eventTarget.parentNode as HTMLElement; | ||
if (!eventTarget) return false; | ||
|
||
const pt = eventLocation(event); | ||
const keyboardModifiers = keyboardModifiersFromEvent(event); | ||
onLongPress(event, () => { | ||
if (menu.state !== 'closed') return; | ||
onTrigger?.(); | ||
menu.show({ parent: target, location: pt, keyboardModifiers }); | ||
// // @ts-ignore | ||
// if (menu.state === 'open') menu.state = 'modal'; | ||
}); | ||
return true; | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.