Skip to content

Instantly share code, notes, and snippets.

@Uninen
Created September 28, 2022 12:30
Show Gist options
  • Save Uninen/5377381eb81bdcd71b9d1859e46e3e29 to your computer and use it in GitHub Desktop.
Save Uninen/5377381eb81bdcd71b9d1859e46e3e29 to your computer and use it in GitHub Desktop.
Tauri implementation for titleBarStyle: 'hidden'
// See https://github.com/Uninen/tauri-vue-template for more info
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use cocoa::appkit::{NSWindow, NSWindowStyleMask, NSWindowTitleVisibility};
use tauri::api::shell;
use tauri::{CustomMenuItem, Manager, Menu, Runtime, Submenu, Window, WindowEvent};
#[tauri::command]
fn backend_add(number: i32) -> i32 {
println!("Backend was called with an argument: {}", number);
number + 2
}
pub trait WindowExt2 {
#[cfg(target_os = "macos")]
fn set_transparent_titlebar(&self, transparent: bool);
#[cfg(target_os = "macos")]
fn position_traffic_lights(&self, x: f64, y: f64);
}
impl<R: Runtime> WindowExt2 for Window<R> {
#[cfg(target_os = "macos")]
fn position_traffic_lights(&self, x: f64, y: f64) {
use cocoa::appkit::{NSView, NSWindowButton};
use cocoa::foundation::NSRect;
use objc::{msg_send, sel, sel_impl};
let window = self.ns_window().unwrap() as cocoa::base::id;
unsafe {
let close = window.standardWindowButton_(NSWindowButton::NSWindowCloseButton);
let miniaturize =
window.standardWindowButton_(NSWindowButton::NSWindowMiniaturizeButton);
let zoom = window.standardWindowButton_(NSWindowButton::NSWindowZoomButton);
let title_bar_container_view = close.superview().superview();
let close_rect: NSRect = msg_send![close, frame];
let button_height = close_rect.size.height;
let title_bar_frame_height = button_height + y;
let mut title_bar_rect = NSView::frame(title_bar_container_view);
title_bar_rect.size.height = title_bar_frame_height;
title_bar_rect.origin.y = NSView::frame(window).size.height - title_bar_frame_height;
let _: () = msg_send![title_bar_container_view, setFrame: title_bar_rect];
let window_buttons = vec![close, miniaturize, zoom];
let space_between = NSView::frame(miniaturize).origin.x - NSView::frame(close).origin.x;
for (i, button) in window_buttons.into_iter().enumerate() {
let mut rect: NSRect = NSView::frame(button);
rect.origin.x = x + (i as f64 * space_between);
button.setFrameOrigin(rect.origin);
}
}
}
#[cfg(target_os = "macos")]
fn set_transparent_titlebar(&self, transparent: bool) {
unsafe {
let id = self.ns_window().unwrap() as cocoa::base::id;
let mut style_mask = id.styleMask();
style_mask.set(
NSWindowStyleMask::NSFullSizeContentViewWindowMask,
transparent,
);
id.setStyleMask_(style_mask);
id.setTitleVisibility_(if transparent {
NSWindowTitleVisibility::NSWindowTitleHidden
} else {
NSWindowTitleVisibility::NSWindowTitleVisible
});
id.setTitlebarAppearsTransparent_(if transparent {
cocoa::base::YES
} else {
cocoa::base::NO
});
}
}
}
fn main() {
let ctx = tauri::generate_context!();
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![backend_add])
.menu(
tauri::Menu::os_default("Tauri Vue Template").add_submenu(Submenu::new(
"Help",
Menu::with_items([CustomMenuItem::new(
"Online Documentation",
"Online Documentation",
)
.into()]),
)),
)
.on_menu_event(|event| {
let event_name = event.menu_item_id();
match event_name {
"Online Documentation" => {
let url = "https://github.com/Uninen/tauri-vue-template".to_string();
shell::open(&event.window().shell_scope(), url, None).unwrap();
}
_ => {}
}
})
.setup(|app| {
let main_window = app.get_window("main").unwrap();
main_window.set_transparent_titlebar(true);
main_window.position_traffic_lights(22.0, 24.0);
main_window.open_devtools();
Ok(())
})
.on_window_event(|e| {
if let WindowEvent::Resized(..) = e.event() {
let win = e.window();
win.position_traffic_lights(22.0, 24.0);
}
})
.run(ctx)
.expect("error while running tauri application");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment