Hunter0x7c7
2024-09-12 60a3f2bc64b7a5f502e4133ced31f0b25c88d3f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.github.hunter0x7c7.sync.utils;
 
 
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 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;
 
public class TrayUtil {
 
    private static TrayUtil mInstance;
 
    private TrayUtil() {
    }
 
    public static TrayUtil getInstance() {
        if (mInstance == null) {
            synchronized (TrayUtil.class) {
                if (mInstance == null) {
                    mInstance = new TrayUtil();
                }
            }
        }
        return mInstance;
    }
 
 
    public void addSystemTray(Stage primaryStage, String tooltip, String mipmap, MenuItem... items) {
        addSystemTray(primaryStage, tooltip, mipmap, null, items);
    }
 
    public void addSystemTray(Stage stage, String tooltip, String mipmap, Callback<Object> callback, MenuItem... items) {
        //检查系统是否支持托盘
        if (!SystemTray.isSupported()) {
            //系统托盘不支持
            return;
        }
        //执行stage.close()方法,窗口不直接退出
        Platform.setImplicitExit(false);
 
        //弹出式菜单组件
        MenuItem exit = new MenuItem("退出");//退出Exit
        final PopupMenu popup = new PopupMenu();
        if (items != null) {
            for (MenuItem item : items) {
                if (item == null) continue;
                popup.add(item);
            }
        }
        popup.add(exit);
 
        //通过类加载器获取图像
        ImageIcon imageIcon = new ImageIcon(ClassLoader.getSystemResource(mipmap));//"mipmap/ic_class_2_dex_tools_16.png"
        Image img = imageIcon.getImage();
 
 
        final TrayIcon trayIcon = new TrayIcon(img, tooltip, popup);
        //trayIcon.setImageAutoSize(true);//设置图标尺寸自动适应
        trayIcon.setActionCommand(APPID);
        trayIcon.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //鼠标双击系统托盘图标
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        if (stage != null) {
                            if (stage.isIconified()) {//最小化
                                stage.setIconified(false);
                            }
                            if (!stage.isShowing()) {
                                stage.show();
                            }
                            stage.toFront();
                        }
                    }
                });
            }
        });
        //系统托盘
        final SystemTray tray = SystemTray.getSystemTray();
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                try {
                    tray.add(trayIcon);
                } catch (Exception e) {
                    e.printStackTrace();
                    //系统托盘添加失败
                }
            }
        });
 
        //绑定系统托盘事件
        exit.addActionListener(actionListener -> {
            if (callback != null) {
                callback.onCall(true);
            }
            if (tray != null) {
                tray.remove(trayIcon);
            }
            exitApp();
        });
        //点击关闭按钮时隐藏场景
        stage.setOnCloseRequest(event -> {
            Platform.runLater(() -> {
                if (stage != null) {
                    stage.hide();
                }
            });
        });
    }
 
 
    //退出
    public void exitApp() {
        //UI线程
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                //退出前先关闭窗口
                Stage stage = Session.getInstance().getPrimaryStage();
                if (stage != null) {
                    stage.close();
                }
                //退出
                Platform.exit();
            }
        });
    }
 
}