-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
numeric_field.go
138 lines (123 loc) · 4.14 KB
/
numeric_field.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
// Copyright (c) 2021-2024 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"fmt"
"strings"
"unicode"
"github.com/richardwilkes/toolbox/i18n"
"github.com/richardwilkes/toolbox/xmath"
)
// NumericField holds a numeric value that can be edited.
type NumericField[T xmath.Numeric] struct {
*Field
Format func(T) string
Extract func(s string) (T, error)
Prototypes func(minimum, maximum T) []T
minimum T
maximum T
}
// NewNumericField creates a new field that holds a numeric value and limits its input to a specific range of values.
// The format and extract functions allow the field to be presented as something other than numbers.
func NewNumericField[T xmath.Numeric](current, minimum, maximum T, format func(T) string, extract func(s string) (T, error), prototypes func(minimum, maximum T) []T) *NumericField[T] {
f := &NumericField[T]{
Field: NewField(),
Prototypes: prototypes,
Format: format,
Extract: extract,
minimum: minimum,
maximum: maximum,
}
f.Self = f
f.LostFocusCallback = f.DefaultFocusLost
f.RuneTypedCallback = f.DefaultRuneTyped
f.ValidateCallback = f.DefaultValidate
f.SetText(f.Format(current))
f.adjustMinimumTextWidth()
return f
}
// Value returns the current value of the field.
func (f *NumericField[T]) Value() T {
v, _ := f.Extract(strings.TrimSpace(f.Text())) //nolint:errcheck // Default value in case of error is acceptable
return min(max(v, f.minimum), f.maximum)
}
// SetValue sets the current value of the field.
func (f *NumericField[T]) SetValue(value T) {
text := f.Format(value)
if text != f.Text() {
f.SetText(text)
}
}
// Min returns the minimum value allowed.
func (f *NumericField[T]) Min() T {
return f.minimum
}
// Max returns the maximum value allowed.
func (f *NumericField[T]) Max() T {
return f.maximum
}
// DefaultFocusLost is the default implementation for the LostFocusCallback.
func (f *NumericField[T]) DefaultFocusLost() {
f.SetText(f.Format(f.Value()))
f.Field.DefaultFocusLost()
}
// DefaultRuneTyped is the default implementation for the RuneTypedCallback.
func (f *NumericField[T]) DefaultRuneTyped(ch rune) bool {
if !unicode.IsControl(ch) {
if _, err := f.Extract(strings.TrimSpace(string(f.RunesIfPasted([]rune{ch})))); err != nil {
Beep()
return false
}
}
return f.Field.DefaultRuneTyped(ch)
}
// DefaultValidate is the default implementation for the ValidateCallback.
func (f *NumericField[T]) DefaultValidate() bool {
if text := f.tooltipTextForValidation(); text != "" {
f.Tooltip = NewTooltipWithText(text)
return false
}
f.Tooltip = nil
return true
}
func (f *NumericField[T]) tooltipTextForValidation() string {
s := strings.TrimSpace(f.Text())
v, err := f.Extract(s)
if err != nil || s == "-" || s == "+" {
return i18n.Text("Invalid value")
}
if minimum := f.minimum; v < minimum {
return fmt.Sprintf(i18n.Text("Value must be at least %s"), f.Format(minimum))
}
if maximum := f.maximum; v > maximum {
return fmt.Sprintf(i18n.Text("Value must be no more than %s"), f.Format(maximum))
}
return ""
}
// SetMinMax sets the minimum and maximum values and then adjusts the minimum text width, if a prototype function has
// been set.
func (f *NumericField[T]) SetMinMax(minimum, maximum T) {
if f.minimum != minimum || f.maximum != maximum {
f.minimum = minimum
f.maximum = maximum
f.adjustMinimumTextWidth()
v, _ := f.Extract(strings.TrimSpace(f.Text())) //nolint:errcheck // Default value in case of error is acceptable
f.SetValue(min(max(v, f.minimum), f.maximum))
}
}
func (f *NumericField[T]) adjustMinimumTextWidth() {
if f.Prototypes != nil {
prototypes := f.Prototypes(f.minimum, f.maximum)
candidates := make([]string, 0, len(prototypes))
for _, v := range prototypes {
candidates = append(candidates, f.Format(v))
}
f.SetMinimumTextWidthUsing(candidates...)
}
}