-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tray icon and custom window impl
- Loading branch information
Showing
9 changed files
with
159 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,63 @@ | ||
// Prevents additional console window on Windows in release, DO NOT REMOVE!! | ||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] | ||
#![cfg_attr( | ||
all(not(debug_assertions), target_os = "windows"), | ||
windows_subsystem = "windows" | ||
)] | ||
|
||
#[cfg(target_os = "macos")] | ||
#[macro_use] | ||
extern crate objc; | ||
|
||
mod window_custom; | ||
|
||
use tauri::{ | ||
CustomMenuItem, Manager, RunEvent, SystemTray, SystemTrayEvent, | ||
SystemTrayMenu, | ||
}; | ||
use window_custom::WindowExt as _; | ||
|
||
fn main() { | ||
tauri::Builder::default() | ||
// System tray configuration | ||
let tray = SystemTray::new().with_menu( | ||
SystemTrayMenu::new() | ||
.add_item(CustomMenuItem::new("show_overlayed", "Show Overlayed")) | ||
.add_item(CustomMenuItem::new("quit", "Quit")) | ||
); | ||
|
||
let app = tauri::Builder::default() | ||
.plugin(tauri_plugin_websocket::init()) | ||
.run(tauri::generate_context!()) | ||
.expect("error while running tauri application"); | ||
.setup(|app| { | ||
let window = app.get_window("main").unwrap(); | ||
#[cfg(target_os = "macos")] | ||
window.set_transparent_titlebar(true, true); | ||
// Move the window to the center of the screen | ||
window.center().expect("Cannot move window!"); | ||
|
||
// Open dev tools | ||
#[cfg(debug_assertions)] | ||
window.open_devtools(); | ||
Ok(()) | ||
}) | ||
// Add the system tray | ||
.system_tray(tray) | ||
// Handle system tray events | ||
.on_system_tray_event(|app, event| match event { | ||
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() { | ||
"quit" => std::process::exit(0), | ||
"show_overlayed" => { | ||
let window = app.get_window("main").unwrap(); | ||
window.show().unwrap(); | ||
window.set_focus().unwrap(); | ||
} | ||
_ => {} | ||
}, | ||
_ => {} | ||
}) | ||
.build(tauri::generate_context!()) | ||
.expect("An error occured while running the app!"); | ||
|
||
app.run(|_app_handle, e| match e { | ||
RunEvent::Ready => { | ||
} | ||
_ => {} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#[cfg(target_os = "macos")] | ||
use cocoa::appkit::{NSWindow, NSWindowButton, NSWindowStyleMask, NSWindowTitleVisibility}; | ||
|
||
#[cfg(target_os = "macos")] | ||
use objc::runtime::YES; | ||
use tauri::{Runtime, Window}; | ||
|
||
pub trait WindowExt { | ||
#[cfg(target_os = "macos")] | ||
fn set_transparent_titlebar(&self, title_transparent: bool, remove_toolbar: bool); | ||
} | ||
|
||
impl<R: Runtime> WindowExt for Window<R> { | ||
#[cfg(target_os = "macos")] | ||
fn set_transparent_titlebar(&self, title_transparent: bool, remove_tool_bar: bool) { | ||
unsafe { | ||
let id = self.ns_window().unwrap() as cocoa::base::id; | ||
NSWindow::setTitlebarAppearsTransparent_(id, cocoa::base::YES); | ||
let mut style_mask = id.styleMask(); | ||
style_mask.set( | ||
NSWindowStyleMask::NSFullSizeContentViewWindowMask, | ||
title_transparent, | ||
); | ||
id.setStyleMask_(style_mask); | ||
if remove_tool_bar { | ||
let close_button = id.standardWindowButton_(NSWindowButton::NSWindowCloseButton); | ||
let _: () = msg_send![close_button, setHidden: YES]; | ||
let min_button = | ||
id.standardWindowButton_(NSWindowButton::NSWindowMiniaturizeButton); | ||
let _: () = msg_send![min_button, setHidden: YES]; | ||
let zoom_button = id.standardWindowButton_(NSWindowButton::NSWindowZoomButton); | ||
let _: () = msg_send![zoom_button, setHidden: YES]; | ||
} | ||
id.setTitleVisibility_(if title_transparent { | ||
NSWindowTitleVisibility::NSWindowTitleHidden | ||
} else { | ||
NSWindowTitleVisibility::NSWindowTitleVisible | ||
}); | ||
id.setTitlebarAppearsTransparent_(if title_transparent { | ||
cocoa::base::YES | ||
} else { | ||
cocoa::base::NO | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters