Skip to content

Commit

Permalink
toggle-able progress
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlienard committed Sep 2, 2023
1 parent 139a7be commit 264ebe1
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 73 deletions.
31 changes: 31 additions & 0 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[tauri::command]
pub fn update_tray(
app_handle: tauri::AppHandle,
title: Option<String>,
description: Option<String>,
new_icon: Option<bool>,
) {
let tray_handle = app_handle.tray_handle();
#[cfg(target_os = "macos")]
if let Some(title) = title {
tray_handle.set_title(&title).unwrap();
}
if let Some(description) = description {
tray_handle.get_item("text").set_title(description).unwrap();
}
if let Some(new_icon) = new_icon {
if new_icon {
tray_handle
.set_icon(tauri::Icon::Raw(
include_bytes!("../icons/tray-new.png").to_vec(),
))
.unwrap();
} else {
tray_handle
.set_icon(tauri::Icon::Raw(
include_bytes!("../icons/tray-base.png").to_vec(),
))
.unwrap();
}
}
}
132 changes: 61 additions & 71 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,38 @@
windows_subsystem = "windows"
)]

use std::sync::{Arc, RwLock};
use tauri::{
CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, ActivationPolicy, Size, LogicalSize,
CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
};
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_positioner::{Position, WindowExt};

#[tauri::command]
fn update_tray(
app_handle: tauri::AppHandle,
title: Option<String>,
description: Option<String>,
new_icon: Option<bool>,
) {
let tray_handle = app_handle.tray_handle();
#[cfg(target_os = "macos")]
if let Some(title) = title {
tray_handle.set_title(&title).unwrap();
}
if let Some(description) = description {
tray_handle.get_item("text").set_title(description).unwrap();
}
if let Some(new_icon) = new_icon {
if new_icon {
tray_handle
.set_icon(tauri::Icon::Raw(
include_bytes!("../icons/tray-new.png").to_vec(),
))
.unwrap();
} else {
tray_handle
.set_icon(tauri::Icon::Raw(
include_bytes!("../icons/tray-base.png").to_vec(),
))
.unwrap();
}
}
}
mod commands;
mod tray;

fn main() {
tauri_plugin_deep_link::prepare("de.fabianlars.deep-link-test");
tauri_plugin_deep_link::prepare("app.gitlight");

let title = CustomMenuItem::new("title".to_string(), "GitHub notifications").disabled();
let text = CustomMenuItem::new("text".to_string(), "1 pinned • 2 unread");
let focus = CustomMenuItem::new("focus".to_string(), "Dashboard");
let toggle = CustomMenuItem::new("toggle".to_string(), "Toggle");
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let tray_menu = SystemTrayMenu::new()
.add_item(title)
.add_item(text)
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(focus)
.add_item(toggle)
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(quit);
let tray = SystemTray::new().with_menu(tray_menu);

let is_tray_app = Arc::new(RwLock::new(false));
let is_tray_app_clone = is_tray_app.clone();

tauri::Builder::default()
.setup(|app| {
.setup(move |app| {
let handle = app.handle();
tauri_plugin_deep_link::register("gitlight", move |request| {
handle.emit_all("scheme-request", request).unwrap();
Expand All @@ -70,7 +46,13 @@ fn main() {
app.emit_all("scheme-request", url).unwrap();
}

app.set_activation_policy(ActivationPolicy::Accessory);
if *is_tray_app.read().unwrap() {
tray::activate_tray_mode(&app.handle());
let window = app.get_window("main").unwrap();
window.move_window(Position::TrayCenter).unwrap();
} else {
tray::deactivate_tray_mode(&app.handle());
}

Ok(())
})
Expand All @@ -81,60 +63,68 @@ fn main() {
))
.plugin(tauri_plugin_positioner::init())
.system_tray(tray)
.on_system_tray_event(|app, event| {
.on_system_tray_event(move |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();
if *is_tray_app_clone.read().unwrap() {
let window = app.get_window("main").unwrap();
window.move_window(Position::TrayCenter).unwrap();
if window.is_visible().unwrap() {
window.hide().unwrap();
} else {
window.show().unwrap();
window.set_focus().unwrap();
}
}

window.set_decorations(false).unwrap();
window.set_size(Size::Logical(LogicalSize { width: 400.0, height: 600.0 })).unwrap();
}
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"text" | "focus" => {
let window = app.get_window("main").unwrap();
window.set_focus().unwrap();
}
"toggle" => {
let is_tray_app_value = *is_tray_app_clone.read().unwrap();
let mut is_tray_app_lock = is_tray_app_clone.write().unwrap();
*is_tray_app_lock = !is_tray_app_value;
if is_tray_app_value {
tray::deactivate_tray_mode(app);
} else {
tray::activate_tray_mode(app);
let window = app.get_window("main").unwrap();
window.move_window(Position::TrayCenter).unwrap();
}
}
"quit" => {
std::process::exit(0);
}
_ => {}
},
_ => {}
}
// if let SystemTrayEvent::MenuItemClick { id, .. } = event {
// match id.as_str() {
// "text" | "focus" => {
// let window = app.get_window("main").unwrap();
// window.set_focus().unwrap();
// }
// "quit" => {
// std::process::exit(0);
// }
// _ => {}
// }
// }
})
.on_window_event(|event| match event.event() {
tauri::WindowEvent::CloseRequested { api, .. } => {
#[cfg(not(target_os = "macos"))]
event.window().hide().unwrap();

#[cfg(target_os = "macos")]
tauri::AppHandle::hide(&event.window().app_handle()).unwrap();

api.prevent_close();
}
tauri::WindowEvent::Focused(is_focused) => {
// detect click outside of the focused window and hide the app
if !is_focused {
event.window().hide().unwrap();
}
}
_ => {} // if let tauri::WindowEvent::CloseRequested { api, .. } = event.event() {
// #[cfg(not(target_os = "macos"))]
// event.window().hide().unwrap();

// #[cfg(target_os = "macos")]
// tauri::AppHandle::hide(&event.window().app_handle()).unwrap();

// api.prevent_close();
// }
_ => {}
})
.invoke_handler(tauri::generate_handler![update_tray])
.invoke_handler(tauri::generate_handler![commands::update_tray])
.build(tauri::generate_context!())
.expect("error while running tauri application")
.run(|_app_handle, event| {
Expand Down
20 changes: 20 additions & 0 deletions src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use tauri::{LogicalSize, Manager, Size};
use tauri_plugin_window_state::{AppHandleExt, StateFlags, WindowExt};

pub fn activate_tray_mode(app: &tauri::AppHandle) {
app.save_window_state(StateFlags::all()).unwrap();
let window = app.get_window("main").unwrap();
window.set_decorations(false).unwrap();
window
.set_size(Size::Logical(LogicalSize {
width: 400.0,
height: 600.0,
}))
.unwrap();
}

pub fn deactivate_tray_mode(app: &tauri::AppHandle) {
let window = app.get_window("main").unwrap();
window.restore_state(StateFlags::all()).unwrap();
window.set_decorations(true).unwrap();
}
5 changes: 3 additions & 2 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@
"resizable": true,
"title": "GitLight",
"width": 1400,
"titleBarStyle": "Overlay",
"titleBarStyle": "Transparent",
"hiddenTitle": true,
"url": "/dashboard"
}
],
"systemTray": {
"iconPath": "icons/tray-base.png"
"iconPath": "icons/tray-base.png",
"menuOnLeftClick": false
}
}
}

0 comments on commit 264ebe1

Please sign in to comment.