-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_feature.go
226 lines (203 loc) · 6.58 KB
/
report_feature.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2022 Stephan Lukits. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package lines
// LineSelecter is implemented by a component who wants to be informed
// when its focused line was selected.
type LineSelecter interface {
// OnLineSelection is called by Lines if the focused line having
// index i of implementing component was selected.
OnLineSelection(_ *Env, cIdx, sIdx int)
}
// LineFocuser is implemented by a component who wants to be informed when
// one of its lines receives the focus.
type LineFocuser interface {
// OnLineFocus is called by Lines if the line with given content
// line index cIdx and given screen line index sIdx receives the
// focus. Note is a component is associated with a source or there
// are more content lines than fitting on the screen cLine and sLine
// may differ.
OnLineFocus(_ *Env, cIdx, sIdx int)
}
// LineFocusLooser is implemented by a component who wants to be informed when
// a focused line looses its focus.
type LineFocusLooser interface {
// OnLineFocusLost is called by Lines if the line with given content
// line index cIdx and given screen line index sIdx of implementing
// component lost the focus. Note "on line focus lost" is reported
// after the focus change has happened, i.e. given screen line sIdx
// not necessary displays the content line cIdx. "on line focus
// lost" is reported before "on line focus" is report (if it is
// reported).
OnLineFocusLost(_ *Env, cIdx, sIdx int)
}
// LineOverflower implementations are called back when a line receives
// the focus whose content either overflows to the left or to the right
// or at both sides.
type LineOverflower interface {
// LineOverflows is called by Lines if a components line receives
// the focus whose content overflows to the left or to the right or
// at both sides.
OnLineOverflowing(_ *Env, left, right bool)
}
// Cursorer is implemented by a component which wants to be notified
// about cursor movement.
type Cursorer interface {
// OnCursor implemented by a component c is called by Lines if the
// cursor position has changed; use c.CursorPosition() to retrieve
// the current cursor position. Note if the display resizes Lines
// either removes the cursor iff it is not in the content area of a
// component; otherwise it keeps the cursor in c's content area
// trying to keep it relative to the content areas origin at the
// same position. If the later can be achieved absOnly is true.
OnCursor(_ *Env, absOnly bool)
}
// execute given feature f on given user-component usr.
func execute(cntx *rprContext, usr Componenter, f FeatureMask) {
switch f {
case UpScrollable:
usr.embedded().Scroll.Up()
case DownScrollable:
usr.embedded().Scroll.Down()
case NextLineFocusable:
executeLineFocus(cntx, usr, usr.embedded().LL.Focus.Next)
case PreviousLineFocusable:
executeLineFocus(cntx, usr, usr.embedded().LL.Focus.Previous)
case FirstCellFocusable:
executeCellFocus(cntx, usr, usr.embedded().LL.Focus.FirstCell)
case PreviousCellFocusable:
executeCellFocus(cntx, usr, usr.embedded().LL.Focus.PreviousCell)
case NextCellFocusable:
executeCellFocus(cntx, usr, usr.embedded().LL.Focus.NextCell)
case LastCellFocusable:
executeCellFocus(cntx, usr, usr.embedded().LL.Focus.LastCell)
case LineUnfocusable:
executeResetLineFocus(cntx, usr)
case LineSelectable:
reportSelectedLine(cntx, usr)
case editable:
editorInsert(cntx, usr)
}
}
func executeLineFocus(
cntx *rprContext, usr Componenter, f func() (int, int),
) {
clIdx, slIdx := usr.embedded().LL.Focus.Current(),
usr.embedded().LL.Focus.Screen()
_, column, _ := usr.embedded().CursorPosition()
ln, cl := f()
if clIdx == ln {
if cl != column {
reportCursorChange(cntx, usr)
}
return
}
reportLineFocus(cntx, usr, clIdx, slIdx)
if cl == column && column == -1 {
return
}
reportCursorChange(cntx, usr)
}
func executeResetLineFocus(cntx *rprContext, usr Componenter) {
cIdx, sIdx := usr.embedded().LL.Focus.Current(),
usr.embedded().LL.Focus.Screen()
_, _, haveCursor := usr.embedded().CursorPosition()
usr.embedded().LL.Focus.Reset()
if haveCursor {
reportCursorChange(cntx, usr)
}
reportLineFocus(cntx, usr, cIdx, sIdx)
if usr.embedded().Edit != nil {
usr.embedded().Edit.Suspend()
}
}
func executeCellFocus(
cntx *rprContext, usr Componenter, f func() (int, int, bool),
) {
_, _, movedCursor := f()
if movedCursor && reportCursorChange(cntx, usr) {
usr.enable()
}
reportLineOverflow(cntx, usr, usr.embedded().LL.Focus.Screen())
}
func reportCursorChange(cntx *rprContext, usr Componenter) bool {
c, ok := usr.(Cursorer)
if !ok {
return false
}
callback(usr, cntx, func(c Cursorer) func(e *Env) {
return func(e *Env) { c.OnCursor(e, false) }
}(c))
return true
}
func lfCurry(cb func(*Env, int, int), cLine, sLine int) func(*Env) {
return func(e *Env) { cb(e, cLine, sLine) }
}
func ofCurry(of LineOverflower, left, right bool) func(*Env) {
return func(e *Env) { of.OnLineOverflowing(e, left, right) }
}
func reportLineFocus(cntx *rprContext, usr Componenter, cIdx, sIdx int) {
fl, ok := usr.(LineFocusLooser)
if cIdx >= 0 && ok {
callback(usr, cntx, lfCurry(fl.OnLineFocusLost, cIdx, sIdx))
usr.enable()
}
cmp := usr.embedded()
sIdx = cmp.LL.Focus.Screen()
cIdx = cmp.LL.Focus.Current()
if cIdx < 0 {
return
}
lf, ok := usr.(LineFocuser)
if ok {
callback(usr, cntx, lfCurry(lf.OnLineFocus, cIdx, sIdx))
usr.enable()
}
reportLineOverflow(cntx, usr, sIdx)
}
func reportLineOverflow(cntx *rprContext, usr Componenter, sIdx int) {
if sIdx < 0 {
return
}
of, ok := usr.(LineOverflower)
if !ok {
return
}
cmp := usr.embedded()
_, _, width, _ := cmp.ContentArea()
l, r, changed := cmp.LL.By(sIdx).isOverflowing(width)
if !l && !r || !changed {
return
}
callback(usr, cntx, ofCurry(of, l, r))
}
func lsCurry(ls LineSelecter, cIdx, sIdx int) func(*Env) {
return func(e *Env) { ls.OnLineSelection(e, cIdx, sIdx) }
}
func reportSelectedLine(cntx *rprContext, usr Componenter) {
cIdx, sIdx := usr.embedded().LL.Focus.Current(),
usr.embedded().LL.Focus.Screen()
if cIdx < 0 {
return
}
ls, ok := usr.(LineSelecter)
if !ok {
return
}
callback(usr, cntx, lsCurry(ls, cIdx, sIdx))
}
func editorInsert(cntx *rprContext, usr Componenter) {
_, _, hasCursor := usr.layoutComponent().wrapped().cursorPosition()
if !hasCursor {
executeLineFocus(cntx, usr, func() (int, int) {
return 0, 0
})
usr.embedded().Edit.Resume()
return
}
if usr.embedded().Edit.IsReplacing() {
usr.embedded().LL.Focus.EolAtLastRune()
} else {
usr.embedded().LL.Focus.EolAfterLastRune()
}
}