Detect double and triple clicks

This commit is contained in:
Zachary Yedidia
2016-03-26 16:32:19 -04:00
parent bc4fdcff43
commit 53290919c8
3 changed files with 71 additions and 5 deletions

View File

@@ -90,6 +90,25 @@ func (c *Cursor) GetSelection() string {
return string([]rune(c.v.buf.text)[c.selectionStart : c.selectionEnd+1])
}
// SelectLine selects the current line
func (c *Cursor) SelectLine() {
c.Start()
c.selectionStart = c.Loc()
c.End()
c.selectionEnd = c.Loc()
}
// AddLineToSelection adds the current line to the selection
func (c *Cursor) AddLineToSelection() {
if c.loc < c.selectionStart {
c.Start()
c.selectionStart = c.Loc()
} else if c.loc > c.selectionEnd {
c.End()
c.selectionEnd = c.Loc()
}
}
// RuneUnder returns the rune under the cursor
func (c *Cursor) RuneUnder() rune {
line := c.v.buf.lines[c.y]

View File

@@ -10,8 +10,9 @@ import (
)
const (
synLinesUp = 75 // How many lines up to look to do syntax highlighting
synLinesDown = 75 // How many lines down to look to do syntax highlighting
synLinesUp = 75 // How many lines up to look to do syntax highlighting
synLinesDown = 75 // How many lines down to look to do syntax highlighting
doubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click
)
// The main screen

View File

@@ -6,6 +6,7 @@ import (
"io/ioutil"
"strconv"
"strings"
"time"
)
// The View struct stores information about a view into a buffer.
@@ -44,6 +45,17 @@ type View struct {
// mouse release events
mouseReleased bool
// This stores when the last click was
// This is useful for detecting double and triple clicks
lastClickTime time.Time
// Was the last mouse event actually a double click?
// Useful for detecting triple clicks -- if a double click is detected
// but the last mouse event was actually a double click, it's a triple click
doubleClick bool
// Same here, just to keep track for mouse move events
tripleClick bool
// Syntax highlighting matches
matches SyntaxMatches
// The matches from the last frame
@@ -90,6 +102,7 @@ func NewViewWidthHeight(buf *Buffer, w, h int) *View {
// Set mouseReleased to true because we assume the mouse is not being pressed when
// the editor is opened
v.mouseReleased = true
v.lastClickTime = time.Time{}
return v
}
@@ -432,14 +445,47 @@ func (v *View) HandleEvent(event tcell.Event) {
switch button {
case tcell.Button1:
// Left click
origX, origY := v.cursor.x, v.cursor.y
v.MoveToMouseClick(x, y)
loc := v.cursor.Loc()
if v.mouseReleased {
v.cursor.selectionStart = loc
if (time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold) &&
(origX == v.cursor.x && origY == v.cursor.y) {
if v.doubleClick {
// Triple click
v.lastClickTime = time.Now()
v.tripleClick = true
v.doubleClick = false
messenger.Error("Triple click")
v.cursor.SelectLine()
} else {
// Double click
v.doubleClick = true
v.tripleClick = false
v.lastClickTime = time.Now()
messenger.Error("Double click")
}
} else {
messenger.Error("Single click")
v.doubleClick = false
v.tripleClick = false
v.lastClickTime = time.Now()
loc := v.cursor.Loc()
v.cursor.selectionStart = loc
v.cursor.selectionEnd = loc
}
} else {
if v.tripleClick {
v.cursor.AddLineToSelection()
} else if v.doubleClick {
} else {
v.cursor.selectionEnd = v.cursor.Loc()
}
}
v.cursor.selectionEnd = loc
v.mouseReleased = false
case tcell.ButtonNone:
// Mouse event with no click
if !v.mouseReleased {