Skip to content

Commit

Permalink
Merge pull request #57 from greenhat616/fix-window-state
Browse files Browse the repository at this point in the history
fix: window size state persistence
  • Loading branch information
keiko233 authored Dec 7, 2023
2 parents a3cb48b + 5127f77 commit 0946b3c
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 153 deletions.
175 changes: 86 additions & 89 deletions backend/Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions backend/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ serde_yaml = "0.9"
auto-launch = "0.5"
once_cell = "1.14.0"
port_scanner = "0.1.5"
delay_timer = "0.11.1"
delay_timer = "0.11.4"
parking_lot = "0.12.0"
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.11", features = ["json", "rustls-tls"] }
tauri = { version = "1.2.4", features = ["global-shortcut-all", "notification-all", "process-all", "shell-all", "system-tray", "updater", "window-all"] }
tauri = { version = "1.5.3", features = ["global-shortcut-all", "notification-all", "process-all", "shell-all", "system-tray", "updater", "window-all"] }
window-vibrancy = { version = "0.3.0" }
window-shadows = { version = "0.2.0" }
wry = { version = "0.24.3" }
window-shadows = { version = "0.2.2" }
wry = { version = "0.24.6" }


[target.'cfg(windows)'.dependencies]
Expand All @@ -53,7 +53,7 @@ windows-sys = { version = "0.48", features = [
] }

[target.'cfg(windows)'.dependencies.tauri]
version = "1.2.4"
version = "1.5.3"
features = [
"global-shortcut-all",
"icon-png",
Expand All @@ -65,7 +65,7 @@ features = [
]

[target.'cfg(linux)'.dependencies.tauri]
version = "1.2.4"
version = "1.5.3"
features = [
"global-shortcut-all",
"process-all",
Expand Down
8 changes: 7 additions & 1 deletion backend/tauri/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
config::*,
core::*,
feat,
utils::{dirs, help},
utils::{dirs, help, resolve::save_window_state},
};
use crate::{ret_err, wrap_err};
use anyhow::{Context, Result};
Expand Down Expand Up @@ -234,6 +234,12 @@ pub fn open_web_url(url: String) -> CmdResult<()> {
wrap_err!(open::that(url))
}

#[tauri::command]
pub fn save_window_size_state() -> CmdResult<()> {
let handle = handle::Handle::global().app_handle.lock().clone().unwrap();
wrap_err!(save_window_state(&handle, true))
}

#[cfg(windows)]
pub mod uwp {
use super::*;
Expand Down
15 changes: 15 additions & 0 deletions backend/tauri/src/config/verge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,30 @@ pub struct IVerge {
pub proxy_layout_column: Option<i32>,

/// window size and position
#[deprecated(note = "use `window_size_state` instead")]
#[serde(skip_serializing_if = "Option::is_none")]
pub window_size_position: Option<Vec<f64>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub window_size_state: Option<WindowState>,

/// 是否启用随机端口
pub enable_random_port: Option<bool>,

/// verge mixed port 用于覆盖 clash 的 mixed port
pub verge_mixed_port: Option<u16>,
}

#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct WindowState {
pub width: f64,
pub height: f64,
pub x: f64,
pub y: f64,
pub maximized: bool,
pub fullscreen: bool,
}

#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct IVergeTheme {
pub primary_color: Option<String>,
Expand Down Expand Up @@ -202,6 +216,7 @@ impl IVerge {
patch!(enable_clash_fields);

patch!(window_size_position);
patch!(window_size_state);
}

/// 在初始化前尝试拿到单例端口的值
Expand Down
9 changes: 6 additions & 3 deletions backend/tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fn main() -> std::io::Result<()> {
cmds::delete_profile,
cmds::read_profile_file,
cmds::save_profile_file,
cmds::save_window_size_state,
// service mode
cmds::service::check_service,
cmds::service::install_service,
Expand Down Expand Up @@ -111,7 +112,7 @@ fn main() -> std::io::Result<()> {
match event {
tauri::WindowEvent::CloseRequested { api, .. } => {
api.prevent_close();
let _ = resolve::save_window_size_position(&app_handle, true);
let _ = resolve::save_window_state(app_handle, true);

app_handle.get_window("main").map(|win| {
let _ = win.hide();
Expand All @@ -126,10 +127,12 @@ fn main() -> std::io::Result<()> {
if label == "main" {
match event {
tauri::WindowEvent::CloseRequested { .. } => {
let _ = resolve::save_window_size_position(&app_handle, true);
// log::info!(target: "app", "window close requested");
let _ = resolve::save_window_state(app_handle, true);
}
tauri::WindowEvent::Moved(_) | tauri::WindowEvent::Resized(_) => {
let _ = resolve::save_window_size_position(&app_handle, false);
// log::info!(target: "app", "window moved or resized");
let _ = resolve::save_window_state(app_handle, false);
}
_ => {}
}
Expand Down
70 changes: 60 additions & 10 deletions backend/tauri/src/utils/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::config::IVerge;
use crate::config::{IVerge, WindowState};
use crate::{config::Config, core::*, utils::init, utils::server};
use crate::{log_err, trace_err};
use anyhow::Result;
use serde::de;
use serde_yaml::Mapping;
use std::net::TcpListener;
use tauri::{App, AppHandle, Manager};
Expand Down Expand Up @@ -109,14 +110,12 @@ pub fn create_window(app_handle: &AppHandle) {
.title("Clash Nyanpasu")
.fullscreen(false)
.min_inner_size(600.0, 520.0);

match Config::verge().latest().window_size_position.clone() {
Some(size_pos) if size_pos.len() == 4 => {
let size = (size_pos[0], size_pos[1]);
let pos = (size_pos[2], size_pos[3]);
let w = size.0.clamp(600.0, f64::INFINITY);
let h = size.1.clamp(520.0, f64::INFINITY);
builder = builder.inner_size(w, h).position(pos.0, pos.1);
let win_state = &Config::verge().latest().window_size_state.clone();
match win_state {
Some(state) => {
builder = builder
.inner_size(state.width, state.height)
.position(state.x, state.y)
}
_ => {
#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -149,6 +148,14 @@ pub fn create_window(app_handle: &AppHandle) {
.build()
{
Ok(win) => {
if let Some(state) = win_state {
if state.maximized {
trace_err!(win.maximize(), "set win maximize");
}
if state.fullscreen {
trace_err!(win.set_fullscreen(true), "set win fullscreen");
}
}
log::trace!("try to calculate the monitor size");
let center = (|| -> Result<bool> {
let mut center = false;
Expand All @@ -175,7 +182,7 @@ pub fn create_window(app_handle: &AppHandle) {

// 加点延迟避免界面闪一下
tauri::async_runtime::spawn(async move {
sleep(Duration::from_millis(888)).await;
// sleep(Duration::from_millis(888)).await;

if let Some(window) = app_handle.get_window("main") {
trace_err!(set_shadow(&window, true), "set win shadow");
Expand Down Expand Up @@ -203,6 +210,7 @@ pub fn create_window(app_handle: &AppHandle) {
}

/// save window size and position
#[deprecated]
pub fn save_window_size_position(app_handle: &AppHandle, save_to_file: bool) -> Result<()> {
let win = app_handle
.get_window("main")
Expand All @@ -224,3 +232,45 @@ pub fn save_window_size_position(app_handle: &AppHandle, save_to_file: bool) ->

Ok(())
}

pub fn save_window_state(app_handle: &AppHandle, save_to_file: bool) -> Result<()> {
let win = app_handle
.get_window("main")
.ok_or(anyhow::anyhow!("failed to get window"))?;
let current_monitor = win.current_monitor()?;
let verge = Config::verge();
let mut verge = verge.latest();
match current_monitor {
Some(monitor) => {
let previous_state = verge.window_size_state.clone().unwrap_or_default();
let mut state = WindowState {
maximized: win.is_maximized()?,
fullscreen: win.is_fullscreen()?,
..previous_state
};
let is_minimized = win.is_minimized()?;

let scale_factor = monitor.scale_factor();
let size = win.inner_size()?.to_logical(scale_factor);
if size.width > 0. && size.height > 0. && !state.maximized && !is_minimized {
state.width = size.width;
state.height = size.height;
}
let position = win.outer_position()?.to_logical(scale_factor);
if !state.maximized && !is_minimized {
state.x = position.x;
state.y = position.y;
}
verge.window_size_state = Some(state);
}
None => {
verge.window_size_state = None;
}
}

if save_to_file {
verge.save_file()?;
}

Ok(())
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@mui/lab": "5.0.0-alpha.149",
"@mui/material": "^5.14.14",
"@mui/x-data-grid": "^6.16.3",
"@tauri-apps/api": "^1.3.0",
"@tauri-apps/api": "^1.5.1",
"ahooks": "^3.7.2",
"axios": "^1.1.3",
"dayjs": "1.11.5",
Expand All @@ -70,7 +70,7 @@
"@actions/github": "^5.0.3",
"@commitlint/cli": "18.4.3",
"@commitlint/config-conventional": "18.4.3",
"@tauri-apps/cli": "^1.3.1",
"@tauri-apps/cli": "^1.5.7",
"@types/fs-extra": "^9.0.13",
"@types/js-cookie": "^3.0.2",
"@types/lodash": "^4.14.180",
Expand Down
Loading

0 comments on commit 0946b3c

Please sign in to comment.