This commit is contained in:
2025-03-13 17:24:53 +01:00
parent 993fbb30eb
commit 028e45bb7a
2 changed files with 39 additions and 36 deletions

View File

@ -1,4 +1,5 @@
{
"biome.enabled": true,
"editor.defaultFormatter": "biomejs.biome",
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
@ -6,5 +7,9 @@
"[typescriptreact]": {
"editor.defaultFormatter": "biomejs.biome"
},
"editor.formatOnSave": true
"editor.formatOnSave": true,
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
},
"rust-analyzer.linkedProjects": ["./frontend/src-tauri/Cargo.toml"]
}

View File

@ -1,15 +1,14 @@
use std::path::PathBuf;
use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use tauri::Emitter;
use std::sync::mpsc::channel;
use std::fs;
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
use std::sync::Mutex;
use std::path::PathBuf;
use std::sync::mpsc::channel;
use std::sync::Arc;
use std::sync::Mutex;
use tauri::AppHandle;
use tauri::Emitter;
use tauri::{TitleBarStyle, WebviewUrl, WebviewWindowBuilder};
struct WatcherState {
watcher: Option<RecommendedWatcher>,
}
@ -25,8 +24,7 @@ fn process_png_file(path: &PathBuf, app: AppHandle) -> Result<(), String> {
println!("Processing PNG file: {}", path.display());
// Read the file
let contents = fs::read(path)
.map_err(|e| format!("Failed to read file: {}", e))?;
let contents = fs::read(path).map_err(|e| format!("Failed to read file: {}", e))?;
// Convert to base64
let base64_string = BASE64.encode(&contents);
@ -47,26 +45,27 @@ async fn handle_selected_folder(
app: AppHandle,
) -> Result<String, String> {
let path_buf = PathBuf::from(&path);
if !path_buf.exists() || !path_buf.is_dir() {
return Err("Invalid directory path".to_string());
}
// Stop existing watcher if any
let mut state = state.lock().map_err(|_| "Failed to lock state".to_string())?;
let mut state = state
.lock()
.map_err(|_| "Failed to lock state".to_string())?;
state.watcher = None;
// Create a channel to receive file system events
let (tx, rx) = channel();
// Create a new watcher
let mut watcher = RecommendedWatcher::new(
tx,
Config::default(),
).map_err(|e| format!("Failed to create watcher: {}", e))?;
let mut watcher = RecommendedWatcher::new(tx, Config::default())
.map_err(|e| format!("Failed to create watcher: {}", e))?;
// Start watching the directory
watcher.watch(path_buf.as_ref(), RecursiveMode::Recursive)
watcher
.watch(path_buf.as_ref(), RecursiveMode::Recursive)
.map_err(|e| format!("Failed to watch directory: {}", e))?;
// Store the watcher in state
@ -114,38 +113,37 @@ pub fn run() {
.manage(watcher_state)
.invoke_handler(tauri::generate_handler![handle_selected_folder])
.setup(|app| {
let win_builder =
WebviewWindowBuilder::new(app, "main", WebviewUrl::default())
let win_builder = WebviewWindowBuilder::new(app, "main", WebviewUrl::default())
.hidden_title(true)
.inner_size(480.0, 360.0)
.resizable(false);
// set transparent title bar only when building for macOS
#[cfg(target_os = "macos")]
let win_builder = win_builder.title_bar_style(TitleBarStyle::Transparent);
let window = win_builder.build().unwrap();
// set background color only when building for macOS
#[cfg(target_os = "macos")]
{
use cocoa::appkit::{NSColor, NSWindow};
use cocoa::base::{id, nil};
let ns_window = window.ns_window().unwrap() as id;
unsafe {
let bg_color = NSColor::colorWithRed_green_blue_alpha_(
nil,
245.0 / 255.0,
245.0 / 255.0,
245.0 / 255.0,
1.0,
);
ns_window.setBackgroundColor_(bg_color);
}
use cocoa::appkit::{NSColor, NSWindow};
use cocoa::base::{id, nil};
let ns_window = window.ns_window().unwrap() as id;
unsafe {
let bg_color = NSColor::colorWithRed_green_blue_alpha_(
nil,
245.0 / 255.0,
245.0 / 255.0,
245.0 / 255.0,
1.0,
);
ns_window.setBackgroundColor_(bg_color);
}
}
Ok(())
})
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}