From 8de2e05af11c8cc105c4af7061dafb527e5e9c6a Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Thu, 17 Oct 2019 09:39:26 +0300 Subject: [PATCH] Minimize to tray on-close --- README.md | 9 ++- src/main/java/logiled/MainFx.java | 79 +++++++++++++++++++++++- src/main/resources/locale.properties | 1 + src/main/resources/locale_rus.properties | 1 + 4 files changed, 84 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 91386fc..a26114f 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,9 @@ Want to support development? Make a donation* (see below): * [RU] Пожалуйста обратите внимание! Это некоммерческое приложение. Перечисляя средства вы совершаете дарение. #### TODO -[ ] Tray support -[ ] Headless mode (CLI) -[ ] Dark theme \ No newline at end of file + +* [x] Tray support +* [ ] Configuration files support +* [ ] Settings +* [ ] Headless mode (CLI) +* [ ] Dark theme \ No newline at end of file diff --git a/src/main/java/logiled/MainFx.java b/src/main/java/logiled/MainFx.java index 1618b1f..72bebc4 100644 --- a/src/main/java/logiled/MainFx.java +++ b/src/main/java/logiled/MainFx.java @@ -1,20 +1,37 @@ package logiled; import javafx.application.Application; +import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.stage.Stage; +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.io.IOException; import java.util.Locale; import java.util.ResourceBundle; public class MainFx extends Application { public static final String appVersion = "v0.3"; + private static boolean traySupport = true; + + private Stage stage; + private ResourceBundle rb; + @Override public void start(Stage primaryStage) throws Exception{ + //-----------------------Tray support--------------------- + this.stage = primaryStage; + if (traySupport){ + Platform.setImplicitExit(false); + SwingUtilities.invokeLater(this::addAppToTray); + } + //-------------------------------------------------------- Mediator.getInstance().setInstance(getHostServices()); FXMLLoader loader = new FXMLLoader(getClass().getResource("/Main.fxml")); @@ -22,7 +39,7 @@ public class MainFx extends Application { Locale locale = new Locale(Locale.getDefault().getISO3Language()); Locale.setDefault(locale); - ResourceBundle rb = ResourceBundle.getBundle("locale", locale); + rb = ResourceBundle.getBundle("locale", locale); loader.setResources(rb); @@ -34,6 +51,9 @@ public class MainFx extends Application { new Image(getClass().getResourceAsStream("/ico/appIcon_64.png")), new Image(getClass().getResourceAsStream("/ico/appIcon_128.png")) ); + // NOTE: tray leftovers + if (traySupport) + primaryStage.setOnCloseRequest(windowEvent -> primaryStage.hide()); primaryStage.setTitle("LogiLed "+appVersion); primaryStage.setMinWidth(1215); @@ -46,9 +66,62 @@ public class MainFx extends Application { primaryStage.setOnHidden(e->MessagesConsumer.getInstance().stop()); // Useless? } + private void addAppToTray(){ + try { + Toolkit.getDefaultToolkit(); // ??? + + if (! SystemTray.isSupported()){ + System.out.println("No system tray support. Please try executing this application wit '--no-tray' key."); + Platform.exit(); + } + + SystemTray tray = SystemTray.getSystemTray(); + + TrayIcon trayIcon = new TrayIcon(ImageIO.read(getClass().getResourceAsStream("/ico/appIcon_32.png"))); + + trayIcon.addActionListener(ActionEvent -> Platform.runLater(this::showStage)); + + MenuItem closeItem = new MenuItem(rb.getString("tray_close")); + closeItem.addActionListener(actionEvent -> { + Platform.exit(); + tray.remove(trayIcon); + }); + + java.awt.Font defaultFont = java.awt.Font.decode(null); + java.awt.Font boldFont = defaultFont.deriveFont(java.awt.Font.BOLD); + closeItem.setFont(boldFont); + + final PopupMenu popupMenu = new PopupMenu(); + popupMenu.add(closeItem); + trayIcon.setPopupMenu(popupMenu); + tray.add(trayIcon); + } + catch (IOException | AWTException e){ + e.printStackTrace(); + System.out.println("Something wrong with tray support. Please try executing this application wit '--no-tray' key."); + } + } + + private void showStage() { + if (stage != null) { + stage.show(); + stage.toFront(); + } + } + public static void main(String[] args) { - if ((args.length == 1) && (args[0].equals("-v") || args[0].equals("--version"))) - System.out.println("LogiLed "+appVersion); + if ((args.length > 0)) { + if (args[0].equals("--no-tray")){ + traySupport = false; + launch(args); + } + if (args[0].equals("-v") || args[0].equals("--version")) + System.out.println("LogiLed " + appVersion); + else + System.out.println("Usage: LogiLed [KEY]\n" + + " -v, --version\tGet application version\n" + + " --no-tray\tDisable tray support"); + } else launch(args); } diff --git a/src/main/resources/locale.properties b/src/main/resources/locale.properties index 26b0a51..baf0b79 100644 --- a/src/main/resources/locale.properties +++ b/src/main/resources/locale.properties @@ -694,3 +694,4 @@ effect_wave_horizontal_reverse=Wave horizontal (reverse) effect_wave_vertical_reverse=Wave vertical (reverse) effect_wave_edge_to_center=Wave from edges to center btn_reset=Reset +tray_close=Close diff --git a/src/main/resources/locale_rus.properties b/src/main/resources/locale_rus.properties index 52e226c..83ec43e 100644 --- a/src/main/resources/locale_rus.properties +++ b/src/main/resources/locale_rus.properties @@ -20,3 +20,4 @@ effect_wave_vertical_reverse=Волна вертикальная (обратна effect_wave_center_to_edge=Волна от центра к краям effect_wave_edge_to_center=Волна от краёв к центру btn_reset=Сбросить +tray_close=Закрыть