turbo-editors/turbo-corepublic Fork 0
v1.0.1
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

click.go · 55 lines · 1.8 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 17h ago1// Counting mouse clicks, so that a second one on the same spot means
2// something different from the first.
3
4package editor
5
6import (
7 "time"
8
📦 Turbo Core f3ade8d k33g 10h ago9 "rickub.com/turbo-editors/turbo-core/buffer"
🛟 Updated. 28d5985 k33g 17h ago10)
11
12// doubleClickWithin is how long after a press a second one on the same cell
13// still counts as a double click.
14//
15// Four hundred milliseconds is what most desktops settle on. Shorter and a
16// deliberate double click made with an unhurried hand is read as two separate
17// ones; longer and two unrelated clicks in the same place — which happens all
18// the time while reading — start selecting words nobody asked for.
19const doubleClickWithin = 400 * time.Millisecond
20
21// click is where and when a press landed, and how many have landed in a row.
22type click struct {
23 at buffer.Position
24 when time.Time
25 count int
26}
27
28// countClick records a press and returns how many presses in a row have now
29// landed on the same cell: 1 for a fresh click, 2 for a double click.
30//
31// The cell has to be the same, not merely near: a press one column over is a
32// different word, and selecting the first one because the pointer drifted is
33// worse than not selecting at all.
34func (v *View) countClick(at buffer.Position) int {
35 now := v.now()
36 if v.lastClick.count > 0 &&
37 v.lastClick.at == at &&
38 now.Sub(v.lastClick.when) <= doubleClickWithin {
39 v.lastClick.count++
40 } else {
41 v.lastClick.count = 1
42 }
43
44 v.lastClick.at = at
45 v.lastClick.when = now
46 return v.lastClick.count
47}
48
49// SetClock replaces the clock two clicks are timed against.
50//
51// Only a test has any business calling this. A double click measured against
52// the real clock is a test that passes or fails by how fast the machine is.
53//
54// view.SetClock(func() time.Time { return moment })
55func (v *View) SetClock(now func() time.Time) { v.now = now }