Skip to content

Commit

Permalink
allow micro drag for click
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Jun 30, 2024
1 parent a8aa4d5 commit 0159c35
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
20 changes: 18 additions & 2 deletions page/ux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ import {
expand
} from './scroll'

const DRAG_THRESHOLD = 10

let pressed = false
let dragging = false
let startX = 0
let startY = 0
// accumulate
let dX = 0
let dY = 0
let dragOffset = 0

type ShadowBox = {
shadowTop: number,
Expand Down Expand Up @@ -149,6 +155,9 @@ document.addEventListener('mousedown', e => {
pressed = true
startX = e.clientX
startY = e.clientY
dX = 0
dY = 0
dragOffset = 0
})

document.addEventListener('mousemove', e => {
Expand All @@ -157,8 +166,13 @@ document.addEventListener('mousemove', e => {
}
hideContextmenu()
dragging = true
const dx = e.clientX - startX
const dy = e.clientY - startY
dX += dx
dY += dy
dragOffset = Math.max(dragOffset, dX * dX + dY * dY)
// minus because macOS has bottom-left (0, 0)
resize(e.clientX - startX, -(e.clientY - startY), true, false)
resize(dx, -dy, true, false)
})

document.addEventListener('mouseup', e => {
Expand All @@ -168,7 +182,9 @@ document.addEventListener('mouseup', e => {
pressed = false
if (dragging) {
dragging = false
return
if (dragOffset > DRAG_THRESHOLD) {
return
}
}
let target = e.target as Element
if (!isInsideHoverables(target)) {
Expand Down
19 changes: 19 additions & 0 deletions tests/test-generic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,25 @@ test('Drag should not select candidate', async ({ page }) => {
expect(cppCalls[0]).toHaveProperty('resize')
})

test('But micro drag is tolerated', async ({ page }) => {
await init(page)
await setCandidates(page, [
{ text: '微动', label: '1', comment: '', actions: [] },
{ text: '选词', label: '2', comment: '', actions: [] }], 0)

const box = await getBox(candidate(page, 0))
const centerX = box.x + box.width / 2
const centerY = box.y + box.height / 2
await page.mouse.move(centerX, centerY + 1)
await page.mouse.down()
await page.mouse.move(centerX, centerY + 1)
await page.mouse.up()
const cppCalls = await getCppCalls(page)
expect(cppCalls).toHaveLength(2)
expect(cppCalls[0]).toHaveProperty('resize')
expect(cppCalls[1]).toEqual({ select: 0 })
})

test('Set layout', async ({ page }) => {
await init(page)
await setCandidates(page, [
Expand Down

0 comments on commit 0159c35

Please sign in to comment.