2024-12-14 15:38:32 +08:00
|
|
|
mod autostart;
|
2025-01-20 19:38:59 +08:00
|
|
|
mod shortcut;
|
2025-01-10 14:59:03 +08:00
|
|
|
|
2025-01-20 19:38:59 +08:00
|
|
|
use autostart::{change_autostart, enable_autostart};
|
2024-12-22 15:35:05 +08:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
use tauri::ActivationPolicy;
|
2025-01-20 19:38:59 +08:00
|
|
|
use tauri::{AppHandle, Emitter, Listener, Manager, WebviewWindow};
|
|
|
|
|
use tauri_plugin_autostart::MacosLauncher;
|
|
|
|
|
use tauri_plugin_deep_link::DeepLinkExt;
|
2024-10-13 17:41:16 +08:00
|
|
|
|
2024-11-30 15:29:00 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
fn change_window_height(handle: AppHandle, height: u32) {
|
|
|
|
|
let window: WebviewWindow = handle.get_webview_window("main").unwrap();
|
|
|
|
|
|
|
|
|
|
let mut size = window.outer_size().unwrap();
|
|
|
|
|
size.height = height;
|
|
|
|
|
window.set_size(size).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// #[tauri::command]
|
|
|
|
|
// fn show_panel(handle: AppHandle) {
|
|
|
|
|
// let panel = handle.get_webview_panel("main").unwrap();
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// panel.show();
|
|
|
|
|
// }
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// #[tauri::command]
|
|
|
|
|
// fn hide_panel(handle: AppHandle) {
|
|
|
|
|
// let panel = handle.get_webview_panel("main").unwrap();
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// panel.order_out(None);
|
|
|
|
|
// }
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// #[tauri::command]
|
|
|
|
|
// fn close_panel(handle: AppHandle) {
|
|
|
|
|
// let panel = handle.get_webview_panel("main").unwrap();
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// panel.released_when_closed(true);
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// panel.close();
|
|
|
|
|
// }
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2024-12-26 10:08:55 +08:00
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
|
struct ThemeChangedPayload {
|
|
|
|
|
is_dark_mode: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 14:59:03 +08:00
|
|
|
#[derive(Clone, serde::Serialize)]
|
|
|
|
|
struct Payload {
|
|
|
|
|
args: Vec<String>,
|
|
|
|
|
cwd: String,
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-13 17:41:16 +08:00
|
|
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
|
|
|
|
pub fn run() {
|
2024-12-19 12:06:40 +08:00
|
|
|
let mut ctx = tauri::generate_context!();
|
|
|
|
|
|
2024-10-13 17:41:16 +08:00
|
|
|
tauri::Builder::default()
|
2025-01-06 10:50:44 +08:00
|
|
|
// .plugin(tauri_nspanel::init())
|
2025-01-09 17:41:03 +08:00
|
|
|
.plugin(tauri_plugin_oauth::init())
|
2024-10-28 17:34:48 +08:00
|
|
|
.plugin(tauri_plugin_http::init())
|
2024-10-13 17:41:16 +08:00
|
|
|
.plugin(tauri_plugin_shell::init())
|
2024-12-11 10:45:54 +08:00
|
|
|
.plugin(tauri_plugin_autostart::init(
|
|
|
|
|
MacosLauncher::AppleScript,
|
|
|
|
|
None,
|
|
|
|
|
))
|
2024-12-19 12:06:40 +08:00
|
|
|
.plugin(tauri_plugin_theme::init(ctx.config_mut()))
|
2025-01-10 14:59:03 +08:00
|
|
|
.plugin(tauri_plugin_deep_link::init())
|
|
|
|
|
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
|
|
|
|
|
// println!("{}, {argv:?}, {cwd}", app.package_info().name);
|
|
|
|
|
app.emit("single-instance", Payload { args: argv, cwd })
|
|
|
|
|
.unwrap();
|
|
|
|
|
}))
|
2025-01-20 19:38:59 +08:00
|
|
|
.plugin(tauri_plugin_store::Builder::default().build())
|
2024-11-30 15:29:00 +08:00
|
|
|
.invoke_handler(tauri::generate_handler![
|
|
|
|
|
change_window_height,
|
2025-01-20 19:38:59 +08:00
|
|
|
shortcut::change_shortcut,
|
|
|
|
|
shortcut::unregister_shortcut,
|
|
|
|
|
shortcut::get_current_shortcut,
|
2024-12-14 15:38:32 +08:00
|
|
|
change_autostart,
|
2024-12-24 18:24:53 +08:00
|
|
|
hide_coco,
|
2024-12-26 14:22:20 +08:00
|
|
|
switch_tray_icon,
|
2025-01-06 10:50:44 +08:00
|
|
|
// show_panel,
|
|
|
|
|
// hide_panel,
|
|
|
|
|
// close_panel
|
2025-01-20 19:38:59 +08:00
|
|
|
shortcut::check_shortcut_available,
|
2024-11-30 15:29:00 +08:00
|
|
|
])
|
|
|
|
|
.setup(|app| {
|
|
|
|
|
init(app.app_handle());
|
|
|
|
|
|
2025-01-20 19:38:59 +08:00
|
|
|
shortcut::enable_shortcut(app);
|
2024-11-30 20:41:47 +08:00
|
|
|
enable_tray(app);
|
2024-12-14 15:38:32 +08:00
|
|
|
enable_autostart(app);
|
2024-11-30 15:29:00 +08:00
|
|
|
|
2024-12-17 11:20:50 +08:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
app.set_activation_policy(ActivationPolicy::Accessory);
|
|
|
|
|
|
2024-12-26 10:08:55 +08:00
|
|
|
app.listen("theme-changed", move |event| {
|
2025-01-20 19:38:59 +08:00
|
|
|
if let Ok(payload) = serde_json::from_str::<ThemeChangedPayload>(event.payload()) {
|
2024-12-26 10:08:55 +08:00
|
|
|
// switch_tray_icon(app.app_handle(), payload.is_dark_mode);
|
|
|
|
|
println!("Theme changed: is_dark_mode = {}", payload.is_dark_mode);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-10 14:59:03 +08:00
|
|
|
#[cfg(any(target_os = "linux", all(debug_assertions, windows)))]
|
|
|
|
|
app.deep_link().register_all()?;
|
|
|
|
|
|
|
|
|
|
app.deep_link().on_open_url(|event| {
|
|
|
|
|
dbg!(event.urls());
|
|
|
|
|
});
|
|
|
|
|
|
2024-11-30 15:29:00 +08:00
|
|
|
Ok(())
|
|
|
|
|
})
|
2024-12-19 12:06:40 +08:00
|
|
|
.run(ctx)
|
2024-10-13 17:41:16 +08:00
|
|
|
.expect("error while running tauri application");
|
|
|
|
|
}
|
2024-11-30 15:29:00 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
fn init(_app_handle: &AppHandle) {
|
|
|
|
|
// let window: WebviewWindow = app_handle.get_webview_window("main").unwrap();
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// let panel = window.to_panel().unwrap();
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// let delegate = panel_delegate!(MyPanelDelegate {
|
|
|
|
|
// window_did_become_key,
|
|
|
|
|
// window_did_resign_key
|
|
|
|
|
// });
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// let handle = app_handle.to_owned();
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// delegate.set_listener(Box::new(move |delegate_name: String| {
|
|
|
|
|
// match delegate_name.as_str() {
|
|
|
|
|
// "window_did_become_key" => {
|
|
|
|
|
// let app_name = handle.package_info().name.to_owned();
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// println!("[info]: {:?} panel becomes key window!", app_name);
|
|
|
|
|
// }
|
|
|
|
|
// "window_did_resign_key" => {
|
|
|
|
|
// println!("[info]: panel resigned from key window!");
|
|
|
|
|
// }
|
|
|
|
|
// _ => (),
|
|
|
|
|
// }
|
|
|
|
|
// }));
|
2025-01-02 14:41:54 +08:00
|
|
|
|
2025-01-06 10:50:44 +08:00
|
|
|
// panel.set_delegate(delegate);
|
2025-01-02 14:41:54 +08:00
|
|
|
}
|
2024-11-30 15:29:00 +08:00
|
|
|
|
2024-12-24 18:24:53 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
fn hide_coco(app: tauri::AppHandle) {
|
|
|
|
|
if let Some(window) = app.get_window("main") {
|
|
|
|
|
match window.is_visible() {
|
|
|
|
|
Ok(true) => {
|
|
|
|
|
if let Err(err) = window.hide() {
|
|
|
|
|
eprintln!("Failed to hide the window: {}", err);
|
|
|
|
|
} else {
|
|
|
|
|
println!("Window successfully hidden.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(false) => {
|
|
|
|
|
println!("Window is already hidden.");
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
eprintln!("Failed to check window visibility: {}", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("Main window not found.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 11:20:50 +08:00
|
|
|
fn handle_open_coco(app: &AppHandle) {
|
|
|
|
|
println!("Open Coco menu clicked!");
|
|
|
|
|
|
|
|
|
|
if let Some(window) = app.get_window("main") {
|
|
|
|
|
window.show().unwrap();
|
|
|
|
|
window.set_focus().unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("Failed to get main window.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-24 18:24:53 +08:00
|
|
|
fn handle_hide_coco(app: &AppHandle) {
|
|
|
|
|
println!("Hide Coco menu clicked!");
|
|
|
|
|
|
|
|
|
|
if let Some(window) = app.get_window("main") {
|
|
|
|
|
if let Err(err) = window.hide() {
|
|
|
|
|
eprintln!("Failed to hide the window: {}", err);
|
|
|
|
|
} else {
|
|
|
|
|
println!("Window successfully hidden.");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("Main window not found.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-26 14:22:20 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
fn switch_tray_icon(app: tauri::AppHandle, is_dark_mode: bool) {
|
|
|
|
|
let app_handle = app.app_handle();
|
|
|
|
|
|
|
|
|
|
println!("is_dark_mode: {}", is_dark_mode);
|
|
|
|
|
|
|
|
|
|
const DARK_ICON_PATH: &[u8] = include_bytes!("../icons/dark@2x.png");
|
|
|
|
|
const LIGHT_ICON_PATH: &[u8] = include_bytes!("../icons/light@2x.png");
|
|
|
|
|
|
|
|
|
|
let icon_path: &[u8] = if is_dark_mode {
|
|
|
|
|
DARK_ICON_PATH
|
2024-12-26 10:08:55 +08:00
|
|
|
} else {
|
2024-12-26 14:22:20 +08:00
|
|
|
LIGHT_ICON_PATH
|
2024-12-26 10:08:55 +08:00
|
|
|
};
|
|
|
|
|
|
2024-12-26 14:22:20 +08:00
|
|
|
let tray = match app_handle.tray_by_id("tray") {
|
|
|
|
|
Some(tray) => tray,
|
|
|
|
|
None => {
|
|
|
|
|
eprintln!("Tray with ID 'tray' not found");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-12-26 10:08:55 +08:00
|
|
|
};
|
2024-12-26 14:22:20 +08:00
|
|
|
|
|
|
|
|
if let Err(e) = tray.set_icon(Some(
|
|
|
|
|
tauri::image::Image::from_bytes(icon_path)
|
|
|
|
|
.unwrap_or_else(|e| panic!("Failed to load icon from bytes: {}", e)),
|
|
|
|
|
)) {
|
|
|
|
|
eprintln!("Failed to set tray icon: {}", e);
|
|
|
|
|
}
|
2024-12-26 10:08:55 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-30 20:41:47 +08:00
|
|
|
fn enable_tray(app: &mut tauri::App) {
|
|
|
|
|
use tauri::{
|
2024-12-25 17:43:56 +08:00
|
|
|
image::Image,
|
2024-12-11 16:52:13 +08:00
|
|
|
menu::{MenuBuilder, MenuItem},
|
2024-11-30 20:41:47 +08:00
|
|
|
tray::TrayIconBuilder,
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-11 16:52:13 +08:00
|
|
|
let quit_i = MenuItem::with_id(app, "quit", "Quit Coco", true, None::<&str>).unwrap();
|
2024-12-17 16:25:45 +08:00
|
|
|
let settings_i = MenuItem::with_id(app, "settings", "Settings...", true, None::<&str>).unwrap();
|
2024-12-11 10:45:54 +08:00
|
|
|
let open_i = MenuItem::with_id(app, "open", "Open Coco", true, None::<&str>).unwrap();
|
2024-12-17 16:25:45 +08:00
|
|
|
let about_i = MenuItem::with_id(app, "about", "About Coco", true, None::<&str>).unwrap();
|
2025-01-15 10:33:51 +08:00
|
|
|
// let hide_i = MenuItem::with_id(app, "hide", "Hide Coco", true, None::<&str>).unwrap();
|
2024-12-11 10:45:54 +08:00
|
|
|
|
2024-12-11 16:52:13 +08:00
|
|
|
let menu = MenuBuilder::new(app)
|
|
|
|
|
.item(&open_i)
|
2024-12-17 16:25:45 +08:00
|
|
|
.separator()
|
2025-01-15 10:33:51 +08:00
|
|
|
// .item(&hide_i)
|
2024-12-17 16:25:45 +08:00
|
|
|
.item(&about_i)
|
2024-12-11 16:52:13 +08:00
|
|
|
.item(&settings_i)
|
|
|
|
|
.separator()
|
|
|
|
|
.item(&quit_i)
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2024-12-26 10:08:55 +08:00
|
|
|
let _tray = TrayIconBuilder::with_id("tray")
|
2024-12-25 17:43:56 +08:00
|
|
|
// .icon(app.default_window_icon().unwrap().clone())
|
|
|
|
|
.icon(Image::from_bytes(include_bytes!("../icons/light@2x.png")).expect("REASON"))
|
2024-11-30 20:41:47 +08:00
|
|
|
.menu(&menu)
|
|
|
|
|
.on_menu_event(|app, event| match event.id.as_ref() {
|
2024-12-17 16:25:45 +08:00
|
|
|
"open" => {
|
2024-12-17 11:20:50 +08:00
|
|
|
handle_open_coco(app);
|
2024-12-11 10:45:54 +08:00
|
|
|
}
|
2024-12-24 18:24:53 +08:00
|
|
|
"hide" => {
|
|
|
|
|
handle_hide_coco(app);
|
|
|
|
|
}
|
2024-12-17 16:25:45 +08:00
|
|
|
"about" => {
|
|
|
|
|
let _ = app.emit("open_settings", "about");
|
|
|
|
|
}
|
2024-12-10 13:38:31 +08:00
|
|
|
"settings" => {
|
2024-12-22 15:35:05 +08:00
|
|
|
// windows failed to open second window, issue: https://github.com/tauri-apps/tauri/issues/11144 https://github.com/tauri-apps/tauri/issues/8196
|
2024-12-14 15:38:32 +08:00
|
|
|
//#[cfg(windows)]
|
2024-12-15 21:06:23 +08:00
|
|
|
let _ = app.emit("open_settings", "");
|
2024-12-14 15:38:32 +08:00
|
|
|
|
|
|
|
|
// #[cfg(not(windows))]
|
2024-12-22 15:35:05 +08:00
|
|
|
// open_settings(&app);
|
2024-12-10 13:38:31 +08:00
|
|
|
}
|
2024-11-30 20:41:47 +08:00
|
|
|
"quit" => {
|
|
|
|
|
println!("quit menu item was clicked");
|
|
|
|
|
app.exit(0);
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
println!("menu item {:?} not handled", event.id);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.build(app)
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
2024-12-14 15:38:32 +08:00
|
|
|
|
2024-12-15 21:06:23 +08:00
|
|
|
#[allow(dead_code)]
|
2024-12-22 15:35:05 +08:00
|
|
|
fn open_settings(app: &tauri::AppHandle) {
|
2024-12-15 21:06:23 +08:00
|
|
|
use tauri::webview::WebviewBuilder;
|
2024-12-14 15:38:32 +08:00
|
|
|
println!("settings menu item was clicked");
|
|
|
|
|
let window = app.get_webview_window("settings");
|
|
|
|
|
if let Some(window) = window {
|
|
|
|
|
window.show().unwrap();
|
|
|
|
|
window.set_focus().unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
let window = tauri::window::WindowBuilder::new(app, "settings")
|
|
|
|
|
.title("Settings Window")
|
|
|
|
|
.fullscreen(false)
|
2024-12-17 16:25:45 +08:00
|
|
|
.resizable(false)
|
|
|
|
|
.minimizable(false)
|
|
|
|
|
.maximizable(false)
|
|
|
|
|
.inner_size(800.0, 600.0)
|
2024-12-14 15:38:32 +08:00
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let webview_builder =
|
|
|
|
|
WebviewBuilder::new("settings", tauri::WebviewUrl::App("/ui/settings".into()));
|
|
|
|
|
let _webview = window
|
|
|
|
|
.add_child(
|
|
|
|
|
webview_builder,
|
|
|
|
|
tauri::LogicalPosition::new(0, 0),
|
|
|
|
|
window.inner_size().unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|