diff --git a/src/qz/common/TrayManager.java b/src/qz/common/TrayManager.java index 54bd78364..a612eeffe 100644 --- a/src/qz/common/TrayManager.java +++ b/src/qz/common/TrayManager.java @@ -19,6 +19,8 @@ import qz.auth.Certificate; import qz.auth.RequestState; import qz.installer.shortcut.ShortcutCreator; +import qz.printer.PrintServiceMatcher; +import qz.printer.action.WebApp; import qz.ui.*; import qz.ui.component.IconCache; import qz.ui.tray.TrayType; @@ -32,8 +34,9 @@ import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.File; -import java.nio.file.Paths; +import java.io.IOException; import java.util.ArrayList; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -76,6 +79,9 @@ public class TrayManager { // Action to run when reload is triggered private Thread reloadThread; + // Actions to run if idle after startup + private ArrayList idleTimers; + public TrayManager() { this(false); } @@ -162,7 +168,7 @@ public TrayManager(boolean isHeadless) { try { Thread.sleep(1000); if (darkDesktopMode != SystemUtilities.isDarkDesktop(true) || - darkTaskbarMode != SystemUtilities.isDarkTaskbar(true)) { + darkTaskbarMode != SystemUtilities.isDarkTaskbar(true)) { darkDesktopMode = SystemUtilities.isDarkDesktop(); darkTaskbarMode = SystemUtilities.isDarkTaskbar(); iconCache.fixTrayIcons(darkTaskbarMode); @@ -182,7 +188,8 @@ public TrayManager(boolean isHeadless) { } }); } - } catch(InterruptedException ignore) {} + } + catch(InterruptedException ignore) {} } }).start(); } @@ -190,6 +197,25 @@ public TrayManager(boolean isHeadless) { if (tray != null) { addMenuItems(); } + + // Initialize idle actions + idleTimers = new ArrayList<>(); + + // Slow to find printers the first time if a lot of printers are installed + performIfIdle((int)TimeUnit.SECONDS.toMillis(10), evt -> { + log.debug("IDLE: Performing first run of find printers"); + PrintServiceMatcher.getNativePrinterList(false, true); + }); + // Slow to start JavaFX the first time + performIfIdle((int)TimeUnit.SECONDS.toMillis(60), evt -> { + log.debug("IDLE: Starting up JFX for HTML printing"); + try { + WebApp.initialize(); + } + catch(IOException e) { + log.error("Idle runner failed to preemptively start JavaFX service"); + } + }); } /** @@ -647,4 +673,23 @@ public boolean isHeadless() { return headless; } + private void performIfIdle(int idleQualifier, ActionListener performer) { + Timer timer = new Timer(idleQualifier, evt -> { + performer.actionPerformed(evt); + idleTimers.remove(evt.getSource()); + }); + timer.setRepeats(false); + timer.start(); + + idleTimers.add(timer); + } + + public void voidIdleActions() { + if (idleTimers.size() > 0) { + log.trace("Not idle, stopping any actions that haven't ran yet"); + idleTimers.forEach(Timer::stop); + idleTimers.clear(); + } + } + } diff --git a/src/qz/printer/PrintServiceMatcher.java b/src/qz/printer/PrintServiceMatcher.java index ef9d350a0..c420c9d9f 100644 --- a/src/qz/printer/PrintServiceMatcher.java +++ b/src/qz/printer/PrintServiceMatcher.java @@ -31,13 +31,18 @@ public class PrintServiceMatcher { private static final Logger log = LoggerFactory.getLogger(PrintServiceMatcher.class); - public static NativePrinterMap getNativePrinterList(boolean silent) { + public static NativePrinterMap getNativePrinterList(boolean silent, boolean withAttributes) { NativePrinterMap printers = NativePrinterMap.getInstance(); printers.putAll(PrintServiceLookup.lookupPrintServices(null, null)); if(!silent) log.debug("Found {} printers", printers.size()); + if(withAttributes) printers.values().forEach(NativePrinter::getDriverAttributes); return printers; } + public static NativePrinterMap getNativePrinterList(boolean silent) { + return getNativePrinterList(silent, false); + } + public static NativePrinterMap getNativePrinterList() { return getNativePrinterList(false); } diff --git a/src/qz/ws/PrintSocketClient.java b/src/qz/ws/PrintSocketClient.java index c684dcb43..83b1660f4 100644 --- a/src/qz/ws/PrintSocketClient.java +++ b/src/qz/ws/PrintSocketClient.java @@ -207,6 +207,10 @@ private void processMessage(Session session, JSONObject json, SocketConnection c return; } + if (call != SocketMethod.GET_VERSION) { + trayManager.voidIdleActions(); + } + // used in usb calls DeviceOptions dOpts = new DeviceOptions(params, DeviceOptions.DeviceMode.parse(call.getCallName()));