DTG: On OSX, set dock icon, and enable by default

This commit is contained in:
zzz
2017-11-19 00:40:50 +00:00
parent 0ba207cb01
commit 85bb495754
3 changed files with 54 additions and 8 deletions

View File

@ -4,6 +4,12 @@ package net.i2p.desktopgui;
* Main.java
*/
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import javax.swing.SwingUtilities;
import net.i2p.I2PAppContext;
@ -24,7 +30,9 @@ import net.i2p.util.I2PProperties.I2PPropertyCallback;
*/
public class Main implements RouterApp {
// non-null
private final I2PAppContext _appContext;
// warning, null in app context
private final RouterContext _context;
private final ClientAppManager _mgr;
private final Log log;
@ -106,6 +114,8 @@ public class Main implements RouterApp {
changeState(START_FAILED, "Headless environment: not starting desktopgui!", null);
return;
}
if (SystemVersion.isMac())
setMacTrayIcon();
// TODO process args with getopt if needed
@ -129,6 +139,45 @@ public class Main implements RouterApp {
}
/**
* Unless we do this, when we start DesktopGUI we get a Java coffee cup
* in the tray.
*
* Based on code from https://gist.github.com/bchapuis/1562406 , no apparent license.
* See also https://stackoverflow.com/questions/6006173/how-do-you-change-the-dock-icon-of-a-java-program
*
* TODO, if we wanted to add our own menu, see
* https://stackoverflow.com/questions/1319805/java-os-x-dock-menu
*
* TODO, if we want to make it bounce, see
* https://stackoverflow.com/questions/15079783/how-to-make-my-app-icon-bounce-in-the-mac-dock
*
* TODO, if we want to handle Quit, see
* https://nakkaya.com/2009/04/19/java-osx-integration/
*
* @since 0.9.33
*/
@SuppressWarnings("unchecked")
private void setMacTrayIcon() {
File f = new File(_appContext.getBaseDir(), "docs/themes/console/images/itoopie_sm.png");
if (!f.exists())
return;
try {
Class util = Class.forName("com.apple.eawt.Application");
Method getApplication = util.getMethod("getApplication", new Class[0]);
Object application = getApplication.invoke(util);
Class params[] = new Class[1];
params[0] = Image.class;
Method setDockIconImage = util.getMethod("setDockIconImage", params);
URL url = f.toURI().toURL();
Image image = Toolkit.getDefaultToolkit().getImage(url);
setDockIconImage.invoke(application, image);
} catch (Exception e) {
if (log.shouldWarn())
log.warn("Can't set OSX Dock icon", e);
}
}
/**
* Avoids the app terminating because no Window is opened anymore.
* More info: http://java.sun.com/javase/6/docs/api/java/awt/doc-files/AWTThreadIssues.html#Autoshutdown

View File

@ -202,10 +202,7 @@ public class ConfigServiceHandler extends FormHandler {
public boolean shouldShowSystray() {
return !
(SystemVersion.isLinuxService() ||
(SystemVersion.isWindows() && _context.hasWrapper() && WrapperManager.isLaunchedAsService()) ||
// headless=true is forced in i2prouter script to prevent useless dock icon;
// must fix this first
SystemVersion.isMac());
(SystemVersion.isWindows() && _context.hasWrapper() && WrapperManager.isLaunchedAsService()));
}
/**
@ -214,10 +211,10 @@ public class ConfigServiceHandler extends FormHandler {
* @since 0.9.26
*/
public boolean isSystrayEnabled() {
// default false for now, except on non-service windows
// default false for now, except on OSX and non-service windows
String sdtg = _context.getProperty(RouterConsoleRunner.PROP_DTG_ENABLED);
return Boolean.parseBoolean(sdtg) ||
(sdtg == null && SystemVersion.isWindows());
(sdtg == null && (SystemVersion.isWindows() || SystemVersion.isMac()));
}
@Override

View File

@ -298,10 +298,10 @@ public class RouterConsoleRunner implements RouterApp {
return;
}
try {
// default false for now, except on non-service windows
// default false for now, except on OSX and non-service windows
String sdtg = _context.getProperty(PROP_DTG_ENABLED);
boolean desktopguiEnabled = Boolean.parseBoolean(sdtg) ||
(sdtg == null && SystemVersion.isWindows());
(sdtg == null && (SystemVersion.isWindows() || SystemVersion.isMac()));
if (desktopguiEnabled) {
System.setProperty("java.awt.headless", "false");
net.i2p.desktopgui.Main dtg = new net.i2p.desktopgui.Main(_context, _mgr, null);