From a4d0c3e7026fab3f1433f4496536f3d995333084 Mon Sep 17 00:00:00 2001 From: Inkeliz Date: Sun, 6 Dec 2020 03:35:09 +0000 Subject: [PATCH] widget: add support for copy/paste in the Editor The Editor will handle CTRL+C and CTRL+V. The CTRL+V will paste the content on the Editor, and CTRL+C will copy all the Editor content. Signed-off-by: Inkeliz --- widget/editor.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/widget/editor.go b/widget/editor.go index 30c6fb6d..c1620033 100644 --- a/widget/editor.go +++ b/widget/editor.go @@ -16,6 +16,7 @@ import ( "gioui.org/f32" "gioui.org/gesture" + "gioui.org/io/clipboard" "gioui.org/io/key" "gioui.org/io/pointer" "gioui.org/layout" @@ -250,7 +251,7 @@ func (e *Editor) processKey(gtx layout.Context) { continue } } - if e.command(ke) { + if e.command(gtx, ke) { e.caret.scroll = true e.scroller.Stop() } @@ -258,6 +259,10 @@ func (e *Editor) processKey(gtx layout.Context) { e.caret.scroll = true e.scroller.Stop() e.append(ke.Text) + case clipboard.Event: + e.caret.scroll = true + e.scroller.Stop() + e.append(ke.Text) } if e.rr.Changed() { e.events = append(e.events, ChangeEvent{}) @@ -269,7 +274,7 @@ func (e *Editor) moveLines(distance int) { e.moveToLine(e.caret.x+e.caret.xoff, e.caret.line+distance) } -func (e *Editor) command(k key.Event) bool { +func (e *Editor) command(gtx layout.Context, k key.Event) bool { modSkip := key.ModCtrl if runtime.GOOS == "darwin" { modSkip = key.ModAlt @@ -313,6 +318,16 @@ func (e *Editor) command(k key.Event) bool { e.moveStart() case key.NameEnd: e.moveEnd() + case "V": + if k.Modifiers != key.ModShortcut { + return false + } + clipboard.ReadOp{Tag: &e.eventKey}.Add(gtx.Ops) + case "C": + if k.Modifiers != key.ModShortcut { + return false + } + clipboard.WriteOp{Text: e.Text()}.Add(gtx.Ops) default: return false }