Skip to content

Commit

Permalink
Detect availability of Monocle before use
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Apr 28, 2021
1 parent f12bfc0 commit 6b39f81
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ant/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ javafx.x86_64.arch=x64
javafx.x86_64.separator=-

# JavaFX ARM64
javafx.aarch64.version=17-ea+6
javafx.aarch64.version=17-ea+8
javafx.aarch64.mirror=https://download2.gluonhq.com/openjfx/17
javafx.aarch64.arch=aarch64
javafx.aarch64.separator=_
Expand Down
5 changes: 5 additions & 0 deletions src/qz/common/TrayManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ private void addMenuItems() {
monocleItem.setToolTipText("Use monocle platform for HTML printing (restart required)");
monocleItem.setMnemonic(KeyEvent.VK_U);
monocleItem.setState(prefs.getBoolean(Constants.PREFS_MONOCLE, true));
if(!SystemUtilities.hasMonocle()) {
log.warn("Monocle engine was not detected");
monocleItem.setEnabled(false);
monocleItem.setToolTipText("Monocle HTML engine was not detected");
}
monocleItem.addActionListener(monocleListener);

if (Constants.JAVA_VERSION.greaterThanOrEqualTo(Version.valueOf("11.0.0"))) { //only include if it can be used
Expand Down
4 changes: 3 additions & 1 deletion src/qz/printer/action/WebApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static synchronized void initialize() throws IOException {
// Fallback for JDK11+
headless = true;
}
if (useMonocle) {
if (useMonocle && SystemUtilities.hasMonocle()) {
log.trace("Initializing monocle platform");
System.setProperty("javafx.platform", "monocle");
// Don't set glass.platform on Linux per https://github.com/qzind/tray/issues/702
Expand All @@ -188,6 +188,8 @@ public static synchronized void initialize() throws IOException {
if (headless) {
System.setProperty("prism.order", "sw");
}
} else {
log.warn("Monocle platform will not be used");
}
}

Expand Down
24 changes: 16 additions & 8 deletions src/qz/ui/tray/AWTMenuWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
* Wraps a Swing JMenuItem or JSeparator into an AWT item including text, shortcuts and action listeners.
Expand All @@ -28,21 +26,25 @@ public class AWTMenuWrapper {
private AWTMenuWrapper(JCheckBoxMenuItem item) {
this.item = new CheckboxMenuItem(item.getText());
wrapState(item);
wrapEnabled(item);
wrapShortcut(item);
wrapItemListeners(item);
}

private AWTMenuWrapper(JMenuItem item) {
this.item = new MenuItem(item.getText());
wrapEnabled(item);
wrapShortcut(item);
wrapActionListeners(item);
}

private AWTMenuWrapper(JMenu menu) {
this.item = new Menu(menu.getText());
wrapEnabled(menu);
wrapShortcut(menu);
}

@SuppressWarnings("unused")
private AWTMenuWrapper(JSeparator ignore) {
this.item = new MenuItem("-");
}
Expand All @@ -64,12 +66,9 @@ private void wrapActionListeners(JMenuItem item) {
// Special case for CheckboxMenuItem
private void wrapItemListeners(final JMenuItem item) {
for (final ActionListener l : item.getActionListeners()) {
((CheckboxMenuItem)this.item).addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
((JCheckBoxMenuItem)item).setState(((CheckboxMenuItem)AWTMenuWrapper.this.item).getState());
l.actionPerformed(new ActionEvent(item, e.getID(), item.getActionCommand()));
}
((CheckboxMenuItem)this.item).addItemListener(e -> {
((JCheckBoxMenuItem)item).setState(((CheckboxMenuItem)AWTMenuWrapper.this.item).getState());
l.actionPerformed(new ActionEvent(item, e.getID(), item.getActionCommand()));
});
}
}
Expand All @@ -80,6 +79,15 @@ private void wrapState(JMenuItem item) {
}
}

private void wrapEnabled(JMenuItem item) {
// Match initial state
this.item.setEnabled(item.isEnabled());
// Monitor future state
item.addPropertyChangeListener("enabled", evt -> {
this.item.setEnabled((Boolean)evt.getNewValue());
});
}

public static MenuItem wrap(Component c) {
if (c instanceof JCheckBoxMenuItem) {
return new AWTMenuWrapper((JCheckBoxMenuItem)c).getMenuItem();
Expand Down
13 changes: 13 additions & 0 deletions src/qz/utils/SystemUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class SystemUtilities {

private static Boolean darkDesktop;
private static Boolean darkTaskbar;
private static Boolean hasMonocle;
private static String uname;
private static String linuxRelease;
private static String classProtocol;
Expand Down Expand Up @@ -494,4 +495,16 @@ public static boolean isJDK() {
}
return false;
}

public static boolean hasMonocle() {
if(hasMonocle == null) {
try {
Class.forName("com.sun.glass.ui.monocle.MonoclePlatformFactory");
hasMonocle = true;
} catch (ClassNotFoundException e) {
hasMonocle = false;
}
}
return hasMonocle;
}
}

0 comments on commit 6b39f81

Please sign in to comment.