/
bars.go
491 lines (446 loc) · 12.6 KB
/
bars.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright (c) 2018, The GoKi 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 gi
import (
"image"
"github.com/goki/gi/oswin"
"github.com/goki/gi/oswin/key"
"github.com/goki/gi/oswin/mouse"
"github.com/goki/gi/units"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
)
////////////////////////////////////////////////////////////////////////////////////////
// MenuBar
// MenuBar is a Layout (typically LayoutHoriz) that renders a gradient
// background and has convenience methods for adding menus.
type MenuBar struct {
Layout
MainMenu bool `desc:"is this the main menu bar for a window? controls whether displayed on macOS"`
OSMainMenus map[string]*Action `json:"-" xml:"-" desc:"map of main menu items for callback from OS main menu (MacOS specific)"`
}
var KiT_MenuBar = kit.Types.AddType(&MenuBar{}, MenuBarProps)
// AddNewMenuBar adds a new menubar to given parent node, with given name.
func AddNewMenuBar(parent ki.Ki, name string) *MenuBar {
return parent.AddNewChild(KiT_MenuBar, name).(*MenuBar)
}
func (mb *MenuBar) CopyFieldsFrom(frm interface{}) {
fr := frm.(*MenuBar)
mb.Layout.CopyFieldsFrom(&fr.Layout)
mb.MainMenu = fr.MainMenu
}
var MenuBarProps = ki.Props{
"EnumType:Flag": KiT_NodeFlags,
"padding": units.NewPx(2),
"margin": units.NewPx(0),
"spacing": units.NewPx(4),
"color": &Prefs.Colors.Font,
"background-color": "linear-gradient(pref(Control), highlight-10)",
}
// MenuBarStdRender does the standard rendering of the bar
func (mb *MenuBar) MenuBarStdRender() {
rs, pc, st := mb.RenderLock()
pos := mb.LayState.Alloc.Pos
sz := mb.LayState.Alloc.Size
pc.FillBox(rs, pos, sz, &st.Font.BgColor)
mb.RenderUnlock(rs)
}
func (mb *MenuBar) ShowMenuBar() bool {
if len(mb.Kids) == 0 {
return false
}
if mb.MainMenu {
if oswin.TheApp.Platform() == oswin.MacOS && !LocalMainMenu {
return false
}
}
return true
}
func (mb *MenuBar) Size2D(iter int) {
if !mb.ShowMenuBar() {
return
}
mb.Layout.Size2D(iter)
}
func (mb *MenuBar) Layout2D(parBBox image.Rectangle, iter int) bool {
if !mb.ShowMenuBar() {
return false
}
return mb.Layout.Layout2D(parBBox, iter)
}
func (mb *MenuBar) Render2D() {
if !mb.ShowMenuBar() {
return
}
if mb.PushBounds() {
mb.MenuBarStdRender()
mb.This().(Node2D).ConnectEvents2D()
mb.RenderScrolls()
mb.Render2DChildren()
mb.PopBounds()
} else {
mb.DisconnectAllEvents(AllPris) // uses both Low and Hi
}
}
// UpdateActions calls UpdateFunc on all actions in menu -- individual menus
// are automatically updated just prior to menu popup
func (mb *MenuBar) UpdateActions() {
if mb == nil {
return
}
for _, mi := range mb.Kids {
if ki.TypeEmbeds(mi, KiT_Action) {
ac := mi.Embed(KiT_Action).(*Action)
ac.UpdateActions()
}
}
}
// SetShortcuts sets the shortcuts to window associated with Toolbar
// Called in ConnectEvents2D()
func (mb *MenuBar) SetShortcuts() {
win := mb.ParentWindow()
if win == nil {
return
}
for _, k := range mb.Kids {
if ki.TypeEmbeds(k, KiT_Action) {
ac := k.Embed(KiT_Action).(*Action)
win.AddShortcut(ac.Shortcut, ac)
}
}
}
func (mb *MenuBar) Destroy() {
mb.DeleteShortcuts()
mb.Layout.Destroy()
}
// DeleteShortcuts deletes the shortcuts -- called when destroyed
func (mb *MenuBar) DeleteShortcuts() {
win := mb.ParentWindow()
if win == nil {
return
}
for _, k := range mb.Kids {
if ki.TypeEmbeds(k, KiT_Action) {
ac := k.Embed(KiT_Action).(*Action)
win.DeleteShortcut(ac.Shortcut, ac)
}
}
}
// FindActionByName finds an action on the menu, or any sub-menu, with given
// name (exact match) -- this is not the Text label but the Name of the
// element (for AddAction items, this is the same as Label or Icon (if Label
// is empty)) -- returns false if not found
func (m *MenuBar) FindActionByName(name string) (*Action, bool) {
if m == nil {
return nil, false
}
for _, mi := range m.Kids {
if ki.TypeEmbeds(mi, KiT_Action) {
ac := mi.Embed(KiT_Action).(*Action)
if ac.Name() == name {
return ac, true
}
if ac.Menu != nil {
if sac, ok := ac.Menu.FindActionByName(name); ok {
return sac, ok
}
}
}
}
return nil, false
}
// ConfigMenus configures Action items as children of MenuBar with the given
// names, which function as the main menu panels for the menu bar (File, Edit,
// etc). Access the resulting menus as .ChildByName("name").(*Action).
func (mb *MenuBar) ConfigMenus(menus []string) {
if mb == nil {
return
}
sz := len(menus)
tnl := make(kit.TypeAndNameList, sz+1)
typ := KiT_Action // note: could pass in action type to make it more flexible, but..
for i, m := range menus {
tnl[i].Type = typ
tnl[i].Name = m
}
tnl[sz].Type = KiT_Stretch
tnl[sz].Name = "menstr"
_, updt := mb.ConfigChildren(tnl)
for i, m := range menus {
mi := mb.Kids[i]
if ki.TypeEmbeds(mi, KiT_Action) {
ac := mi.Embed(KiT_Action).(*Action)
ac.SetText(m)
ac.SetAsMenu()
}
}
mb.UpdateEnd(updt)
}
// MainMenuFunc is the callback function for OS-generated menu actions.
func MainMenuFunc(owin oswin.Window, title string, tag int) {
win, ok := owin.Parent().(*Window)
if !ok {
return
}
mb := win.MainMenu
if mb == nil {
return
}
if mb.OSMainMenus == nil {
return
}
ma, ok := mb.OSMainMenus[title]
if !ok {
return
}
// fmt.Printf("triggering OS main menu: %v\n", title)
ma.Trigger()
}
// UpdateMainMenu updates the OS-specific, separate main menu of given window based
// on this MenuBar -- called by Window.MainMenuUpdated.
func (mb *MenuBar) UpdateMainMenu(win *Window) {
osmm := win.OSWin.MainMenu()
if osmm == nil { // no OS main menu
return
}
mb.UpdateActions()
osmm.SetFunc(MainMenuFunc)
mm := osmm.StartUpdate() // locks
osmm.Reset(mm)
mb.OSMainMenus = make(map[string]*Action, 100)
for _, mi := range mb.Kids {
if ki.TypeEmbeds(mi, KiT_Action) {
ac := mi.Embed(KiT_Action).(*Action)
subm := osmm.AddSubMenu(mm, ac.Text)
mb.SetMainMenuSub(osmm, subm, ac)
}
}
osmm.EndUpdate(mm) // unlocks
}
// SetMainMenu sets this menu as the current OS-specific, separate main menu
// for given window -- only should be called in window.Focus event.
// Does nothing if menu is empty.
func (mb *MenuBar) SetMainMenu(win *Window) {
osmm := win.OSWin.MainMenu()
if osmm == nil { // no OS main menu
return
}
if len(mb.Kids) == 0 {
return
}
if mb.OSMainMenus == nil {
mb.UpdateMainMenu(win)
}
osmm.SetMenu()
}
// SetMainMenuSub iterates over sub-menus, adding items to overall main menu.
func (mb *MenuBar) SetMainMenuSub(osmm oswin.MainMenu, subm oswin.Menu, am *Action) {
for i, mi := range am.Menu {
if ki.TypeEmbeds(mi, KiT_Action) {
ac := mi.Embed(KiT_Action).(*Action)
if len(ac.Menu) > 0 {
ssubm := osmm.AddSubMenu(subm, ac.Text)
mb.SetMainMenuSub(osmm, ssubm, ac)
} else {
mid := osmm.AddItem(subm, ac.Text, string(ac.Shortcut), i, ac.IsActive())
mb.OSMainMenus[ac.Text] = ac
ac.SetProp("__OSMainMenuItemID", mid)
}
} else if _, ok := mi.(*Separator); ok {
osmm.AddSeparator(subm)
}
}
}
// MainMenuUpdateActives updates the active state of all menu items, based on
// active state of corresponding Actions (action self-update functions are
// called via UpdateActions) -- can be called by method of same name on
// Window.
func (mb *MenuBar) MainMenuUpdateActives(win *Window) {
osmm := win.OSWin.MainMenu()
if osmm == nil { // no OS main menu
return
}
mb.UpdateActions()
if mb.OSMainMenus == nil {
return
}
for _, ma := range mb.OSMainMenus {
mid, err := ma.PropTry("__OSMainMenuItemID")
if err != nil {
continue
}
osmm.SetItemActive(mid.(oswin.MenuItem), ma.IsActive()) // assuming this is threadsafe
}
}
////////////////////////////////////////////////////////////////////////////////////////
// ToolBar
// ToolBar is a Layout (typically LayoutHoriz) that renders a gradient
// background and is useful for holding Actions that do things
type ToolBar struct {
Layout
}
var KiT_ToolBar = kit.Types.AddType(&ToolBar{}, ToolBarProps)
// AddNewToolBar adds a new toolbar to given parent node, with given name.
func AddNewToolBar(parent ki.Ki, name string) *ToolBar {
return parent.AddNewChild(KiT_ToolBar, name).(*ToolBar)
}
func (tb *ToolBar) CopyFieldsFrom(frm interface{}) {
fr := frm.(*ToolBar)
tb.Layout.CopyFieldsFrom(&fr.Layout)
}
var ToolBarProps = ki.Props{
"EnumType:Flag": KiT_NodeFlags,
"padding": units.NewPx(2),
"margin": units.NewPx(0),
"spacing": units.NewPx(4),
"color": &Prefs.Colors.Font,
"background-color": "linear-gradient(pref(Control), highlight-10)",
}
// AddAction adds an action to the toolbar using given options, and connects
// the action signal to given receiver object and function, along with given
// data which is stored on the action and then passed in the action signal.
// Optional updateFunc is a function called prior to showing the menu to
// update the actions (enabled or not typically).
func (tb *ToolBar) AddAction(opts ActOpts, sigTo ki.Ki, fun ki.RecvFunc) *Action {
nm := opts.Name
if nm == "" {
nm = opts.Label
}
if nm == "" {
nm = opts.Icon
}
ac := AddNewAction(tb, nm)
ac.Text = opts.Label
ac.Icon = IconName(opts.Icon)
ac.Tooltip = opts.Tooltip
ac.Shortcut = key.Chord(opts.Shortcut).OSShortcut()
if opts.ShortcutKey != KeyFunNil {
ac.Shortcut = ShortcutForFun(opts.ShortcutKey)
}
ac.Data = opts.Data
ac.UpdateFunc = opts.UpdateFunc
if sigTo != nil && fun != nil {
ac.ActionSig.Connect(sigTo, fun)
}
return ac
}
// AddSeparator adds a new separator to the toolbar -- automatically sets orientation
// depending on layout. All nodes need a name identifier.
func (tb *ToolBar) AddSeparator(sepnm string) *Separator {
sp := AddNewSeparator(tb, sepnm, false)
if tb.Lay == LayoutHoriz {
sp.Horiz = false
} else {
sp.Horiz = true
}
return sp
}
// ToolBarStdRender does the standard rendering of the bar
func (tb *ToolBar) ToolBarStdRender() {
rs, pc, st := tb.RenderLock()
pos := tb.LayState.Alloc.Pos
sz := tb.LayState.Alloc.Size
pc.FillBox(rs, pos, sz, &st.Font.BgColor)
tb.RenderUnlock(rs)
}
func (tb *ToolBar) Render2D() {
if len(tb.Kids) == 0 { // todo: check for mac menu and don't render -- also need checks higher up
return
}
if tb.FullReRenderIfNeeded() {
return
}
if tb.PushBounds() {
tb.ToolBarStdRender()
tb.This().(Node2D).ConnectEvents2D()
tb.RenderScrolls()
tb.Render2DChildren()
tb.PopBounds()
} else {
tb.DisconnectAllEvents(AllPris) // uses both Low and Hi
}
}
func (tb *ToolBar) MouseFocusEvent() {
tb.ConnectEvent(oswin.MouseFocusEvent, HiPri, func(recv, send ki.Ki, sig int64, d interface{}) {
me := d.(*mouse.FocusEvent)
if me.Action == mouse.Enter {
tbb := recv.Embed(KiT_ToolBar).(*ToolBar)
tbb.UpdateActions()
// do NOT mark as processed -- HiPri and not mutex
}
})
}
func (tb *ToolBar) ConnectEvents2D() {
tb.Layout.ConnectEvents2D()
tb.MouseFocusEvent()
tb.SetShortcuts()
}
// SetShortcuts sets the shortcuts to window associated with Toolbar
// Called in ConnectEvents2D()
func (tb *ToolBar) SetShortcuts() {
win := tb.ParentWindow()
if win == nil {
return
}
for _, k := range tb.Kids {
if ki.TypeEmbeds(k, KiT_Action) {
ac := k.Embed(KiT_Action).(*Action)
win.AddShortcut(ac.Shortcut, ac)
}
}
}
func (tb *ToolBar) Destroy() {
tb.DeleteShortcuts()
tb.Layout.Destroy()
}
// DeleteShortcuts deletes the shortcuts -- called when destroyed
func (tb *ToolBar) DeleteShortcuts() {
win := tb.ParentWindow()
if win == nil {
return
}
for _, k := range tb.Kids {
if ki.TypeEmbeds(k, KiT_Action) {
ac := k.Embed(KiT_Action).(*Action)
win.DeleteShortcut(ac.Shortcut, ac)
}
}
}
// UpdateActions calls UpdateFunc on all actions in toolbar -- individual
// menus are automatically updated just prior to menu popup
func (tb *ToolBar) UpdateActions() {
if tb == nil {
return
}
if tb.ParentWindow() != nil {
wupdt := tb.TopUpdateStart()
defer tb.TopUpdateEnd(wupdt)
}
for _, mi := range tb.Kids {
if ki.TypeEmbeds(mi, KiT_Action) {
ac := mi.Embed(KiT_Action).(*Action)
ac.UpdateActions()
}
}
}
// FindActionByName finds an action 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 AddAction items, this is the same as Label or Icon (if Label
// is empty)) -- returns false if not found
func (tb *ToolBar) FindActionByName(name string) (*Action, bool) {
for _, mi := range tb.Kids {
if ki.TypeEmbeds(mi, KiT_Action) {
ac := mi.Embed(KiT_Action).(*Action)
if ac.Name() == name {
return ac, true
}
if ac.Menu != nil {
if sac, ok := ac.Menu.FindActionByName(name); ok {
return sac, ok
}
}
}
}
return nil, false
}