Created
January 28, 2023 22:59
-
-
Save 4gray/402c88307c11b9e48209b360c5b6560d to your computer and use it in GitHub Desktop.
Create menubar app with Tauri
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
$ npm create tauri-app |
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
"systemTray": { | |
"iconPath": "icons/icon.png", | |
"iconAsTemplate": true | |
} |
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
use tauri::{Manager, SystemTray, SystemTrayEvent, SystemTrayMenu}; | |
fn main() { | |
let system_tray_menu = SystemTrayMenu::new(); | |
tauri::Builder::default() | |
.system_tray(SystemTray::new().with_menu(system_tray_menu)) | |
.on_system_tray_event(|app, event| match event { | |
SystemTrayEvent::LeftClick { | |
position: _, | |
size: _, | |
.. | |
} => { | |
let window = app.get_window("main").unwrap(); | |
// toggle application window | |
if window.is_visible().unwrap() { | |
window.hide().unwrap(); | |
} else { | |
window.show().unwrap(); | |
window.set_focus().unwrap(); | |
} | |
}, | |
_ => {} | |
}) | |
.run(tauri::generate_context!()) | |
.expect("error while running tauri application"); | |
} |
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
[dependencies] | |
tauri-plugin-positioner = { version = "1.0.4", features = ["system-tray"] } |
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
use tauri::{Manager, SystemTray, SystemTrayEvent, SystemTrayMenu}; | |
use tauri_plugin_positioner::{Position, WindowExt}; | |
fn main() { | |
let system_tray_menu = SystemTrayMenu::new(); | |
tauri::Builder::default() | |
.plugin(tauri_plugin_positioner::init()) | |
.system_tray(SystemTray::new().with_menu(system_tray_menu)) | |
.on_system_tray_event(|app, event| { | |
tauri_plugin_positioner::on_tray_event(app, &event); | |
match event { | |
SystemTrayEvent::LeftClick { | |
position: _, | |
size: _, | |
.. | |
} => { | |
let window = app.get_window("main").unwrap(); | |
// use TrayCenter as initial window position | |
let _ = window.move_window(Position::TrayCenter); | |
if window.is_visible().unwrap() { | |
window.hide().unwrap(); | |
} else { | |
window.show().unwrap(); | |
window.set_focus().unwrap(); | |
} | |
} | |
_ => {} | |
} | |
}) | |
.run(tauri::generate_context!()) | |
.expect("error while running tauri application"); | |
} |
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
{ | |
"fullscreen": false, | |
"height": 600, | |
"resizable": false, | |
"title": "menubar", | |
"width": 600, | |
"visible": false, | |
"hiddenTitle": true, | |
"decorations": false, | |
"focus": false, | |
"transparent": false | |
} |
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
fn main() { | |
... | |
tauri::Builder::default() | |
.plugin(tauri_plugin_positioner::init()) | |
.system_tray(SystemTray::new().with_menu(system_tray_menu)) | |
.on_system_tray_event(|app, event| { | |
... | |
}) | |
.on_window_event(|event| match event.event() { | |
tauri::WindowEvent::Focused(is_focused) => { | |
// detect click outside of the focused window and hide the app | |
if !is_focused { | |
event.window().hide().unwrap(); | |
} | |
} | |
_ => {} | |
}) | |
.run(tauri::generate_context!()) | |
.expect("error while running tauri application"); | |
} |
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
"systemTray": { | |
"iconPath": "icons/icon.png", | |
"iconAsTemplate": true, | |
"menuOnLeftClick": false | |
} |
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
fn main() { | |
let quit = CustomMenuItem::new("quit".to_string(), "Quit").accelerator("Cmd+Q"); | |
let system_tray_menu = SystemTrayMenu::new().add_item(quit); | |
tauri::Builder::default() | |
... | |
.on_system_tray_event(|app, event| { | |
tauri_plugin_positioner::on_tray_event(app, &event); | |
match event { | |
... | |
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() { | |
"quit" => { | |
std::process::exit(0); | |
} | |
_ => {} | |
}, | |
_ => {} | |
} | |
}) |
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
..., | |
"macOSPrivateApi": true, | |
"windows": [ | |
{ | |
..., | |
"transparent": true | |
} |
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
body { | |
margin: 0; | |
} | |
.arrow { | |
position: relative; | |
padding: 12px 0; | |
} | |
.arrow:before { | |
content: ''; | |
height: 0; | |
width: 0; | |
border-width: 0 8px 12px 8px; | |
border-style: solid; | |
border-color: transparent transparent #2f2f2f transparent; | |
position: absolute; | |
top: 0px; | |
left: 50%; | |
transform: translateX(-50%); | |
} | |
@media (prefers-color-scheme: dark) { | |
.container { | |
border-radius: 7px; | |
padding: 10px; | |
color: #f6f6f6; | |
background-color: #2f2f2f; | |
} | |
} |
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
... | |
<body class="arrow"> | |
... | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment