Skip to content

Commit

Permalink
Change global hotkeys handling
Browse files Browse the repository at this point in the history
- Change to be always enabled on Windows
- Only output warning about missing jintellitype library when a global hotkey is added
  • Loading branch information
tduva committed Aug 16, 2016
1 parent e1fab93 commit 5aefd9b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/chatty/Chatty.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Chatty {
/**
* Enables the hotkey feature for running commercials (windows only).
*/
public static final boolean HOTKEY = false;
public static final boolean HOTKEY = MiscUtil.OS_WINDOWS;

/**
* The Chatty website as it can be opened in the menu.
Expand Down
28 changes: 24 additions & 4 deletions src/chatty/util/hotkeys/GlobalHotkeySetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public class GlobalHotkeySetter implements HotkeyListener {
private static final Logger LOGGER = Logger.getLogger(GlobalHotkeySetter.class.getName());

private boolean initialized = false;

/**
* Error that occured when initializing (usually the correct dll not found).
*/
private String error;

private final GlobalHotkeyListener listener;

/**
Expand All @@ -46,17 +52,31 @@ public final void initialize() {
JIntellitype.getInstance().addHotKeyListener(this);
initialized = true;
} catch (JIntellitypeException ex) {
String info = "Failed adding HotKeyListener: "+ex.getLocalizedMessage();
LOGGER.log(Logging.USERINFO, info+" (if you don't use global "
+ "hotkeys you can just ignore this)");
LOGGER.warning(info);
error = "Failed adding global hotkeys listener: "+ex.getLocalizedMessage();
LOGGER.warning(error);
}
}

/**
* Whether the listener has been added without an error. If false, then an
* error occured which can be retrieved with the
* {@link #getError() getError} method.
*
* @return true if global hotkeys can be added, false if an error occured
*/
public boolean isInitalized() {
return initialized;
}

/**
* Returns an error message if an error occured while initializing.
*
* @return The error message, or null if no error occured
*/
public String getError() {
return error;
}

/**
* Called when global hotkey is pressed. This is run in the EDT.
*
Expand Down
33 changes: 30 additions & 3 deletions src/chatty/util/hotkeys/HotkeyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public class HotkeyManager {

private GlobalHotkeySetter globalHotkeys;

/**
* Warning text intended to be output to the user, about an error of the
* global hotkey feature. Should be set to null if warning was output, so
* it's only shown once.
*/
private String globalHotkeyErrorWarning;

public HotkeyManager(MainGui main) {
this.main = main;

Expand All @@ -87,13 +94,12 @@ public void onHotkey(Object hotkeyId) {
// If an error occured during initialization, then set to null
// which means it's not going to be used.
if (!globalHotkeys.isInitalized()) {
globalHotkeyErrorWarning = globalHotkeys.getError();
globalHotkeys = null;
}
} catch (NoClassDefFoundError ex) {
LOGGER.warning("Failed to initialize hotkey setter [" + ex + "]");
LOGGER.log(Logging.USERINFO, "Failed to initialize global "
+ "hotkeys (if you don't use global hotkeys you can "
+ "just ignore this).");
globalHotkeyErrorWarning = "Failed to initialize global hotkeys (jintellitype-xx.jar not found).";
globalHotkeys = null;
}
}
Expand Down Expand Up @@ -400,6 +406,7 @@ public synchronized void loadFromSettings(Settings settings) {
}
}
updateHotkeys();
checkGlobalHotkeyWarning();
}

/**
Expand Down Expand Up @@ -453,6 +460,26 @@ private Hotkey listToHotkey(List list) {
}
}

/**
* Output warning of error when initializing global hotkey feature. Only
* output once and only when a global hotkey is currently configured.
*/
private void checkGlobalHotkeyWarning() {
if (globalHotkeyErrorWarning == null) {
return;
}
for (Hotkey hotkey : hotkeys) {
if (doesHotkeyHaveAction(hotkey) && hotkey.type == Type.GLOBAL) {
LOGGER.log(Logging.USERINFO, globalHotkeyErrorWarning+" "
+ "[You are getting this message because you have a "
+ "global hotkey configured. If you don't use it you "
+ "can ignore this warning.]");
globalHotkeyErrorWarning = null;
return;
}
}
}

/**
* Creates a bridge Action which calls the actual registered action for this
* hotkey, adding some more information.
Expand Down

0 comments on commit 5aefd9b

Please sign in to comment.