Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow GUI to be shown from API #1044

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions js/qz-tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -2653,6 +2653,43 @@ var qz = (function() {
}
},

/**
* @namespace qz.gui
*/
gui: {
/**
* Show the the actions/options menu
* @memberof qz.gui
*/
showMenu: function(event) {
return _qz.websocket.dataPromise('gui.showMenu');
},

/**
* Show the About dialog
* @memberof qz.gui
*/
showAbout: function() {
return _qz.websocket.dataPromise('gui.showAbout');
},

/**
* Show the Site Manager dialog
* @memberof qz.gui
*/
showSites: function() {
return _qz.websocket.dataPromise('gui.showSites');
},

/**
* Show the Log dialog
* @memberof qz.gui
*/
showLog: function() {
return _qz.websocket.dataPromise('gui.showLog');
}
},

/**
* Calls related to compatibility adjustments
* @namespace qz.api
Expand Down
5 changes: 5 additions & 0 deletions sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ <h1 id="title" class="page-header">QZ Tray v<span id="qz-version">0</span></h1>
<div class="col-md-4">
<div id="qz-connection" class="panel panel-default">
<div class="panel-heading">
<button class="close tip" data-toggle="tooltip" title="QZ Tray Menu" id="menu" href="#" onclick="qz.gui.showMenu();" style="display: none;">
<i class="fa fa-bars"></i>
</button>
<button class="close tip" data-toggle="tooltip" title="Launch QZ" id="launch" href="#" onclick="launchQZ();" style="display: none;">
<i class="fa fa-external-link"></i>
</button>
Expand Down Expand Up @@ -3017,8 +3020,10 @@ <h4 class="panel-title">Options</h4>

if (text === "Inactive" || text === "Error") {
$("#launch").show();
$("#menu").hide();
} else {
$("#launch").hide();
$("#menu").show();
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/qz/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public class Constants {
public static final Color TRUSTED_COLOR_LITE = Color.BLUE;
public static final Color WARNING_COLOR_DARK = Color.decode("#EB6261");
public static final Color TRUSTED_COLOR_DARK = Color.decode("#589DF6");
public static final Color RUNNING_COLOR = Color.decode("#00AD47");
public static final Color PENDING_COLOR = Color.decode("#B2B247");
public static final Color STOPPED_COLOR = Color.decode("#AE4745");

public static Color WARNING_COLOR = WARNING_COLOR_LITE;
public static Color TRUSTED_COLOR = TRUSTED_COLOR_LITE;

Expand Down
131 changes: 113 additions & 18 deletions src/qz/common/TrayManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
import qz.utils.*;
import qz.ws.PrintSocketServer;
import qz.ws.SingleInstanceChecker;
import qz.ws.SocketMethod;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -62,6 +61,7 @@ public class TrayManager {
private AboutDialog aboutDialog;
private LogDialog logDialog;
private SiteManagerDialog sitesDialog;
private ControlDialog controlDialog;
private ArrayList<Component> componentList;
private IconCache.Icon shownIcon;

Expand Down Expand Up @@ -254,6 +254,10 @@ private void addMenuItems() {
sitesDialog = new SiteManagerDialog(sitesItem, iconCache, prefs);
componentList.add(sitesDialog);

JMenuItem controlMenuItem = new JMenuItem("Control Menu...", iconCache.getIcon(SAVED_ICON));
controlMenuItem.setMnemonic(KeyEvent.VK_C);
controlMenuItem.addActionListener(controlMenuListener);

JMenuItem diagnosticMenu = new JMenu("Diagnostic");

JMenuItem browseApp = new JMenuItem("Browse App folder...", iconCache.getIcon(FOLDER_ICON));
Expand Down Expand Up @@ -328,6 +332,7 @@ private void addMenuItems() {
advancedMenu.add(new JSeparator());
}
advancedMenu.add(sitesItem);
advancedMenu.add(controlMenuItem);
advancedMenu.add(desktopItem);
advancedMenu.add(new JSeparator());
advancedMenu.add(anonymousItem);
Expand Down Expand Up @@ -369,26 +374,87 @@ private void addMenuItems() {
popup.add(separator);
popup.add(exitItem);

controlDialog = new ControlDialog(iconCache);
componentList.add(controlDialog);

// Status/control section
controlDialog.add(reloadItem, ControlDialog.Category.STATUS);
controlDialog.add(exitItem, "Shut down", ControlDialog.Category.STATUS);

// General section
controlDialog.add(aboutItem, ControlDialog.Category.GENERAL);
controlDialog.add(sitesItem, ControlDialog.Category.GENERAL);
controlDialog.add(logItem, ControlDialog.Category.GENERAL);

// Advanced section
controlDialog.add(startupItem, ControlDialog.Category.ADVANCED);
controlDialog.add(notificationsItem, ControlDialog.Category.ADVANCED);
controlDialog.add(monocleItem, ControlDialog.Category.ADVANCED);
controlDialog.add(anonymousItem, ControlDialog.Category.ADVANCED);
controlDialog.add(new JSeparator(), ControlDialog.Category.ADVANCED);
controlDialog.add(desktopItem, ControlDialog.Category.ADVANCED);

// Diagnostic section
controlDialog.add(zipLogs, ControlDialog.Category.DIAGNOSTIC);
controlDialog.add(new JSeparator(), ControlDialog.Category.DIAGNOSTIC);
controlDialog.add(browseApp, ControlDialog.Category.DIAGNOSTIC);
controlDialog.add(browseUser, ControlDialog.Category.DIAGNOSTIC);
controlDialog.add(browseShared, ControlDialog.Category.DIAGNOSTIC);

if (tray != null) {
tray.setJPopupMenu(popup);
} // FIXME REMOVE THIS COMMENT // else {
// Display and minimize
controlDialog.setPersistent(true);
controlDialog.setVisible(true);
controlDialog.setState(Frame.ICONIFIED);
// FIXME REMOVE THIS COMMENT // controlMenuDialog.setState(Frame.ICONIFIED);
// FIXME REMOVE THIS COMMENT // }


}

private static boolean getCheckBoxState(ActionEvent e) {
Object o = e.getSource();
if(o instanceof JCheckBox) {
return ((JCheckBox)o).isSelected();
} else if(o instanceof JCheckBoxMenuItem) {
return ((JCheckBoxMenuItem)o).getState();
} else if(o instanceof CheckboxMenuItem) {
return ((CheckboxMenuItem)o).getState();
}
throw new UnsupportedOperationException("Cannot get checkbox state of " + o.getClass().getSimpleName());
}

private static void setCheckBoxState(ActionEvent e, boolean state) {
Object o = e.getSource();
if(o instanceof JCheckBox) {
((JCheckBox)o).setSelected(state);
return;
} else if(o instanceof JCheckBoxMenuItem) {
((JCheckBoxMenuItem)o).setState(state);
return;
} else if(o instanceof CheckboxMenuItem) {
((CheckboxMenuItem)o).setState(state);
return;
}
throw new UnsupportedOperationException("Cannot get checkbox state of " + o.getClass().getSimpleName());
}

private final ActionListener notificationsListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
prefs.setProperty(Constants.PREFS_NOTIFICATIONS, ((JCheckBoxMenuItem)e.getSource()).getState());
prefs.setProperty(Constants.PREFS_NOTIFICATIONS, getCheckBoxState(e));
}
};

private final ActionListener monocleListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JCheckBoxMenuItem j = (JCheckBoxMenuItem)e.getSource();
prefs.setProperty(Constants.PREFS_MONOCLE, j.getState());
boolean state = getCheckBoxState(e);
prefs.setProperty(Constants.PREFS_MONOCLE, state);
displayWarningMessage(String.format("A restart of %s is required to ensure this feature is %sabled.",
Constants.ABOUT_TITLE, j.getState()? "en":"dis"));
Constants.ABOUT_TITLE, state? "en":"dis"));
}
};

Expand All @@ -405,10 +471,7 @@ public void actionPerformed(ActionEvent e) {
};

private final ActionListener anonymousListener = e -> {
boolean checkBoxState = true;
if (e.getSource() instanceof JCheckBoxMenuItem) {
checkBoxState = ((JCheckBoxMenuItem)e.getSource()).getState();
}
boolean checkBoxState = getCheckBoxState(e);

log.debug("Block unsigned: {}", checkBoxState);

Expand All @@ -429,17 +492,18 @@ public void actionPerformed(ActionEvent e) {

private ActionListener startupListener() {
return e -> {
JCheckBoxMenuItem source = (JCheckBoxMenuItem)e.getSource();
if (!source.getState() && !confirmDialog.prompt("Remove " + name + " from startup?")) {
source.setState(true);
if (!getCheckBoxState(e) && !confirmDialog.prompt("Remove " + name + " from startup?")) {
setCheckBoxState(e,true);
return;
}
if (FileUtilities.setAutostart(source.getState())) {
displayInfoMessage("Successfully " + (source.getState() ? "enabled" : "disabled") + " autostart");

boolean state = getCheckBoxState(e);
if (FileUtilities.setAutostart(state)) {
displayInfoMessage("Successfully " + (state ? "enabled" : "disabled") + " autostart");
} else {
displayErrorMessage("Error " + (source.getState() ? "enabling" : "disabling") + " autostart");
displayErrorMessage("Error " + (state ? "enabling" : "disabling") + " autostart");
}
source.setState(FileUtilities.isAutostart());
setCheckBoxState(e, FileUtilities.isAutostart());
};
}

Expand Down Expand Up @@ -475,6 +539,11 @@ public void actionPerformed(ActionEvent e) {
}
};


private final ActionListener controlMenuListener = e -> {
controlDialog.setVisible(true);
};

public void exit(int returnCode) {
prefs.save();
System.exit(returnCode);
Expand Down Expand Up @@ -602,6 +671,7 @@ private void setIcon(final IconCache.Icon i) {
public void refreshIcon(final Runnable whenDone) {
SwingUtilities.invokeLater(() -> {
tray.setIcon(shownIcon);
controlDialog.setRunningIndicator(shownIcon);
if(whenDone != null) {
whenDone.run();
}
Expand Down Expand Up @@ -630,6 +700,31 @@ private void displayMessage(final String caption, final String text, final TrayI
}
}

public void displayComponent(SocketMethod method, Point location) {
SwingUtilities.invokeLater(() -> {
switch(method) {
case GUI_SHOW_ABOUT:
SystemUtilities.centerWindow(aboutDialog, location);
aboutDialog.setVisible(true);
break;
case GUI_SHOW_LOG:
SystemUtilities.centerWindow(logDialog, location);
logDialog.setVisible(true);
break;
case GUI_SHOW_SITES:
SystemUtilities.centerWindow(sitesDialog, location);
sitesDialog.setVisible(true);
break;
case GUI_SHOW_MENU:
SystemUtilities.centerWindow(controlDialog, location);
controlDialog.setVisible(true);
break;
default:
throw new UnsupportedOperationException("Call \"" + method.getCallName() + "\" is not yet supported");
}
});
}

public void singleInstanceCheck(java.util.List<Integer> insecurePorts, Integer insecurePortIndex) {
for(int port : insecurePorts) {
if (port != insecurePorts.get(insecurePortIndex)) {
Expand Down
8 changes: 4 additions & 4 deletions src/qz/ui/BasicDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Created by Tres on 2/23/2015.
*/
public class BasicDialog extends JDialog implements Themeable {
public class BasicDialog extends JFrame implements Themeable {
private JPanel mainPanel;
private JComponent headerComponent;
private JComponent contentComponent;
Expand All @@ -30,13 +30,13 @@ public class BasicDialog extends JDialog implements Themeable {
private int stockButtonCount = 0;

public BasicDialog(JMenuItem caller, IconCache iconCache) {
super((Frame)null, caller.getText().replaceAll("\\.+", ""), true);
super(caller.getText().replaceAll("\\.+", ""));
this.iconCache = iconCache;
initBasicComponents();
}

public BasicDialog(Frame owner, String title, IconCache iconCache) {
super(owner, title, true);
public BasicDialog(String title, IconCache iconCache) {
super(title);
this.iconCache = iconCache;
initBasicComponents();
}
Expand Down
Loading