feat: save window size and position

This commit is contained in:
GyDi 2023-07-22 10:58:16 +08:00
parent 6b0ca2966e
commit 177a22df59
No known key found for this signature in database
GPG Key ID: 9C3AD40F1F99880A
4 changed files with 71 additions and 11 deletions

View File

@ -79,6 +79,10 @@ pub struct IVerge {
/// proxy 页面布局 列数
pub proxy_layout_column: Option<i32>,
/// window size and position
#[serde(skip_serializing_if = "Option::is_none")]
pub window_size_position: Option<Vec<f64>>,
}
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
@ -174,6 +178,8 @@ impl IVerge {
patch!(enable_builtin_enhanced);
patch!(proxy_layout_column);
patch!(enable_clash_fields);
patch!(window_size_position);
}
/// 在初始化前尝试拿到单例端口的值

View File

@ -141,6 +141,8 @@ impl Tray {
"restart_clash" => feat::restart_clash_core(),
"restart_app" => api::process::restart(&app_handle.env()),
"quit" => {
let _ = resolve::save_window_size_position(app_handle);
resolve::resolve_reset();
api::process::kill_children();
app_handle.exit(0);

View File

@ -109,6 +109,8 @@ fn main() -> std::io::Result<()> {
match event {
tauri::WindowEvent::CloseRequested { api, .. } => {
api.prevent_close();
let _ = resolve::save_window_size_position(&app_handle);
app_handle.get_window("main").map(|win| {
let _ = win.hide();
});
@ -117,6 +119,19 @@ fn main() -> std::io::Result<()> {
}
}
}
#[cfg(not(target_os = "macos"))]
tauri::RunEvent::WindowEvent { label, event, .. } => {
use tauri::Manager;
if label == "main" {
match event {
tauri::WindowEvent::CloseRequested { .. } => {
let _ = resolve::save_window_size_position(&app_handle);
}
_ => {}
}
}
}
_ => {}
});

View File

@ -1,6 +1,6 @@
use crate::config::Config;
use crate::log_err;
use crate::{core::*, utils::init, utils::server};
use crate::{config::Config, core::*, utils::init, utils::server};
use anyhow::Result;
use tauri::{App, AppHandle, Manager};
/// handle something when start app
@ -49,16 +49,39 @@ pub fn create_window(app_handle: &AppHandle) {
return;
}
let builder = tauri::window::WindowBuilder::new(
let mut builder = tauri::window::WindowBuilder::new(
app_handle,
"main".to_string(),
tauri::WindowUrl::App("index.html".into()),
)
.title("Clash Verge")
.center()
.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]);
builder = builder.inner_size(size.0, size.1).position(pos.0, pos.1);
}
_ => {
#[cfg(target_os = "windows")]
{
builder = builder.inner_size(800.0, 636.0).center();
}
#[cfg(target_os = "macos")]
{
builder = builder.inner_size(800.0, 642.0).center();
}
#[cfg(target_os = "linux")]
{
builder = builder.inner_size(800.0, 642.0).center();
}
}
};
#[cfg(target_os = "windows")]
{
use std::time::Duration;
@ -68,7 +91,6 @@ pub fn create_window(app_handle: &AppHandle) {
match builder
.decorations(false)
.transparent(true)
.inner_size(800.0, 636.0)
.visible(false)
.build()
{
@ -96,15 +118,30 @@ pub fn create_window(app_handle: &AppHandle) {
#[cfg(target_os = "macos")]
crate::log_err!(builder
.decorations(true)
.inner_size(800.0, 642.0)
.hidden_title(true)
.title_bar_style(tauri::TitleBarStyle::Overlay)
.build());
#[cfg(target_os = "linux")]
crate::log_err!(builder
.decorations(true)
.transparent(false)
.inner_size(800.0, 642.0)
.build());
crate::log_err!(builder.decorations(true).transparent(false).build());
}
/// save window size and position
pub fn save_window_size_position(app_handle: &AppHandle) -> Result<()> {
let win = app_handle
.get_window("main")
.ok_or(anyhow::anyhow!("failed to get window"))?;
let scale = win.scale_factor()?;
let size = win.inner_size()?;
let size = size.to_logical::<f64>(scale);
let pos = win.outer_position()?;
let pos = pos.to_logical::<f64>(scale);
let verge = Config::verge();
let mut verge = verge.latest();
verge.window_size_position = Some(vec![size.width, size.height, pos.x, pos.y]);
verge.save_file()?;
Ok(())
}