/
label.go
395 lines (366 loc) · 11.7 KB
/
label.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright (c) 2018, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package core
import (
"fmt"
"cogentcore.org/core/abilities"
"cogentcore.org/core/cursors"
"cogentcore.org/core/events"
"cogentcore.org/core/keymap"
"cogentcore.org/core/math32"
"cogentcore.org/core/mimedata"
"cogentcore.org/core/paint"
"cogentcore.org/core/states"
"cogentcore.org/core/styles"
"cogentcore.org/core/system"
)
// Label is a widget for rendering text labels. It supports full HTML styling,
// including links. By default, labels wrap and collapse whitespace, although
// you can change this by changing [styles.Text.WhiteSpace].
type Label struct { //core:embedder
WidgetBase
// label to display
Text string
// the type of label
Type LabelTypes
// render data for text label
TextRender paint.Text `copier:"-" xml:"-" json:"-" set:"-"`
// NormalCursor is the cached cursor to display when there
// is no link being hovered.
NormalCursor cursors.Cursor `copier:"-" xml:"-" json:"-" set:"-"`
}
// LabelTypes is an enum containing the different
// possible types of labels
type LabelTypes int32 //enums:enum -trim-prefix Label
const (
// LabelDisplayLarge is a large, short, and important
// display label with a default font size of 57dp.
LabelDisplayLarge LabelTypes = iota
// LabelDisplayMedium is a medium-sized, short, and important
// display label with a default font size of 45dp.
LabelDisplayMedium
// LabelDisplaySmall is a small, short, and important
// display label with a default font size of 36dp.
LabelDisplaySmall
// LabelHeadlineLarge is a large, high-emphasis
// headline label with a default font size of 32dp.
LabelHeadlineLarge
// LabelHeadlineMedium is a medium-sized, high-emphasis
// headline label with a default font size of 28dp.
LabelHeadlineMedium
// LabelHeadlineSmall is a small, high-emphasis
// headline label with a default font size of 24dp.
LabelHeadlineSmall
// LabelTitleLarge is a large, medium-emphasis
// title label with a default font size of 22dp.
LabelTitleLarge
// LabelTitleMedium is a medium-sized, medium-emphasis
// title label with a default font size of 16dp.
LabelTitleMedium
// LabelTitleSmall is a small, medium-emphasis
// title label with a default font size of 14dp.
LabelTitleSmall
// LabelBodyLarge is a large body label used for longer
// passages of text with a default font size of 16dp.
LabelBodyLarge
// LabelBodyMedium is a medium-sized body label used for longer
// passages of text with a default font size of 14dp.
LabelBodyMedium
// LabelBodySmall is a small body label used for longer
// passages of text with a default font size of 12dp.
LabelBodySmall
// LabelLabelLarge is a large label used for label text (like a caption
// or the text inside a button) with a default font size of 14dp.
LabelLabelLarge
// LabelLabelMedium is a medium-sized label used for label text (like a caption
// or the text inside a button) with a default font size of 12dp.
LabelLabelMedium
// LabelLabelSmall is a small label used for label text (like a caption
// or the text inside a button) with a default font size of 11dp.
LabelLabelSmall
)
func (lb *Label) OnInit() {
lb.WidgetBase.OnInit()
lb.HandleEvents()
lb.SetStyles()
}
func (lb *Label) SetStyles() {
lb.Type = LabelBodyLarge
lb.Style(func(s *styles.Style) {
s.SetAbilities(true, abilities.Selectable, abilities.DoubleClickable)
if len(lb.TextRender.Links) > 0 {
s.SetAbilities(true, abilities.Clickable, abilities.LongHoverable, abilities.LongPressable)
}
if !lb.IsReadOnly() {
s.Cursor = cursors.Text
}
s.GrowWrap = true
// Label styles based on https://m3.material.io/styles/typography/type-scale-tokens
switch lb.Type {
case LabelLabelLarge:
s.Text.LineHeight.Dp(20)
s.Font.Size.Dp(14)
s.Text.LetterSpacing.Dp(0.1)
s.Font.Weight = styles.WeightMedium // note: excludes all fonts except Go!
case LabelLabelMedium:
s.Text.LineHeight.Dp(16)
s.Font.Size.Dp(12)
s.Text.LetterSpacing.Dp(0.5)
s.Font.Weight = styles.WeightMedium
case LabelLabelSmall:
s.Text.LineHeight.Dp(16)
s.Font.Size.Dp(11)
s.Text.LetterSpacing.Dp(0.5)
s.Font.Weight = styles.WeightMedium
case LabelBodyLarge:
s.Text.LineHeight.Dp(24)
s.Font.Size.Dp(16)
s.Text.LetterSpacing.Dp(0.5)
s.Font.Weight = styles.WeightNormal
case LabelBodyMedium:
s.Text.LineHeight.Dp(20)
s.Font.Size.Dp(14)
s.Text.LetterSpacing.Dp(0.25)
s.Font.Weight = styles.WeightNormal
case LabelBodySmall:
s.Text.LineHeight.Dp(16)
s.Font.Size.Dp(12)
s.Text.LetterSpacing.Dp(0.4)
s.Font.Weight = styles.WeightNormal
case LabelTitleLarge:
s.Text.LineHeight.Dp(28)
s.Font.Size.Dp(22)
s.Text.LetterSpacing.Zero()
s.Font.Weight = styles.WeightNormal
case LabelTitleMedium:
s.Text.LineHeight.Dp(24)
s.Font.Size.Dp(16)
s.Text.LetterSpacing.Dp(0.15)
s.Font.Weight = styles.WeightMedium
case LabelTitleSmall:
s.Text.LineHeight.Dp(20)
s.Font.Size.Dp(14)
s.Text.LetterSpacing.Dp(0.1)
s.Font.Weight = styles.WeightMedium
case LabelHeadlineLarge:
s.Text.LineHeight.Dp(40)
s.Font.Size.Dp(32)
s.Text.LetterSpacing.Zero()
s.Font.Weight = styles.WeightNormal
case LabelHeadlineMedium:
s.Text.LineHeight.Dp(36)
s.Font.Size.Dp(28)
s.Text.LetterSpacing.Zero()
s.Font.Weight = styles.WeightNormal
case LabelHeadlineSmall:
s.Text.LineHeight.Dp(32)
s.Font.Size.Dp(24)
s.Text.LetterSpacing.Zero()
s.Font.Weight = styles.WeightNormal
case LabelDisplayLarge:
s.Text.LineHeight.Dp(64)
s.Font.Size.Dp(57)
s.Text.LetterSpacing.Dp(-0.25)
s.Font.Weight = styles.WeightNormal
case LabelDisplayMedium:
s.Text.LineHeight.Dp(52)
s.Font.Size.Dp(45)
s.Text.LetterSpacing.Zero()
s.Font.Weight = styles.WeightNormal
case LabelDisplaySmall:
s.Text.LineHeight.Dp(44)
s.Font.Size.Dp(36)
s.Text.LetterSpacing.Zero()
s.Font.Weight = styles.WeightNormal
}
})
lb.StyleFinal(func(s *styles.Style) {
lb.NormalCursor = s.Cursor
})
}
func (lb *Label) HandleEvents() {
lb.HandleLabelClick(func(tl *paint.TextLink) {
system.TheApp.OpenURL(tl.URL)
})
lb.OnDoubleClick(func(e events.Event) {
lb.SetSelected(true)
lb.SetFocus()
})
lb.OnFocusLost(func(e events.Event) {
lb.SetSelected(false)
})
lb.OnKeyChord(func(e events.Event) {
// TODO(kai): get label copying working
if !lb.StateIs(states.Selected) {
return
}
kf := keymap.Of(e.KeyChord())
if kf == keymap.Copy {
e.SetHandled()
lb.Copy(true)
}
})
lb.On(events.MouseMove, func(e events.Event) {
pos := lb.Geom.Pos.Content
inLink := false
for _, tl := range lb.TextRender.Links {
// TODO(kai/link): is there a better way to be safe here?
if tl.Label == "" {
continue
}
tlb := tl.Bounds(&lb.TextRender, pos)
if e.Pos().In(tlb) {
inLink = true
if lb.StateIs(states.LongHovered) || lb.StateIs(states.LongPressed) {
NewTooltipTextAt(lb, tl.URL, tlb.Min, tlb.Size()).Run()
e.SetHandled()
}
break
}
}
if inLink {
lb.Styles.Cursor = cursors.Pointer
} else {
lb.Styles.Cursor = lb.NormalCursor
}
})
}
// HandleLabelClick handles click events such that the given function will be called
// on any links that are clicked on.
func (lb *Label) HandleLabelClick(openLink func(tl *paint.TextLink)) {
lb.OnClick(func(e events.Event) {
pos := lb.Geom.Pos.Content
for _, tl := range lb.TextRender.Links {
// TODO(kai/link): is there a better way to be safe here?
if tl.Label == "" {
continue
}
tlb := tl.Bounds(&lb.TextRender, pos)
if e.Pos().In(tlb) {
openLink(&tl)
e.SetHandled()
return
}
}
})
}
func (lb *Label) Copy(reset bool) {
md := mimedata.NewText(lb.Text)
em := lb.EventMgr()
if em != nil {
em.Clipboard().Write(md)
}
}
func (lb *Label) Label() string {
if lb.Text != "" {
return lb.Text
}
return lb.Nm
}
// todo: ideally it would be possible to only call SetHTML once during config
// and then do the layout only during sizing. However, layout starts with
// existing line breaks (which could come from <br> and <p> in HTML),
// so that is never able to undo initial word wrapping from constrained sizes.
// ConfigLabel does the HTML and Layout in TextRender for label text,
// using actual content size to constrain layout.
func (lb *Label) Config() {
lb.ConfigLabelSize(lb.Geom.Size.Actual.Content)
}
// ConfigLabel does the HTML and Layout in TextRender for label text,
// using given size to constrain layout.
func (lb *Label) ConfigLabelSize(sz math32.Vector2) {
// todo: last arg is CSSAgg. Can synthesize that some other way?
fs := lb.Styles.FontRender()
txs := &lb.Styles.Text
lb.TextRender.SetHTML(lb.Text, fs, txs, &lb.Styles.UnitContext, nil)
lb.TextRender.Layout(txs, fs, &lb.Styles.UnitContext, sz)
}
// ConfigLabelAlloc is used for determining how much space the label
// takes, using given size (typically Alloc).
// In this case, alignment factors are turned off,
// because they otherwise can absorb much more space, which should
// instead be controlled by the base Align X,Y factors.
func (lb *Label) ConfigLabelAlloc(sz math32.Vector2) math32.Vector2 {
// todo: last arg is CSSAgg. Can synthesize that some other way?
fs := lb.Styles.FontRender()
txs := &lb.Styles.Text
align, alignV := txs.Align, txs.AlignV
txs.Align, txs.AlignV = styles.Start, styles.Start
lb.TextRender.SetHTML(lb.Text, fs, txs, &lb.Styles.UnitContext, nil)
lb.TextRender.Layout(txs, fs, &lb.Styles.UnitContext, sz)
rsz := lb.TextRender.Size.Ceil()
txs.Align, txs.AlignV = align, alignV
lb.TextRender.Layout(txs, fs, &lb.Styles.UnitContext, rsz)
return rsz
}
// TextWrapSizeEstimate is the size to use for layout during the SizeUp pass,
// for word wrap case, where the sizing actually matters,
// based on trying to fit the given number of characters into the given content size
// with given font height.
func TextWrapSizeEstimate(csz math32.Vector2, nChars int, fs *styles.Font) math32.Vector2 {
chars := float32(nChars)
fht := float32(16)
if fs.Face != nil {
fht = fs.Face.Metrics.Height
}
area := chars * fht * fht
ratio := float32(1.618) // default to golden
if csz.X > 0 && csz.Y > 0 {
ratio = csz.X / csz.Y
// fmt.Println(lb, "content size ratio:", ratio)
}
// w = ratio * h
// w^2 + h^2 = a
// (ratio*h)^2 + h^2 = a
h := math32.Sqrt(area) / math32.Sqrt(ratio+1)
w := ratio * h
if w < csz.X { // must be at least this
w = csz.X
h = area / w
h = max(h, csz.Y)
}
sz := math32.Vec2(w, h)
if DebugSettings.LayoutTrace {
fmt.Println("TextWrapSizeEstimate chars:", chars, "area:", area, "sz:", sz)
}
return sz
}
func (lb *Label) SizeUp() {
lb.WidgetBase.SizeUp() // sets Actual size based on styles
sz := &lb.Geom.Size
if lb.Styles.Text.HasWordWrap() {
lb.ConfigLabelSize(TextWrapSizeEstimate(lb.Geom.Size.Actual.Content, len(lb.Text), &lb.Styles.Font))
} else {
lb.ConfigLabelSize(sz.Actual.Content)
}
rsz := lb.TextRender.Size.Ceil()
sz.FitSizeMax(&sz.Actual.Content, rsz)
sz.SetTotalFromContent(&sz.Actual)
if DebugSettings.LayoutTrace {
fmt.Println(lb, "Label SizeUp:", rsz, "Actual:", sz.Actual.Content)
}
}
func (lb *Label) SizeDown(iter int) bool {
if !lb.Styles.Text.HasWordWrap() || iter > 1 {
return false
}
sz := &lb.Geom.Size
rsz := lb.ConfigLabelAlloc(sz.Alloc.Content) // use allocation
prevContent := sz.Actual.Content
// start over so we don't reflect hysteresis of prior guess
sz.SetInitContentMin(lb.Styles.Min.Dots().Ceil())
sz.FitSizeMax(&sz.Actual.Content, rsz)
sz.SetTotalFromContent(&sz.Actual)
chg := prevContent != sz.Actual.Content
if chg {
if DebugSettings.LayoutTrace {
fmt.Println(lb, "Label Size Changed:", sz.Actual.Content, "was:", prevContent)
}
}
return chg
}
func (lb *Label) Render() {
lb.RenderStandardBox()
lb.TextRender.Render(&lb.Scene.PaintContext, lb.Geom.Pos.Content)
}