Skip to content

Commit 6a9d115

Browse files
committed
Try fixing ipad long-press menu
1 parent be233ac commit 6a9d115

1 file changed

Lines changed: 50 additions & 4 deletions

File tree

editor/src/main/scala/dev/sacode/flowrun/FlowRunEditor.scala

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,55 @@ class FlowRunEditor(
425425
if mode.editable then flowRunElements.addFunButton.onclick = _ => programModel.addFunction()
426426
else flowRunElements.addFunButton.remove()
427427

428-
flowRunElements.drawArea.addEventListener(
429-
"contextmenu",
430-
(event: dom.MouseEvent) => {
428+
// context menu setup
429+
// handling pointer events manually because of ipad quirks
430+
locally {
431+
println("Setting up context menu...")
432+
var pressTimer = 0
433+
var startX = 0D
434+
var startY = 0D
435+
var isLongPress = false
436+
val element = flowRunElements.drawArea
437+
element.addEventListener(
438+
"pointerdown",
439+
(event: dom.PointerEvent) => {
440+
startX = event.clientX;
441+
startY = event.clientY;
442+
isLongPress = false;
443+
444+
// Only set timer for touch/pen, not mouse
445+
if event.pointerType != "mouse" then {
446+
pressTimer = dom.window.setTimeout(
447+
() => {
448+
isLongPress = true
449+
showContextMenu(event)
450+
},
451+
500
452+
)
453+
}
454+
}
455+
)
456+
element.addEventListener("pointermove", (_: dom.Event) =>
457+
dom.window.clearTimeout(pressTimer)
458+
)
459+
element.addEventListener("pointerup", (_: dom.Event) =>
460+
dom.window.clearTimeout(pressTimer)
461+
)
462+
463+
// Handle right-click for mouse
464+
element.addEventListener(
465+
"contextmenu",
466+
(event: dom.MouseEvent) => {
467+
println("contextmenu event..")
468+
// Don't show if long-press already triggered
469+
if isLongPress then
470+
isLongPress = false;
471+
showContextMenu(event)
472+
}
473+
)
474+
475+
def showContextMenu(event: dom.MouseEvent) = {
476+
println(s"Showing context menu..")
431477
event.preventDefault()
432478
DomUtils.getNearestSvgNode(event) match {
433479
case ("NODE", n) =>
@@ -441,7 +487,7 @@ class FlowRunEditor(
441487
flowrunChannel := FlowRun.Event.Deselected
442488
}
443489
}
444-
)
490+
}
445491

446492
flowRunElements.showFunctionsCheckbox.checked = programModel.ast.config.showFunctions
447493
flowRunElements.showCodeCheckbox.checked = programModel.ast.config.showGenCode

0 commit comments

Comments
 (0)