Skip to content

Commit 52b9717

Browse files
committed
✨ 支持窗口状态记忆 (fix #130)
1 parent a6b0feb commit 52b9717

File tree

7 files changed

+57
-43
lines changed

7 files changed

+57
-43
lines changed

app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"dependencies": {
1919
"@generouted/react-router": "^1.19.9",
2020
"@marsidev/react-turnstile": "^1.1.0",
21-
"@msgpack/msgpack": "^3.1.1",
21+
"@msgpack/msgpack": "^3.1.2",
2222
"@octokit/rest": "^21.0.2",
2323
"@tauri-apps/api": "^2.1.1",
2424
"@tauri-apps/plugin-cli": "~2.2.0",
@@ -30,6 +30,7 @@
3030
"@tauri-apps/plugin-shell": "2.2.0",
3131
"@tauri-apps/plugin-store": "^2.2.0",
3232
"@tauri-apps/plugin-updater": "~2",
33+
"@tauri-apps/plugin-window-state": ">=2",
3334
"bcrypt": "^5.1.1",
3435
"decimal.js": "^10.5.0",
3536
"driver.js": "^1.3.1",

app/src-tauri/Cargo.lock

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ tauri-plugin-process = "2"
3030
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
3131
tauri-plugin-cli = "2"
3232
tauri-plugin-updater = "2"
33+
tauri-plugin-window-state = "2"

app/src-tauri/capabilities/desktop.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"identifier": "desktop-capability",
33
"platforms": ["macOS", "windows", "linux"],
44
"windows": ["main"],
5-
"permissions": ["cli:default", "updater:default"]
5+
"permissions": ["cli:default", "updater:default", "window-state:default"]
66
}

app/src-tauri/src/lib.rs

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use base64::engine::general_purpose;
66
use base64::Engine;
77

88
use tauri::Manager;
9-
use tauri::Runtime;
10-
use tauri::Url;
119

1210
#[derive(Debug, Serialize, Deserialize)]
1311
struct FolderEntry {
@@ -17,14 +15,13 @@ struct FolderEntry {
1715
#[serde(skip_serializing_if = "Option::is_none")]
1816
children: Option<Vec<FolderEntry>>,
1917
}
20-
#[cfg(desktop)]
21-
use tauri_plugin_updater::UpdaterExt;
2218

2319
/// 递归读取文件夹结构,返回嵌套的文件夹结构
2420
#[tauri::command]
2521
fn read_folder_structure(path: String) -> FolderEntry {
2622
let path_buf = std::path::PathBuf::from(&path);
27-
let name = path_buf.file_name()
23+
let name = path_buf
24+
.file_name()
2825
.and_then(|n| n.to_str())
2926
.unwrap_or("")
3027
.to_string();
@@ -36,7 +33,7 @@ fn read_folder_structure(path: String) -> FolderEntry {
3633
let path = entry.path();
3734
let child_name = entry.file_name().to_string_lossy().to_string();
3835
let path_str = path.to_string_lossy().to_string();
39-
36+
4037
if path.is_file() {
4138
children.push(FolderEntry {
4239
name: child_name,
@@ -50,7 +47,7 @@ fn read_folder_structure(path: String) -> FolderEntry {
5047
}
5148
}
5249
}
53-
50+
5451
FolderEntry {
5552
name,
5653
is_file: false,
@@ -88,20 +85,23 @@ fn read_folder(path: String) -> Vec<String> {
8885
/// 如果文件夹不存在,返回空列表
8986
/// fileExts: 要读取的文件扩展名列表,例如:[".txt", ".md"]
9087
#[tauri::command]
91-
fn read_folder_recursive(path: String, fileExts: Vec<String>) -> Vec<String> {
88+
fn read_folder_recursive(path: String, file_exts: Vec<String>) -> Vec<String> {
9289
let mut files = Vec::new();
9390
if let Ok(entries) = std::fs::read_dir(path) {
9491
for entry in entries {
9592
if let Ok(entry) = entry {
9693
let path = entry.path();
9794
if path.is_file() {
9895
if let Some(file_name) = path.to_str() {
99-
if fileExts.iter().any(|ext| file_name.ends_with(ext)) {
96+
if file_exts.iter().any(|ext| file_name.ends_with(ext)) {
10097
files.push(file_name.to_string());
10198
}
10299
}
103100
} else if path.is_dir() {
104-
let mut sub_files = read_folder_recursive(path.to_str().unwrap().to_string(), fileExts.clone());
101+
let mut sub_files = read_folder_recursive(
102+
path.to_str().unwrap().to_string(),
103+
file_exts.clone(),
104+
);
105105
files.append(&mut sub_files);
106106
}
107107
}
@@ -110,15 +110,13 @@ fn read_folder_recursive(path: String, fileExts: Vec<String>) -> Vec<String> {
110110
files
111111
}
112112

113-
114113
/// 删除文件
115114
#[tauri::command]
116115
fn delete_file(path: String) -> Result<(), String> {
117116
std::fs::remove_file(path).map_err(|e| e.to_string())?;
118117
Ok(())
119118
}
120119

121-
122120
/// 读取文件,返回字符串
123121
#[tauri::command]
124122
fn read_text_file(path: String) -> String {
@@ -180,26 +178,6 @@ fn exit(code: i32) {
180178
std::process::exit(code);
181179
}
182180

183-
#[cfg(desktop)]
184-
#[tauri::command]
185-
async fn set_update_channel<R: Runtime>(
186-
app: tauri::AppHandle<R>,
187-
channel: String,
188-
) -> Result<(), tauri_plugin_updater::Error> {
189-
println!("Setting update channel to {}", channel);
190-
app.updater_builder()
191-
.endpoints(vec![Url::parse(
192-
format!(
193-
"https://github.com/LiRenTech/project-graph/releases/{channel}/download/latest.json"
194-
)
195-
.as_str(),
196-
)?])?
197-
.build()?
198-
.check()
199-
.await?;
200-
Ok(())
201-
}
202-
203181
#[cfg_attr(mobile, tauri::mobile_entry_point)]
204182
pub fn run() {
205183
// 在 Linux 上禁用 DMA-BUF 渲染器
@@ -226,6 +204,8 @@ pub fn run() {
226204
{
227205
app.handle().plugin(tauri_plugin_cli::init())?;
228206
app.handle().plugin(tauri_plugin_process::init())?;
207+
app.handle()
208+
.plugin(tauri_plugin_window_state::Builder::new().build())?;
229209
app.handle()
230210
.plugin(tauri_plugin_updater::Builder::new().build())?;
231211
}
@@ -244,9 +224,7 @@ pub fn run() {
244224
write_file_base64,
245225
write_stdout,
246226
write_stderr,
247-
exit,
248-
#[cfg(desktop)]
249-
set_update_channel
227+
exit
250228
])
251229
.run(tauri::generate_context!())
252230
.expect("error while running tauri application");

app/src/pages/_app.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { LogicalSize } from "@tauri-apps/api/dpi";
2+
import { restoreStateCurrent, saveWindowState, StateFlags } from "@tauri-apps/plugin-window-state";
23
import { useAtom } from "jotai";
34
import { Copy, Cpu, FlaskConical, Menu, Minus, PanelTop, Square, Tag, TextSearch, X, Zap } from "lucide-react";
45
import React from "react";
@@ -99,6 +100,8 @@ export default function App() {
99100
*/
100101
getCurrentWindow().onCloseRequested(async (e) => {
101102
e.preventDefault();
103+
// 保存窗口位置
104+
await saveWindowState(StateFlags.SIZE | StateFlags.POSITION | StateFlags.MAXIMIZED);
102105
try {
103106
if (Stage.path.getFilePath() === Stage.path.draftName) {
104107
if (StageManager.isEmpty()) {
@@ -186,6 +189,10 @@ export default function App() {
186189
Stage.path.setPathHook = (pathString: string) => {
187190
setFile(pathString);
188191
};
192+
193+
// 恢复窗口位置大小
194+
restoreStateCurrent(StateFlags.SIZE | StateFlags.POSITION | StateFlags.MAXIMIZED);
195+
189196
return () => {
190197
// 经过测试发现,只要是不关闭软件,根本不会执行这里
191198
// 随意切换软件内部界面不会执行这里

pnpm-lock.yaml

Lines changed: 16 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)