package sun.lwawt.macosx;
import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.peer.WindowPeer;
import java.beans.*;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
import sun.awt.*;
import sun.awt.AWTAccessor.ComponentAccessor;
import sun.awt.AWTAccessor.WindowAccessor;
import sun.java2d.SurfaceData;
import sun.java2d.opengl.CGLSurfaceData;
import sun.lwawt.*;
import sun.util.logging.PlatformLogger;
import com.apple.laf.*;
import com.apple.laf.ClientPropertyApplicator.Property;
import com.sun.awt.AWTUtilities;
public class CPlatformWindow extends CFRetainedResource implements PlatformWindow {
private native long nativeCreateNSWindow(long nsViewPtr,long ownerPtr, long styleBits, double x, double y, double w, double h);
private static native void nativeSetNSWindowStyleBits(long nsWindowPtr, int mask, int data);
private static native void (long nsWindowPtr, long menuBarPtr);
private static native Insets nativeGetNSWindowInsets(long nsWindowPtr);
private static native void nativeSetNSWindowBounds(long nsWindowPtr, double x, double y, double w, double h);
private static native void nativeSetNSWindowMinMax(long nsWindowPtr, double minW, double minH, double maxW, double maxH);
private static native void nativePushNSWindowToBack(long nsWindowPtr);
private static native void nativePushNSWindowToFront(long nsWindowPtr);
private static native void nativeSetNSWindowTitle(long nsWindowPtr, String title);
private static native void nativeRevalidateNSWindowShadow(long nsWindowPtr);
private static native void nativeSetNSWindowMinimizedIcon(long nsWindowPtr, long nsImage);
private static native void nativeSetNSWindowRepresentedFilename(long nsWindowPtr, String representedFilename);
private static native void nativeSetEnabled(long nsWindowPtr, boolean isEnabled);
private static native void nativeSynthesizeMouseEnteredExitedEvents();
private static native void nativeDispose(long nsWindowPtr);
private static native void nativeEnterFullScreenMode(long nsWindowPtr);
private static native void nativeExitFullScreenMode(long nsWindowPtr);
static native CPlatformWindow nativeGetTopmostPlatformWindowUnderMouse();
private static final PlatformLogger logger = PlatformLogger.getLogger("sun.lwawt.macosx.CPlatformWindow");
private static final PlatformLogger focusLogger = PlatformLogger.getLogger("sun.lwawt.macosx.focus.CPlatformWindow");
public static final String WINDOW_BRUSH_METAL_LOOK = "apple.awt.brushMetalLook";
public static final String WINDOW_DRAGGABLE_BACKGROUND = "apple.awt.draggableWindowBackground";
public static final String WINDOW_ALPHA = "Window.alpha";
public static final String WINDOW_SHADOW = "Window.shadow";
public static final String WINDOW_STYLE = "Window.style";
public static final String WINDOW_SHADOW_REVALIDATE_NOW = "apple.awt.windowShadow.revalidateNow";
public static final String WINDOW_DOCUMENT_MODIFIED = "Window.documentModified";
public static final String WINDOW_DOCUMENT_FILE = "Window.documentFile";
public static final String WINDOW_CLOSEABLE = "Window.closeable";
public static final String WINDOW_MINIMIZABLE = "Window.minimizable";
public static final String WINDOW_ZOOMABLE = "Window.zoomable";
public static final String WINDOW_HIDES_ON_DEACTIVATE="Window.hidesOnDeactivate";
public static final String WINDOW_DOC_MODAL_SHEET = "apple.awt.documentModalSheet";
public static final String WINDOW_FADE_DELEGATE = "apple.awt._windowFadeDelegate";
public static final String WINDOW_FADE_IN = "apple.awt._windowFadeIn";
public static final String WINDOW_FADE_OUT = "apple.awt._windowFadeOut";
public static final String WINDOW_FULLSCREENABLE = "apple.awt.fullscreenable";
static final int MODELESS = 0;
static final int DOCUMENT_MODAL = 1;
static final int APPLICATION_MODAL = 2;
static final int TOOLKIT_MODAL = 3;
static final int _RESERVED_FOR_DATA = 1 << 0;
static final int DECORATED = 1 << 1;
static final int TEXTURED = 1 << 2;
static final int UNIFIED = 1 << 3;
static final int UTILITY = 1 << 4;
static final int HUD = 1 << 5;
static final int SHEET = 1 << 6;
static final int CLOSEABLE = 1 << 7;
static final int MINIMIZABLE = 1 << 8;
static final int RESIZABLE = 1 << 9;
static final int NONACTIVATING = 1 << 24;
static final int IS_DIALOG = 1 << 25;
static final int IS_MODAL = 1 << 26;
static final int = 1 << 27;
static final int _STYLE_PROP_BITMASK = DECORATED | TEXTURED | UNIFIED | UTILITY | HUD | SHEET | CLOSEABLE | MINIMIZABLE | RESIZABLE;
static final int HAS_SHADOW = 1 << 10;
static final int ZOOMABLE = 1 << 11;
static final int ALWAYS_ON_TOP = 1 << 15;
static final int HIDES_ON_DEACTIVATE = 1 << 17;
static final int DRAGGABLE_BACKGROUND = 1 << 19;
static final int DOCUMENT_MODIFIED = 1 << 21;
static final int FULLSCREENABLE = 1 << 23;
static final int _METHOD_PROP_BITMASK = RESIZABLE | HAS_SHADOW | ZOOMABLE | ALWAYS_ON_TOP | HIDES_ON_DEACTIVATE | DRAGGABLE_BACKGROUND | DOCUMENT_MODIFIED | FULLSCREENABLE;
static final int SHOULD_BECOME_KEY = 1 << 12;
static final int SHOULD_BECOME_MAIN = 1 << 13;
static final int MODAL_EXCLUDED = 1 << 16;
static final int _CALLBACK_PROP_BITMASK = SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN | MODAL_EXCLUDED;
static int SET(final int bits, final int mask, final boolean value) {
if (value) return (bits | mask);
return bits & ~mask;
}
static boolean IS(final int bits, final int mask) {
return (bits & mask) != 0;
}
@SuppressWarnings("unchecked")
static ClientPropertyApplicator<JRootPane, CPlatformWindow> CLIENT_PROPERTY_APPLICATOR = new ClientPropertyApplicator<JRootPane, CPlatformWindow>(new Property[] {
new Property<CPlatformWindow>(WINDOW_DOCUMENT_MODIFIED) { public void applyProperty(final CPlatformWindow c, final Object value) {
c.setStyleBits(DOCUMENT_MODIFIED, value == null ? false : Boolean.parseBoolean(value.toString()));
}},
new Property<CPlatformWindow>(WINDOW_BRUSH_METAL_LOOK) { public void applyProperty(final CPlatformWindow c, final Object value) {
c.setStyleBits(TEXTURED, Boolean.parseBoolean(value.toString()));
}},
new Property<CPlatformWindow>(WINDOW_ALPHA) { public void applyProperty(final CPlatformWindow c, final Object value) {
AWTUtilities.setWindowOpacity(c.target, value == null ? 1.0f : Float.parseFloat(value.toString()));
}},
new Property<CPlatformWindow>(WINDOW_SHADOW) { public void applyProperty(final CPlatformWindow c, final Object value) {
c.setStyleBits(HAS_SHADOW, value == null ? true : Boolean.parseBoolean(value.toString()));
}},
new Property<CPlatformWindow>(WINDOW_MINIMIZABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
c.setStyleBits(MINIMIZABLE, Boolean.parseBoolean(value.toString()));
}},
new Property<CPlatformWindow>(WINDOW_CLOSEABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
c.setStyleBits(CLOSEABLE, Boolean.parseBoolean(value.toString()));
}},
new Property<CPlatformWindow>(WINDOW_ZOOMABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
c.setStyleBits(ZOOMABLE, Boolean.parseBoolean(value.toString()));
}},
new Property<CPlatformWindow>(WINDOW_FULLSCREENABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
c.setStyleBits(FULLSCREENABLE, Boolean.parseBoolean(value.toString()));
}},
new Property<CPlatformWindow>(WINDOW_SHADOW_REVALIDATE_NOW) { public void applyProperty(final CPlatformWindow c, final Object value) {
c.execute(new CFNativeAction() {
@Override
public void run(long ptr) {
nativeRevalidateNSWindowShadow(ptr);
}
});
}},
new Property<CPlatformWindow>(WINDOW_DOCUMENT_FILE) { public void applyProperty(final CPlatformWindow c, final Object value) {
if (value == null || !(value instanceof java.io.File)) {
c.execute(new CFNativeAction() {
@Override
public void run(long ptr) {
nativeSetNSWindowRepresentedFilename(ptr, null);
}
});
return;
}
final String filename = ((java.io.File)value).getAbsolutePath();
c.execute(new CFNativeAction() {
@Override
public void run(long ptr) {
nativeSetNSWindowRepresentedFilename(ptr, filename);
}
});
}}
}) {
public CPlatformWindow convertJComponentToTarget(final JRootPane p) {
Component root = SwingUtilities.getRoot(p);
if (root == null || (LWWindowPeer)root.getPeer() == null) return null;
return (CPlatformWindow)((LWWindowPeer)root.getPeer()).getPlatformWindow();
}
};
private Rectangle nativeBounds = new Rectangle(0, 0, 0, 0);
private volatile boolean isFullScreenMode;
private boolean isFullScreenAnimationOn;
private volatile boolean isIconifyAnimationActive;
private Window target;
private LWWindowPeer peer;
protected CPlatformView contentView;
protected CPlatformWindow owner;
protected boolean visible = false;
private boolean undecorated;
private Rectangle normalBounds = null;
private CPlatformResponder responder;
public CPlatformWindow() {
super(0, true);
}
@Override
public void initialize(Window _target, LWWindowPeer _peer, PlatformWindow _owner) {
this.peer = _peer;
this.target = _target;
if (_owner instanceof CPlatformWindow) {
this.owner = (CPlatformWindow)_owner;
}
final int styleBits = getInitialStyleBits();
responder = createPlatformResponder();
contentView = createContentView();
contentView.initialize(peer, responder);
final Rectangle bounds;
if (!IS(DECORATED, styleBits)) {
bounds = new Rectangle(0, 0, 1, 1);
} else {
bounds = _peer.constrainBounds(_target.getBounds());
}
final AtomicLong ref = new AtomicLong();
contentView.execute(new CFNativeAction() {
@Override
public void run(final long viewPtr) {
boolean hasOwnerPtr = false;
if (owner != null) {
hasOwnerPtr = 0L != owner.executeGet(new CFNativeActionGet() {
@Override
public long run(long ownerPtr) {
ref.set(nativeCreateNSWindow(viewPtr, ownerPtr, styleBits,
bounds.x, bounds.y,
bounds.width, bounds.height));
return 1;
}
});
}
if (!hasOwnerPtr) {
ref.set(nativeCreateNSWindow(viewPtr, 0,
styleBits, bounds.x, bounds.y,
bounds.width, bounds.height));
}
}
});
setPtr(ref.get());
if (target instanceof javax.swing.RootPaneContainer) {
final javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer)target).getRootPane();
if (rootpane != null) rootpane.addPropertyChangeListener("ancestor", new PropertyChangeListener() {
public void propertyChange(final PropertyChangeEvent evt) {
CLIENT_PROPERTY_APPLICATOR.attachAndApplyClientProperties(rootpane);
rootpane.removePropertyChangeListener("ancestor", this);
}
});
}
validateSurface();
}
protected CPlatformResponder createPlatformResponder() {
return new CPlatformResponder(peer, false);
}
protected CPlatformView createContentView() {
return new CPlatformView();
}
protected int getInitialStyleBits() {
int styleBits = DECORATED | HAS_SHADOW | CLOSEABLE | MINIMIZABLE | ZOOMABLE | RESIZABLE;
if (isNativelyFocusableWindow()) {
styleBits = SET(styleBits, SHOULD_BECOME_KEY, true);
styleBits = SET(styleBits, SHOULD_BECOME_MAIN, true);
}
final boolean isFrame = (target instanceof Frame);
final boolean isDialog = (target instanceof Dialog);
final boolean isPopup = (target.getType() == Window.Type.POPUP);
if (isDialog) {
styleBits = SET(styleBits, MINIMIZABLE, false);
}
{
this.undecorated = isFrame ? ((Frame)target).isUndecorated() : (isDialog ? ((Dialog)target).isUndecorated() : true);
if (this.undecorated) styleBits = SET(styleBits, DECORATED, false);
}
{
final boolean resizable = isFrame ? ((Frame)target).isResizable() : (isDialog ? ((Dialog)target).isResizable() : false);
styleBits = SET(styleBits, RESIZABLE, resizable);
if (!resizable) {
styleBits = SET(styleBits, RESIZABLE, false);
styleBits = SET(styleBits, ZOOMABLE, false);
}
}
if (target.isAlwaysOnTop()) {
styleBits = SET(styleBits, ALWAYS_ON_TOP, true);
}
if (target.getModalExclusionType() == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) {
styleBits = SET(styleBits, MODAL_EXCLUDED, true);
}
if (isPopup) {
styleBits = SET(styleBits, TEXTURED, false);
styleBits = SET(styleBits, NONACTIVATING, true);
styleBits = SET(styleBits, IS_POPUP, true);
}
if (Window.Type.UTILITY.equals(target.getType())) {
styleBits = SET(styleBits, UTILITY, true);
}
if (target instanceof javax.swing.RootPaneContainer) {
javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer)target).getRootPane();
Object prop = null;
prop = rootpane.getClientProperty(WINDOW_BRUSH_METAL_LOOK);
if (prop != null) {
styleBits = SET(styleBits, TEXTURED, Boolean.parseBoolean(prop.toString()));
}
if (isDialog && ((Dialog)target).getModalityType() == ModalityType.DOCUMENT_MODAL) {
prop = rootpane.getClientProperty(WINDOW_DOC_MODAL_SHEET);
if (prop != null) {
styleBits = SET(styleBits, SHEET, Boolean.parseBoolean(prop.toString()));
}
}
prop = rootpane.getClientProperty(WINDOW_STYLE);
if (prop != null) {
if ("small".equals(prop)) {
styleBits = SET(styleBits, UTILITY, true);
if (target.isAlwaysOnTop() && rootpane.getClientProperty(WINDOW_HIDES_ON_DEACTIVATE) == null) {
styleBits = SET(styleBits, HIDES_ON_DEACTIVATE, true);
}
}
if ("textured".equals(prop)) styleBits = SET(styleBits, TEXTURED, true);
if ("unified".equals(prop)) styleBits = SET(styleBits, UNIFIED, true);
if ("hud".equals(prop)) styleBits = SET(styleBits, HUD, true);
}
prop = rootpane.getClientProperty(WINDOW_HIDES_ON_DEACTIVATE);
if (prop != null) {
styleBits = SET(styleBits, HIDES_ON_DEACTIVATE, Boolean.parseBoolean(prop.toString()));
}
prop = rootpane.getClientProperty(WINDOW_CLOSEABLE);
if (prop != null) {
styleBits = SET(styleBits, CLOSEABLE, Boolean.parseBoolean(prop.toString()));
}
prop = rootpane.getClientProperty(WINDOW_MINIMIZABLE);
if (prop != null) {
styleBits = SET(styleBits, MINIMIZABLE, Boolean.parseBoolean(prop.toString()));
}
prop = rootpane.getClientProperty(WINDOW_ZOOMABLE);
if (prop != null) {
styleBits = SET(styleBits, ZOOMABLE, Boolean.parseBoolean(prop.toString()));
}
prop = rootpane.getClientProperty(WINDOW_FULLSCREENABLE);
if (prop != null) {
styleBits = SET(styleBits, FULLSCREENABLE, Boolean.parseBoolean(prop.toString()));
}
prop = rootpane.getClientProperty(WINDOW_SHADOW);
if (prop != null) {
styleBits = SET(styleBits, HAS_SHADOW, Boolean.parseBoolean(prop.toString()));
}
prop = rootpane.getClientProperty(WINDOW_DRAGGABLE_BACKGROUND);
if (prop != null) {
styleBits = SET(styleBits, DRAGGABLE_BACKGROUND, Boolean.parseBoolean(prop.toString()));
}
}
if (isDialog) {
styleBits = SET(styleBits, IS_DIALOG, true);
if (((Dialog) target).isModal()) {
styleBits = SET(styleBits, IS_MODAL, true);
}
}
peer.setTextured(IS(TEXTURED, styleBits));
return styleBits;
}
protected void setStyleBits(final int mask, final boolean value) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
nativeSetNSWindowStyleBits(ptr, mask, value ? mask : 0);
}
});
}
private native void _toggleFullScreenMode(final long model);
public void toggleFullScreen() {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
_toggleFullScreenMode(ptr);
}
});
}
@Override
public void (MenuBar mb) {
final CMenuBar mbPeer = (CMenuBar)LWToolkit.targetToPeer(mb);
execute(new CFNativeAction() {
@Override
public void run(final long nsWindowPtr) {
if (mbPeer != null) {
mbPeer.execute(new CFNativeAction() {
@Override
public void run(long ptr) {
nativeSetNSWindowMenuBar(nsWindowPtr, ptr);
}
});
} else {
nativeSetNSWindowMenuBar(nsWindowPtr, 0);
}
}
});
}
@Override
public void dispose() {
contentView.dispose();
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CPlatformWindow.nativeDispose(ptr);
}
});
CPlatformWindow.super.dispose();
}
@Override
public FontMetrics getFontMetrics(Font f) {
(new RuntimeException("unimplemented")).printStackTrace();
return null;
}
@Override
public Insets getInsets() {
final AtomicReference<Insets> ref = new AtomicReference<>();
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
ref.set(nativeGetNSWindowInsets(ptr));
}
});
return ref.get() != null ? ref.get() : new Insets(0, 0, 0, 0);
}
@Override
public Point getLocationOnScreen() {
return new Point(nativeBounds.x, nativeBounds.y);
}
@Override
public GraphicsDevice getGraphicsDevice() {
return contentView.getGraphicsDevice();
}
@Override
public SurfaceData getScreenSurface() {
return null;
}
@Override
public SurfaceData replaceSurfaceData() {
return contentView.replaceSurfaceData();
}
@Override
public void setBounds(final int x, final int y, final int w, final int h) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
nativeSetNSWindowBounds(ptr, x, y, w, h);
}
});
}
public boolean isVisible() {
return this.visible;
}
private boolean isMaximized() {
if (undecorated) {
return this.normalBounds != null;
}
final AtomicBoolean ref = new AtomicBoolean();
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
ref.set(CWrapper.NSWindow.isZoomed(ptr));
}
});
return ref.get();
}
private void maximize() {
if (peer == null || isMaximized()) {
return;
}
if (!undecorated) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.zoom(ptr);
}
});
} else {
deliverZoom(true);
this.normalBounds = peer.getBounds();
GraphicsConfiguration config = getPeer().getGraphicsConfiguration();
Insets i = ((CGraphicsDevice)config.getDevice()).getScreenInsets();
Rectangle toBounds = config.getBounds();
setBounds(toBounds.x + i.left,
toBounds.y + i.top,
toBounds.width - i.left - i.right,
toBounds.height - i.top - i.bottom);
}
}
private void unmaximize() {
if (!isMaximized()) {
return;
}
if (!undecorated) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.zoom(ptr);
}
});
} else {
deliverZoom(false);
Rectangle toBounds = this.normalBounds;
this.normalBounds = null;
setBounds(toBounds.x, toBounds.y, toBounds.width, toBounds.height);
}
}
@Override
public void setVisible(boolean visible) {
updateIconImages();
updateFocusabilityForAutoRequestFocus(false);
LWWindowPeer blocker = (peer == null)? null : peer.getBlocker();
if (blocker == null || !visible) {
if (visible) {
contentView.execute(new CFNativeAction() {
@Override
public void run(final long viewPtr) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.makeFirstResponder(ptr,
viewPtr);
}
});
}
});
final boolean isPopup = (target.getType() == Window.Type.POPUP);
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
if (isPopup) {
CWrapper.NSWindow.orderFrontRegardless(ptr);
} else {
CWrapper.NSWindow.orderFront(ptr);
}
boolean isKeyWindow = CWrapper.NSWindow.isKeyWindow(ptr);
if (!isKeyWindow) {
CWrapper.NSWindow.makeKeyWindow(ptr);
}
}
});
} else {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.orderOut(ptr);
CWrapper.NSWindow.close(ptr);
}
});
}
} else {
CPlatformWindow bw
= (CPlatformWindow) blocker.getPlatformWindow();
bw.execute(new CFNativeAction() {
@Override
public void run(final long blockerPtr) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.orderWindow(ptr,
CWrapper.NSWindow.NSWindowBelow,
blockerPtr);
}
});
}
});
}
this.visible = visible;
if (visible) {
if (target instanceof Frame) {
int frameState = ((Frame)target).getExtendedState();
if ((frameState & Frame.ICONIFIED) != 0) {
frameState = Frame.ICONIFIED;
}
switch (frameState) {
case Frame.ICONIFIED:
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.miniaturize(ptr);
}
});
break;
case Frame.MAXIMIZED_BOTH:
maximize();
break;
default:
unmaximize();
break;
}
}
}
nativeSynthesizeMouseEnteredExitedEvents();
updateFocusabilityForAutoRequestFocus(true);
if (visible) {
if (owner != null && owner.isVisible()) {
owner.execute(new CFNativeAction() {
@Override
public void run(final long ownerPtr) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.orderWindow(ptr, CWrapper.NSWindow.NSWindowAbove, ownerPtr);
}
});
}
});
applyWindowLevel(target);
}
for (Window w : target.getOwnedWindows()) {
WindowPeer p = (WindowPeer)w.getPeer();
if (p instanceof LWWindowPeer) {
CPlatformWindow pw = (CPlatformWindow)((LWWindowPeer)p).getPlatformWindow();
if (pw != null && pw.isVisible()) {
pw.execute(new CFNativeAction() {
@Override
public void run(final long childPtr) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.orderWindow(childPtr, CWrapper.NSWindow.NSWindowAbove, ptr);
}
});
}
});
pw.applyWindowLevel(w);
}
}
}
}
if (blocker != null && visible) {
((CPlatformWindow)blocker.getPlatformWindow()).orderAboveSiblings();
}
}
@Override
public void setTitle(final String title) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
nativeSetNSWindowTitle(ptr, title);
}
});
}
@Override
public void updateIconImages() {
final CImage cImage = getImageForTarget();
execute(new CFNativeAction() {
@Override
public void run(final long ptr) {
if (cImage == null) {
nativeSetNSWindowMinimizedIcon(ptr, 0L);
} else {
cImage.execute(new CFNativeAction() {
@Override
public void run(long imagePtr) {
nativeSetNSWindowMinimizedIcon(ptr, imagePtr);
}
});
}
}
});
}
public SurfaceData getSurfaceData() {
return contentView.getSurfaceData();
}
@Override
public void toBack() {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CPlatformWindow.nativePushNSWindowToBack(ptr);
}
});
}
@Override
public void toFront() {
LWCToolkit lwcToolkit = (LWCToolkit) Toolkit.getDefaultToolkit();
Window w = DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
if( w != null && w.getPeer() != null
&& ((LWWindowPeer)w.getPeer()).getPeerType() == LWWindowPeer.PeerType.EMBEDDED_FRAME
&& !lwcToolkit.isApplicationActive()) {
lwcToolkit.activateApplicationIgnoringOtherApps();
}
updateFocusabilityForAutoRequestFocus(false);
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CPlatformWindow.nativePushNSWindowToFront(ptr);
}
});
updateFocusabilityForAutoRequestFocus(true);
}
@Override
public void setResizable(boolean resizable) {
if (peer == null) {
return;
}
setStyleBits(RESIZABLE, resizable);
peer.updateMinimumSize();
Rectangle bounds = peer.getBounds();
setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
}
@Override
public void setSizeConstraints(final int minW, final int minH, final int maxW, final int maxH) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
nativeSetNSWindowMinMax(ptr, minW, minH, maxW, maxH);
}
});
}
@Override
public boolean rejectFocusRequest(CausedFocusEvent.Cause cause) {
if (cause != CausedFocusEvent.Cause.MOUSE_EVENT &&
!((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive())
{
focusLogger.fine("the app is inactive, so the request is rejected");
return true;
}
return false;
}
@Override
public boolean requestWindowFocus() {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
if (CWrapper.NSWindow.canBecomeMainWindow(ptr)) {
CWrapper.NSWindow.makeMainWindow(ptr);
}
CWrapper.NSWindow.makeKeyAndOrderFront(ptr);
}
});
return true;
}
@Override
public boolean isActive() {
final AtomicBoolean ref = new AtomicBoolean();
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
ref.set(CWrapper.NSWindow.isKeyWindow(ptr));
}
});
return ref.get();
}
@Override
public void updateFocusableWindowState() {
final boolean isFocusable = isNativelyFocusableWindow();
setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable);
}
@Override
public Graphics transformGraphics(Graphics g) {
return g;
}
@Override
public void setAlwaysOnTop(boolean isAlwaysOnTop) {
setStyleBits(ALWAYS_ON_TOP, isAlwaysOnTop);
}
@Override
public void setOpacity(final float opacity) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.setAlphaValue(ptr, opacity);
}
});
}
@Override
public void setOpaque(final boolean isOpaque) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.setOpaque(ptr, isOpaque);
}
});
boolean isTextured = (peer == null) ? false : peer.isTextured();
if (!isTextured) {
if (!isOpaque) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.setBackgroundColor(ptr, 0);
}
});
} else if (peer != null) {
Color color = peer.getBackground();
if (color != null) {
final int rgb = color.getRGB();
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.setBackgroundColor(ptr, rgb);
}
});
}
}
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
invalidateShadow();
}
});
}
@Override
public void enterFullScreenMode() {
isFullScreenMode = true;
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CPlatformWindow.nativeEnterFullScreenMode(ptr);
}
});
}
@Override
public void exitFullScreenMode() {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CPlatformWindow.nativeExitFullScreenMode(ptr);
}
});
isFullScreenMode = false;
}
@Override
public boolean isFullScreenMode() {
return isFullScreenMode;
}
@Override
public void setWindowState(int windowState) {
if (peer == null || !peer.isVisible()) {
return;
}
int prevWindowState = peer.getState();
if (prevWindowState == windowState) return;
if ((windowState & Frame.ICONIFIED) != 0) {
windowState = Frame.ICONIFIED;
}
switch (windowState) {
case Frame.ICONIFIED:
if (prevWindowState == Frame.MAXIMIZED_BOTH) {
unmaximize();
}
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.miniaturize(ptr);
}
});
break;
case Frame.MAXIMIZED_BOTH:
if (prevWindowState == Frame.ICONIFIED) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.deminiaturize(ptr);
}
});
}
maximize();
break;
case Frame.NORMAL:
if (prevWindowState == Frame.ICONIFIED) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.deminiaturize(ptr);
}
});
} else if (prevWindowState == Frame.MAXIMIZED_BOTH) {
unmaximize();
}
break;
default:
throw new RuntimeException("Unknown window state: " + windowState);
}
nativeSynthesizeMouseEnteredExitedEvents();
}
@Override
public void setModalBlocked(final boolean blocked) {
if (target.getModalExclusionType() == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) {
return;
}
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
nativeSetEnabled(ptr, !blocked);
}
});
}
public final void invalidateShadow(){
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
nativeRevalidateNSWindowShadow(ptr);
}
});
}
private CImage getImageForTarget() {
List<Image> icons = target.getIconImages();
if (icons == null || icons.size() == 0) {
return null;
}
return CImage.getCreator().createFromImages(icons);
}
@Override
public LWWindowPeer getPeer() {
return peer;
}
@Override
public boolean isUnderMouse() {
return contentView.isUnderMouse();
}
public CPlatformView getContentView() {
return contentView;
}
@Override
public long getLayerPtr() {
return contentView.getWindowLayerPtr();
}
private void validateSurface() {
SurfaceData surfaceData = getSurfaceData();
if (surfaceData instanceof CGLSurfaceData) {
((CGLSurfaceData)surfaceData).validate();
}
}
void flushBuffers() {
if (isVisible() && !nativeBounds.isEmpty() && !isFullScreenMode) {
try {
LWCToolkit.invokeAndWait(new Runnable() {
@Override
public void run() {
}
}, target);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
static long getNativeViewPtr(PlatformWindow platformWindow) {
long nativePeer = 0L;
if (platformWindow instanceof CPlatformWindow) {
nativePeer = ((CPlatformWindow) platformWindow).getContentView().getAWTView();
} else if (platformWindow instanceof CViewPlatformEmbeddedFrame){
nativePeer = ((CViewPlatformEmbeddedFrame) platformWindow).getNSViewPtr();
}
return nativePeer;
}
private void deliverWindowFocusEvent(boolean gained, CPlatformWindow opposite){
if (gained && !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) {
focusLogger.fine("the app is inactive, so the notification is ignored");
return;
}
LWWindowPeer oppositePeer = (opposite == null)? null : opposite.getPeer();
responder.handleWindowFocusEvent(gained, oppositePeer);
}
protected void deliverMoveResizeEvent(int x, int y, int width, int height,
boolean byUser) {
final Rectangle oldB = nativeBounds;
nativeBounds = new Rectangle(x, y, width, height);
if (peer != null) {
peer.notifyReshape(x, y, width, height);
if ((byUser && !oldB.getSize().equals(nativeBounds.getSize()))
|| isFullScreenAnimationOn) {
flushBuffers();
}
}
}
private void deliverWindowClosingEvent() {
if (peer != null && peer.getBlocker() == null) {
peer.postEvent(new WindowEvent(target, WindowEvent.WINDOW_CLOSING));
}
}
private void deliverIconify(final boolean iconify) {
if (peer != null) {
peer.notifyIconify(iconify);
}
if (iconify) {
isIconifyAnimationActive = false;
}
}
private void deliverZoom(final boolean isZoomed) {
if (peer != null) {
peer.notifyZoom(isZoomed);
}
}
private void deliverNCMouseDown() {
if (peer != null) {
peer.notifyNCMouseDown();
}
}
private boolean isNativelyFocusableWindow() {
if (peer == null) {
return false;
}
return !peer.isSimpleWindow() && target.getFocusableWindowState();
}
private void updateFocusabilityForAutoRequestFocus(boolean isFocusable) {
if (target.isAutoRequestFocus() || !isNativelyFocusableWindow()) return;
setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable);
}
private boolean checkBlocking() {
LWWindowPeer blocker = (peer == null)? null : peer.getBlocker();
if (blocker == null) {
return false;
}
if (blocker instanceof CPrinterDialogPeer) {
return true;
}
CPlatformWindow pWindow = (CPlatformWindow)blocker.getPlatformWindow();
pWindow.orderAboveSiblings();
pWindow.execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.orderFrontRegardless(ptr);
CWrapper.NSWindow.makeKeyAndOrderFront(ptr);
CWrapper.NSWindow.makeMainWindow(ptr);
}
});
return true;
}
private boolean isIconified() {
boolean isIconified = false;
if (target instanceof Frame) {
int state = ((Frame)target).getExtendedState();
if ((state & Frame.ICONIFIED) != 0) {
isIconified = true;
}
}
return isIconifyAnimationActive || isIconified;
}
private boolean isOneOfOwnersOrSelf(CPlatformWindow window) {
while (window != null) {
if (this == window) {
return true;
}
window = window.owner;
}
return false;
}
private CPlatformWindow getRootOwner() {
CPlatformWindow rootOwner = this;
while (rootOwner.owner != null) {
rootOwner = rootOwner.owner;
}
return rootOwner;
}
private void orderAboveSiblings() {
CPlatformWindow rootOwner = getRootOwner();
if (rootOwner.isVisible() && !rootOwner.isIconified()) {
rootOwner.execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.orderFront(ptr);
}
});
}
if (!rootOwner.isIconified()) {
final WindowAccessor windowAccessor = AWTAccessor.getWindowAccessor();
orderAboveSiblingsImpl(windowAccessor.getOwnedWindows(rootOwner.target));
}
}
private void orderAboveSiblingsImpl(Window[] windows) {
ArrayList<Window> childWindows = new ArrayList<Window>();
final ComponentAccessor componentAccessor = AWTAccessor.getComponentAccessor();
final WindowAccessor windowAccessor = AWTAccessor.getWindowAccessor();
for (Window w : windows) {
boolean iconified = false;
final Object p = componentAccessor.getPeer(w);
if (p instanceof LWWindowPeer) {
final CPlatformWindow pw = (CPlatformWindow)((LWWindowPeer)p).getPlatformWindow();
iconified = isIconified();
if (pw != null && pw.isVisible() && !iconified) {
if (pw.isOneOfOwnersOrSelf(this)) {
pw.execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.orderFront(ptr);
}
});
} else {
pw.owner.execute(new CFNativeAction() {
@Override
public void run(final long ownerPtr) {
pw.execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.orderWindow(ptr, CWrapper.NSWindow.NSWindowAbove, ownerPtr);
}
});
}
});
}
pw.applyWindowLevel(w);
}
}
if (!iconified) {
childWindows.addAll(Arrays.asList(windowAccessor.getOwnedWindows(w)));
}
}
if (!childWindows.isEmpty()) {
orderAboveSiblingsImpl(childWindows.toArray(new Window[0]));
}
}
protected void applyWindowLevel(Window target) {
if (target.isAlwaysOnTop() && target.getType() != Window.Type.POPUP) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.setLevel(ptr, CWrapper.NSWindow.NSFloatingWindowLevel);
}
});
} else if (target.getType() == Window.Type.POPUP) {
execute(new CFNativeAction() {
@Override
public void run(long ptr) {
CWrapper.NSWindow.setLevel(ptr, CWrapper.NSWindow.NSPopUpMenuWindowLevel);
}
});
}
}
private void windowWillMiniaturize() {
isIconifyAnimationActive = true;
}
private void windowDidBecomeMain() {
assert CThreading.assertAppKit();
if (checkBlocking()) return;
orderAboveSiblings();
}
private void windowWillEnterFullScreen() {
isFullScreenAnimationOn = true;
}
private void windowDidEnterFullScreen() {
isFullScreenAnimationOn = false;
}
private void windowWillExitFullScreen() {
isFullScreenAnimationOn = true;
}
private void windowDidExitFullScreen() {
isFullScreenAnimationOn = false;
}
}