-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout.go
185 lines (166 loc) · 5.13 KB
/
layout.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
// Copyright (c) 2023, The GoKi 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 texteditor
import (
"image"
"goki.dev/gi/v2/gi"
"goki.dev/girl/paint"
"goki.dev/mat32/v2"
)
// StyleSizes gets the size info based on Style settings.
func (ed *Editor) StyleSizes() {
sty := &ed.Styles
spc := sty.BoxSpace()
sty.Font = paint.OpenFont(sty.FontRender(), &sty.UnContext)
ed.FontHeight = sty.Font.Face.Metrics.Height
ed.LineHeight = sty.Text.EffLineHeight(ed.FontHeight)
ed.LineNoDigs = max(1+int(mat32.Log10(float32(ed.NLines))), 3)
lno := true
if ed.Buf != nil {
lno = ed.Buf.Opts.LineNos
}
if lno {
ed.SetFlag(true, EditorHasLineNos)
// SidesTODO: this is sketchy
ed.LineNoOff = float32(ed.LineNoDigs+3)*sty.Font.Face.Metrics.Ch + spc.Left // space for icon
} else {
ed.SetFlag(false, EditorHasLineNos)
ed.LineNoOff = 0
}
}
// UpdateFromAlloc updates size info based on allocated size:
// NLinesChars, LineNoOff, LineLayoutSize
func (ed *Editor) UpdateFromAlloc() {
sty := &ed.Styles
spc := sty.BoxSpace()
asz := ed.LayState.Alloc.SizeOrig
nv := mat32.Vec2{}
if asz == nv {
ed.NLinesChars.Y = 40
ed.NLinesChars.X = 80
} else {
ed.NLinesChars.Y = int(mat32.Floor(float32(asz.Y) / ed.LineHeight))
ed.NLinesChars.X = int(mat32.Floor(float32(asz.X) / sty.Font.Face.Metrics.Ch))
}
ed.LineLayoutSize = asz.Sub(ed.ExtraSize).Sub(spc.Size())
ed.LineLayoutSize.X -= ed.LineNoOff
// SidesTODO: this is sketchy
// ed.LineLayoutSize.X -= spc.Size().X / 2 // extra space for word wrap
}
func (ed *Editor) GetSize(sc *gi.Scene, iter int) {
ed.InitLayout(sc)
ed.LayoutAllLines()
ed.LayState.Size.Need = ed.TotalSize
ed.LayState.Size.Pref = ed.LayState.Size.Need
// fmt.Println("GetSize: need:", ed.LayState.Size.Need)
}
func (ed *Editor) DoLayout(sc *gi.Scene, parBBox image.Rectangle, iter int) bool {
ed.SetFlag(false, gi.LayoutNeedsRedo)
ed.DoLayoutBase(sc, parBBox, iter)
spc := ed.BoxSpace()
ed.ChildSize = ed.LayState.Size.Need.Sub(spc.Size()) // we are what we need
ed.ManageOverflow(sc)
redo := ed.DoLayoutChildren(sc, iter)
if redo {
ed.SetFlag(true, gi.LayoutNeedsRedo)
}
if !redo || iter == 1 {
delta := ed.LayoutScrollDelta((image.Point{}))
if delta != (image.Point{}) {
ed.LayoutScrollChildren(sc, delta) // move is a separate step
}
}
ed.UpdateFromAlloc()
return ed.Is(gi.LayoutNeedsRedo)
}
// LayoutAllLines generates TextRenders of lines
// from the Markup version of the source in Buf.
// It computes the total LinesSize and TotalSize.
func (ed *Editor) LayoutAllLines() {
if ed.LineLayoutSize == mat32.Vec2Zero || ed.Styles.Font.Size.Val == 0 {
return
}
if ed.Buf == nil || ed.Buf.NumLines() == 0 {
ed.NLines = 0
return
}
ed.lastFilename = ed.Buf.Filename
ed.Buf.Hi.TabSize = ed.Styles.Text.TabSize
ed.HiStyle()
// fmt.Printf("layout all: %v\n", ed.Nm)
ed.NLines = ed.Buf.NumLines()
nln := ed.NLines
if cap(ed.Renders) >= nln {
ed.Renders = ed.Renders[:nln]
} else {
ed.Renders = make([]paint.Text, nln)
}
if cap(ed.Offs) >= nln {
ed.Offs = ed.Offs[:nln]
} else {
ed.Offs = make([]float32, nln)
}
sz := ed.LineLayoutSize
// fmt.Println("LineLayoutSize:", sz)
sty := &ed.Styles
fst := sty.FontRender()
fst.BackgroundColor.SetSolid(nil)
off := float32(0)
mxwd := sz.X // always start with our render size
ed.Buf.MarkupMu.RLock()
ed.HasLinks = false
for ln := 0; ln < nln; ln++ {
ed.Renders[ln].SetHTMLPre(ed.Buf.Markup[ln], fst, &sty.Text, &sty.UnContext, ed.CSS)
ed.Renders[ln].LayoutStdLR(&sty.Text, sty.FontRender(), &sty.UnContext, sz)
if !ed.HasLinks && len(ed.Renders[ln].Links) > 0 {
ed.HasLinks = true
}
ed.Offs[ln] = off
lsz := mat32.Max(ed.Renders[ln].Size.Y, ed.LineHeight)
off += lsz
mxwd = mat32.Max(mxwd, ed.Renders[ln].Size.X)
}
ed.Buf.MarkupMu.RUnlock()
ed.LinesSize = mat32.Vec2{mxwd, off}
spc := sty.BoxSpace()
ed.TotalSize = ed.LinesSize.Add(spc.Size())
ed.TotalSize.X += ed.LineNoOff
// extraHalf := ed.LineHeight * 0.5 * float32(ed.NLinesChars.Y)
// todo: add extra half to bottom of size?
}
// LayoutLine generates render of given line (including highlighting).
// If the line with exceeds the current maximum, or the number of effective
// lines (e.g., from word-wrap) is different, then SetNeedsLayout is called
// and it returns true.
func (ed *Editor) LayoutLine(ln int) bool {
if ed.Buf == nil || ed.Buf.NumLines() == 0 {
return false
}
sty := &ed.Styles
fst := sty.FontRender()
fst.BackgroundColor.SetSolid(nil)
mxwd := float32(ed.LinesSize.X)
needLay := false
ed.Buf.MarkupMu.RLock()
curspans := len(ed.Renders[ln].Spans)
ed.Renders[ln].SetHTMLPre(ed.Buf.Markup[ln], fst, &sty.Text, &sty.UnContext, ed.CSS)
ed.Renders[ln].LayoutStdLR(&sty.Text, sty.FontRender(), &sty.UnContext, ed.LineLayoutSize)
if !ed.HasLinks && len(ed.Renders[ln].Links) > 0 {
ed.HasLinks = true
}
nwspans := len(ed.Renders[ln].Spans)
if nwspans != curspans && (nwspans > 1 || curspans > 1) {
needLay = true
}
if ed.Renders[ln].Size.X > mxwd {
needLay = true
}
ed.Buf.MarkupMu.RUnlock()
if needLay {
ed.SetNeedsLayout()
} else {
ed.SetNeedsRender()
}
return needLay
}