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 <inkeliz@inkeliz.com>
This commit is contained in:
Inkeliz
2020-12-06 03:35:09 +00:00
committed by Elias Naur
parent a76f816ae9
commit a4d0c3e702
+17 -2
View File
@@ -16,6 +16,7 @@ import (
"gioui.org/f32" "gioui.org/f32"
"gioui.org/gesture" "gioui.org/gesture"
"gioui.org/io/clipboard"
"gioui.org/io/key" "gioui.org/io/key"
"gioui.org/io/pointer" "gioui.org/io/pointer"
"gioui.org/layout" "gioui.org/layout"
@@ -250,7 +251,7 @@ func (e *Editor) processKey(gtx layout.Context) {
continue continue
} }
} }
if e.command(ke) { if e.command(gtx, ke) {
e.caret.scroll = true e.caret.scroll = true
e.scroller.Stop() e.scroller.Stop()
} }
@@ -258,6 +259,10 @@ func (e *Editor) processKey(gtx layout.Context) {
e.caret.scroll = true e.caret.scroll = true
e.scroller.Stop() e.scroller.Stop()
e.append(ke.Text) e.append(ke.Text)
case clipboard.Event:
e.caret.scroll = true
e.scroller.Stop()
e.append(ke.Text)
} }
if e.rr.Changed() { if e.rr.Changed() {
e.events = append(e.events, ChangeEvent{}) 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) 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 modSkip := key.ModCtrl
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
modSkip = key.ModAlt modSkip = key.ModAlt
@@ -313,6 +318,16 @@ func (e *Editor) command(k key.Event) bool {
e.moveStart() e.moveStart()
case key.NameEnd: case key.NameEnd:
e.moveEnd() 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: default:
return false return false
} }