From f8f9e391be831356fa504d209c2148bf0c333359 Mon Sep 17 00:00:00 2001 From: Kyle Nusbaum Date: Thu, 9 Apr 2020 16:35:30 -0500 Subject: [PATCH] duit: add mouse chording cut/paste for Edit and Field --- edit.go | 44 +++++++++++++++++++++++++++++++++++++++++--- field.go | 53 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 85 insertions(+), 12 deletions(-) diff --git a/edit.go b/edit.go index 019d022..1b4e6b5 100644 --- a/edit.go +++ b/edit.go @@ -114,6 +114,7 @@ type Edit struct { textM, prevTextB1 draw.Mouse + selecting bool lastCursorPoint image.Point } @@ -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 { @@ -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 diff --git a/field.go b/field.go index e4ebf6b..06e4655 100644 --- a/field.go +++ b/field.go @@ -2,6 +2,7 @@ package duit import ( "image" + "log" "strings" "unicode/utf8" @@ -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 } @@ -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 }