Skip to content

Commit 22b0690

Browse files
committed
Refactor tray icon creation and management API
Removed TrayManager::Create and Destroy methods and their C API equivalents. TrayIcon instances should now be created directly using std::make_shared<TrayIcon>(). Updated examples and documentation to reflect this change, simplifying tray icon lifecycle management.
1 parent 81c3828 commit 22b0690

File tree

11 files changed

+15
-151
lines changed

11 files changed

+15
-151
lines changed

examples/tray_icon_c_example/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ int main() {
160160
}
161161

162162
// Create tray icon
163-
native_tray_icon_t tray_icon = native_tray_manager_create();
163+
native_tray_icon_t tray_icon = native_tray_icon_create();
164164
if (!tray_icon) {
165165
printf("Error: Failed to create tray icon!\n");
166166
native_menu_destroy(menu);

examples/tray_icon_example/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ int main() {
2727
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
2828
#endif
2929

30-
// Get TrayManager instance (singleton pattern)
30+
// Check if tray icons are supported
3131
TrayManager& trayManager = TrayManager::GetInstance();
3232
if (!trayManager.IsSupported()) {
3333
std::cerr << "Tray icons are not supported on this platform!" << std::endl;
3434
return 1;
3535
}
3636

37-
// Create a tray icon
38-
auto trayIcon = trayManager.Create();
37+
// Create a tray icon directly
38+
auto trayIcon = std::make_shared<TrayIcon>();
3939
if (!trayIcon) {
4040
std::cerr << "Failed to create tray icon!" << std::endl;
4141
return 1;

examples/window_c_example/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ int main() {
290290
native_window_options_destroy(options);
291291

292292
// Create tray icon
293-
g_tray_icon = native_tray_manager_create();
293+
g_tray_icon = native_tray_icon_create();
294294
if (g_tray_icon != NULL) {
295295
native_tray_icon_set_icon(g_tray_icon,
296296
"data:image/png;base64,"

examples/window_example/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int main() {
4242
options.centered = true;
4343
std::shared_ptr<Window> window_ptr = window_manager.Create(options);
4444

45-
std::shared_ptr<TrayIcon> tray_icon_ptr = tray_manager.Create();
45+
std::shared_ptr<TrayIcon> tray_icon_ptr = std::make_shared<TrayIcon>();
4646
if (tray_icon_ptr != nullptr) {
4747
TrayIcon& tray_icon = *tray_icon_ptr;
4848
tray_icon.SetIcon(

src/capi/tray_manager_c.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@ bool native_tray_manager_is_supported(void) {
1414
}
1515
}
1616

17-
native_tray_icon_t native_tray_manager_create(void) {
18-
try {
19-
auto tray_icon = TrayManager::GetInstance().Create();
20-
return tray_icon ? static_cast<native_tray_icon_t>(tray_icon.get())
21-
: nullptr;
22-
} catch (...) {
23-
return nullptr;
24-
}
25-
}
26-
2717
native_tray_icon_t native_tray_manager_get(native_tray_icon_id_t tray_icon_id) {
2818
try {
2919
auto tray_icon = TrayManager::GetInstance().Get(tray_icon_id);
@@ -67,14 +57,6 @@ native_tray_icon_list_t native_tray_manager_get_all(void) {
6757
}
6858
}
6959

70-
bool native_tray_manager_destroy(native_tray_icon_id_t tray_icon_id) {
71-
try {
72-
return TrayManager::GetInstance().Destroy(tray_icon_id);
73-
} catch (...) {
74-
return false;
75-
}
76-
}
77-
7860
// Utility functions
7961

8062
void native_tray_icon_list_free(native_tray_icon_list_t list) {

src/capi/tray_manager_c.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ typedef struct {
3535
FFI_PLUGIN_EXPORT
3636
bool native_tray_manager_is_supported(void);
3737

38-
/**
39-
* Create a new tray icon
40-
* @return Tray icon handle, or NULL if creation failed
41-
*/
42-
FFI_PLUGIN_EXPORT
43-
native_tray_icon_t native_tray_manager_create(void);
44-
4538
/**
4639
* Get a tray icon by its ID
4740
* @param tray_icon_id The tray icon ID
@@ -57,14 +50,6 @@ native_tray_icon_t native_tray_manager_get(native_tray_icon_id_t tray_icon_id);
5750
FFI_PLUGIN_EXPORT
5851
native_tray_icon_list_t native_tray_manager_get_all(void);
5952

60-
/**
61-
* Destroy a tray icon by its ID
62-
* @param tray_icon_id The tray icon ID to destroy
63-
* @return true if tray icon was found and destroyed, false otherwise
64-
*/
65-
FFI_PLUGIN_EXPORT
66-
bool native_tray_manager_destroy(native_tray_icon_id_t tray_icon_id);
67-
6853
/**
6954
* Utility functions
7055
*/

src/platform/linux/tray_manager_linux.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,6 @@ bool TrayManager::IsSupported() {
6262
#endif
6363
}
6464

65-
std::shared_ptr<TrayIcon> TrayManager::Create() {
66-
std::lock_guard<std::mutex> lock(mutex_);
67-
68-
#if HAS_GTK && HAS_AYATANA_APPINDICATOR
69-
// Create tray icon with platform-specific initialization handled internally
70-
auto tray = std::make_shared<TrayIcon>();
71-
tray->id = next_tray_id_++;
72-
trays_[tray->id] = tray;
73-
74-
return tray;
75-
#else
76-
// AppIndicator not available, return nullptr
77-
return nullptr;
78-
#endif
79-
}
80-
8165
std::shared_ptr<TrayIcon> TrayManager::Get(TrayIconID id) {
8266
std::lock_guard<std::mutex> lock(mutex_);
8367

@@ -98,18 +82,4 @@ std::vector<std::shared_ptr<TrayIcon>> TrayManager::GetAll() {
9882
return trays;
9983
}
10084

101-
bool TrayManager::Destroy(TrayIconID id) {
102-
std::lock_guard<std::mutex> lock(mutex_);
103-
104-
auto it = trays_.find(id);
105-
if (it != trays_.end()) {
106-
// Remove the tray icon from our container
107-
// The shared_ptr will automatically clean up when the last reference is
108-
// released
109-
trays_.erase(it);
110-
return true;
111-
}
112-
return false;
113-
}
114-
11585
} // namespace nativeapi

src/platform/macos/tray_manager_macos.mm

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,6 @@
5656
return true;
5757
}
5858

59-
std::shared_ptr<TrayIcon> TrayManager::Create() {
60-
std::lock_guard<std::mutex> lock(mutex_);
61-
62-
// Create tray icon with platform-specific initialization handled internally
63-
auto tray = std::make_shared<TrayIcon>();
64-
tray->id = next_tray_id_++;
65-
trays_[tray->id] = tray;
66-
67-
return tray;
68-
}
69-
7059
std::shared_ptr<TrayIcon> TrayManager::Get(TrayIconID id) {
7160
std::lock_guard<std::mutex> lock(mutex_);
7261

@@ -87,17 +76,4 @@
8776
return trays;
8877
}
8978

90-
bool TrayManager::Destroy(TrayIconID id) {
91-
std::lock_guard<std::mutex> lock(mutex_);
92-
93-
auto it = trays_.find(id);
94-
if (it != trays_.end()) {
95-
// Remove the tray icon from our container
96-
// The shared_ptr will automatically clean up when the last reference is released
97-
trays_.erase(it);
98-
return true;
99-
}
100-
return false;
101-
}
102-
10379
} // namespace nativeapi

src/platform/windows/tray_manager_windows.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,6 @@ bool TrayManager::IsSupported() {
8787
return true; // Windows always supports system tray
8888
}
8989

90-
std::shared_ptr<TrayIcon> TrayManager::Create() {
91-
std::lock_guard<std::mutex> lock(mutex_);
92-
93-
// Create tray icon with platform-specific initialization handled internally
94-
auto tray = std::make_shared<TrayIcon>();
95-
tray->id = next_tray_id_++;
96-
trays_[tray->id] = tray;
97-
98-
return tray;
99-
}
100-
10190
std::shared_ptr<TrayIcon> TrayManager::Get(TrayIconID id) {
10291
std::lock_guard<std::mutex> lock(mutex_);
10392

@@ -118,18 +107,4 @@ std::vector<std::shared_ptr<TrayIcon>> TrayManager::GetAll() {
118107
return trays;
119108
}
120109

121-
bool TrayManager::Destroy(TrayIconID id) {
122-
std::lock_guard<std::mutex> lock(mutex_);
123-
124-
auto it = trays_.find(id);
125-
if (it != trays_.end()) {
126-
// Remove the tray icon from our container
127-
// The shared_ptr will automatically clean up when the last reference is
128-
// released
129-
trays_.erase(it);
130-
return true;
131-
}
132-
return false;
133-
}
134-
135110
} // namespace nativeapi

src/tray_icon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef long TrayIconID;
5858
* trayIcon->SetContextMenu(menu);
5959
*
6060
* // Show the tray icon
61-
* trayIcon->Show();
61+
* trayIcon->SetVisible(true);
6262
* ```
6363
*/
6464
class TrayIcon : public EventEmitter, public NativeObjectProvider {

0 commit comments

Comments
 (0)