Skip to content

Commit

Permalink
跨平台
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtualHotBar committed Apr 25, 2024
1 parent a1bb1fe commit bf1633a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 57 deletions.
35 changes: 13 additions & 22 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@ use std::fs;
use tauri::Manager;

mod autostart;
mod localized;
mod tray;
mod utils;
mod localized;

use crate::autostart::is_autostart;
use crate::autostart::set_autostart;
use crate::utils::download_with_progress;
#[cfg(target_os = "windows")]
use crate::utils::find_first_available_drive_letter;
#[cfg(target_os = "windows")]
use crate::utils::set_window_shadow;
#[cfg(target_os = "windows")]
use crate::utils::is_winfsp_installed;
use crate::utils::set_window_shadow;

//use crate::localized::LANGUAGE_PACK;
//use crate::localized::get_localized_text;
Expand All @@ -33,10 +30,6 @@ const CONFIG_PATH: &str = "res/config.json";

use std::sync::Mutex;

// 从指定路径加载语言包 JSON 文件并解析为 Map<String, Value>

// 从语言包中获取指定键的翻译文本

fn main() {
// 确保应用程序只有一个实例运行
ensure_single_instance();
Expand All @@ -52,20 +45,19 @@ fn main() {
download_file,
get_autostart_state,
set_autostart_state,
]);

// 针对Windows系统额外注册函数
#[cfg(target_os = "windows")]
let builder = builder.invoke_handler(tauri::generate_handler![
get_winfsp_install_state,
get_available_drive_letter
]).setup(|app| {
set_window_shadow(app); // 设置窗口阴影
Ok(())
});
get_winfsp_install_state,
get_available_drive_letter
])
.setup(|app| {
#[cfg(target_os = "windows")]
set_window_shadow(app); // 设置窗口阴影
Ok(())
});

// 运行Tauri应用,使用`generate_context!()`来加载应用配置
builder.run(tauri::generate_context!()).expect("error while running tauri application");
builder
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

use once_cell::sync::Lazy;
Expand Down Expand Up @@ -193,4 +185,3 @@ async fn write_config_file(config_data: Value) -> Result<(), String> {

Ok(())
}

88 changes: 53 additions & 35 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,36 @@ use std::fs;
use std::io::{self, Write};
//use tauri::AppHandle;

#[cfg(target_os = "windows")]
pub fn set_window_shadow<R: Runtime>(app: &tauri::App<R>) {
let window = app.get_window("main").unwrap();
set_shadow(&window, true).expect("Unsupported platform!");
#[cfg(target_os = "linux")]
{
Ok(())
}
#[cfg(target_os = "windows")]
{
let window = app.get_window("main").unwrap();
set_shadow(&window, true).expect("Unsupported platform!");
}
}

#[cfg(target_os = "windows")]
pub fn find_first_available_drive_letter() -> Result<Option<String>, io::Error> {
for drive in ('A'..='Z').rev().map(|c| format!("{}:", c)) {
let drive_path = format!("{}\\", drive);
if fs::metadata(&drive_path).is_err() {
// 如果检测到错误,假设盘符未被使用并返回
return Ok(Some(drive));
}
#[cfg(target_os = "linux")]
{
Ok(None)
}
#[cfg(target_os = "windows")]
{
for drive in ('A'..='Z').rev().map(|c| format!("{}:", c)) {
let drive_path = format!("{}\\", drive);
if fs::metadata(&drive_path).is_err() {
// 如果检测到错误,假设盘符未被使用并返回
return Ok(Some(drive));
}
}

// 如果所有盘符都被占用,返回None
Ok(None)
// 如果所有盘符都被占用,返回None
Ok(None)
}
}

use futures_util::stream::StreamExt;
Expand Down Expand Up @@ -67,30 +79,36 @@ where
Ok(())
}

#[cfg(target_os = "windows")]
pub fn is_winfsp_installed() -> Result<bool, Box<dyn Error>> {
extern crate winreg;
use winreg::enums::*;
use winreg::RegKey;
// 打开HKEY_LOCAL_MACHINE
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);

// 定义需要检查的注册表键路径
let registry_keys = [
"SOFTWARE\\WinFsp",
"SOFTWARE\\WOW6432Node\\WinFsp",
"SYSTEM\\CurrentControlSet\\Services\\WinFsp.Launcher",
];

// 遍历每个注册表键路径
for &registry_key in registry_keys.iter() {
// 尝试打开指定键
match hklm.open_subkey(registry_key) {
Ok(_) => return Ok(true), // 如果键存在(即WinFsp已安装),返回true
Err(_) => continue, // 如果打开键失败(例如键不存在),忽略错误并继续
}
#[cfg(target_os = "linux")]
{
Ok(false)
}
#[cfg(target_os = "windows")]
{
extern crate winreg;
use winreg::enums::*;
use winreg::RegKey;
// 打开HKEY_LOCAL_MACHINE
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);

// 定义需要检查的注册表键路径
let registry_keys = [
"SOFTWARE\\WinFsp",
"SOFTWARE\\WOW6432Node\\WinFsp",
"SYSTEM\\CurrentControlSet\\Services\\WinFsp.Launcher",
];

// 遍历每个注册表键路径
for &registry_key in registry_keys.iter() {
// 尝试打开指定键
match hklm.open_subkey(registry_key) {
Ok(_) => return Ok(true), // 如果键存在(即WinFsp已安装),返回true
Err(_) => continue, // 如果打开键失败(例如键不存在),忽略错误并继续
}
}

// 如果所有键都不存在(即WinFsp未安装),返回false
Ok(false)
// 如果所有键都不存在(即WinFsp未安装),返回false
Ok(false)
}
}

0 comments on commit bf1633a

Please sign in to comment.