Skip to content

Commit 11e7a8a

Browse files
committed
支持窗口自定义大小,修复不同分辨率屏幕间拖动时窗口大小异常
1 parent d52ce50 commit 11e7a8a

File tree

7 files changed

+78
-21
lines changed

7 files changed

+78
-21
lines changed

components.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export {}
88
/* prettier-ignore */
99
declare module 'vue' {
1010
export interface GlobalComponents {
11-
ElHelp: typeof import('element-plus/es')['ElHelp']
1211
ElOption: typeof import('element-plus/es')['ElOption']
1312
ElSelect: typeof import('element-plus/es')['ElSelect']
1413
ElTooltip: typeof import('element-plus/es')['ElTooltip']

src-tauri/src/constants.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ lazy_static! {
66

77
pub static APPLICATION_NAME: &str = "一键启动";
88
pub static THEME: &str = "theme";
9+
pub static WINDOW_MIN_WIDTH: u32 = 800;
10+
pub static WINDOW_MIN_HEIGHT: u32 = 600;

src-tauri/src/events/system_listeners.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn refresh_tray(app: &AppHandle) {
7474
if let Err(e) = window_api::refresh_tray(app_cloned).await {
7575
error!("刷新系统图标失败.{:?}", e);
7676
} else {
77-
debug!("刷新系统图标失败成功.");
77+
debug!("刷新系统图标成功.");
7878
}
7979
});
8080
}

src-tauri/src/lib.rs

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use crate::error::OneClickLaunchError;
22
use anyhow::Result;
33
use api::{launcher_api, setting_api};
4-
use constants::AUTO_START_FLAG;
4+
use constants::{AUTO_START_FLAG, WINDOW_MIN_HEIGHT, WINDOW_MIN_WIDTH};
55
use db::{launcher, launcher_resource, settings};
66
use events::EventDispatcher;
77
use events::system_listeners::register_system_listeners;
88
use events::types::{ApplicationStartupComplete, ApplicationStartupCompletePayload};
99
use sqlx::{Executor, Sqlite};
1010
use sqlx::{SqlitePool, sqlite::SqlitePoolOptions};
1111
use std::path::PathBuf;
12+
use std::sync::Mutex;
13+
use std::time::Instant;
1214
use std::{env, fs};
1315
use tauri::Emitter;
1416
use tauri::tray::{MouseButton, MouseButtonState, TrayIcon, TrayIconEvent};
@@ -121,7 +123,7 @@ pub async fn run() -> Result<()> {
121123
tauri::Builder::default()
122124
.setup(move |app| {
123125
// 初始化窗口
124-
setup_window(app.handle())?;
126+
setup_tray(app.handle())?;
125127

126128
// 注册监听器,之后添加新的监听器时在这个方法内部添加
127129
register_listeners(app.handle());
@@ -136,13 +138,11 @@ pub async fn run() -> Result<()> {
136138

137139
Ok(())
138140
})
139-
.on_window_event(|window, event| {
140-
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
141-
api.prevent_close();
142-
let _ = window.hide();
143-
}
144-
})
141+
.on_window_event(handle_window_event)
145142
.manage(db_manager)
143+
.manage(ScaleFactorChangedState {
144+
last_reset: Mutex::new(None),
145+
})
146146
// 优先注册单例插件
147147
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
148148
info!("{}, {argv:?}, {cwd}", app.package_info().name);
@@ -190,7 +190,7 @@ fn register_listeners(app: &AppHandle) {
190190
}
191191

192192
/// 初始化窗口
193-
fn setup_window(app: &AppHandle) -> Result<()> {
193+
fn setup_tray(app: &AppHandle) -> Result<()> {
194194
let tray_icon = TrayIconBuilder::new()
195195
// 设置系统托盘的提示,鼠标悬浮时会显示
196196
.tooltip(constants::APPLICATION_NAME)
@@ -247,3 +247,64 @@ fn setup_window(app: &AppHandle) -> Result<()> {
247247

248248
Ok(())
249249
}
250+
251+
struct ScaleFactorChangedState {
252+
last_reset: Mutex<Option<Instant>>,
253+
}
254+
255+
fn handle_window_event(window: &tauri::Window, event: &tauri::WindowEvent) {
256+
match event {
257+
tauri::WindowEvent::CloseRequested { api, .. } => {
258+
api.prevent_close();
259+
let _ = window.hide();
260+
}
261+
tauri::WindowEvent::ScaleFactorChanged {
262+
scale_factor,
263+
new_inner_size,
264+
..
265+
} => {
266+
print!(
267+
"ScaleFactorChanged scale_factor: {}, new_inner_size: {:?}",
268+
scale_factor, new_inner_size
269+
);
270+
271+
let now = Instant::now();
272+
let state = window.state::<ScaleFactorChangedState>();
273+
let mut lock = state.last_reset.try_lock();
274+
275+
if let Ok(ref mut last_reset) = lock {
276+
// 500ms防抖间隔
277+
if last_reset.map_or(true, |t| now.duration_since(t).as_millis() > 500) {
278+
if let Ok(physical_size) = window.inner_size() {
279+
// 如果窗口大小异常,强制调整到正常大小
280+
if physical_size.width != WINDOW_MIN_WIDTH
281+
|| physical_size.height != WINDOW_MIN_HEIGHT
282+
{
283+
let _ = window.set_size(tauri::Size::Logical(tauri::LogicalSize {
284+
width: WINDOW_MIN_WIDTH as f64,
285+
height: WINDOW_MIN_HEIGHT as f64,
286+
}));
287+
**last_reset = Some(now);
288+
debug!("DPI发生变化,触发窗口大小重置");
289+
}
290+
}
291+
} else {
292+
debug!("DPI重置窗口防抖生效");
293+
}
294+
}
295+
}
296+
tauri::WindowEvent::DragDrop(drag_drop_event) => todo!(),
297+
tauri::WindowEvent::Resized(physical_size) => {
298+
// 如果窗口大小过小,强制调整到正常大小
299+
if physical_size.width < WINDOW_MIN_WIDTH || physical_size.height < WINDOW_MIN_HEIGHT {
300+
let _ = window.set_size(tauri::Size::Logical(tauri::LogicalSize {
301+
width: WINDOW_MIN_WIDTH as f64,
302+
height: WINDOW_MIN_HEIGHT as f64,
303+
}));
304+
debug!("窗口大小过小,触发窗口大小重置");
305+
}
306+
}
307+
_ => {
308+
}
309+
}
310+
}

src-tauri/tauri.conf.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
"height": 600,
1919
"minWidth": 800,
2020
"minHeight": 600,
21-
"maxWidth": 800,
22-
"maxHeight": 600,
23-
"maximizable":false,
2421
"devtools": true
2522
}
2623
],

src/home.vue

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="home" style="margin: 0; padding: 0;">
2+
<div class="home" style="margin: 0;">
33
<div class="topbar">
44
<button class="create-launcher-button" @click="createLauncher">创建新编组</button>
55
<div class="topbar-button">
@@ -144,8 +144,8 @@ export default {
144144
<style scoped>
145145
.home {
146146
width: 100%;
147-
height: 100%;
148-
padding: 0px;
147+
height: 98vh;
148+
padding: 0px 0px 10px 0px;
149149
box-sizing: border-box;
150150
display: flex;
151151
flex-flow: column nowrap;
@@ -164,7 +164,7 @@ export default {
164164
padding: 10px 10px 10px 10px;
165165
scrollbar-width: auto;
166166
/* 调整滚动条宽度 */
167-
/* flex:1; */
167+
flex:1;
168168
/* height: clac(100vh -50px); */
169169
}
170170
@@ -184,8 +184,6 @@ export default {
184184
.launcher-container>* {
185185
flex: 0 0 300px;
186186
width: 0;
187-
/* 高度固定为 500px,和原始组件一致 */
188-
height: 500px;
189187
padding: 10px 10px 10px 10px;
190188
}
191189

src/launcher.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export default {
280280
<style scoped>
281281
.launcher {
282282
width: 300px;
283-
height: 500px;
283+
height: 100%;
284284
border: 1px solid #ccc;
285285
border-radius: 8px;
286286
padding: 10px;

0 commit comments

Comments
 (0)