Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duit: add mouse chording cut/paste for Edit and Field #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type Edit struct {

textM,
prevTextB1 draw.Mouse
selecting bool

lastCursorPoint image.Point
}
Expand Down Expand Up @@ -877,7 +878,10 @@ func (ui *Edit) Mouse(dui *DUI, self *Kid, m draw.Mouse, origM draw.Mouse, orig
ui.command = ""
ui.visual = ""
}
if m.Buttons == Button1 {
if om.Buttons&Button1 == 0 && m.Buttons&Button1 == 1 {
ui.selecting=true
}
if m.Buttons == Button1 && ui.selecting {
ui.cursor.Cur = mouseOffset()
ui.ScrollCursor(dui)
if om.Buttons == 0 {
Expand All @@ -899,10 +903,44 @@ func (ui *Edit) Mouse(dui *DUI, self *Kid, m draw.Mouse, origM draw.Mouse, orig
if m.Buttons^om.Buttons != 0 {
ui.text.closeHist(ui)
// log.Printf("in text, mouse buttons changed %v -> %v\n", om, m)
if (om.Buttons&Button1 == 1 || m.Buttons&Button1 == 1) && om.Buttons&Button2 == 0 && m.Buttons&Button2 == Button2 {
sel, err := ui.Selection()
if err != nil {
log.Printf("duit: Selection: %s", err)
return
}
err = dui.Display.WriteSnarf([]byte(sel))
if err != nil {
log.Printf("duit: WriteSnarf: %s", err)
return
}
ui.Replace(ui.cursor, []byte(""))
s, e := ui.cursor.Start, ui.cursor.Cur
if s>e {
s,e = e,s
}
ui.cursor.Start=s
ui.cursor.Cur=s
ui.selecting = false
self.Draw = Dirty
ui.ScrollCursor(dui)
}
if (om.Buttons&Button1 == 1 || m.Buttons&Button1 == 1) && om.Buttons&Button3 == 0 && m.Buttons&Button3 == Button3 {
b, ok := dui.ReadSnarf()
if !ok {
// Just return, ReadSnarf doesn't return an error.
return
}
ui.Replace(ui.cursor, b)
ui.cursor.Cur = ui.cursor.Cur + int64(len(b))
self.Draw = Dirty
ui.ScrollCursor(dui)
ui.selecting = false
}
} else if m.Buttons != 0 && m.Buttons == om.Buttons {
// log.Printf("in text, mouse drag %v\n", m)
//log.Printf("in text, mouse drag %v\n", m)
} else if om.Buttons != 0 && m.Buttons == 0 {
// log.Printf("in text, button release %v -> %v\n", om, m)
//log.Printf("in text, button release %v -> %v\n", om, m)
}
r.Consumed = true
return
Expand Down
53 changes: 44 additions & 9 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package duit

import (
"image"
"log"
"strings"
"unicode/utf8"

Expand Down Expand Up @@ -301,6 +302,7 @@ func expandSelection(t string, i int) (s, e int) {
}

func (ui *Field) Mouse(dui *DUI, self *Kid, m draw.Mouse, origM draw.Mouse, orig image.Point) (r Result) {
defer func() { ui.m = m }()
if ui.Disabled {
return
}
Expand Down Expand Up @@ -330,21 +332,54 @@ func (ui *Field) Mouse(dui *DUI, self *Kid, m draw.Mouse, origM draw.Mouse, orig
ui.SelectionStart1 = ui.Cursor1
r.Consumed = true
self.Draw = Dirty
if m.Msec-ui.prevB1Release.Msec < 400 {
s, e := expandSelection(ui.Text, ui.cursor0())
ui.Cursor1 = 1 + s
ui.SelectionStart1 = 1 + e
}
} else if ui.m.Buttons&1 == 1 || m.Buttons&1 == 1 {
// continue selection
ui.Cursor1 = 1 + locateCursor()
r.Consumed = true
self.Draw = Dirty
if ui.m.Buttons&1 == 1 && m.Buttons&1 == 0 {
if m.Msec-ui.prevB1Release.Msec < 400 {
s, e := expandSelection(ui.Text, ui.cursor0())
ui.Cursor1 = 1 + s
ui.SelectionStart1 = 1 + e
if ui.m.Buttons&4 == 0 && m.Buttons&4 == 4 {
// b1 + b3 chord (Paste)
b, ok := dui.ReadSnarf()
if !ok {
// ReadSnarf does not return an error.
return
}
s, e, _ := ui.selection0()
if s != e {
ui.Text = ui.Text[:s] + string(b) + ui.Text[e:]
ui.Cursor1 = s + len(b) + 1
} else {
c := ui.cursor0()
ui.Text = ui.Text[:c] + string(b) + ui.Text[c:]
ui.Cursor1 = c + len(b) + 1
}
ui.SelectionStart1 = 0
} else if ui.m.Buttons&2 == 0 && m.Buttons&2 == 2 {
// b1 + b2 chord (Cut)
_, _, sel := ui.selection0()
err := dui.Display.WriteSnarf([]byte(sel))
if err != nil {
log.Printf("duit: writesnarf: %s\n", err)
return
}
ui.removeSelection()
} else if ui.SelectionStart1 > 0 {
// continue selection
ui.Cursor1 = 1 + locateCursor()
if ui.m.Buttons&1 == 1 && m.Buttons&1 == 0 {
if m.Msec-ui.prevB1Release.Msec < 400 {
s, e := expandSelection(ui.Text, ui.cursor0())
ui.Cursor1 = 1 + s
ui.SelectionStart1 = 1 + e
}
ui.prevB1Release = m
}
ui.prevB1Release = m
}
}
ui.m = m

return
}

Expand Down