forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.go
222 lines (177 loc) · 4.92 KB
/
widget.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Package widget defines the UI widgets within the Fyne toolkit.
package widget // import "fyne.io/fyne/v2/widget"
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal/cache"
internalWidget "fyne.io/fyne/v2/internal/widget"
"fyne.io/fyne/v2/theme"
)
// BaseWidget provides a helper that handles basic widget behaviours.
type BaseWidget struct {
noCopy noCopy // so `go vet` can complain if a widget is passed by value (copied)
size fyne.Size
position fyne.Position
Hidden bool
impl fyne.Widget
themeCache fyne.Theme
}
// ExtendBaseWidget is used by an extending widget to make use of BaseWidget functionality.
func (w *BaseWidget) ExtendBaseWidget(wid fyne.Widget) {
if w.super() != nil {
return
}
w.impl = wid
}
// Size gets the current size of this widget.
func (w *BaseWidget) Size() fyne.Size {
return w.size
}
// Resize sets a new size for a widget.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (w *BaseWidget) Resize(size fyne.Size) {
if size == w.Size() {
return
}
w.size = size
impl := w.super()
if impl == nil {
return
}
cache.Renderer(impl).Layout(size)
}
// Position gets the current position of this widget, relative to its parent.
func (w *BaseWidget) Position() fyne.Position {
return w.position
}
// Move the widget to a new position, relative to its parent.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (w *BaseWidget) Move(pos fyne.Position) {
w.position = pos
internalWidget.Repaint(w.super())
}
// MinSize for the widget - it should never be resized below this value.
func (w *BaseWidget) MinSize() fyne.Size {
impl := w.super()
r := cache.Renderer(impl)
if r == nil {
return fyne.Size{}
}
return r.MinSize()
}
// Visible returns whether or not this widget should be visible.
// Note that this may not mean it is currently visible if a parent has been hidden.
func (w *BaseWidget) Visible() bool {
return !w.Hidden
}
// Show this widget so it becomes visible
func (w *BaseWidget) Show() {
if w.Visible() {
return
}
w.Hidden = false
impl := w.super()
if impl == nil {
return
}
impl.Refresh()
}
// Hide this widget so it is no longer visible
func (w *BaseWidget) Hide() {
if !w.Visible() {
return
}
w.Hidden = true
impl := w.super()
if impl == nil {
return
}
canvas.Refresh(impl)
}
// Refresh causes this widget to be redrawn in its current state
func (w *BaseWidget) Refresh() {
impl := w.super()
if impl == nil {
return
}
w.themeCache = nil
cache.Renderer(impl).Refresh()
}
// Theme returns a cached Theme instance for this widget (or its extending widget).
// This will be the app theme in most cases, or a widget specific theme if it is inside a ThemeOverride container.
//
// Since: 2.5
func (w *BaseWidget) Theme() fyne.Theme {
cached := w.themeCache
if cached != nil {
return cached
}
cached = cache.WidgetTheme(w.super())
// don't cache the default as it may change
if cached == nil {
return theme.Current()
}
w.themeCache = cached
return cached
}
// super will return the actual object that this represents.
// If extended then this is the extending widget, otherwise it is nil.
func (w *BaseWidget) super() fyne.Widget {
return w.impl
}
// DisableableWidget describes an extension to BaseWidget which can be disabled.
// Disabled widgets should have a visually distinct style when disabled, normally using theme.DisabledButtonColor.
type DisableableWidget struct {
BaseWidget
disabled bool
}
// Enable this widget, updating any style or features appropriately.
func (w *DisableableWidget) Enable() {
if !w.Disabled() {
return // Enabled already
}
w.disabled = false
impl := w.super()
if impl == nil {
return
}
impl.Refresh()
}
// Disable this widget so that it cannot be interacted with, updating any style appropriately.
func (w *DisableableWidget) Disable() {
if w.Disabled() {
return // Disabled already
}
w.disabled = true
impl := w.super()
if impl == nil {
return
}
impl.Refresh()
}
// Disabled returns true if this widget is currently disabled or false if it can currently be interacted with.
func (w *DisableableWidget) Disabled() bool {
return w.disabled
}
// NewSimpleRenderer creates a new SimpleRenderer to render a widget using a
// single fyne.CanvasObject.
//
// Since: 2.1
func NewSimpleRenderer(object fyne.CanvasObject) fyne.WidgetRenderer {
return internalWidget.NewSimpleRenderer(object)
}
// Orientation controls the horizontal/vertical layout of a widget
type Orientation int
// Orientation constants to control widget layout
const (
Horizontal Orientation = 0
Vertical Orientation = 1
// Adaptive will switch between horizontal and vertical layouts according to device orientation.
// This orientation is not always supported and interpretation can vary per-widget.
//
// Since: 2.5
Adaptive Orientation = 2
)
type noCopy struct{}
func (*noCopy) Lock() {}
func (*noCopy) Unlock() {}