forked from google/gxui
/
code_editor_line.go
150 lines (132 loc) · 4.11 KB
/
code_editor_line.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mixins
import (
"github.com/google/gxui"
"github.com/google/gxui/interval"
"github.com/google/gxui/math"
)
type CodeEditorLinePaintInfo struct {
LineSpan interval.IntData
Runes []rune
GlyphOffsets []math.Point
GlyphWidth int
LineHeight int
Font gxui.Font
}
type CodeEditorLineOuter interface {
DefaultTextBoxLineOuter
PaintBackgroundSpans(c gxui.Canvas, info CodeEditorLinePaintInfo)
PaintGlyphs(c gxui.Canvas, info CodeEditorLinePaintInfo)
PaintBorders(c gxui.Canvas, info CodeEditorLinePaintInfo)
}
// CodeEditorLine
type CodeEditorLine struct {
DefaultTextBoxLine
outer CodeEditorLineOuter
ce *CodeEditor
}
func (l *CodeEditorLine) Init(outer CodeEditorLineOuter, theme gxui.Theme, ce *CodeEditor, lineIndex int) {
l.DefaultTextBoxLine.Init(outer, theme, &ce.TextBox, lineIndex)
l.outer = outer
l.ce = ce
// Interface compliance test
_ = TextBoxLine(l)
}
func (t *CodeEditorLine) PaintBackgroundSpans(c gxui.Canvas, info CodeEditorLinePaintInfo) {
start, _ := info.LineSpan.Span()
offsets := info.GlyphOffsets
remaining := interval.IntDataList{info.LineSpan}
for _, l := range t.ce.layers {
if l != nil && l.BackgroundColor() != nil {
color := *l.BackgroundColor()
for _, span := range l.Spans().Overlaps(info.LineSpan) {
interval.Visit(&remaining, span, func(vs, ve uint64, _ int) {
s, e := vs-start, ve-start
r := math.CreateRect(offsets[s].X, 0, offsets[e-1].X+info.GlyphWidth, info.LineHeight)
c.DrawRoundedRect(r, 3, 3, 3, 3, gxui.TransparentPen, gxui.Brush{Color: color})
})
interval.Remove(&remaining, span)
}
}
}
}
func (t *CodeEditorLine) PaintGlyphs(c gxui.Canvas, info CodeEditorLinePaintInfo) {
start, _ := info.LineSpan.Span()
runes, offsets, font := info.Runes, info.GlyphOffsets, info.Font
remaining := interval.IntDataList{info.LineSpan}
for _, l := range t.ce.layers {
if l != nil && l.Color() != nil {
color := *l.Color()
for _, span := range l.Spans().Overlaps(info.LineSpan) {
interval.Visit(&remaining, span, func(vs, ve uint64, _ int) {
s, e := vs-start, ve-start
c.DrawRunes(font, runes[s:e], offsets[s:e], color)
})
interval.Remove(&remaining, span)
}
}
}
for _, span := range remaining {
s, e := span.Span()
s, e = s-start, e-start
c.DrawRunes(font, runes[s:e], offsets[s:e], t.ce.textColor)
}
}
func (t *CodeEditorLine) PaintBorders(c gxui.Canvas, info CodeEditorLinePaintInfo) {
start, _ := info.LineSpan.Span()
offsets := info.GlyphOffsets
for _, l := range t.ce.layers {
if l != nil && l.BorderColor() != nil {
color := *l.BorderColor()
interval.Visit(l.Spans(), info.LineSpan, func(vs, ve uint64, _ int) {
s, e := vs-start, ve-start
r := math.CreateRect(offsets[s].X, 0, offsets[e-1].X+info.GlyphWidth, info.LineHeight)
c.DrawRoundedRect(r, 3, 3, 3, 3, gxui.CreatePen(0.5, color), gxui.TransparentBrush)
})
}
}
}
// DefaultTextBoxLine overrides
func (t *CodeEditorLine) Paint(c gxui.Canvas) {
font := t.ce.font
rect := t.Size().Rect().OffsetX(t.caretWidth)
controller := t.ce.controller
runes := controller.LineRunes(t.lineIndex)
start := controller.LineStart(t.lineIndex)
end := controller.LineEnd(t.lineIndex)
if start != end {
lineSpan := interval.CreateIntData(start, end, nil)
lineHeight := t.Size().H
glyphWidth := font.GlyphMaxSize().W
offsets := font.Layout(&gxui.TextBlock{
Runes: runes,
AlignRect: rect,
H: gxui.AlignLeft,
V: gxui.AlignMiddle,
})
info := CodeEditorLinePaintInfo{
LineSpan: lineSpan,
Runes: runes, // TODO gxui.TextBlock?
GlyphOffsets: offsets,
GlyphWidth: glyphWidth,
LineHeight: lineHeight,
Font: font,
}
// Background
t.outer.PaintBackgroundSpans(c, info)
// Selections
if t.textbox.HasFocus() {
t.outer.PaintSelections(c)
}
// Glyphs
t.outer.PaintGlyphs(c, info)
// Borders
t.outer.PaintBorders(c, info)
}
// Carets
if t.textbox.HasFocus() {
t.outer.PaintCarets(c)
}
}