forked from nelsam/gxui
/
label.go
136 lines (114 loc) · 2.79 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
// 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 (
"strings"
"github.com/nelsam/gxui"
"github.com/nelsam/gxui/math"
"github.com/nelsam/gxui/mixins/base"
)
type LabelOuter interface {
base.ControlOuter
}
type Label struct {
base.Control
outer LabelOuter
font gxui.Font
color gxui.Color
horizontalAlignment gxui.HorizontalAlignment
verticalAlignment gxui.VerticalAlignment
multiline bool
text string
}
func (l *Label) Init(outer LabelOuter, theme gxui.Theme, font gxui.Font, color gxui.Color) {
if font == nil {
panic("Cannot create a label with a nil font")
}
l.Control.Init(outer, theme)
l.outer = outer
l.font = font
l.color = color
l.horizontalAlignment = gxui.AlignLeft
l.verticalAlignment = gxui.AlignMiddle
// Interface compliance test
_ = gxui.Label(l)
}
func (l *Label) Text() string {
return l.text
}
func (l *Label) SetText(text string) {
if l.text != text {
l.text = text
l.outer.Relayout()
}
}
func (l *Label) Font() gxui.Font {
return l.font
}
func (l *Label) SetFont(font gxui.Font) {
if l.font != font {
l.font = font
l.Relayout()
}
}
func (l *Label) Color() gxui.Color {
return l.color
}
func (l *Label) SetColor(color gxui.Color) {
if l.color != color {
l.color = color
l.outer.Redraw()
}
}
func (l *Label) Multiline() bool {
return l.multiline
}
func (l *Label) SetMultiline(multiline bool) {
if l.multiline != multiline {
l.multiline = multiline
l.outer.Relayout()
}
}
func (l *Label) DesiredSize(min, max math.Size) math.Size {
t := l.text
if !l.multiline {
t = strings.Replace(t, "\n", " ", -1)
}
s := l.font.Measure(&gxui.TextBlock{Runes: []rune(t)})
return s.Clamp(min, max)
}
func (l *Label) SetHorizontalAlignment(horizontalAlignment gxui.HorizontalAlignment) {
if l.horizontalAlignment != horizontalAlignment {
l.horizontalAlignment = horizontalAlignment
l.Redraw()
}
}
func (l *Label) HorizontalAlignment() gxui.HorizontalAlignment {
return l.horizontalAlignment
}
func (l *Label) SetVerticalAlignment(verticalAlignment gxui.VerticalAlignment) {
if l.verticalAlignment != verticalAlignment {
l.verticalAlignment = verticalAlignment
l.Redraw()
}
}
func (l *Label) VerticalAlignment() gxui.VerticalAlignment {
return l.verticalAlignment
}
// parts.DrawPaint overrides
func (l *Label) Paint(c gxui.Canvas) {
r := l.outer.Size().Rect()
t := l.text
if !l.multiline {
t = strings.Replace(t, "\n", " ", -1)
}
runes := []rune(t)
offsets := l.font.Layout(&gxui.TextBlock{
Runes: runes,
AlignRect: r,
H: l.horizontalAlignment,
V: l.verticalAlignment,
})
c.DrawRunes(l.font, runes, offsets, l.color)
}