-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmenu_factory_darwin.go
87 lines (77 loc) · 2.16 KB
/
menu_factory_darwin.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
// Copyright (c) 2021-2025 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 (
"github.com/richardwilkes/toolbox"
"github.com/richardwilkes/unison/internal/ns"
)
type macMenuFactory struct {
bar *macMenu
}
func platformNewDefaultMenuFactory() MenuFactory {
return &macMenuFactory{}
}
func (f *macMenuFactory) BarForWindowNoCreate(_ *Window) Menu {
if f.bar == nil {
return nil
}
return f.bar
}
func (f *macMenuFactory) BarForWindow(_ *Window, initializer func(Menu)) Menu {
if f.bar == nil {
f.bar = f.newMenu(RootMenuID, "", nil)
initializer(f.bar)
InvokeTask(func() { ns.SetMainMenu(f.bar.menu) })
}
return f.bar
}
func (f *macMenuFactory) BarIsPerWindow() bool {
return false
}
func (f *macMenuFactory) NewMenu(id int, title string, updater func(Menu)) Menu {
return f.newMenu(id, title, updater)
}
func (f *macMenuFactory) newMenu(id int, title string, updater func(Menu)) *macMenu {
var u func(ns.Menu)
if updater != nil {
u = func(m ns.Menu) {
updater(&macMenu{factory: f, menu: m})
}
}
m := ns.NewMenu(title, u)
return &macMenu{
factory: f,
id: id,
menu: m,
}
}
func (f *macMenuFactory) NewItem(id int, title string, keyBinding KeyBinding, validator func(MenuItem) bool, handler func(MenuItem)) MenuItem {
var h func(ns.MenuItem)
if handler != nil {
h = func(mi ns.MenuItem) {
toolbox.Call(func() { handler(&macMenuItem{factory: f, item: mi}) })
}
}
mi := ns.NewMenuItem(id, title, macKeyCodeToMenuEquivalentMap[keyBinding.KeyCode],
keyBinding.Modifiers.eventModifierFlags(), func(mi ns.MenuItem) bool {
if DisableMenus {
return false
}
if validator != nil {
var result bool
toolbox.Call(func() { result = validator(&macMenuItem{factory: f, item: mi}) })
return result
}
return true
}, h)
return &macMenuItem{
factory: f,
item: mi,
}
}