-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtext.go
77 lines (61 loc) · 1.4 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"io"
"runtime"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
)
// Declare conformity with editor interface
var _ editor = (*textEditor)(nil)
type codeEntry struct {
widget.Entry
editor *textEditor
}
func (e *codeEntry) TypedShortcut(shortcut fyne.Shortcut) {
if sh, ok := shortcut.(*desktop.CustomShortcut); ok {
ctrlSuper := (runtime.GOOS == "darwin" && sh.Modifier == fyne.KeyModifierSuper) ||
(runtime.GOOS != "darwin" && sh.Modifier == fyne.KeyModifierControl)
if sh.KeyName == "S" && ctrlSuper {
e.editor.save()
}
}
}
type textEditor struct {
uri fyne.URI
entry *codeEntry
edited bool
}
func newTextEditor(u fyne.URI, _ fyne.Window) editor {
text := &codeEntry{}
text.MultiLine = true
text.TextStyle.Monospace = true
text.ExtendBaseWidget(text)
editor := &textEditor{uri: u, entry: text}
text.editor = editor
f, _ := storage.Reader(u)
b, _ := io.ReadAll(f)
text.SetText(string(b))
_ = f.Close()
text.OnChanged = func(_ string) {
editor.edited = true
}
return editor
}
func (t *textEditor) changed() bool {
return t.edited
}
func (t *textEditor) content() fyne.CanvasObject {
return t.entry
}
func (t *textEditor) close() {
}
func (t *textEditor) run() {
}
func (t *textEditor) save() {
w, _ := storage.Writer(t.uri)
_, _ = w.Write([]byte(t.entry.Text))
_ = w.Close()
t.edited = false
}