summarylogtreecommitdiffstats
path: root/system_tray_item.patch
blob: 78ce23db39d7f0274391c744ea5bf134a3a94eae (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
From c1e4cdcb68c40e723c2f58c3ed319c506e03122c Mon Sep 17 00:00:00 2001
From: bytedream <me@bytedream.dev>
Date: Wed, 29 Oct 2025 15:37:57 +0100
Subject: [PATCH] add system tray item

---
 src-tauri/Cargo.toml      |  2 +-
 src-tauri/build.rs        |  2 +-
 src-tauri/src/main.rs     |  6 +++
 src-tauri/src/tray.rs     | 77 +++++++++++++++++++++++++++++++++++++++
 src-tauri/tauri.conf.json |  5 +++
 5 files changed, 90 insertions(+), 2 deletions(-)
 create mode 100644 src-tauri/src/tray.rs

diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index 16d846a..371df47 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -17,7 +17,7 @@ tauri-build = { version = "1.5.5", features = [] }
 [dependencies]
 serde_json = "1.0.109"
 serde = { version = "1.0.193", features = ["derive"] }
-tauri = { version = "1.8.0", features = ["api-all", "devtools", "updater"] }
+tauri = { version = "1.8.0", features = ["api-all", "devtools", "system-tray", "updater"] }
 tauri-plugin-localhost = "0.1.0"
 tauri-plugin-window-state = "0.1.1"
 
diff --git a/src-tauri/build.rs b/src-tauri/build.rs
index 795b9b7..d860e1e 100644
--- a/src-tauri/build.rs
+++ b/src-tauri/build.rs
@@ -1,3 +1,3 @@
 fn main() {
-  tauri_build::build()
+    tauri_build::build()
 }
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index 4231f30..0682873 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -5,6 +5,7 @@
 
 #[cfg(target_os = "macos")]
 mod menu;
+mod tray;
 
 use tauri::{utils::config::AppUrl, WindowUrl};
 
@@ -19,12 +20,17 @@ fn main() {
     context.config_mut().build.dev_path = AppUrl::Url(window_url.clone());
     let builder = tauri::Builder::default();
 
+    let builder = builder
+        .system_tray(tray::system_tray())
+        .on_system_tray_event(tray::system_tray_handler);
+
     #[cfg(target_os = "macos")]
     let builder = builder.menu(menu::menu());
 
     builder
         .plugin(tauri_plugin_localhost::Builder::new(port).build())
         .plugin(tauri_plugin_window_state::Builder::default().build())
+        .on_window_event(tray::window_event_handler)
         .run(context)
         .expect("error while building tauri application")
 }
diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs
new file mode 100644
index 0000000..7dd5f72
--- /dev/null
+++ b/src-tauri/src/tray.rs
@@ -0,0 +1,77 @@
+use tauri::{
+    CustomMenuItem, GlobalWindowEvent, Manager, SystemTray, SystemTrayEvent, SystemTrayHandle,
+    SystemTrayMenu, SystemTrayMenuItem, Window, WindowEvent,
+};
+
+pub const TRAY_LABEL: &'static str = "main-tray";
+
+pub fn window_event_handler<R: tauri::Runtime>(event: GlobalWindowEvent<R>) {
+    match event.event() {
+        // Prevent Cinny from closing, instead hide it and let it be
+        // reopened through the tray.
+        WindowEvent::CloseRequested { api, .. } => {
+            api.prevent_close();
+
+            let window = event.window().clone();
+            let tray_handle = window.app_handle().tray_handle_by_id(TRAY_LABEL).unwrap();
+            toggle_window_state(window, tray_handle);
+        }
+        _ => {}
+    }
+}
+
+/// Build the system tray object
+pub fn system_tray() -> SystemTray {
+    let toggle = CustomMenuItem::new("toggle".to_owned(), "Hide Cinny");
+    let quit = CustomMenuItem::new("quit".to_owned(), "Quit");
+    let menu = SystemTrayMenu::new()
+        .add_item(toggle)
+        .add_native_item(SystemTrayMenuItem::Separator)
+        .add_item(quit);
+
+    SystemTray::new()
+        .with_menu(menu)
+        .with_id(TRAY_LABEL.to_owned())
+}
+
+pub fn toggle_window_state<R: tauri::Runtime>(window: Window<R>, tray_handle: SystemTrayHandle<R>) {
+    // Hide the window if it's visible, show it if not
+    // `is_visible` returns true for minimized state for whatever reason
+    if window.is_visible().unwrap() {
+        window.hide().unwrap();
+        tray_handle
+            .get_item("toggle")
+            .set_title("Show Cinny")
+            .unwrap();
+    } else {
+        window.unminimize().unwrap();
+        window.show().unwrap();
+        window.set_focus().unwrap();
+        tray_handle
+            .get_item("toggle")
+            .set_title("Hide Cinny")
+            .unwrap();
+    };
+}
+
+pub fn system_tray_handler<R: tauri::Runtime>(app: &tauri::AppHandle<R>, event: SystemTrayEvent) {
+    let tray_handle = match app.tray_handle_by_id(TRAY_LABEL) {
+        Some(h) => h,
+        None => return,
+    };
+    let window = app.get_window("main").unwrap();
+
+    match event {
+        SystemTrayEvent::LeftClick { .. } => {
+            toggle_window_state(window, tray_handle);
+        }
+        SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
+            "quit" => {
+                app.exit(0);
+            }
+            "toggle" => toggle_window_state(window, tray_handle),
+            _ => {}
+        },
+        _ => {}
+    }
+}
diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json
index c5fa963..30df2f3 100644
--- a/src-tauri/tauri.conf.json
+++ b/src-tauri/tauri.conf.json
@@ -72,6 +72,11 @@
     ],
     "security": {
       "csp": "script-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'"
+    },
+    "systemTray": {
+      "iconPath": "icons/32x32.png",
+      "iconAsTemplate": true,
+      "menuOnLeftClick": false
     }
   }
 }
-- 
2.51.2