Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaMenuBarUI.java
41154 views
1
/*
2
* Copyright (c) 2011, 2021, 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.laf;
27
28
import java.awt.Component;
29
import java.awt.Dimension;
30
import java.awt.Graphics;
31
import java.awt.MenuBar;
32
import java.security.AccessController;
33
34
import javax.swing.JComponent;
35
import javax.swing.JFrame;
36
import javax.swing.JMenuBar;
37
import javax.swing.plaf.ComponentUI;
38
import javax.swing.plaf.basic.BasicMenuBarUI;
39
40
import sun.lwawt.macosx.LWCToolkit;
41
import sun.security.action.GetBooleanAction;
42
43
// MenuBar implementation for Mac L&F
44
@SuppressWarnings("removal")
45
public class AquaMenuBarUI extends BasicMenuBarUI implements ScreenMenuBarProvider {
46
47
static {
48
java.security.AccessController.doPrivileged(
49
(java.security.PrivilegedAction<Void>) () -> {
50
System.loadLibrary("osxui");
51
return null;
52
});
53
}
54
55
// Utilities
56
public void uninstallUI(final JComponent c) {
57
if (fScreenMenuBar != null) {
58
final JFrame frame = (JFrame)(c.getTopLevelAncestor());
59
if (frame != null && frame.getMenuBar() == fScreenMenuBar) {
60
frame.setMenuBar((MenuBar)null);
61
}
62
fScreenMenuBar = null;
63
}
64
super.uninstallUI(c);
65
}
66
67
// Create PLAF
68
public static ComponentUI createUI(final JComponent c) {
69
return new AquaMenuBarUI();
70
}
71
72
// [3320390] -- If the screen menu bar is in use, don't register keyboard actions that
73
// show the menus when F10 is pressed.
74
protected void installKeyboardActions() {
75
if (!useScreenMenuBar) {
76
super.installKeyboardActions();
77
}
78
}
79
80
protected void uninstallKeyboardActions() {
81
if (!useScreenMenuBar) {
82
super.uninstallKeyboardActions();
83
}
84
}
85
86
// Paint Methods
87
public void paint(final Graphics g, final JComponent c) {
88
AquaMenuPainter.instance().paintMenuBarBackground(g, c.getWidth(), c.getHeight(), c);
89
}
90
91
public Dimension getPreferredSize(final JComponent c) {
92
if (isScreenMenuBar((JMenuBar)c)) {
93
if (setScreenMenuBar((JFrame)(c.getTopLevelAncestor()))) {
94
return new Dimension(0, 0);
95
}
96
}
97
return null;
98
}
99
100
void clearScreenMenuBar(final JFrame frame) {
101
if (useScreenMenuBar) {
102
frame.setMenuBar(null);
103
}
104
}
105
106
boolean setScreenMenuBar(final JFrame frame) {
107
if (useScreenMenuBar) {
108
try {
109
getScreenMenuBar();
110
} catch(final Throwable t) {
111
return false;
112
}
113
114
frame.setMenuBar(fScreenMenuBar);
115
}
116
117
return true;
118
}
119
120
public ScreenMenuBar getScreenMenuBar() {
121
// Lazy init of member variables means we should use a synchronized block.
122
synchronized(this) {
123
if (fScreenMenuBar == null) fScreenMenuBar = new ScreenMenuBar(this.menuBar);
124
}
125
return fScreenMenuBar;
126
}
127
128
// JMenuBars are in frame unless we're using ScreenMenuBars *and* it's
129
// been set by JFrame.setJMenuBar
130
// unless the JFrame has a normal java.awt.MenuBar (it's possible!)
131
// Other JMenuBars appear where the programmer puts them - top of window or elsewhere
132
public static final boolean isScreenMenuBar(final JMenuBar c) {
133
final javax.swing.plaf.ComponentUI ui = c.getUI();
134
if (ui instanceof AquaMenuBarUI) {
135
if (!((AquaMenuBarUI)ui).useScreenMenuBar) return false;
136
137
final Component parent = c.getTopLevelAncestor();
138
if (parent instanceof JFrame) {
139
final MenuBar mb = ((JFrame)parent).getMenuBar();
140
final boolean thisIsTheJMenuBar = (((JFrame)parent).getJMenuBar() == c);
141
if (mb == null) return thisIsTheJMenuBar;
142
return (mb instanceof ScreenMenuBar && thisIsTheJMenuBar);
143
}
144
}
145
return false;
146
}
147
148
ScreenMenuBar fScreenMenuBar;
149
boolean useScreenMenuBar = getScreenMenuBarProperty();
150
151
public static boolean getScreenMenuBarProperty() {
152
// Do not allow AWT to set the screen menu bar if it's embedded in another UI toolkit
153
if (LWCToolkit.isEmbedded()) return false;
154
return AccessController.doPrivileged(new GetBooleanAction(
155
AquaLookAndFeel.sPropertyPrefix + "useScreenMenuBar"));
156
}
157
}
158
159