mirror of
https://github.com/0PandaDEV/Qopy.git
synced 2025-04-21 21:24:05 +02:00
refactor: cleaning and formatting
This commit is contained in:
parent
e674e0a0ec
commit
00749a9d3a
12 changed files with 479 additions and 120 deletions
|
@ -3,9 +3,9 @@ use global_hotkey::{
|
|||
hotkey::{Code, HotKey, Modifiers},
|
||||
GlobalHotKeyEvent, GlobalHotKeyManager, HotKeyState,
|
||||
};
|
||||
use std::str::FromStr;
|
||||
use std::cell::RefCell;
|
||||
use tauri::{AppHandle, Manager, Listener};
|
||||
use std::str::FromStr;
|
||||
use tauri::{AppHandle, Listener, Manager};
|
||||
|
||||
thread_local! {
|
||||
static HOTKEY_MANAGER: RefCell<Option<GlobalHotKeyManager>> = RefCell::new(None);
|
||||
|
@ -18,20 +18,21 @@ pub fn setup(app_handle: tauri::AppHandle) {
|
|||
HOTKEY_MANAGER.with(|m| *m.borrow_mut() = Some(manager));
|
||||
|
||||
let rt = app_handle.state::<tokio::runtime::Runtime>();
|
||||
let initial_keybind = rt.block_on(crate::db::settings::get_keybind(app_handle_clone.clone()))
|
||||
let initial_keybind = rt
|
||||
.block_on(crate::db::settings::get_keybind(app_handle_clone.clone()))
|
||||
.expect("Failed to get initial keybind");
|
||||
let initial_shortcut = initial_keybind.join("+");
|
||||
|
||||
|
||||
let initial_shortcut_for_update = initial_shortcut.clone();
|
||||
let initial_shortcut_for_save = initial_shortcut.clone();
|
||||
|
||||
|
||||
if let Err(e) = register_shortcut(&initial_shortcut) {
|
||||
eprintln!("Error registering initial shortcut: {:?}", e);
|
||||
}
|
||||
|
||||
app_handle.listen("update-shortcut", move |event| {
|
||||
let payload_str = event.payload().to_string();
|
||||
|
||||
|
||||
if let Ok(old_hotkey) = parse_hotkey(&initial_shortcut_for_update) {
|
||||
HOTKEY_MANAGER.with(|manager| {
|
||||
if let Some(manager) = manager.borrow().as_ref() {
|
||||
|
@ -47,7 +48,7 @@ pub fn setup(app_handle: tauri::AppHandle) {
|
|||
|
||||
app_handle.listen("save_keybind", move |event| {
|
||||
let payload_str = event.payload().to_string();
|
||||
|
||||
|
||||
if let Ok(old_hotkey) = parse_hotkey(&initial_shortcut_for_save) {
|
||||
HOTKEY_MANAGER.with(|manager| {
|
||||
if let Some(manager) = manager.borrow().as_ref() {
|
||||
|
@ -110,14 +111,17 @@ fn parse_hotkey(shortcut: &str) -> Result<HotKey, Box<dyn std::error::Error>> {
|
|||
} else {
|
||||
key.to_string()
|
||||
};
|
||||
|
||||
code = Some(Code::from_str(&key_code)
|
||||
.map_err(|_| format!("Invalid key code: {}", key_code))?);
|
||||
|
||||
code = Some(
|
||||
Code::from_str(&key_code)
|
||||
.map_err(|_| format!("Invalid key code: {}", key_code))?,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let key_code = code.ok_or_else(|| format!("No valid key code found in shortcut: {}", shortcut))?;
|
||||
let key_code =
|
||||
code.ok_or_else(|| format!("No valid key code found in shortcut: {}", shortcut))?;
|
||||
Ok(HotKey::new(Some(modifiers), key_code))
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
pub mod updater;
|
||||
pub mod clipboard;
|
||||
pub mod tray;
|
||||
pub mod hotkeys;
|
||||
pub mod tray;
|
||||
pub mod updater;
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use tauri::{
|
||||
menu::{MenuBuilder, MenuItemBuilder}, tray::TrayIconBuilder, Emitter, Manager
|
||||
menu::{MenuBuilder, MenuItemBuilder},
|
||||
tray::TrayIconBuilder,
|
||||
Emitter, Manager,
|
||||
};
|
||||
|
||||
pub fn setup(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use tauri::{AppHandle, async_runtime};
|
||||
use tauri_plugin_dialog::{DialogExt, MessageDialogKind, MessageDialogButtons};
|
||||
use tauri::{async_runtime, AppHandle};
|
||||
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons, MessageDialogKind};
|
||||
use tauri_plugin_updater::UpdaterExt;
|
||||
|
||||
pub async fn check_for_updates(app: AppHandle) {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use include_dir::{include_dir, Dir};
|
||||
use sqlx::sqlite::{SqlitePool, SqlitePoolOptions};
|
||||
use std::fs;
|
||||
use tauri::Manager;
|
||||
use tokio::runtime::Runtime as TokioRuntime;
|
||||
use include_dir::{include_dir, Dir};
|
||||
|
||||
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/src/db/migrations");
|
||||
|
||||
|
@ -49,39 +49,32 @@ pub fn setup(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
|
|||
}
|
||||
|
||||
async fn apply_migrations(pool: &SqlitePool) -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("Starting migration process");
|
||||
|
||||
// Create schema_version table
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS schema_version (
|
||||
version INTEGER PRIMARY KEY,
|
||||
applied_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);"
|
||||
);",
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
let current_version: Option<i64> = sqlx::query_scalar(
|
||||
"SELECT MAX(version) FROM schema_version"
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
let current_version: Option<i64> =
|
||||
sqlx::query_scalar("SELECT MAX(version) FROM schema_version")
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
let current_version = current_version.unwrap_or(0);
|
||||
println!("Current database version: {}", current_version);
|
||||
|
||||
let mut migration_files: Vec<(i64, &str)> = MIGRATIONS_DIR
|
||||
.files()
|
||||
.filter_map(|file| {
|
||||
let file_name = file.path().file_name()?.to_str()?;
|
||||
println!("Processing file: {}", file_name);
|
||||
if file_name.ends_with(".sql") && file_name.starts_with("migration") {
|
||||
let version: i64 = file_name
|
||||
.trim_start_matches("migration")
|
||||
.trim_end_matches(".sql")
|
||||
.parse()
|
||||
.ok()?;
|
||||
println!("Found migration version: {}", version);
|
||||
Some((version, file.contents_utf8()?))
|
||||
} else {
|
||||
None
|
||||
|
@ -93,8 +86,6 @@ async fn apply_migrations(pool: &SqlitePool) -> Result<(), Box<dyn std::error::E
|
|||
|
||||
for (version, content) in migration_files {
|
||||
if version > current_version {
|
||||
println!("Applying migration {}", version);
|
||||
|
||||
let statements: Vec<&str> = content
|
||||
.split(';')
|
||||
.map(|s| s.trim())
|
||||
|
@ -102,7 +93,6 @@ async fn apply_migrations(pool: &SqlitePool) -> Result<(), Box<dyn std::error::E
|
|||
.collect();
|
||||
|
||||
for statement in statements {
|
||||
println!("Executing statement: {}", statement);
|
||||
sqlx::query(statement)
|
||||
.execute(pool)
|
||||
.await
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
pub mod database;
|
||||
pub mod history;
|
||||
pub mod settings;
|
||||
pub mod settings;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::SqlitePool;
|
||||
use serde_json;
|
||||
use tauri::{Emitter, Manager};
|
||||
use sqlx::Row;
|
||||
use sqlx::SqlitePool;
|
||||
use tauri::{Emitter, Manager};
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct KeybindSetting {
|
||||
|
@ -15,12 +15,10 @@ pub async fn initialize_settings(pool: &SqlitePool) -> Result<(), Box<dyn std::e
|
|||
};
|
||||
let json = serde_json::to_string(&default_keybind)?;
|
||||
|
||||
sqlx::query(
|
||||
"INSERT INTO settings (key, value) VALUES ('keybind', ?)"
|
||||
)
|
||||
.bind(json)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
sqlx::query("INSERT INTO settings (key, value) VALUES ('keybind', ?)")
|
||||
.bind(json)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -50,7 +48,7 @@ pub async fn save_keybind(
|
|||
#[tauri::command]
|
||||
pub async fn get_setting(
|
||||
pool: tauri::State<'_, SqlitePool>,
|
||||
key: String
|
||||
key: String,
|
||||
) -> Result<String, String> {
|
||||
let row = sqlx::query("SELECT value FROM settings WHERE key = ?")
|
||||
.bind(key)
|
||||
|
@ -65,7 +63,7 @@ pub async fn get_setting(
|
|||
pub async fn save_setting(
|
||||
pool: tauri::State<'_, SqlitePool>,
|
||||
key: String,
|
||||
value: String
|
||||
value: String,
|
||||
) -> Result<(), String> {
|
||||
sqlx::query("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)")
|
||||
.bind(key)
|
||||
|
@ -78,23 +76,18 @@ pub async fn save_setting(
|
|||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_keybind(
|
||||
app_handle: tauri::AppHandle,
|
||||
) -> Result<Vec<String>, String> {
|
||||
pub async fn get_keybind(app_handle: tauri::AppHandle) -> Result<Vec<String>, String> {
|
||||
let pool = app_handle.state::<SqlitePool>();
|
||||
|
||||
|
||||
let row = sqlx::query("SELECT value FROM settings WHERE key = 'keybind'")
|
||||
.fetch_optional(&*pool)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let json = row
|
||||
.map(|r| r.get::<String, _>("value"))
|
||||
.unwrap_or_else(|| {
|
||||
serde_json::to_string(&vec!["Meta".to_string(), "V".to_string()])
|
||||
.expect("Failed to serialize default keybind")
|
||||
});
|
||||
let json = row.map(|r| r.get::<String, _>("value")).unwrap_or_else(|| {
|
||||
serde_json::to_string(&vec!["Meta".to_string(), "V".to_string()])
|
||||
.expect("Failed to serialize default keybind")
|
||||
});
|
||||
|
||||
serde_json::from_str::<Vec<String>>(&json)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
serde_json::from_str::<Vec<String>>(&json).map_err(|e| e.to_string())
|
||||
}
|
||||
|
|
|
@ -4,7 +4,9 @@ use image::ImageFormat;
|
|||
use reqwest;
|
||||
use url::Url;
|
||||
|
||||
pub async fn fetch_favicon_as_base64(url: Url) -> Result<Option<String>, Box<dyn std::error::Error>> {
|
||||
pub async fn fetch_favicon_as_base64(
|
||||
url: Url,
|
||||
) -> Result<Option<String>, Box<dyn std::error::Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let favicon_url = format!("https://favicone.com/{}", url.host_str().unwrap());
|
||||
let response = client.get(&favicon_url).send().await?;
|
||||
|
@ -18,4 +20,4 @@ pub async fn fetch_favicon_as_base64(url: Url) -> Result<Option<String>, Box<dyn
|
|||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
pub mod types;
|
||||
pub mod commands;
|
||||
pub mod favicon;
|
||||
pub mod favicon;
|
||||
pub mod types;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue