Skip to content

Commit

Permalink
✨ feat(background): 添加换背景功能
Browse files Browse the repository at this point in the history
  • Loading branch information
CakeAL committed Nov 2, 2024
1 parent cdc01a3 commit 995bd1d
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 41 deletions.
5 changes: 5 additions & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ declare module 'vue' {
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NDataTable: typeof import('naive-ui')['NDataTable']
NDatePicker: typeof import('naive-ui')['NDatePicker']
NFloatButton: typeof import('naive-ui')['NFloatButton']
NGrid: typeof import('naive-ui')['NGrid']
NGridItem: typeof import('naive-ui')['NGridItem']
NH2: typeof import('naive-ui')['NH2']
NIcon: typeof import('naive-ui')['NIcon']
NImage: typeof import('naive-ui')['NImage']
NInput: typeof import('naive-ui')['NInput']
NLoadingBarProvider: typeof import('naive-ui')['NLoadingBarProvider']
NMenu: typeof import('naive-ui')['NMenu']
NMessageProvider: typeof import('naive-ui')['NMessageProvider']
NModal: typeof import('naive-ui')['NModal']
NModalProvider: typeof import('naive-ui')['NModalProvider']
NPopover: typeof import('naive-ui')['NPopover']
NProgress: typeof import('naive-ui')['NProgress']
NResult: typeof import('naive-ui')['NResult']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSelect: typeof import('naive-ui')['NSelect']
NSlider: typeof import('naive-ui')['NSlider']
NSpace: typeof import('naive-ui')['NSpace']
NSplit: typeof import('naive-ui')['NSplit']
NSwitch: typeof import('naive-ui')['NSwitch']
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ustb-wifi-tools",
"private": true,
"version": "0.8.3",
"version": "0.8.5",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
9 changes: 8 additions & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ustb-wifi-tools"
version = "0.8.3"
version = "0.8.5"
description = "A Tauri App that can be used to get information of the USTB Wifi"
authors = ["CakeAL"]
edition = "2021"
Expand All @@ -15,7 +15,7 @@ crate-type = ["staticlib", "cdylib", "rlib"]
tauri-build = { version = "2", features = [] }

[dependencies]
tauri = { version = "2", features = [ "macos-private-api"] }
tauri = { version = "2", features = [ "protocol-asset", "macos-private-api"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1", features = ["full"] }
Expand Down
89 changes: 87 additions & 2 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
entities::{AppState, EveryLoginData},
requests::*,
setting::Setting,
utils,
};

#[tauri::command(async)]
Expand Down Expand Up @@ -364,6 +363,92 @@ pub fn load_setting(
}
}

#[tauri::command(async)]
pub fn set_background_image(
app: tauri::AppHandle,
app_state: tauri::State<'_, AppState>,
) -> Result<(), String> {
use tauri_plugin_dialog::DialogExt;
let path = app
.dialog()
.file()
.add_filter("", &["png", "jpg", "jpeg"])
.blocking_pick_file();
dbg!(&path);
if let Some(path) = path {
app_state
.setting
.write()
.unwrap()
.set_background_image_path(&app, &path.into_path().map_err(|err| err.to_string())?)
.map_err(|err| err.to_string())?;
app_state
.setting
.read()
.unwrap()
.write_setting(&app)
.map_err(|err| err.to_string())?;
Ok(())
} else {
Err("请选择一个图片".into())
}
}

#[tauri::command(async)]
pub fn reset_background_image(
app: tauri::AppHandle,
app_state: tauri::State<'_, AppState>,
) -> Result<(), String> {
app_state.setting.write().unwrap().reset_background_image();
app_state
.setting
.read()
.unwrap()
.write_setting(&app)
.map_err(|err| err.to_string())?;
Ok(())
}

#[tauri::command(async)]
pub fn set_background_transparence(
app: tauri::AppHandle,
app_state: tauri::State<'_, AppState>,
transparence: u32,
) -> Result<(), String> {
app_state
.setting
.write()
.unwrap()
.set_background_transparence(transparence);
app_state
.setting
.read()
.unwrap()
.write_setting(&app)
.map_err(|err| err.to_string())?;
Ok(())
}

#[tauri::command(async)]
pub fn set_background_blur(
app: tauri::AppHandle,
app_state: tauri::State<'_, AppState>,
blur: u32,
) -> Result<(), String> {
app_state
.setting
.write()
.unwrap()
.set_background_blur(blur);
app_state
.setting
.read()
.unwrap()
.write_setting(&app)
.map_err(|err| err.to_string())?;
Ok(())
}

#[tauri::command(async)]
pub async fn manually_check_update(app: tauri::AppHandle) -> Result<(), String> {
#[cfg(not(any(target_os = "android", target_os = "linux")))]
Expand Down Expand Up @@ -420,7 +505,7 @@ pub async fn return_os_type() -> i32 {
let mut res = 0; // others

#[cfg(target_os = "windows")]
if utils::get_windows_build_number() >= 22000 {
if crate::utils::get_windows_build_number() >= 22000 {
res = 1; // win11
} else {
res = 2; // win10 及以下
Expand Down
6 changes: 5 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ pub fn run() {
load_ammeter,
load_user_flow,
submit_login_ustb_wifi,
return_os_type
return_os_type,
set_background_image,
reset_background_image,
set_background_transparence,
set_background_blur
])
.setup(|app| {
#[cfg(not(any(target_os = "android", target_os = "linux")))]
Expand Down
43 changes: 42 additions & 1 deletion src-tauri/src/setting.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
fs::{create_dir, File, OpenOptions},
fs::{self, create_dir, File, OpenOptions},
io::{Read, Write},
path::PathBuf,
};
Expand All @@ -13,6 +13,9 @@ pub struct Setting {
pub username: Option<String>,
pub password: Option<String>,
pub ammeter_number: Option<u32>,
pub background_image_path: Option<String>,
pub background_transparence: Option<u32>,
pub background_blur: Option<u32>
}

impl Setting {
Expand Down Expand Up @@ -51,6 +54,44 @@ impl Setting {
pub fn set_ammeter_number(&mut self, ammeter_number: u32) {
self.ammeter_number = Some(ammeter_number);
}

pub fn set_background_image_path(
&mut self,
app: &tauri::AppHandle,
src_path: &PathBuf,
) -> Result<()> {
self.reset_background_image();
let mut dest_path = get_config_path(app)?;
dest_path.pop();
dest_path.push(
src_path
.file_name()
.ok_or_else(|| anyhow!("Source image file name is invalid"))?,
);
fs::copy(src_path, &dest_path)?;
self.background_image_path = Some(
dest_path
.to_str()
.ok_or_else(|| anyhow!("Saved image file name is invalid"))?
.to_string(),
);
Ok(())
}

pub fn reset_background_image(&mut self) {
if let Some(path) = &self.background_image_path {
let _err = fs::remove_file(path);
}
self.background_image_path = None;
}

pub fn set_background_transparence(&mut self, background_transparence: u32) {
self.background_transparence = Some(background_transparence);
}

pub fn set_background_blur(&mut self, background_blur: u32) {
self.background_blur = Some(background_blur);
}
}

fn get_config_path(app: &tauri::AppHandle) -> Result<PathBuf> {
Expand Down
8 changes: 6 additions & 2 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"productName": "ustb-wifi-tools",
"mainBinaryName": "ustb-wifi-tools",
"version": "0.8.3",
"version": "0.8.5",
"identifier": "ustb.wifi.tools",
"build": {
"beforeDevCommand": "pnpm dev",
Expand All @@ -23,7 +23,11 @@
}
],
"security": {
"csp": null
"csp": "default-src 'self' ipc: http://ipc.localhost; img-src 'self' asset: http://asset.localhost",
"assetProtocol": {
"enable": true,
"scope": ["*/**"]
}
}
},
"bundle": {
Expand Down
Loading

0 comments on commit 995bd1d

Please sign in to comment.