From 00eb6d6e18b09b6c6083327614a8d8b32b82748e Mon Sep 17 00:00:00 2001 From: ByteAtATime Date: Thu, 19 Jun 2025 20:49:39 -0700 Subject: [PATCH] style: formatting --- src-tauri/src/browser_extension.rs | 30 +++- src-tauri/src/clipboard.rs | 148 ++++++++++---------- src-tauri/src/extensions.rs | 2 +- src-tauri/src/filesystem.rs | 2 +- src-tauri/src/lib.rs | 212 ++++++++++++++--------------- src-tauri/src/oauth.rs | 95 +++++++------ 6 files changed, 253 insertions(+), 236 deletions(-) diff --git a/src-tauri/src/browser_extension.rs b/src-tauri/src/browser_extension.rs index b74e097..29d0dad 100644 --- a/src-tauri/src/browser_extension.rs +++ b/src-tauri/src/browser_extension.rs @@ -56,7 +56,8 @@ enum IncomingMessage { pub struct WsState { pub connection: Arc>>>, pub is_connected: Arc>, - pub pending_requests: Arc>>>>, + pub pending_requests: + Arc>>>>, pub request_id_counter: Arc>, } @@ -90,7 +91,11 @@ async fn handle_connection(stream: TcpStream, app_handle: AppHandle) { let sender_task = tokio::spawn(async move { while let Some(msg_to_send) = rx.recv().await { - if ws_sender.send(Message::Binary(msg_to_send.into())).await.is_err() { + if ws_sender + .send(Message::Binary(msg_to_send.into())) + .await + .is_err() + { break; } } @@ -113,14 +118,24 @@ async fn handle_connection(stream: TcpStream, app_handle: AppHandle) { "result": null, "id": id }); - let tx = app_clone_for_receiver.state::().connection.lock().unwrap().clone(); + let tx = app_clone_for_receiver + .state::() + .connection + .lock() + .unwrap() + .clone(); if let Some(tx) = tx { let _ = tx.send(response.to_string()).await; } } } Ok(IncomingMessage::Response { id, result, error }) => { - let sender = app_clone_for_receiver.state::().pending_requests.lock().unwrap().remove(&id); + let sender = app_clone_for_receiver + .state::() + .pending_requests + .lock() + .unwrap() + .remove(&id); if let Some(sender) = sender { if !error.is_null() { let _ = sender.send(Err(error.to_string())); @@ -161,7 +176,9 @@ pub async fn run_server(app_handle: AppHandle) { } #[tauri::command] -pub async fn browser_extension_check_connection(state: tauri::State<'_, WsState>) -> Result { +pub async fn browser_extension_check_connection( + state: tauri::State<'_, WsState>, +) -> Result { Ok(*state.is_connected.lock().unwrap()) } @@ -172,7 +189,7 @@ pub async fn browser_extension_request( state: tauri::State<'_, WsState>, ) -> Result { use std::time::Duration; - + let tx = { let lock = state.connection.lock().unwrap(); lock.clone() @@ -212,4 +229,3 @@ pub async fn browser_extension_request( Err("Browser extension not connected".into()) } } - diff --git a/src-tauri/src/clipboard.rs b/src-tauri/src/clipboard.rs index f4ae7a1..1c25ed5 100644 --- a/src-tauri/src/clipboard.rs +++ b/src-tauri/src/clipboard.rs @@ -5,119 +5,121 @@ use tauri_plugin_clipboard_manager::ClipboardExt; #[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] pub struct ReadResult { - text: Option, - html: Option, - file: Option, + text: Option, + html: Option, + file: Option, } #[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] pub struct ClipboardContent { - text: Option, - html: Option, - file: Option, + text: Option, + html: Option, + file: Option, } #[derive(serde::Serialize, serde::Deserialize, Debug, Default, Clone)] #[serde(rename_all = "camelCase")] pub struct CopyOptions { - concealed: Option, + concealed: Option, } #[tauri::command] pub async fn clipboard_read_text(app: tauri::AppHandle) -> Result { - let clipboard = app.clipboard(); - let text = clipboard.read_text().ok(); - Ok(ReadResult { - text, - html: None, - file: None - }) + let clipboard = app.clipboard(); + let text = clipboard.read_text().ok(); + Ok(ReadResult { + text, + html: None, + file: None, + }) } #[tauri::command] pub async fn clipboard_read(app: tauri::AppHandle) -> Result { - let clipboard = app.clipboard(); - let text = clipboard.read_text().ok(); - let html = None; // read_html is not supported by the plugin + let clipboard = app.clipboard(); + let text = clipboard.read_text().ok(); + let html = None; // read_html is not supported by the plugin - let file = if let Some(ref text_content) = text { - if text_content.lines().count() == 1 - && (text_content.starts_with('/') || text_content.starts_with("file://")) - { - Some(text_content.clone()) - } else { - None - } - } else { - None - }; + let file = if let Some(ref text_content) = text { + if text_content.lines().count() == 1 + && (text_content.starts_with('/') || text_content.starts_with("file://")) + { + Some(text_content.clone()) + } else { + None + } + } else { + None + }; - Ok(ReadResult { text, html, file }) + Ok(ReadResult { text, html, file }) } #[tauri::command] pub async fn clipboard_copy( - app: tauri::AppHandle, - content: ClipboardContent, - _options: Option + app: tauri::AppHandle, + content: ClipboardContent, + _options: Option, ) -> Result<(), String> { - let clipboard = app.clipboard(); + let clipboard = app.clipboard(); - if let Some(file_path) = &content.file { - clipboard - .write_text(file_path.clone()) - .map_err(|e| e.to_string())?; - } else if let Some(html) = &content.html { - clipboard - .write_html(html.clone(), content.text) - .map_err(|e| e.to_string())?; - } else if let Some(text) = &content.text { - clipboard.write_text(text.clone()).map_err(|e| e.to_string())?; - } + if let Some(file_path) = &content.file { + clipboard + .write_text(file_path.clone()) + .map_err(|e| e.to_string())?; + } else if let Some(html) = &content.html { + clipboard + .write_html(html.clone(), content.text) + .map_err(|e| e.to_string())?; + } else if let Some(text) = &content.text { + clipboard + .write_text(text.clone()) + .map_err(|e| e.to_string())?; + } - Ok(()) + Ok(()) } #[tauri::command] pub async fn clipboard_paste( - app: tauri::AppHandle, - content: ClipboardContent + app: tauri::AppHandle, + content: ClipboardContent, ) -> Result<(), String> { - let clipboard = app.clipboard(); - let original_text = clipboard.read_text().ok(); + let clipboard = app.clipboard(); + let original_text = clipboard.read_text().ok(); - clipboard_copy(app.clone(), content, None).await?; + clipboard_copy(app.clone(), content, None).await?; - thread::sleep(Duration::from_millis(100)); + thread::sleep(Duration::from_millis(100)); - let mut enigo = Enigo::new(&Settings::default()).map_err(|e| e.to_string())?; + let mut enigo = Enigo::new(&Settings::default()).map_err(|e| e.to_string())?; - #[cfg(target_os = "macos")] - { - enigo.key(Key::Meta, enigo::Direction::Press).ok(); - enigo.key(Key::Unicode('v'), enigo::Direction::Click).ok(); - enigo.key(Key::Meta, enigo::Direction::Release).ok(); - } - #[cfg(not(target_os = "macos"))] - { - enigo.key(Key::Control, enigo::Direction::Press).ok(); - enigo.key(Key::Unicode('v'), enigo::Direction::Click).ok(); - enigo.key(Key::Control, enigo::Direction::Release).ok(); - } + #[cfg(target_os = "macos")] + { + enigo.key(Key::Meta, enigo::Direction::Press).ok(); + enigo.key(Key::Unicode('v'), enigo::Direction::Click).ok(); + enigo.key(Key::Meta, enigo::Direction::Release).ok(); + } + #[cfg(not(target_os = "macos"))] + { + enigo.key(Key::Control, enigo::Direction::Press).ok(); + enigo.key(Key::Unicode('v'), enigo::Direction::Click).ok(); + enigo.key(Key::Control, enigo::Direction::Release).ok(); + } - thread::sleep(Duration::from_millis(100)); + thread::sleep(Duration::from_millis(100)); - if let Some(text) = original_text { - clipboard.write_text(text).map_err(|e| e.to_string())?; - } else { - clipboard.clear().map_err(|e| e.to_string())?; - } + if let Some(text) = original_text { + clipboard.write_text(text).map_err(|e| e.to_string())?; + } else { + clipboard.clear().map_err(|e| e.to_string())?; + } - Ok(()) + Ok(()) } #[tauri::command] pub async fn clipboard_clear(app: tauri::AppHandle) -> Result<(), String> { - app.clipboard().clear().map_err(|e| e.to_string()) -} \ No newline at end of file + app.clipboard().clear().map_err(|e| e.to_string()) +} diff --git a/src-tauri/src/extensions.rs b/src-tauri/src/extensions.rs index 90c15a9..b59f120 100644 --- a/src-tauri/src/extensions.rs +++ b/src-tauri/src/extensions.rs @@ -112,4 +112,4 @@ pub async fn install_extension( } Ok(()) -} \ No newline at end of file +} diff --git a/src-tauri/src/filesystem.rs b/src-tauri/src/filesystem.rs index eb078e6..5774f4b 100644 --- a/src-tauri/src/filesystem.rs +++ b/src-tauri/src/filesystem.rs @@ -263,4 +263,4 @@ async fn get_selected_finder_items_linux() -> Result, String } Err("Could not determine selected files. Please copy them to your clipboard.".to_string()) -} \ No newline at end of file +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 591ab4f..bef2af3 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -14,142 +14,142 @@ use selection::get_text; use std::process::Command; use std::thread; use std::time::Duration; -use tauri::{Manager, Emitter}; +use tauri::{Emitter, Manager}; use tauri_plugin_deep_link::DeepLinkExt; #[tauri::command] fn get_installed_apps() -> Vec { - match AppCache::get_apps() { - Ok(apps) => apps, - Err(e) => { - eprintln!("Failed to get apps: {:?}", e); - Vec::new() - } - } + match AppCache::get_apps() { + Ok(apps) => apps, + Err(e) => { + eprintln!("Failed to get apps: {:?}", e); + Vec::new() + } + } } #[tauri::command] fn launch_app(exec: String) -> Result<(), String> { - let exec_parts: Vec<&str> = exec.split_whitespace().collect(); - if exec_parts.is_empty() { - return Err("Empty exec command".to_string()); - } + let exec_parts: Vec<&str> = exec.split_whitespace().collect(); + if exec_parts.is_empty() { + return Err("Empty exec command".to_string()); + } - let mut command = Command::new(exec_parts[0]); - for arg in &exec_parts[1..] { - if !arg.starts_with('%') { - command.arg(arg); - } - } + let mut command = Command::new(exec_parts[0]); + for arg in &exec_parts[1..] { + if !arg.starts_with('%') { + command.arg(arg); + } + } - command - .spawn() - .map_err(|e| format!("Failed to launch app: {}", e))?; + command + .spawn() + .map_err(|e| format!("Failed to launch app: {}", e))?; - Ok(()) + Ok(()) } #[tauri::command] fn get_selected_text() -> String { - get_text() + get_text() } fn setup_background_refresh() { - thread::spawn(|| { - thread::sleep(Duration::from_secs(60)); - loop { - AppCache::refresh_background(); - thread::sleep(Duration::from_secs(300)); - } - }); + thread::spawn(|| { + thread::sleep(Duration::from_secs(60)); + loop { + AppCache::refresh_background(); + thread::sleep(Duration::from_secs(300)); + } + }); } fn setup_global_shortcut(app: &mut tauri::App) -> Result<(), Box> { - use tauri_plugin_global_shortcut::{ - Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState - }; + use tauri_plugin_global_shortcut::{ + Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState, + }; - let spotlight_shortcut = Shortcut::new(Some(Modifiers::ALT), Code::Space); - let handle = app.handle().clone(); + let spotlight_shortcut = Shortcut::new(Some(Modifiers::ALT), Code::Space); + let handle = app.handle().clone(); - println!("Spotlight shortcut: {:?}", spotlight_shortcut); + println!("Spotlight shortcut: {:?}", spotlight_shortcut); - app.handle().plugin( - tauri_plugin_global_shortcut::Builder::new() - .with_handler(move |_app, shortcut, event| { - println!("Shortcut: {:?}, Event: {:?}", shortcut, event); - if shortcut == &spotlight_shortcut && event.state() == ShortcutState::Pressed { - let spotlight_window = handle.get_webview_window("raycast-linux").unwrap(); - println!("Spotlight window: {:?}", spotlight_window); - if spotlight_window.is_visible().unwrap_or(false) { - spotlight_window.hide().unwrap(); - } else { - spotlight_window.show().unwrap(); - spotlight_window.set_focus().unwrap(); - } - } - }) - .build() - )?; + app.handle().plugin( + tauri_plugin_global_shortcut::Builder::new() + .with_handler(move |_app, shortcut, event| { + println!("Shortcut: {:?}, Event: {:?}", shortcut, event); + if shortcut == &spotlight_shortcut && event.state() == ShortcutState::Pressed { + let spotlight_window = handle.get_webview_window("raycast-linux").unwrap(); + println!("Spotlight window: {:?}", spotlight_window); + if spotlight_window.is_visible().unwrap_or(false) { + spotlight_window.hide().unwrap(); + } else { + spotlight_window.show().unwrap(); + spotlight_window.set_focus().unwrap(); + } + } + }) + .build(), + )?; - app.global_shortcut().register(spotlight_shortcut)?; - Ok(()) + app.global_shortcut().register(spotlight_shortcut)?; + Ok(()) } #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { - tauri::Builder::default() - .manage(WsState::default()) - .plugin(tauri_plugin_single_instance::init(|app, args, _cwd| { - if args.len() > 1 && args[1].starts_with("raycast://") { - if let Some(window) = app.get_webview_window("main") { - let _ = window.emit("deep-link", args[1].to_string()); - window.show().unwrap(); - } + tauri::Builder::default() + .manage(WsState::default()) + .plugin(tauri_plugin_single_instance::init(|app, args, _cwd| { + if args.len() > 1 && args[1].starts_with("raycast://") { + if let Some(window) = app.get_webview_window("main") { + let _ = window.emit("deep-link", args[1].to_string()); + window.show().unwrap(); + } - return; - } + return; + } - if let Some(window) = app.get_webview_window("main") { - if let Ok(true) = window.is_visible() { - let _ = window.hide(); - } else { - let _ = window.show(); - let _ = window.set_focus(); - } - } - })) - .plugin(tauri_plugin_deep_link::init()) - .plugin(tauri_plugin_global_shortcut::Builder::new().build()) - .plugin(tauri_plugin_shell::init()) - .plugin(tauri_plugin_clipboard_manager::init()) - .plugin(tauri_plugin_opener::init()) - .invoke_handler(tauri::generate_handler![ - get_installed_apps, - launch_app, - get_selected_text, - filesystem::get_selected_finder_items, - extensions::install_extension, - browser_extension::browser_extension_check_connection, - browser_extension::browser_extension_request, - clipboard::clipboard_read_text, - clipboard::clipboard_read, - clipboard::clipboard_copy, - clipboard::clipboard_paste, - clipboard::clipboard_clear, - oauth::oauth_set_tokens, - oauth::oauth_get_tokens, - oauth::oauth_remove_tokens - ]) - .setup(|app| { - let app_handle = app.handle().clone(); - tauri::async_runtime::spawn(browser_extension::run_server(app_handle)); + if let Some(window) = app.get_webview_window("main") { + if let Ok(true) = window.is_visible() { + let _ = window.hide(); + } else { + let _ = window.show(); + let _ = window.set_focus(); + } + } + })) + .plugin(tauri_plugin_deep_link::init()) + .plugin(tauri_plugin_global_shortcut::Builder::new().build()) + .plugin(tauri_plugin_shell::init()) + .plugin(tauri_plugin_clipboard_manager::init()) + .plugin(tauri_plugin_opener::init()) + .invoke_handler(tauri::generate_handler![ + get_installed_apps, + launch_app, + get_selected_text, + filesystem::get_selected_finder_items, + extensions::install_extension, + browser_extension::browser_extension_check_connection, + browser_extension::browser_extension_request, + clipboard::clipboard_read_text, + clipboard::clipboard_read, + clipboard::clipboard_copy, + clipboard::clipboard_paste, + clipboard::clipboard_clear, + oauth::oauth_set_tokens, + oauth::oauth_get_tokens, + oauth::oauth_remove_tokens + ]) + .setup(|app| { + let app_handle = app.handle().clone(); + tauri::async_runtime::spawn(browser_extension::run_server(app_handle)); - setup_background_refresh(); - setup_global_shortcut(app)?; + setup_background_refresh(); + setup_global_shortcut(app)?; - Ok(()) - }) - .run(tauri::generate_context!()) - .expect("error while running tauri application"); -} \ No newline at end of file + Ok(()) + }) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); +} diff --git a/src-tauri/src/oauth.rs b/src-tauri/src/oauth.rs index abb54df..550a6a3 100644 --- a/src-tauri/src/oauth.rs +++ b/src-tauri/src/oauth.rs @@ -8,80 +8,79 @@ use tauri::Manager; #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] struct StoredTokenSet { - access_token: String, - refresh_token: Option, - expires_in: Option, - scope: Option, - id_token: Option, - updated_at: String, + access_token: String, + refresh_token: Option, + expires_in: Option, + scope: Option, + id_token: Option, + updated_at: String, } type TokenStore = HashMap; fn get_storage_path(app: &tauri::AppHandle) -> Result { - let data_dir = app - .path() - .app_local_data_dir() - .map_err(|_| "Failed to get app local data dir".to_string())?; + let data_dir = app + .path() + .app_local_data_dir() + .map_err(|_| "Failed to get app local data dir".to_string())?; - if !data_dir.exists() { - fs::create_dir_all(&data_dir).map_err(|e| e.to_string())?; - } + if !data_dir.exists() { + fs::create_dir_all(&data_dir).map_err(|e| e.to_string())?; + } - Ok(data_dir.join("oauth_tokens.json")) + Ok(data_dir.join("oauth_tokens.json")) } fn read_store(path: &Path) -> Result { - if !path.exists() { - return Ok(HashMap::new()); - } - let content = fs::read_to_string(path).map_err(|e| e.to_string())?; - if content.trim().is_empty() { - return Ok(HashMap::new()); - } - serde_json::from_str(&content).map_err(|e| e.to_string()) + if !path.exists() { + return Ok(HashMap::new()); + } + let content = fs::read_to_string(path).map_err(|e| e.to_string())?; + if content.trim().is_empty() { + return Ok(HashMap::new()); + } + serde_json::from_str(&content).map_err(|e| e.to_string()) } fn write_store(path: &Path, store: &TokenStore) -> Result<(), String> { - let content = serde_json::to_string_pretty(store).map_err(|e| e.to_string())?; - fs::write(path, content).map_err(|e| e.to_string()) + let content = serde_json::to_string_pretty(store).map_err(|e| e.to_string())?; + fs::write(path, content).map_err(|e| e.to_string()) } #[tauri::command] pub fn oauth_set_tokens( - app: tauri::AppHandle, - provider_id: String, - tokens: serde_json::Value, + app: tauri::AppHandle, + provider_id: String, + tokens: serde_json::Value, ) -> Result<(), String> { - let path = get_storage_path(&app)?; - let mut store = read_store(&path)?; + let path = get_storage_path(&app)?; + let mut store = read_store(&path)?; - let token_set: StoredTokenSet = - serde_json::from_value(tokens).map_err(|e| e.to_string())?; + let token_set: StoredTokenSet = serde_json::from_value(tokens).map_err(|e| e.to_string())?; - store.insert(provider_id, token_set); - write_store(&path, &store) + store.insert(provider_id, token_set); + write_store(&path, &store) } #[tauri::command] pub fn oauth_get_tokens( - app: tauri::AppHandle, - provider_id: String, + app: tauri::AppHandle, + provider_id: String, ) -> Result, String> { - let path = get_storage_path(&app)?; - let store = read_store(&path)?; - if let Some(token_set) = store.get(&provider_id) { - let value = serde_json::to_value(token_set).map_err(|e| e.to_string())?; - Ok(Some(value)) - } else { - Ok(None) - } + let path = get_storage_path(&app)?; + let store = read_store(&path)?; + if let Some(token_set) = store.get(&provider_id) { + let value = serde_json::to_value(token_set).map_err(|e| e.to_string())?; + Ok(Some(value)) + } else { + Ok(None) + } } #[tauri::command] pub fn oauth_remove_tokens(app: tauri::AppHandle, provider_id: String) -> Result<(), String> { - let path = get_storage_path(&app)?; - let mut store = read_store(&path)?; - store.remove(&provider_id); - write_store(&path, &store) -} \ No newline at end of file + let path = get_storage_path(&app)?; + let mut store = read_store(&path)?; + store.remove(&provider_id); + write_store(&path, &store) +}