-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted native Windows logic and fixed #194
- Loading branch information
1 parent
4f121a1
commit d0d6789
Showing
3 changed files
with
74 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
70 changes: 70 additions & 0 deletions
70
src/main/java/net/sf/jabref/gui/native_ext/WindowsExtensions.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package net.sf.jabref.gui.native_ext; | ||
|
||
import com.sun.jna.Native; | ||
import com.sun.jna.NativeLong; | ||
import com.sun.jna.Pointer; | ||
import com.sun.jna.WString; | ||
import com.sun.jna.ptr.PointerByReference; | ||
import net.sf.jabref.Globals; | ||
import net.sf.jabref.logic.util.OS; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
/** | ||
* Native extensions for Windows. | ||
*/ | ||
public class WindowsExtensions { | ||
private static final Log LOGGER = LogFactory.getLog(WindowsExtensions.class); | ||
|
||
// Register native calls for pin to taskbar functionality | ||
static { | ||
if (OS.WINDOWS) { | ||
LOGGER.info("Registered Shell32 DLL"); | ||
Native.register("shell32"); | ||
} | ||
} | ||
|
||
public static void enablePinToTaskbar() { | ||
// Set application user model id so that pinning JabRef to the Win7/8 taskbar works | ||
// Based on http://stackoverflow.com/a/1928830 | ||
// Only do this if Windows is capable >= Windows 7 | ||
if(supportsPinToTaskbar()) { | ||
setCurrentProcessExplicitAppUserModelID("JabRef." + Globals.BUILD_INFO.getVersion()); | ||
} | ||
} | ||
|
||
// Do not use this code in release version, it contains some memory leaks | ||
private static String getCurrentProcessExplicitAppUserModelID() { | ||
final PointerByReference r = new PointerByReference(); | ||
|
||
if (GetCurrentProcessExplicitAppUserModelID(r).longValue() == 0) { | ||
final Pointer p = r.getValue(); | ||
return p.getString(0, true); // here we leak native memory by lazyness | ||
} | ||
return "N/A"; | ||
} | ||
|
||
private static void setCurrentProcessExplicitAppUserModelID(final String appID) { | ||
if (SetCurrentProcessExplicitAppUserModelID(new WString(appID)).longValue() != 0) { | ||
throw new RuntimeException("unable to set current process explicit AppUserModelID to: " + appID); | ||
} | ||
} | ||
|
||
private static native NativeLong GetCurrentProcessExplicitAppUserModelID(PointerByReference appID); | ||
|
||
private static native NativeLong SetCurrentProcessExplicitAppUserModelID(WString appID); | ||
|
||
private static boolean supportsPinToTaskbar() { | ||
if (!OS.WINDOWS) { | ||
return false; | ||
} | ||
|
||
try { | ||
Float version = Float.parseFloat(System.getProperty("os.version")); | ||
// Windows 7 == 6.1 | ||
return version >= 6.1; | ||
} catch (NumberFormatException ex) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains 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