| 🛟 Updated. 28d5985 k33g 6h ago | 1 | // Counting mouse clicks, so that a second one on the same spot means |
| 2 | // something different from the first. |
| 3 | |
| 4 | package editor |
| 5 | |
| 6 | import ( |
| 7 | "time" |
| 8 | |
| 9 | "codeberg.org/turbo-editors/turbo-core/buffer" |
| 10 | ) |
| 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. |
| 19 | const doubleClickWithin = 400 * time.Millisecond |
| 20 | |
| 21 | // click is where and when a press landed, and how many have landed in a row. |
| 22 | type 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. |
| 34 | func (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 }) |
| 55 | func (v *View) SetClock(now func() time.Time) { v.now = now } |