-
Notifications
You must be signed in to change notification settings - Fork 27
/
label.go
128 lines (108 loc) · 2.78 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
package nanogui
import (
"github.com/shibukawa/nanovgo"
)
// Text label widget
// The font and color can be customized. When SetFixedWidth()
// is used, the text is wrapped when it surpasses the specified width
//
type Label struct {
WidgetImplement
caption string
fontFace string
color nanovgo.Color
columnWidth int
wrap bool
}
func NewLabel(parent Widget, caption string) *Label {
label := &Label{
caption: caption,
color: parent.Theme().TextColor,
wrap: true,
}
InitWidget(label, parent)
return label
}
// Caption() gets the label's text caption
func (l *Label) Caption() string {
return l.caption
}
// SetCaption() sets the label's text caption
func (l *Label) SetCaption(caption string) {
l.caption = caption
}
// Font() gets the currently active font
func (l *Label) Font() string {
if l.fontFace == "" {
return l.theme.FontBold
}
return l.fontFace
}
// SetFont() sets the currently active font (2 are available by default: 'sans' and 'sans-bold')
func (l *Label) SetFont(fontFace string) {
l.fontFace = fontFace
}
// Color() gets the label color
func (l *Label) Color() nanovgo.Color {
return l.color
}
// SetColor() sets the label color
func (l *Label) SetColor(color nanovgo.Color) {
l.color = color
}
func (l *Label) ColumnWidth() int {
return l.columnWidth
}
func (l *Label) SetColumnWidth(width int) {
l.columnWidth = width
}
func (l *Label) Wrap() bool {
return l.wrap
}
func (l *Label) SetWrap(wrap bool) {
l.wrap = wrap
}
func (l *Label) PreferredSize(self Widget, ctx *nanovgo.Context) (int, int) {
if l.caption == "" {
return 0, 0
}
ctx.SetFontSize(float32(l.FontSize()))
ctx.SetFontFace(l.Font())
width := 0
if l.FixedWidth() > 0 {
width = l.FixedWidth()
} else if l.columnWidth > 0 && l.wrap {
width = l.columnWidth
}
if width > 0 {
ctx.SetTextAlign(nanovgo.AlignLeft | nanovgo.AlignTop)
bounds := ctx.TextBoxBounds(0, 0, float32(width), l.caption)
return width, int(bounds[3] - bounds[1])
} else {
ctx.SetTextAlign(nanovgo.AlignLeft | nanovgo.AlignTop)
w, _ := ctx.TextBounds(0, 0, l.caption)
return int(w), l.Theme().StandardFontSize
}
}
func (l *Label) Draw(self Widget, ctx *nanovgo.Context) {
l.WidgetImplement.Draw(self, ctx)
ctx.SetFontSize(float32(l.FontSize()))
ctx.SetFontFace(l.Font())
ctx.SetFillColor(l.color)
width := 0
if l.FixedWidth() > 0 {
width = l.FixedWidth()
} else if l.columnWidth > 0 && l.wrap {
width = l.columnWidth
}
if width > 0 {
ctx.SetTextAlign(nanovgo.AlignLeft | nanovgo.AlignTop)
ctx.TextBox(float32(l.x), float32(l.y), float32(width), l.caption)
} else {
ctx.SetTextAlign(nanovgo.AlignLeft | nanovgo.AlignMiddle)
ctx.Text(float32(l.x), float32(l.y)+float32(l.h)*0.5, l.caption)
}
}
func (l *Label) String() string {
return l.StringHelper("Label", l.caption)
}