-
Notifications
You must be signed in to change notification settings - Fork 885
/
numberlabel.go
160 lines (120 loc) · 2.9 KB
/
numberlabel.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
// Copyright 2018 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"strings"
)
type NumberLabel struct {
static
decimals int
decimalsChangedPublisher EventPublisher
suffix string
suffixChangedPublisher EventPublisher
value float64
valueChangedPublisher EventPublisher
}
func NewNumberLabel(parent Container) (*NumberLabel, error) {
nl := new(NumberLabel)
if err := nl.init(nl, parent, 0); err != nil {
return nil, err
}
nl.SetTextAlignment(AlignFar)
if _, err := nl.updateText(); err != nil {
return nil, err
}
nl.MustRegisterProperty("Decimals", NewProperty(
func() interface{} {
return nl.Decimals()
},
func(v interface{}) error {
return nl.SetDecimals(assertIntOr(v, 0))
},
nl.decimalsChangedPublisher.Event()))
nl.MustRegisterProperty("Suffix", NewProperty(
func() interface{} {
return nl.Suffix()
},
func(v interface{}) error {
return nl.SetSuffix(assertStringOr(v, ""))
},
nl.suffixChangedPublisher.Event()))
nl.MustRegisterProperty("Value", NewProperty(
func() interface{} {
return nl.Value()
},
func(v interface{}) error {
return nl.SetValue(assertFloat64Or(v, 0.0))
},
nl.valueChangedPublisher.Event()))
return nl, nil
}
func (nl *NumberLabel) asStatic() *static {
return &nl.static
}
func (nl *NumberLabel) TextAlignment() Alignment1D {
return nl.textAlignment1D()
}
func (nl *NumberLabel) SetTextAlignment(alignment Alignment1D) error {
if alignment == AlignDefault {
alignment = AlignFar
}
return nl.setTextAlignment1D(alignment)
}
func (nl *NumberLabel) Decimals() int {
return nl.decimals
}
func (nl *NumberLabel) SetDecimals(decimals int) error {
if decimals == nl.decimals {
return nil
}
old := nl.decimals
nl.decimals = decimals
if _, err := nl.updateText(); err != nil {
nl.decimals = old
return err
}
nl.decimalsChangedPublisher.Publish()
return nil
}
func (nl *NumberLabel) Suffix() string {
return nl.suffix
}
func (nl *NumberLabel) SetSuffix(suffix string) error {
if suffix == nl.suffix {
return nil
}
old := nl.suffix
nl.suffix = suffix
if _, err := nl.updateText(); err != nil {
nl.suffix = old
return err
}
nl.suffixChangedPublisher.Publish()
return nil
}
func (nl *NumberLabel) Value() float64 {
return nl.value
}
func (nl *NumberLabel) SetValue(value float64) error {
if value == nl.value {
return nil
}
old := nl.value
nl.value = value
if _, err := nl.updateText(); err != nil {
nl.value = old
return err
}
nl.valueChangedPublisher.Publish()
return nil
}
func (nl *NumberLabel) updateText() (changed bool, err error) {
var sb strings.Builder
sb.WriteString(FormatFloatGrouped(nl.value, nl.decimals))
if nl.suffix != "" {
sb.WriteString(nl.suffix)
}
return nl.setText(sb.String())
}