-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
menu_factory.go
105 lines (93 loc) · 3.65 KB
/
menu_factory.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
// 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
var (
// DisableMenus overrides all application menus when set to true, causing them to become disabled. This is primarily
// provided to allow a way to disable menu key capture temporarily. This will not allow system keys to be captured,
// but will prevent the menus from capturing keys while it is true.
DisableMenus bool
defaultMenuFactory MenuFactory
_ MenuFactory = &inWindowMenuFactory{}
)
// MenuFactory provides methods for creating a menu bar and its menus.
type MenuFactory interface {
// BarForWindow returns the menu bar for the given window. If this is the first time the menu bar has been returned
// from this call, initializer will be called so that your code can configure the menus.
BarForWindow(window *Window, initializer func(Menu)) Menu
// BarForWindowNoCreate returns the menu bar for the given window. May return nil if no menu bar for the window has
// been created yet.
BarForWindowNoCreate(window *Window) Menu
// BarIsPerWindow returns true if the menu bar returned from this MenuFactory is per-window instead of global.
BarIsPerWindow() bool
// NewMenu creates a new Menu. updater is optional and, if present, will be called prior to showing the Menu, giving
// a chance to modify it.
NewMenu(id int, title string, updater func(Menu)) Menu
// NewItem creates a new MenuItem. Both validator and handler may be nil for default behavior.
NewItem(id int, title string, keyBinding KeyBinding, validator func(MenuItem) bool, handler func(MenuItem)) MenuItem
}
// DefaultMenuFactory returns the default MenuFactory for the platform. Multiple calls always return the same object.
func DefaultMenuFactory() MenuFactory {
if defaultMenuFactory == nil {
if noGlobalMenuBar {
defaultMenuFactory = NewInWindowMenuFactory()
} else {
defaultMenuFactory = platformNewDefaultMenuFactory()
}
}
return defaultMenuFactory
}
type inWindowMenuFactory struct {
showInProgress bool
}
// NewInWindowMenuFactory creates a new MenuFactory for in-window usage. This is the fallback Go-only version of menus
// used when a non-platform-native version doesn't exist.
func NewInWindowMenuFactory() MenuFactory {
return &inWindowMenuFactory{}
}
func (f *inWindowMenuFactory) BarForWindowNoCreate(window *Window) Menu {
return window.root.menuBar
}
func (f *inWindowMenuFactory) BarForWindow(window *Window, initializer func(Menu)) Menu {
if window.root.menuBar != nil {
return window.root.menuBar
}
b := f.newMenu(RootMenuID, "", nil)
initializer(b)
window.root.setMenuBar(b)
return b
}
func (f *inWindowMenuFactory) BarIsPerWindow() bool {
return true
}
func (f *inWindowMenuFactory) NewMenu(id int, title string, updater func(Menu)) Menu {
return f.newMenu(id, title, updater)
}
func (f *inWindowMenuFactory) newMenu(id int, title string, updater func(Menu)) *menu {
m := &menu{
factory: f,
titleItem: &menuItem{
factory: f,
id: id,
title: title,
},
updater: updater,
}
m.titleItem.subMenu = m
return m
}
func (f *inWindowMenuFactory) NewItem(id int, title string, keyBinding KeyBinding, validator func(MenuItem) bool, handler func(MenuItem)) MenuItem {
return &menuItem{
factory: f,
id: id,
title: title,
validator: validator,
handler: handler,
keyBinding: keyBinding,
}
}