From 8f01b04efea8bb309a7ff7161f6da61f6f9ce578 Mon Sep 17 00:00:00 2001 From: Nikola Hristov Date: Sun, 6 Oct 2024 00:03:04 +0300 Subject: [PATCH] --- src-tauri/Source/main.rs | 128 +++++++++++++++++++++++++-------------- src-tauri/build.rs | 4 +- 2 files changed, 83 insertions(+), 49 deletions(-) diff --git a/src-tauri/Source/main.rs b/src-tauri/Source/main.rs index c7d9ef3..a598288 100644 --- a/src-tauri/Source/main.rs +++ b/src-tauri/Source/main.rs @@ -1,17 +1,33 @@ #[allow(unused_attributes)] -#[cfg_attr(all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows")] +#[cfg_attr( + all(not(debug_assertions), target_os = "windows"), + windows_subsystem = "windows" +)] extern crate tauri; use std::fs; -use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu}; + +use tauri::{ + CustomMenuItem, + Manager, + SystemTray, + SystemTrayEvent, + SystemTrayMenu, +}; fn main() { tauri::Builder::default() .system_tray( SystemTray::new().with_menu( SystemTrayMenu::new() - .add_item(CustomMenuItem::new("devtools".to_string(), "Open Devtools")) - .add_item(CustomMenuItem::new("run".to_string(), "Run Scripts")) + .add_item(CustomMenuItem::new( + "devtools".to_string(), + "Open Devtools", + )) + .add_item(CustomMenuItem::new( + "run".to_string(), + "Run Scripts", + )) .add_item(CustomMenuItem::new("show".to_string(), "Show")) .add_item(CustomMenuItem::new("hide".to_string(), "Hide")) .add_item(CustomMenuItem::new("exit".to_string(), "Exit")), @@ -20,58 +36,78 @@ fn main() { .on_system_tray_event(|app, event| { if let SystemTrayEvent::MenuItemClick { id, .. } = event { match id.as_str() { - "devtools" => app.windows().into_iter().for_each(|(_label, window)| { - window.open_devtools(); - }), - "run" => app.windows().into_iter().for_each(|(_label, window)| { - window - .eval( - &fs::read_to_string( - app.path_resolver() - .resolve_resource(format!( - "../Target/scripts/{}.js", - window.label() - )) - .expect("Cannot resolve_resource."), - ) - .expect("Error while reading JS file."), - ) - .expect("Script did not execute successfully."); + "devtools" => { + app.windows().into_iter().for_each( + |(_label, window)| { + window.open_devtools(); + }, + ) + }, + "run" => { + app.windows().into_iter().for_each( + |(_label, window)| { + window + .eval( + &fs::read_to_string( + app.path_resolver() + .resolve_resource(format!( + "../Target/scripts/{}.js", + window.label() + )) + .expect( + "Cannot resolve_resource.", + ), + ) + .expect("Error while reading JS file."), + ) + .expect( + "Script did not execute successfully.", + ); - window - .eval(&format!( - r#" + window + .eval(&format!( + r#" var style = document.createElement('style'); style.innerHTML = `{}`; style.setAttribute('data-from', 'tauri'); document.head.appendChild(style); "#, - &fs::read_to_string( - app.path_resolver() - .resolve_resource(format!( - "../Target/styles/{}.css", - window.label() - )) - .expect("cannot resolve_resource.") - ) - .expect("Error while reading CSS file.") - )) - .expect("Style did not load successfully."); - }), + &fs::read_to_string( + app.path_resolver() + .resolve_resource(format!( + "../Target/styles/{}.css", + window.label() + )) + .expect( + "cannot resolve_resource." + ) + ) + .expect( + "Error while reading CSS file." + ) + )) + .expect("Style did not load successfully."); + }, + ) + }, "show" => { - app.windows().into_iter().for_each(|(_label, window)| { - window.show().unwrap(); - }); - } + app.windows().into_iter().for_each( + |(_label, window)| { + window.show().unwrap(); + }, + ); + }, "hide" => { - app.windows().into_iter().for_each(|(_label, window)| { - window.hide().unwrap(); - }); - } + app.windows().into_iter().for_each( + |(_label, window)| { + window.hide().unwrap(); + }, + ); + }, "exit" => { std::process::exit(0); - } - _ => {} + }, + _ => {}, } } }) diff --git a/src-tauri/build.rs b/src-tauri/build.rs index c1ea373..2846c57 100644 --- a/src-tauri/build.rs +++ b/src-tauri/build.rs @@ -1,3 +1 @@ -fn main() { - tauri_build::build() -} +fn main() { tauri_build::build() }