mac
HunterHuang0X7C7
2023-08-11 6fd17dbfad6b6b48c274007043c904975608a079
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
package com.github.hunter0x7c7.sync.utils;
 
 
import com.github.hunter0x7c7.sync.model.interfaces.Callback;
import javafx.application.Platform;
import javafx.stage.Stage;
 
import java.util.List;
 
import javax.swing.*;
import java.awt.*;
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 {
 
    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();
 
 
        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) {
                //鼠标双击系统托盘图标
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        if (stage != null) {
                            stage.show();
                        }
                    }
                });
            }
        });
        //系统托盘
        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);
            }
            //退出
            Platform.exit();
        });
        //点击关闭按钮时隐藏场景
        stage.setOnCloseRequest(event -> {
            Platform.runLater(() -> {
                if (stage != null) {
                    stage.hide();
                }
            });
        });
    }
}