/
toolbar.go
352 lines (317 loc) · 9.31 KB
/
toolbar.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package core
import (
"cogentcore.org/core/colors"
"cogentcore.org/core/icons"
"cogentcore.org/core/math32"
"cogentcore.org/core/states"
"cogentcore.org/core/styles"
"cogentcore.org/core/tree"
"cogentcore.org/core/units"
)
// Toolbar is a [Frame] that is useful for holding [Button]s that do things.
// It automatically moves items that do not fit into an overflow menu, and
// manages additional items that are always placed onto this overflow menu.
// In general it should be possible to use a single toolbar + overflow to
// manage all an app's functionality, in a way that is portable across
// mobile and desktop environments.
// See [Widget.ConfigToolbar] for the standard toolbar config method for
// any given widget, and [Scene.AppBars] for [ToolbarFuncs] for [Scene]
// elements who should be represented in the main AppBar (e.g., TopAppBar).
type Toolbar struct { //core:embedder
Frame
// items moved from the main toolbar, will be shown in the overflow menu
OverflowItems tree.Slice `set:"-" json:"-" xml:"-"`
// functions for overflow menu: use AddOverflowMenu to add.
// These are processed in _reverse_ order (last in, first called)
// so that the default items are added last.
OverflowMenus []func(m *Scene) `set:"-" json:"-" xml:"-"`
// ToolbarFuncs contains functions for configuring this toolbar,
// called on Config
ToolbarFuncs ToolbarFuncs
// This is the overflow button
OverflowButton *Button `copier:"-"`
}
func (tb *Toolbar) OnInit() {
tb.WidgetBase.OnInit()
ToolbarStyles(tb)
tb.Layout.HandleEvents()
}
func (tb *Toolbar) IsVisible() bool {
// do not render toolbars with no buttons
return tb.WidgetBase.IsVisible() && len(tb.Kids) > 0
}
// AppChooser returns the app [Chooser] used for searching for
// items. It will only be non-nil if this toolbar has been configured
// with an app chooser, which typically only happens for app bars.
func (tb *Toolbar) AppChooser() *Chooser {
ch, _ := tb.ChildByName("app-chooser").(*Chooser)
return ch
}
func (tb *Toolbar) Config() {
if len(tb.Kids) == 0 {
if len(tb.ToolbarFuncs) > 0 {
for _, f := range tb.ToolbarFuncs {
f(tb)
}
}
}
tb.Frame.Config()
}
func (tb *Toolbar) SizeUp() {
tb.AllItemsToChildren()
tb.Frame.SizeUp()
}
// todo: try doing move to overflow in Final
func (tb *Toolbar) SizeDown(iter int) bool {
redo := tb.Frame.SizeDown(iter)
if iter == 0 {
return true // ensure a second pass
}
tb.MoveToOverflow()
return redo
}
func (tb *Toolbar) SizeFromChildren(iter int, pass LayoutPasses) math32.Vector2 {
csz := tb.Frame.SizeFromChildren(iter, pass)
if pass == SizeUpPass || (pass == SizeDownPass && iter == 0) {
dim := tb.Styles.Direction.Dim()
ovsz := tb.OverflowButton.Geom.Size.Actual.Total.Dim(dim)
csz.SetDim(dim, ovsz) // present the minimum size initially
return csz
}
return csz
}
// AllItemsToChildren moves the overflow items back to the children,
// so the full set is considered for the next layout round,
// and ensures the overflow button is made and moves it
// to the end of the list.
func (tb *Toolbar) AllItemsToChildren() {
if tb.OverflowButton == nil {
ic := icons.MoreVert
if tb.Styles.Direction != styles.Row {
ic = icons.MoreHoriz
}
tb.OverflowButton = NewButton(tb, "overflow-menu").SetIcon(ic).
SetTooltip("Additional menu items")
tb.OverflowButton.Menu = tb.OverflowMenu
}
if len(tb.OverflowItems) > 0 {
tb.Kids = append(tb.Kids, tb.OverflowItems...)
tb.OverflowItems = nil
}
ovi := -1
for i, k := range tb.Kids {
_, wb := AsWidget(k)
if wb.This() == tb.OverflowButton.This() {
ovi = i
break
}
}
if ovi >= 0 {
tb.Kids.DeleteAtIndex(ovi)
}
tb.Kids = append(tb.Kids, tb.OverflowButton.This())
tb.OverflowButton.Update()
}
func (tb *Toolbar) ParentSize() float32 {
ma := tb.Styles.Direction.Dim()
psz := tb.ParentWidget().Geom.Size.Alloc.Content.Sub(tb.Geom.Size.Space)
avail := psz.Dim(ma)
return avail
}
// MoveToOverflow moves overflow out of children to the OverflowItems list
func (tb *Toolbar) MoveToOverflow() {
ma := tb.Styles.Direction.Dim()
avail := tb.ParentSize()
ovsz := tb.OverflowButton.Geom.Size.Actual.Total.Dim(ma)
avsz := avail - ovsz
sz := &tb.Geom.Size
sz.Alloc.Total.SetDim(ma, avail)
sz.SetContentFromTotal(&sz.Alloc)
n := len(tb.Kids)
ovidx := n - 1
hasOv := false
szsum := float32(0)
tb.WidgetKidsIter(func(i int, kwi Widget, kwb *WidgetBase) bool {
if i >= n-1 {
return tree.Break
}
ksz := kwb.Geom.Size.Alloc.Total.Dim(ma)
szsum += ksz
if szsum > avsz {
if !hasOv {
ovidx = i
hasOv = true
}
tb.OverflowItems = append(tb.OverflowItems, kwi)
}
return tree.Continue
})
if ovidx != n-1 {
tb.Kids.Move(n-1, ovidx)
tb.Kids = tb.Kids[:ovidx+1]
}
if len(tb.OverflowItems) == 0 && len(tb.OverflowMenus) == 0 {
tb.OverflowButton.SetState(true, states.Invisible)
} else {
tb.OverflowButton.SetState(false, states.Invisible)
tb.OverflowButton.Update()
}
}
// OverflowMenu is the overflow menu function
func (tb *Toolbar) OverflowMenu(m *Scene) {
nm := len(tb.OverflowMenus)
if len(tb.OverflowItems) > 0 {
for _, k := range tb.OverflowItems {
if k.This() == tb.OverflowButton.This() {
continue
}
cl := k.This().Clone()
m.AddChild(cl)
cl.This().(Widget).Config()
}
if nm > 1 { // default includes sep
NewSeparator(m)
}
}
// reverse order so defaults are last
for i := nm - 1; i >= 0; i-- {
fn := tb.OverflowMenus[i]
fn(m)
}
}
// AddOverflowMenu adds the given menu function to the overflow menu list.
// These functions are called in reverse order such that the last added function
// is called first when constructing the menu.
func (tb *Toolbar) AddOverflowMenu(fun func(m *Scene)) {
tb.OverflowMenus = append(tb.OverflowMenus, fun)
}
// SetShortcuts sets the shortcuts to window associated with Toolbar
func (tb *Toolbar) SetShortcuts() {
em := tb.EventMgr()
if em == nil {
return
}
for _, mi := range tb.Kids {
if mi.NodeType().HasEmbed(ButtonType) {
bt := AsButton(mi)
em.AddShortcut(bt.Shortcut, bt)
}
}
}
func (tb *Toolbar) Destroy() {
tb.DeleteShortcuts()
tb.Frame.Destroy()
}
// DeleteShortcuts deletes the shortcuts -- called when destroyed
func (tb *Toolbar) DeleteShortcuts() {
em := tb.EventMgr()
if em == nil {
return
}
for _, mi := range tb.Kids {
if mi.NodeType().HasEmbed(ButtonType) {
ac := AsButton(mi)
em.DeleteShortcut(ac.Shortcut, ac)
}
}
}
// UpdateBar calls ApplyStyleUpdate to update to current state
func (tb *Toolbar) UpdateBar() {
tb.ApplyStyleUpdate()
}
// TODO(kai/menu): figure out what to do here
/*
// FindButtonByName finds an button on the toolbar, or any sub-menu, with
// given name (exact match) -- this is not the Text label but the Name of the
// element (for AddButton items, this is the same as Label or Icon (if Label
// is empty)) -- returns false if not found
func (tb *Toolbar) FindButtonByName(name string) (*Button, bool) {
for _, mi := range tb.Kids {
if mi.NodeType().HasEmbed(ButtonType) {
ac := AsButton(mi)
if ac.Name() == name {
return ac, true
}
if ac.Menu != nil {
if sac, ok := ac.Menu.FindButtonByName(name); ok {
return sac, ok
}
}
}
}
return nil, false
}
*/
//////////////////////////////////////////////////////////////////////////////
// ToolbarFuncs
// ToolbarFuncs are functions for creating control bars,
// attached to different sides of a Scene (e.g., TopAppBar at Top,
// NavBar at Bottom, etc). Functions are called in forward order
// so first added are called first.
type ToolbarFuncs []func(tb *Toolbar)
// Add adds the given function for configuring a toolbar
func (bf *ToolbarFuncs) Add(fun func(tb *Toolbar)) *ToolbarFuncs {
*bf = append(*bf, fun)
return bf
}
// Call calls all the functions for configuring given toolbar
func (bf *ToolbarFuncs) Call(tb *Toolbar) {
for _, fun := range *bf {
fun(tb)
}
}
// IsEmpty returns true if there are no functions added
func (bf *ToolbarFuncs) IsEmpty() bool {
return len(*bf) == 0
}
//////////////////////////////////////////////////////////////////////////////
// ToolbarStyles
// ToolbarStyles can be applied to any layout (e.g., Frame) to achieve
// standard toolbar styling.
func ToolbarStyles(ly Layouter) {
lb := ly.AsLayout()
ly.Style(func(s *styles.Style) {
s.Border.Radius = styles.BorderRadiusFull
s.Background = colors.C(colors.Scheme.SurfaceContainer)
s.Gap.Zero()
s.Align.Items = styles.Center
})
ly.AsWidget().StyleFinal(func(s *styles.Style) {
if s.Direction == styles.Row {
s.Grow.Set(1, 0)
s.Padding.SetHorizontal(units.Dp(16))
} else {
s.Grow.Set(0, 1)
s.Padding.SetVertical(units.Dp(16))
}
})
ly.OnWidgetAdded(func(w Widget) {
if bt := AsButton(w); bt != nil {
bt.Type = ButtonAction
return
}
if sp, ok := w.(*Separator); ok {
sp.Style(func(s *styles.Style) {
s.Direction = lb.Styles.Direction.Other()
})
}
})
}
// BasicBar is just a styled Frame layout for holding buttons
// and other widgets. Use this when the more advanced features
// of the Toolbar are not needed.
type BasicBar struct {
Frame
}
func (tb *BasicBar) OnInit() {
tb.WidgetBase.OnInit()
ToolbarStyles(tb)
tb.Layout.HandleEvents()
}
// UpdateBar calls ApplyStyleUpdate to update to current state
func (tb *BasicBar) UpdateBar() {
tb.ApplyStyleUpdate()
}