Created
October 19, 2018 14:05
-
-
Save RChehowski/2d6b7a4be35747cd863ab10edd02cb55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vizor.unreal.platform.mac; | |
import com.vizor.unreal.platform.FrameDecorator; | |
import com.vizor.unreal.util.MiscUtils; | |
import com.vizor.unreal.util.StringUtils; | |
import com.vizor.unreal.util.logger.LogManager; | |
import com.vizor.unreal.util.logger.Logger; | |
import javax.swing.ImageIcon; | |
import java.awt.Image; | |
import java.lang.invoke.MethodHandle; | |
import java.lang.reflect.Method; | |
import java.net.URL; | |
import java.util.HashMap; | |
import java.util.Map; | |
import static com.vizor.unreal.util.StringUtils.getPrettyClassName; | |
import static java.lang.invoke.MethodHandles.lookup; | |
import static java.lang.invoke.MethodType.methodType; | |
public class MacFrameDecorator extends FrameDecorator | |
{ | |
private static final Logger log = LogManager.getLogger(MacFrameDecorator.class); | |
// Methods: | |
private MethodHandle setDockIconImage = null; | |
private MethodHandle requestForeground = null; | |
private MethodHandle requestUserAttention = null; | |
private Map<String, MethodHandle> mh = new HashMap<>(); | |
public MacFrameDecorator() | |
{ | |
try { | |
// Root application class. Note that all AppKit classes are non-portable. | |
final Class<?> applicationClass = Class.forName("com.apple.eawt.Application"); | |
Object application = applicationClass.getMethod("getApplication").invoke(null); | |
// setDockIconImage(java.awt.Image) | |
mh.put("setDockIconImage", lookup().findVirtual(applicationClass, "setDockIconImage", | |
methodType(void.class, Image.class)).bindTo(application)); | |
// requestForeground(boolean) | |
mh.put("requestForeground", lookup().findVirtual(applicationClass, "requestForeground", | |
methodType(void.class, boolean.class)).bindTo(application)); | |
// requestUserAttention(boolean) | |
mh.put("requestUserAttention", lookup().findVirtual(applicationClass, "requestUserAttention", | |
methodType(void.class, boolean.class)).bindTo(application)); | |
} | |
catch (Throwable t) { | |
log.error("Caught exception: \"{}\": {}", getPrettyClassName(t.getClass()), | |
t.getMessage()); | |
} | |
} | |
// Implementation | |
@Override | |
protected void setApplicationIconImpl(URL iconUrl) | |
{ | |
if (setDockIconImage != null) | |
{ | |
final Image image = new ImageIcon(iconUrl).getImage(); | |
try { | |
mh.get("setDockIconImage").invoke(image); | |
} | |
catch (Throwable t) { | |
log.catching(t); | |
} | |
} | |
else | |
{ | |
log.error("{} was unable to call, method not found \"{}()\"", | |
getPrettyClassName(getClass()), "setDockIconImage"); | |
} | |
} | |
@Override | |
protected void requestForegroundImpl() | |
{ | |
if (requestForeground != null) | |
{ | |
try { | |
requestForeground.invoke(true); | |
} | |
catch (Throwable t) { | |
log.catching(t); | |
} | |
} | |
else | |
{ | |
log.error("{} was unable to call, method not found \"{}()\"", | |
getPrettyClassName(getClass()), "requestForeground"); | |
} | |
} | |
@Override | |
protected void requestUserAttentionImpl() | |
{ | |
if (requestUserAttention != null) | |
{ | |
try { | |
requestUserAttention.invoke(true); | |
} | |
catch (Throwable t) { | |
log.catching(t); | |
} | |
} | |
else | |
{ | |
log.error("{} was unable to call, method not found \"{}()\"", | |
getPrettyClassName(getClass()), "requestUserAttention"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment