Skip to content

Instantly share code, notes, and snippets.

@4gray
Created January 28, 2023 22:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4gray/402c88307c11b9e48209b360c5b6560d to your computer and use it in GitHub Desktop.
Save 4gray/402c88307c11b9e48209b360c5b6560d to your computer and use it in GitHub Desktop.
Create menubar app with Tauri
$ npm create tauri-app
"systemTray": {
"iconPath": "icons/icon.png",
"iconAsTemplate": true
}
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");
}
[dependencies]
tauri-plugin-positioner = { version = "1.0.4", features = ["system-tray"] }
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");
}
{
"fullscreen": false,
"height": 600,
"resizable": false,
"title": "menubar",
"width": 600,
"visible": false,
"hiddenTitle": true,
"decorations": false,
"focus": false,
"transparent": false
}
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");
}
"systemTray": {
"iconPath": "icons/icon.png",
"iconAsTemplate": true,
"menuOnLeftClick": false
}
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);
}
_ => {}
},
_ => {}
}
})
...,
"macOSPrivateApi": true,
"windows": [
{
...,
"transparent": true
}
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;
}
}
...
<body class="arrow">
...
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment