build(init):项目初始化建立,迁移无需重构部分内容。
This commit is contained in:
67
src-tauri/src/config.rs
Normal file
67
src-tauri/src/config.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use std::{
|
||||
fs::{DirBuilder, File},
|
||||
io::{Read, Write},
|
||||
ops::Deref,
|
||||
path::{Path, PathBuf},
|
||||
sync::{Mutex, MutexGuard},
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::keyboard_mapping::KeyboardMapping;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Configuration {
|
||||
pub mappings: Vec<KeyboardMapping>,
|
||||
}
|
||||
|
||||
static CONFIG_INSTANCE: Mutex<Configuration> = Mutex::new(Configuration {
|
||||
mappings: Vec::new(),
|
||||
});
|
||||
|
||||
impl Configuration {
|
||||
// 获取一个可以用于访问应用配置信息的引用。
|
||||
pub fn get() -> MutexGuard<'static, Configuration> {
|
||||
CONFIG_INSTANCE.lock().unwrap()
|
||||
}
|
||||
|
||||
// 确认应用数据目录的存在,如果数据目录不存在,那么将自动创建。
|
||||
fn ensure_data_dir() -> anyhow::Result<PathBuf> {
|
||||
let config_dir = directories::ProjectDirs::from("xyz", "archgrid", "pylib").ok_or(
|
||||
anyhow::anyhow!("unable to fetch application data directory"),
|
||||
)?;
|
||||
let data_dir = config_dir.data_dir();
|
||||
if !data_dir.exists() {
|
||||
let mut dir_creator = DirBuilder::new();
|
||||
dir_creator
|
||||
.recursive(true)
|
||||
.create(config_dir.data_dir())
|
||||
.unwrap();
|
||||
}
|
||||
Ok(data_dir.to_path_buf())
|
||||
}
|
||||
|
||||
// 确认应用配置文件存在,如果配置文件不存在,那么将自动创建一个空白的配置文件。
|
||||
fn ensure_config_file<P: AsRef<Path>>(config_path: P) -> anyhow::Result<File> {
|
||||
let config_file_path = config_path.as_ref().join("config.toml");
|
||||
if !config_file_path.exists() {
|
||||
let mut file = File::create(config_file_path.clone())?;
|
||||
let configuration = Self::get();
|
||||
let toml_string = toml::to_string(configuration.deref())?;
|
||||
file.write_all(toml_string.as_bytes())?;
|
||||
}
|
||||
let config_file = File::open(config_file_path)?;
|
||||
Ok(config_file)
|
||||
}
|
||||
|
||||
// 从应用配置文件中加载配置信息,即便应用配置文件是空白的。
|
||||
pub fn load_settings() -> anyhow::Result<()> {
|
||||
let mut config_file = Self::ensure_config_file(Self::ensure_data_dir()?)?;
|
||||
let mut config_file_content = String::new();
|
||||
config_file.read_to_string(&mut config_file_content)?;
|
||||
let config: Configuration = toml::from_str(&config_file_content).unwrap();
|
||||
let mut configuration = Self::get();
|
||||
*configuration = config;
|
||||
Ok(())
|
||||
}
|
||||
}
|
29
src-tauri/src/keyboard_mapping.rs
Normal file
29
src-tauri/src/keyboard_mapping.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct KeyboardMapping {
|
||||
pub name: String,
|
||||
pub keys: HashMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
impl KeyboardMapping {
|
||||
/// 创建一个新的键盘映射。
|
||||
pub fn new<S: AsRef<str>>(name: S, key: S) -> Self {
|
||||
let mut keys = HashMap::new();
|
||||
keys.insert(key.as_ref().to_string(), vec![key.as_ref().to_string()]);
|
||||
Self {
|
||||
name: name.as_ref().to_string(),
|
||||
keys,
|
||||
}
|
||||
}
|
||||
|
||||
// 重命名键盘映射。
|
||||
pub fn rename<S: AsRef<str>>(self, name: S) -> Self {
|
||||
Self {
|
||||
name: name.as_ref().to_string(),
|
||||
..self
|
||||
}
|
||||
}
|
||||
}
|
21
src-tauri/src/main.rs
Normal file
21
src-tauri/src/main.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
#![allow(dead_code)]
|
||||
|
||||
mod config;
|
||||
mod keyboard_mapping;
|
||||
mod setup;
|
||||
|
||||
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.setup(setup::init)
|
||||
.invoke_handler(tauri::generate_handler![greet])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
19
src-tauri/src/setup.rs
Normal file
19
src-tauri/src/setup.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use tauri::{App, Manager};
|
||||
use window_vibrancy::{self, NSVisualEffectMaterial};
|
||||
|
||||
/// setup
|
||||
pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>> {
|
||||
let win = app.get_window("main").unwrap();
|
||||
|
||||
// 仅在 macOS 下执行
|
||||
#[cfg(target_os = "macos")]
|
||||
window_vibrancy::apply_vibrancy(&win, NSVisualEffectMaterial::FullScreenUI, None, None)
|
||||
.expect("Unsupported platform! 'apply_vibrancy' is only supported on macOS");
|
||||
|
||||
// 仅在 windows 下执行
|
||||
#[cfg(target_os = "windows")]
|
||||
window_vibrancy::apply_blur(&win, Some((18, 18, 18, 125)))
|
||||
.expect("Unsupported platform! 'apply_blur' is only supported on Windows");
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user