-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyleditor.go
More file actions
299 lines (272 loc) · 7.33 KB
/
styleditor.go
File metadata and controls
299 lines (272 loc) · 7.33 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"image"
"image/color"
"io"
"strings"
"gioui.org/f32"
"gioui.org/font"
"gioui.org/io/clipboard"
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/widget"
"gioui.org/widget/material"
)
// StyledLine is a line of text with per-line styling.
// Text is used for display, OriginalText (if set) is used for clipboard copy.
type StyledLine struct {
Text string
OriginalText string
Foreground color.NRGBA
Background color.NRGBA
Bold bool
}
// TextPosition is a position in multi-line text (line and rune column).
type TextPosition struct {
Line int
Col int
}
func (a TextPosition) lessOrEqual(b TextPosition) bool {
return a.Line < b.Line || (a.Line == b.Line && a.Col <= b.Col)
}
// StyledEditor is a read-only text editor with per-line styling and
// multi-line selection. It supports click/drag selection, Shift+click
// to extend, Ctrl+A to select all, and Ctrl+C to copy.
type StyledEditor struct {
Lines []StyledLine
dot TextPosition // caret (moving end of selection)
mark TextPosition // anchor (fixed end of selection)
dragging bool
}
// SetLines replaces the content and resets selection.
func (e *StyledEditor) SetLines(lines []StyledLine) {
e.Lines = lines
e.dot = TextPosition{}
e.mark = TextPosition{}
e.dragging = false
}
// HasSelection reports whether there is a non-empty selection.
func (e *StyledEditor) HasSelection() bool {
return e.dot != e.mark
}
func (e *StyledEditor) orderedSelection() (start, end TextPosition) {
if e.dot.lessOrEqual(e.mark) {
return e.dot, e.mark
}
return e.mark, e.dot
}
// SelectedText returns the currently selected text.
// When OriginalText is set on a line, selection columns are mapped back
// to original positions so that e.g. tabs are preserved in the copy.
func (e *StyledEditor) SelectedText() string {
if !e.HasSelection() {
return ""
}
start, end := e.orderedSelection()
var buf strings.Builder
for i := start.Line; i <= end.Line && i < len(e.Lines); i++ {
line := &e.Lines[i]
src := line.OriginalText
if src == "" {
src = line.Text
}
runes := []rune(src)
lo, hi := 0, len(runes)
if i == start.Line {
lo = displayColToOriginal(line, start.Col)
}
if i == end.Line {
hi = displayColToOriginal(line, end.Col)
}
if lo < 0 {
lo = 0
}
if hi > len(runes) {
hi = len(runes)
}
if lo <= hi {
buf.WriteString(string(runes[lo:hi]))
}
if i < end.Line {
buf.WriteByte('\n')
}
}
return buf.String()
}
// displayColToOriginal maps a display column (in the expanded Text) back to
// the corresponding rune index in OriginalText. If there is no OriginalText
// the column is returned as-is.
func displayColToOriginal(line *StyledLine, displayCol int) int {
if line.OriginalText == "" {
return displayCol
}
dc := 0
runeIdx := 0
for _, r := range line.OriginalText {
if dc >= displayCol {
return runeIdx
}
if r == '\t' {
dc += 4 - (dc % 4)
} else {
dc++
}
runeIdx++
}
return runeIdx
}
// SelectAll selects all text.
func (e *StyledEditor) SelectAll() {
e.mark = TextPosition{}
if len(e.Lines) > 0 {
last := len(e.Lines) - 1
e.dot = TextPosition{Line: last, Col: len([]rune(e.Lines[last].Text))}
} else {
e.dot = TextPosition{}
}
}
// CopyToClipboard copies the selected text to the system clipboard.
func (e *StyledEditor) CopyToClipboard(gtx layout.Context) {
text := e.SelectedText()
if text != "" {
gtx.Execute(clipboard.WriteCmd{
Type: "application/text",
Data: io.NopCloser(strings.NewReader(text)),
})
}
}
// HandlePointer processes a pointer event for text selection.
func (e *StyledEditor) HandlePointer(gtx layout.Context, ev pointer.Event, lineHeight, charWidth, leftInset int, listPos layout.Position) {
pos := e.posFromPointer(ev.Position, lineHeight, charWidth, leftInset, listPos)
switch ev.Kind {
case pointer.Press:
if !gtx.Focused(e) {
gtx.Execute(key.FocusCmd{Tag: e})
}
if ev.Modifiers.Contain(key.ModShift) {
e.dot = pos
} else {
e.dot = pos
e.mark = pos
}
e.dragging = true
gtx.Execute(op.InvalidateCmd{})
case pointer.Drag:
if e.dragging {
e.dot = pos
gtx.Execute(op.InvalidateCmd{})
}
case pointer.Release:
e.dragging = false
}
}
func (e *StyledEditor) clampPos(p TextPosition) TextPosition {
if len(e.Lines) == 0 {
return TextPosition{}
}
if p.Line < 0 {
p.Line = 0
}
if p.Line >= len(e.Lines) {
p.Line = len(e.Lines) - 1
}
maxCol := len([]rune(e.Lines[p.Line].Text))
if p.Col < 0 {
p.Col = 0
}
if p.Col > maxCol {
p.Col = maxCol
}
return p
}
func (e *StyledEditor) posFromPointer(p f32.Point, lineHeight, charWidth, leftInset int, listPos layout.Position) TextPosition {
// Visible Y → absolute Y → line index.
scrollY := listPos.First*lineHeight + listPos.Offset
line := (int(p.Y) + scrollY) / lineHeight
// X → column (accounting for text inset).
col := 0
if charWidth > 0 {
col = (int(p.X) - leftInset) / charWidth
}
return e.clampPos(TextPosition{Line: line, Col: col})
}
// LineSelection returns the selected column range [startCol, endCol) for the
// given line. Returns (-1, -1) if the line is not in the selection.
func (e *StyledEditor) LineSelection(lineIndex int) (startCol, endCol int) {
if !e.HasSelection() {
return -1, -1
}
start, end := e.orderedSelection()
if lineIndex < start.Line || lineIndex > end.Line {
return -1, -1
}
startCol = 0
if lineIndex >= 0 && lineIndex < len(e.Lines) {
endCol = len([]rune(e.Lines[lineIndex].Text))
}
if lineIndex == start.Line {
startCol = start.Col
}
if lineIndex == end.Line {
endCol = end.Col
}
return startCol, endCol
}
// LayoutLine renders a single styled line with selection highlighting.
func (e *StyledEditor) LayoutLine(th *Theme, gtx layout.Context, index, charWidth int, selColor color.NRGBA) layout.Dimensions {
line := &e.Lines[index]
defer clip.Rect{Max: gtx.Constraints.Max}.Push(gtx.Ops).Pop()
// Line background (diff coloring).
if line.Background != (color.NRGBA{}) {
paint.Fill(gtx.Ops, line.Background)
}
inset := layout.Inset{Left: 4, Right: 4}
return inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
// Selection highlight.
startCol, endCol := e.LineSelection(index)
if startCol >= 0 && endCol > startCol && charWidth > 0 {
x0 := startCol * charWidth
x1 := endCol * charWidth
if x1 > gtx.Constraints.Max.X {
x1 = gtx.Constraints.Max.X
}
selRect := image.Rectangle{
Min: image.Pt(x0, 0),
Max: image.Pt(x1, gtx.Constraints.Max.Y),
}
paint.FillShape(gtx.Ops, selColor, clip.Rect(selRect).Op())
}
// Text.
label := material.Body1(th.Theme, line.Text)
label.Color = line.Foreground
label.MaxLines = 1
label.Font = font.Font{Typeface: th.Face}
if line.Bold {
label.Font.Weight = font.Bold
}
gtx.Constraints.Max.X = maxLineWidth
return label.Layout(gtx)
})
}
// MeasureCharWidth measures the pixel width of a single monospace character.
func MeasureCharWidth(th *Theme, gtx layout.Context) int {
macro := op.Record(gtx.Ops)
gtx2 := gtx
gtx2.Constraints.Min = image.Point{}
gtx2.Constraints.Max = image.Pt(maxLineWidth, maxLineWidth)
dims := widget.Label{MaxLines: 1}.Layout(
gtx2, th.Shaper,
font.Font{Typeface: th.Face},
th.TextSize, "M",
op.CallOp{},
)
_ = macro.Stop()
if dims.Size.X > 0 {
return dims.Size.X
}
return gtx.Metric.Sp(th.TextSize) * 6 / 10
}