~whereswaldon/gio-x

ref: b0198a1b547035c76d814b60de9aec3fce6353a8 gio-x/component/truncating_label.go -rw-r--r-- 1.1 KiB
b0198a1bChris Waldon component: document and refactor tooltip types 5 months ago
                                                                                
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
package component

import (
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/widget/material"
)

// TruncatingLabelStyle is a type that forces a label to
// fit on one line and adds a truncation indicator symbol
// to the end of the line if the text has been truncated.
type TruncatingLabelStyle material.LabelStyle

// Layout renders the label into the provided context.
func (t TruncatingLabelStyle) Layout(gtx layout.Context) layout.Dimensions {
	originalMaxX := gtx.Constraints.Max.X
	gtx.Constraints.Max.X *= 2
	asLabel := material.LabelStyle(t)
	asLabel.MaxLines = 1
	macro := op.Record(gtx.Ops)
	dimensions := asLabel.Layout(gtx)
	labelOp := macro.Stop()
	if dimensions.Size.X <= originalMaxX {
		// No need to truncate
		labelOp.Add(gtx.Ops)
		return dimensions
	}
	gtx.Constraints.Max.X = originalMaxX
	truncationIndicator := asLabel
	truncationIndicator.Text = "…"
	return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
		layout.Flexed(1, func(gtx C) D {
			return asLabel.Layout(gtx)
		}),
		layout.Rigid(func(gtx C) D {
			return truncationIndicator.Layout(gtx)
		}),
	)
}