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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use tauri::plugin::{Builder, TauriPlugin};
use tauri::{Emitter, Error, Listener, Runtime, WebviewWindow};
#[cfg(target_os = "macos")]
mod traffic;
#[cfg(target_os = "linux")]
mod dconf;
#[cfg(target_os = "windows")]
mod snap;
#[cfg(target_os = "macos")]
#[macro_use]
extern crate objc;
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the decoration APIs.
pub trait WebviewWindowExt {
fn create_overlay_titlebar(&self) -> Result<&WebviewWindow, Error>;
#[cfg(target_os = "macos")]
fn set_traffic_lights_inset(&self, x: f32, y: f32) -> Result<&WebviewWindow, Error>;
#[cfg(target_os = "macos")]
fn make_transparent(&self) -> Result<&WebviewWindow, Error>;
#[cfg(target_os = "macos")]
fn set_window_level(&self, level: u32) -> Result<&WebviewWindow, Error>;
}
impl<'a> WebviewWindowExt for WebviewWindow {
/// Create a custom titlebar overlay.
/// This will remove the default titlebar and create a draggable area for the titlebar.
/// On Windows, it will also create custom window controls.
fn create_overlay_titlebar(&self) -> Result<&WebviewWindow, Error> {
#[cfg(target_os = "windows")]
self.set_decorations(false)?;
let win2 = self.clone();
self.listen("decoration-page-load", move |_event| {
// println!("decoration-page-load event received")
// Create a transparent draggable area for the titlebar
let script_tb = include_str!("js/titlebar.js");
win2.eval(script_tb)
.unwrap_or_else(|e| println!("decoration error: {:?}", e));
// Custom window controls for linux
#[cfg(target_os = "linux")]
{
use linicon::{lookup_icon, IconType};
use std::io::prelude::*;
let mut control_script = include_str!("./js/linux-controls.js").to_string();
let mut controls = Vec::new();
if win2.is_minimizable().unwrap_or(false) {
controls.push("minimize".to_string());
}
if win2.is_maximizable().unwrap_or(false) && win2.is_resizable().unwrap_or(false) {
controls.push("maximize".to_string());
}
if win2.is_closable().unwrap_or(false) {
controls.push("close".to_string());
}
controls.push("restore".to_string());
for control in controls.iter() {
if let Some(Ok(control_icon)) =
lookup_icon(format!("window-{}-symbolic", control))
.into_iter()
.find(|icon| match icon {
Ok(icon) => icon.icon_type == IconType::SVG,
Err(_) => false,
})
{
let mut icon_data = String::new();
if let Ok(mut f) = std::fs::File::open(&control_icon.path) {
let _ = f.read_to_string(&mut icon_data);
}
control_script =
control_script.replace(&format!("@win-{}", control), &icon_data);
};
}
controls.remove(controls.len() - 1);
// return this string style 'appmenu:minimize,maximize,close'
if let Ok(app_menu_config) =
dconf::read("/org/gnome/desktop/wm/preferences/button-layout")
{
controls = app_menu_config
.trim_start_matches("appmenu:")
.split(',')
.map(|x| x.to_string())
.collect::<Vec<String>>();
};
let controls = format!("{:?}", controls);
let control_script = control_script.replacen(
"[\"minimize\", \"maximize\", \"close\"]",
&controls,
1,
);
win2.eval(&control_script)
.unwrap_or_else(|e| println!("decoration error: {:?}", e));
}
// On Windows, create custom window controls and install
// the native Snap Layout overlay.
#[cfg(target_os = "windows")]
{
let mut controls = Vec::new();
let mut right_index: u32 = 0;
let mut has_maximize = false;
if win2.is_minimizable().unwrap_or(false) {
controls.push("minimize");
right_index += 1;
}
if win2.is_maximizable().unwrap_or(false) && win2.is_resizable().unwrap_or(false) {
controls.push("maximize");
has_maximize = true;
right_index += 1;
}
if win2.is_closable().unwrap_or(false) {
controls.push("close");
}
let script_controls = include_str!("js/controls.js");
let controls = format!("{:?}", controls);
// this line finds ["minimize", "maximize", "close"] in the file
// and replaces it with only the controls enabled for the window
let script_controls = script_controls.replacen(
"[\"minimize\", \"maximize\", \"close\"]",
&controls,
1,
);
win2.eval(script_controls.as_str())
.unwrap_or_else(|e| println!("decoration error: {:?}", e));
// Install the native Win32 child HWND overlay for Snap Layout.
// The overlay returns HTMAXBUTTON from WM_NCHITTEST, which is
// the Windows-supported path for showing the Snap Layout
// flyout on Windows 11 — no keyboard or mouse simulation.
if has_maximize {
let snap_win = win2.clone();
if let Err(e) = snap::install(
&snap_win,
32,
58,
right_index.saturating_sub(1),
) {
eprintln!("decoration: failed to install snap overlay: {:?}", e);
}
}
// Unlisten the page-load event when the window closes so the
// listener doesn't leak across navigations/reloads. We use
// once-only registration via a flag stored on the window to
// avoid stacking duplicate on_window_event handlers on every
// page load.
let win3 = win2.clone();
let event_id = _event.id();
win2.on_window_event(move |eve| match eve {
tauri::WindowEvent::CloseRequested { .. } => {
win3.unlisten(event_id);
#[cfg(target_os = "windows")]
let _ = snap::uninstall(&win3);
}
_ => {}
});
}
});
Ok(self)
}
/// Set the inset of the traffic lights.
/// This will move the traffic lights to the specified position.
/// This is only available on macOS.
#[cfg(target_os = "macos")]
fn set_traffic_lights_inset(&self, x: f32, y: f32) -> Result<&WebviewWindow, Error> {
ensure_main_thread(self, move |win| {
let ns_window = win.ns_window()?;
let ns_window_handle = traffic::UnsafeWindowHandle(ns_window);
// Store the custom position in the window state
traffic::update_traffic_light_positions(win, x.into(), y.into());
// Apply the position immediately
traffic::position_traffic_lights(ns_window_handle, x.into(), y.into());
Ok(win)
})
}
/// Set the window background to transparent.
/// This helper function is different from Tauri's default
/// as it doesn't use the `transparent` flag or macOS Private APIs.
#[cfg(target_os = "macos")]
fn make_transparent(&self) -> Result<&WebviewWindow, Error> {
use cocoa::{
appkit::NSColor,
base::{id, nil},
foundation::NSString,
};
// Make webview background transparent
self.with_webview(|webview| unsafe {
let id = webview.inner() as *mut objc::runtime::Object;
let no: id = msg_send![class!(NSNumber), numberWithBool:0];
let _: id =
msg_send![id, setValue:no forKey: NSString::alloc(nil).init_str("drawsBackground")];
})?;
// Make window background transparent
ensure_main_thread(self, move |win| {
let ns_win = win.ns_window()? as id;
unsafe {
let win_bg_color =
NSColor::colorWithSRGBRed_green_blue_alpha_(nil, 0.0, 0.0, 0.0, 0.0);
let _: id = msg_send![ns_win, setBackgroundColor: win_bg_color];
}
Ok(win)
})
}
/// Set the window level.
/// This will set the window level to the specified value.
/// NSWindowLevel values can be found [here](https://developer.apple.com/documentation/appkit/nswindowlevel?language=objc).
/// This is only available on macOS.
#[cfg(target_os = "macos")]
fn set_window_level(&self, level: u32) -> Result<&WebviewWindow, Error> {
ensure_main_thread(self, move |win| {
let ns_win = win.ns_window()? as cocoa::base::id;
unsafe {
let _: () = msg_send![ns_win, setLevel: level];
}
Ok(win)
})
}
}
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("decoration")
.on_page_load(|win, _payload: &tauri::webview::PageLoadPayload| {
match win.emit("decoration-page-load", ()) {
Ok(_) => {}
Err(e) => println!("decoration error: {:?}", e),
}
})
.on_window_ready(|_win| {
#[cfg(target_os = "macos")]
traffic::setup_traffic_light_positioner(_win);
return;
})
.build()
}
#[cfg(target_os = "macos")]
fn is_main_thread() -> bool {
// pthread_main_np() is the reliable way to check if we're on the main
// thread on macOS. Checking the thread name is fragile because Tauri
// doesn't guarantee the main thread is named "main".
extern "C" {
fn pthread_main_np() -> i32;
}
unsafe { pthread_main_np() != 0 }
}
#[cfg(target_os = "macos")]
fn ensure_main_thread<F>(
win: &WebviewWindow,
main_action: F,
) -> Result<&WebviewWindow, tauri::Error>
where
F: FnOnce(&WebviewWindow) -> Result<&WebviewWindow, Error> + Send + 'static,
{
match is_main_thread() {
true => {
main_action(win)?;
Ok(win)
}
false => {
let win2 = win.clone();
match win.run_on_main_thread(move || {
// Don't unwrap — panicking inside run_on_main_thread is
// silently swallowed and can stop controls from rendering.
if let Err(e) = main_action(&win2) {
eprintln!("decoration: main_thread action failed: {:?}", e);
}
}) {
Ok(_) => Ok(win),
Err(e) => Err(e),
}
}
}
}