Minimize to tray on-close

This commit is contained in:
Dmitry Isaenko 2019-10-17 09:39:26 +03:00
parent d7a47ce9f4
commit 8de2e05af1
4 changed files with 84 additions and 6 deletions

View file

@ -53,6 +53,9 @@ Want to support development? Make a donation* (see below):
* [RU] Пожалуйста обратите внимание! Это некоммерческое приложение. Перечисляя средства вы совершаете дарение.
#### TODO
[ ] Tray support
[ ] Headless mode (CLI)
[ ] Dark theme
* [x] Tray support
* [ ] Configuration files support
* [ ] Settings
* [ ] Headless mode (CLI)
* [ ] Dark theme

View file

@ -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")))
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);
}

View file

@ -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

View file

@ -20,3 +20,4 @@ effect_wave_vertical_reverse=Волна вертикальная (обратна
effect_wave_center_to_edge=Волна от центра к краям
effect_wave_edge_to_center=Волна от краёв к центру
btn_reset=Сбросить
tray_close=Закрыть