-
Notifications
You must be signed in to change notification settings - Fork 2
/
window.go
178 lines (168 loc) · 4.35 KB
/
window.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package mado
import (
"image"
"time"
"unicode"
"unicode/utf16"
"github.com/kanryu/mado/f32"
"github.com/kanryu/mado/io/event"
"github.com/kanryu/mado/io/input"
"github.com/kanryu/mado/io/key"
"github.com/kanryu/mado/io/system"
"github.com/kanryu/mado/op"
"github.com/kanryu/mado/unit"
)
// Option configures a window.
type Option func(unit.Metric, *Config)
type EditorState struct {
input.EditorState
Compose key.Range
}
func (e *EditorState) Replace(r key.Range, text string) {
if r.Start > r.End {
r.Start, r.End = r.End, r.Start
}
runes := []rune(text)
newEnd := r.Start + len(runes)
adjust := func(pos int) int {
switch {
case newEnd < pos && pos <= r.End:
return newEnd
case r.End < pos:
diff := newEnd - r.End
return pos + diff
}
return pos
}
e.Selection.Start = adjust(e.Selection.Start)
e.Selection.End = adjust(e.Selection.End)
if e.Compose.Start != -1 {
e.Compose.Start = adjust(e.Compose.Start)
e.Compose.End = adjust(e.Compose.End)
}
s := e.Snippet
if r.End < s.Start || r.Start > s.End {
// Discard snippet if it doesn't overlap with replacement.
s = key.Snippet{
Range: key.Range{
Start: r.Start,
End: r.Start,
},
}
}
var newSnippet []rune
snippet := []rune(s.Text)
// Append first part of existing snippet.
if end := r.Start - s.Start; end > 0 {
newSnippet = append(newSnippet, snippet[:end]...)
}
// Append replacement.
newSnippet = append(newSnippet, runes...)
// Append last part of existing snippet.
if start := r.End; start < s.End {
newSnippet = append(newSnippet, snippet[start-s.Start:]...)
}
// Adjust snippet range to include replacement.
if r.Start < s.Start {
s.Start = r.Start
}
s.End = s.Start + len(newSnippet)
s.Text = string(newSnippet)
e.Snippet = s
}
// UTF16Index converts the given index in runes into an index in utf16 characters.
func (e *EditorState) UTF16Index(runes int) int {
if runes == -1 {
return -1
}
if runes < e.Snippet.Start {
// Assume runes before sippet are one UTF-16 character each.
return runes
}
chars := e.Snippet.Start
runes -= e.Snippet.Start
for _, r := range e.Snippet.Text {
if runes == 0 {
break
}
runes--
chars++
if r1, _ := utf16.EncodeRune(r); r1 != unicode.ReplacementChar {
chars++
}
}
// Assume runes after snippets are one UTF-16 character each.
return chars + runes
}
// RunesIndex converts the given index in utf16 characters to an index in runes.
func (e *EditorState) RunesIndex(chars int) int {
if chars == -1 {
return -1
}
if chars < e.Snippet.Start {
// Assume runes before offset are one UTF-16 character each.
return chars
}
runes := e.Snippet.Start
chars -= e.Snippet.Start
for _, r := range e.Snippet.Text {
if chars == 0 {
break
}
chars--
runes++
if r1, _ := utf16.EncodeRune(r); r1 != unicode.ReplacementChar {
chars--
}
}
// Assume runes after snippets are one UTF-16 character each.
return runes + chars
}
type Callbacks interface {
GetWindow() Window
SetWindow(w Window)
SetDriver(d Driver)
Event(e event.Event) bool
SemanticRoot() input.SemanticID
LookupSemantic(semID input.SemanticID) (input.SemanticNode, bool)
AppendSemanticDiffs(diffs []input.SemanticID) []input.SemanticID
SemanticAt(pos f32.Point) (input.SemanticID, bool)
EditorState() EditorState
SetComposingRegion(r key.Range)
EditorInsert(text string, preedit bool)
EditorReplace(r key.Range, text string, preedit bool)
SetEditorSelection(r key.Range)
SetEditorSnippet(r key.Range)
ClickFocus()
ActionAt(p f32.Point) (system.Action, bool)
}
type Window interface {
Update(frame *op.Ops)
ValidateAndProcess(d Driver, size image.Point, sync bool, frame *op.Ops, sigChan chan<- struct{}) error
Frame(frame *op.Ops, viewport image.Point) error
ProcessFrame(d Driver)
Invalidate()
Option(opts ...Option)
Run(f func())
DriverDefer(f func(d Driver))
UpdateAnimation(d Driver)
Wakeup()
SetNextFrame(at time.Time)
WaitAck(d Driver)
DestroyGPU()
UpdateSemantics()
CollectSemanticDiffs(diffs *[]input.SemanticID, n input.SemanticNode)
UpdateState(d Driver)
ProcessEvent(d Driver, e event.Event) bool
NextEvent() event.Event
UpdateCursor(d Driver)
FallbackDecorate() bool
Decorate(d Driver, e FrameEvent, o *op.Ops) (size, offset image.Point)
EffectiveConfig() Config
Perform(actions system.Action)
}
func DecoHeightOpt(h unit.Dp) Option {
return func(m unit.Metric, c *Config) {
c.DecoHeight = h
}
}