Skip to content

Instantly share code, notes, and snippets.

@rikschennink
Created December 13, 2022 14:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rikschennink/a074ee148be7869eee543612694a82f0 to your computer and use it in GitHub Desktop.
Save rikschennink/a074ee148be7869eee543612694a82f0 to your computer and use it in GitHub Desktop.
Tauri MacOS tray app
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn sync(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
// tray
use tauri::{SystemTray, CustomMenuItem, SystemTrayMenu, SystemTrayMenuItem, SystemTrayEvent, Manager, Position, PhysicalPosition, WindowEvent};
fn main() {
let tray_menu = SystemTrayMenu::new()
.add_item(CustomMenuItem::new("clear", "Clear completed"))
.add_item(CustomMenuItem::new("version", "Beta 0.0.10").disabled())
.add_item(CustomMenuItem::new("settings", "Settings…"))
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("reset", "Reset list"))
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("update", "Check for updates…"))
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("quit", "Quit"));
let system_tray = SystemTray::new()
.with_menu(tray_menu);
tauri::Builder::default()
.system_tray(system_tray)
.on_system_tray_event(|app, event| match event {
//
// Handle menu item interaction
//
SystemTrayEvent::MenuItemClick { id, .. } => {
match id.as_str() {
// Clicked on Quit button
"quit" => {
std::process::exit(0);
}
// Clicked on Clear button
"clear" => {
print!("HIDE")
}
// Clicked on Settings button
"settings" => {
print!("RESET")
}
// Clicked on Reset button
"reset" => {
print!("RESET")
}
// Clicked on Update button
"update" => {
print!("RESET")
}
// else do nothing
_ => {}
}
}
//
// Handle system tray interaction
//
SystemTrayEvent::LeftClick {
position,
size,
..
} => {
let window = app.get_window("main").unwrap();
if window.is_visible().unwrap() {
window.hide().unwrap();
}
else {
let icon_width = size.width as i32;
let window_size = window.outer_size().unwrap();
let window_width = window_size.width as i32;
let tray_icon_x = position.x as i32;
let window_position = PhysicalPosition {
x: (tray_icon_x + (icon_width / 2)) - (window_width / 2),
y: 0
};
window.set_position(Position::Physical(window_position)).unwrap();
window.show().unwrap();
window.set_focus().unwrap();
}
}
// else do nothing
_ => {}
})
.on_window_event(|event| match event.event() {
// Hide window when clicked outside
WindowEvent::Focused(false) => {
event.window().hide().unwrap();
}
// else do nothing
_ => {}
})
.invoke_handler(tauri::generate_handler![sync])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
{
"build": {
"beforeDevCommand": "",
"beforeBuildCommand": "",
"devPath": "../src",
"distDir": "../src",
"withGlobalTauri": true
},
"package": {
"productName": "hotlist-tauri",
"version": "0.0.0"
},
"tauri": {
"allowlist": {
"all": false,
"shell": {
"all": false,
"open": true
}
},
"bundle": {
"active": true,
"category": "DeveloperTool",
"copyright": "",
"deb": {
"depends": []
},
"externalBin": [],
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
],
"identifier": "com.tauri.dev",
"longDescription": "",
"macOS": {
"entitlements": null,
"exceptionDomain": "",
"frameworks": [],
"providerShortName": null,
"signingIdentity": null
},
"resources": [],
"shortDescription": "",
"targets": "all",
"windows": {
"certificateThumbprint": null,
"digestAlgorithm": "sha256",
"timestampUrl": ""
}
},
"security": {
"csp": null
},
"updater": {
"active": false
},
"macOSPrivateApi": true,
"windows": [
{
"width": 400,
"height": 400,
"title": "Hotlist",
"hiddenTitle": true,
"visible": false,
"focus": false,
"fullscreen": false,
"resizable": false,
"fileDropEnabled": false,
"alwaysOnTop": false,
"decorations": false,
"transparent": true
}
],
"systemTray": {
"iconPath": "icons/icon.png",
"iconAsTemplate": true,
"menuOnLeftClick": false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment