Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/com/apple/eawt/_AppMenuBarHandler.java
41153 views
1
/*
2
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package com.apple.eawt;
27
28
import java.awt.Container;
29
import java.awt.Frame;
30
31
32
import javax.swing.JFrame;
33
import javax.swing.JLayeredPane;
34
import javax.swing.JMenuBar;
35
import javax.swing.plaf.MenuBarUI;
36
37
import com.apple.laf.ScreenMenuBar;
38
import sun.awt.AWTAccessor;
39
import sun.lwawt.macosx.CMenuBar;
40
41
import com.apple.laf.AquaMenuBarUI;
42
43
class _AppMenuBarHandler {
44
private static final int MENU_ABOUT = 1;
45
private static final int MENU_PREFS = 2;
46
47
private static native void nativeSetMenuState(final int menu, final boolean visible, final boolean enabled);
48
private static native void nativeSetDefaultMenuBar(final long menuBarPeer);
49
private static native void nativeActivateDefaultMenuBar(final long menuBarPeer);
50
51
static final _AppMenuBarHandler instance = new _AppMenuBarHandler();
52
static _AppMenuBarHandler getInstance() {
53
return instance;
54
}
55
56
private static ScreenMenuBar defaultMenuBar;
57
58
// callback from the native delegate -init function
59
private static void initMenuStates(final boolean aboutMenuItemVisible,
60
final boolean aboutMenuItemEnabled,
61
final boolean prefsMenuItemVisible,
62
final boolean prefsMenuItemEnabled) {
63
synchronized (instance) {
64
instance.aboutMenuItemVisible = aboutMenuItemVisible;
65
instance.aboutMenuItemEnabled = aboutMenuItemEnabled;
66
instance.prefsMenuItemVisible = prefsMenuItemVisible;
67
instance.prefsMenuItemEnabled = prefsMenuItemEnabled;
68
}
69
}
70
71
_AppMenuBarHandler() { }
72
73
boolean aboutMenuItemVisible;
74
boolean aboutMenuItemEnabled;
75
76
boolean prefsMenuItemVisible;
77
boolean prefsMenuItemEnabled;
78
boolean prefsMenuItemExplicitlySet;
79
80
void setDefaultMenuBar(final JMenuBar menuBar) {
81
installDefaultMenuBar(menuBar);
82
}
83
84
static boolean isMenuBarActivationNeeded() {
85
// scan the current frames, and see if any are foreground
86
final Frame[] frames = Frame.getFrames();
87
for (final Frame frame : frames) {
88
if (frame.isVisible() && !isFrameMinimized(frame)) {
89
return false;
90
}
91
}
92
93
return true;
94
}
95
96
static boolean isFrameMinimized(final Frame frame) {
97
return (frame.getExtendedState() & Frame.ICONIFIED) != 0;
98
}
99
100
static void installDefaultMenuBar(final JMenuBar menuBar) {
101
102
if (menuBar == null) {
103
// intentionally clearing the default menu
104
if (defaultMenuBar != null) {
105
defaultMenuBar.removeNotify();
106
defaultMenuBar = null;
107
}
108
nativeSetDefaultMenuBar(0);
109
return;
110
}
111
112
Container parent = menuBar.getParent();
113
if (parent instanceof JLayeredPane) {
114
((JLayeredPane) parent).remove(menuBar);
115
}
116
117
MenuBarUI ui = menuBar.getUI();
118
if (!(ui instanceof AquaMenuBarUI)) {
119
ui = new AquaMenuBarUI();
120
menuBar.setUI(ui);
121
}
122
123
final AquaMenuBarUI aquaUI = (AquaMenuBarUI)ui;
124
final ScreenMenuBar screenMenuBar = aquaUI.getScreenMenuBar();
125
if (screenMenuBar == null) {
126
// Aqua is installed, but we aren't using the screen menu bar
127
throw new IllegalStateException("Application.setDefaultMenuBar() only works if apple.laf.useScreenMenuBar=true");
128
}
129
130
if (screenMenuBar != defaultMenuBar) {
131
if (defaultMenuBar != null) {
132
defaultMenuBar.removeNotify();
133
}
134
defaultMenuBar = screenMenuBar;
135
screenMenuBar.addNotify();
136
}
137
138
final Object peer = AWTAccessor.getMenuComponentAccessor().getPeer(screenMenuBar);
139
if (!(peer instanceof CMenuBar)) {
140
// such a thing should not be possible
141
throw new IllegalStateException("Unable to determine native menu bar from provided JMenuBar");
142
}
143
144
// grab the pointer to the CMenuBar, and retain it in native
145
((CMenuBar) peer).execute(_AppMenuBarHandler::nativeSetDefaultMenuBar);
146
147
// if there is no currently active frame, install the default menu bar in the application main menu
148
if (isMenuBarActivationNeeded()) {
149
((CMenuBar) peer).execute(_AppMenuBarHandler::nativeActivateDefaultMenuBar);
150
}
151
}
152
153
void setAboutMenuItemVisible(final boolean present) {
154
synchronized (this) {
155
if (aboutMenuItemVisible == present) return;
156
aboutMenuItemVisible = present;
157
}
158
159
nativeSetMenuState(MENU_ABOUT, aboutMenuItemVisible, aboutMenuItemEnabled);
160
}
161
162
void setPreferencesMenuItemVisible(final boolean present) {
163
synchronized (this) {
164
prefsMenuItemExplicitlySet = true;
165
if (prefsMenuItemVisible == present) return;
166
prefsMenuItemVisible = present;
167
}
168
nativeSetMenuState(MENU_PREFS, prefsMenuItemVisible, prefsMenuItemEnabled);
169
}
170
171
void setAboutMenuItemEnabled(final boolean enable) {
172
synchronized (this) {
173
if (aboutMenuItemEnabled == enable) return;
174
aboutMenuItemEnabled = enable;
175
}
176
nativeSetMenuState(MENU_ABOUT, aboutMenuItemVisible, aboutMenuItemEnabled);
177
}
178
179
void setPreferencesMenuItemEnabled(final boolean enable) {
180
synchronized (this) {
181
prefsMenuItemExplicitlySet = true;
182
if (prefsMenuItemEnabled == enable) return;
183
prefsMenuItemEnabled = enable;
184
}
185
nativeSetMenuState(MENU_PREFS, prefsMenuItemVisible, prefsMenuItemEnabled);
186
}
187
188
boolean isAboutMenuItemVisible() {
189
return aboutMenuItemVisible;
190
}
191
192
boolean isPreferencesMenuItemVisible() {
193
return prefsMenuItemVisible;
194
}
195
196
boolean isAboutMenuItemEnabled() {
197
return aboutMenuItemEnabled;
198
}
199
200
boolean isPreferencesMenuItemEnabled() {
201
return prefsMenuItemEnabled;
202
}
203
}
204
205