Hunter0x7c7
2024-09-12 60a3f2bc64b7a5f502e4133ced31f0b25c88d3f1
src/main/java/com/github/hunter0x7c7/sync/utils/TrayUtil.java
@@ -2,17 +2,16 @@
import com.github.hunter0x7c7.sync.model.interfaces.Callback;
import com.github.hunter0x7c7.sync.model.storage.Session;
import javafx.application.Platform;
import javafx.stage.Stage;
import java.util.List;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static com.github.hunter0x7c7.sync.model.global.Parameters.APPID;
import static com.github.hunter0x7c7.sync.model.global.Parameters.AppNameString;
public class TrayUtil {
@@ -62,26 +61,31 @@
        Image img = imageIcon.getImage();
        TrayIcon trayIcon = new TrayIcon(img, tooltip, popup);
        //设置图标尺寸自动适应
        trayIcon.setImageAutoSize(true);
        final TrayIcon trayIcon = new TrayIcon(img, tooltip, popup);
        //trayIcon.setImageAutoSize(true);//设置图标尺寸自动适应
        trayIcon.setActionCommand(APPID);
        trayIcon.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
            public void actionPerformed(ActionEvent e) {
                //鼠标双击系统托盘图标
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        if (stage != null) {
                            stage.show();
                            if (stage.isIconified()) {//最小化
                                stage.setIconified(false);
                            }
                            if (!stage.isShowing()) {
                                stage.show();
                            }
                            stage.toFront();
                        }
                    }
                });
            }
        });
        //系统托盘
        SystemTray tray = SystemTray.getSystemTray();
        final SystemTray tray = SystemTray.getSystemTray();
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
@@ -102,8 +106,7 @@
            if (tray != null) {
                tray.remove(trayIcon);
            }
            //退出
            Platform.exit();
            exitApp();
        });
        //点击关闭按钮时隐藏场景
        stage.setOnCloseRequest(event -> {
@@ -114,4 +117,23 @@
            });
        });
    }
    //退出
    public void exitApp() {
        //UI线程
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                //退出前先关闭窗口
                Stage stage = Session.getInstance().getPrimaryStage();
                if (stage != null) {
                    stage.close();
                }
                //退出
                Platform.exit();
            }
        });
    }
}