forked from antler119/system_tray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_tray_plugin.cc
151 lines (115 loc) · 5.26 KB
/
system_tray_plugin.cc
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
140
141
142
143
144
145
146
147
148
149
150
151
#include "include/system_tray/system_tray_plugin.h"
#include <flutter_linux/flutter_linux.h>
#include <gtk/gtk.h>
#include <sys/utsname.h>
#include <cstring>
#include <memory>
#include "app_window.h"
#include "menu_manager.h"
#include "tray.h"
namespace {
constexpr char kChannelNameAppWindow[] = "flutter/system_tray/app_window";
constexpr char kChannelNameMenuManager[] = "flutter/system_tray/menu_manager";
constexpr char kChannelNameTray[] = "flutter/system_tray/tray";
} // namespace
#define SYSTEM_TRAY_PLUGIN(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), system_tray_plugin_get_type(), \
SystemTrayPlugin))
struct _SystemTrayPlugin {
GObject parent_instance;
FlPluginRegistrar* registrar;
FlMethodChannel* channel_app_window = nullptr;
FlMethodChannel* channel_menu_manager = nullptr;
FlMethodChannel* channel_tray = nullptr;
std::unique_ptr<AppWindow> app_window;
std::shared_ptr<MenuManager> menu_manager;
std::unique_ptr<Tray> tray;
};
G_DEFINE_TYPE(SystemTrayPlugin, system_tray_plugin, g_object_get_type())
SystemTrayPlugin* g_plugin = nullptr;
// Called when a method call is received from Flutter.
static void system_tray_plugin_handle_method_call(SystemTrayPlugin* self,
FlMethodCall* method_call) {
g_autoptr(FlMethodResponse) response = nullptr;
const gchar* method = fl_method_call_get_name(method_call);
g_print("method call %s\n", method);
if (strcmp(method, kInitAppWindow) == 0 ||
strcmp(method, kShowAppWindow) == 0 ||
strcmp(method, kHideAppWindow) == 0 ||
strcmp(method, kCloseAppWindow) == 0) {
self->app_window->handle_method_call(method_call);
} else if (strcmp(method, kCreateContextMenu) == 0 ||
strcmp(method, kSetLabel) == 0 || strcmp(method, kSetImage) == 0 ||
strcmp(method, kSetEnable) == 0 ||
strcmp(method, kSetCheck) == 0) {
self->menu_manager->handle_method_call(method_call);
} else if (strcmp(method, kInitSystemTray) == 0 ||
strcmp(method, kSetSystemTrayInfo) == 0 ||
strcmp(method, kSetContextMenu) == 0 ||
strcmp(method, kPopupContextMenu) == 0 ||
strcmp(method, kGetTitle) == 0 ||
strcmp(method, kDestroySystemTray) == 0) {
self->tray->handle_method_call(method_call);
} else {
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
g_autoptr(GError) error = nullptr;
if (!fl_method_call_respond(method_call, response, &error)) {
g_warning("Failed to send method call response: %s", error->message);
}
}
}
static void system_tray_plugin_dispose(GObject* object) {
SystemTrayPlugin* self = SYSTEM_TRAY_PLUGIN(object);
g_clear_object(&self->registrar);
g_clear_object(&self->channel_app_window);
g_clear_object(&self->channel_menu_manager);
g_clear_object(&self->channel_tray);
G_OBJECT_CLASS(system_tray_plugin_parent_class)->dispose(object);
}
static void system_tray_plugin_class_init(SystemTrayPluginClass* klass) {
G_OBJECT_CLASS(klass)->dispose = system_tray_plugin_dispose;
}
static void system_tray_plugin_init(SystemTrayPlugin* self) {
g_plugin = self;
}
static void method_call_cb(FlMethodChannel* channel,
FlMethodCall* method_call,
gpointer user_data) {
SystemTrayPlugin* plugin = SYSTEM_TRAY_PLUGIN(user_data);
system_tray_plugin_handle_method_call(plugin, method_call);
}
void system_tray_plugin_register_with_registrar(FlPluginRegistrar* registrar) {
SystemTrayPlugin* plugin =
SYSTEM_TRAY_PLUGIN(g_object_new(system_tray_plugin_get_type(), nullptr));
plugin->registrar = FL_PLUGIN_REGISTRAR(g_object_ref(registrar));
g_autoptr(FlStandardMethodCodec) codec_app_window =
fl_standard_method_codec_new();
plugin->channel_app_window = fl_method_channel_new(
fl_plugin_registrar_get_messenger(registrar), kChannelNameAppWindow,
FL_METHOD_CODEC(codec_app_window));
g_autoptr(FlStandardMethodCodec) codec_menu_manager =
fl_standard_method_codec_new();
plugin->channel_menu_manager = fl_method_channel_new(
fl_plugin_registrar_get_messenger(registrar), kChannelNameMenuManager,
FL_METHOD_CODEC(codec_menu_manager));
g_autoptr(FlStandardMethodCodec) codec_tray = fl_standard_method_codec_new();
plugin->channel_tray =
fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
kChannelNameTray, FL_METHOD_CODEC(codec_tray));
plugin->app_window = std::make_unique<AppWindow>(plugin->registrar,
plugin->channel_app_window);
plugin->menu_manager =
std::make_shared<MenuManager>(plugin->channel_menu_manager);
plugin->tray =
std::make_unique<Tray>(plugin->channel_tray, plugin->menu_manager);
fl_method_channel_set_method_call_handler(
plugin->channel_app_window, method_call_cb, g_object_ref(plugin),
g_object_unref);
fl_method_channel_set_method_call_handler(
plugin->channel_menu_manager, method_call_cb, g_object_ref(plugin),
g_object_unref);
fl_method_channel_set_method_call_handler(
plugin->channel_tray, method_call_cb, g_object_ref(plugin),
g_object_unref);
g_object_unref(plugin);
}