From db18bb3b3ec7c530284efd9d27b8d16c72fd2b6f Mon Sep 17 00:00:00 2001 From: lijunjie Date: Tue, 24 Nov 2026 17:53:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E7=BB=84=E4=BB=B6=E5=BD=92=E4=BD=8D?= =?UTF-8?q?=E5=BD=BB=E5=BA=95=E4=BF=AE=E5=A4=8D=EF=BC=9A=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=E5=AE=88=E6=8A=A4=E6=94=B9=E7=BA=AF=20Win32=20=E6=AF=94?= =?UTF-8?q?=E5=AF=B9=EF=BC=8C=E6=97=A7=20DPI=20=E7=A9=BA=E9=97=B4=E8=AE=B0?= =?UTF-8?q?=E5=BF=86=E4=BD=8D=E7=BD=AE=E4=B8=80=E6=AC=A1=E6=80=A7=E4=BD=9C?= =?UTF-8?q?=E5=BA=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 真机取证定根因:150% 缩放下 Tauri 的 outer_position/set_position/primary_monitor 与 Win32 GetWindowRect/MonitorFromWindow/SetWindowPos 处在不同 DPI 空间; 旧 position=(1369,18) 是 RDP 小屏会话里存下的数字,回本机 2560 宽大屏后被 旧 clamp 判为「有效」原样保留——组件永远停在离右缘 660px 的半空,且上一版 守护用同一套失真坐标系自测「已就位」,所以修不动 - 心跳守护全部改用纯 Win32(GetWindowRect/MonitorFromWindow/SetWindowPos 同 一虚拟化):每拍比对实际矩形与 dock_point 应有位置,>2px 即拉回;拖动 3.5 秒宽限逻辑不变 - WidgetPrefs 增加 dock_v2 代际标记:首启丢弃旧空间存下的 position(角位停靠 恢复确定性),此后前端拖拽记忆与本守护同空间,永久被尊重 - 前端拖动记忆补洞:mount 播种真实位置、位移 <5px 的纯点击不再把(RDP 漂移 后的)现位置钉成用户记忆;尺寸自适应后 redock 不再依赖 lastPos 判角位 - dock_position → dock_point(monitor 矩形版,24px 物理边距≈原 16 逻辑边距), 单测更新并新增非原点副屏断言 --- release/0.1.12/README-Windows-x64.txt | 51 +++++ release/0.1.12/RELEASE_NOTES.md | 20 ++ release/0.1.12/SHA256SUMS.txt | 2 + release/0.1.12/squid-update.json | 13 ++ release/0.1.12/一键安装.bat | 5 + src-tauri/Cargo.lock | 12 +- src-tauri/src/widget.rs | 260 ++++++++++++++++++++------ src/pages/WidgetWindow.tsx | 28 ++- 8 files changed, 325 insertions(+), 66 deletions(-) create mode 100644 release/0.1.12/README-Windows-x64.txt create mode 100644 release/0.1.12/RELEASE_NOTES.md create mode 100644 release/0.1.12/SHA256SUMS.txt create mode 100644 release/0.1.12/squid-update.json create mode 100644 release/0.1.12/一键安装.bat diff --git a/release/0.1.12/README-Windows-x64.txt b/release/0.1.12/README-Windows-x64.txt new file mode 100644 index 0000000..96699f2 --- /dev/null +++ b/release/0.1.12/README-Windows-x64.txt @@ -0,0 +1,51 @@ +Squid 0.1.12 - Windows x64 + +系统要求 +-------- +- Windows 10 或 Windows 11(x64;ARM64 设备通过系统兼容层运行)。 +- Microsoft Edge WebView2 Runtime。Windows 10/11 通常已内置;如果 Squid + 启动后窗口空白,请从微软官方页面安装 WebView2 Evergreen Runtime: + https://developer.microsoft.com/microsoft-edge/webview2/ +- 安装与运行都不需要管理员权限。安装位置为 + %LOCALAPPDATA%\Programs\Squid。 +- 权限管理页中,机单(HKLM)范围的启动项开关与系统服务需要以管理员身份 + 运行 Squid 才能修改,界面会给出明确提示。 + +安装 +---- +1. 运行 Squid-Setup-0.1.12-win-x64.exe。 +2. 这是未签名构建。若 SmartScreen 显示警告,请选择“更多信息” > “仍要运行”。 +3. 升级直接覆盖安装,不再询问是否先卸载旧版本;用户配置自动继承。 +4. 卸载请在 Windows“设置 > 应用”中移除 Squid。 + +更新 +---- +Squid 使用与 nex_shift 一致的 releases 分支更新通道。更新包会先校验 +文件大小与 SHA256,再在退出后运行同一个安装程序。若构建时写入发布公钥, +还会验证清单的 Ed25519 签名。 + +校验 +---- +PowerShell: + Get-FileHash .\Squid-Setup-0.1.12-win-x64.exe -Algorithm SHA256 + +提示 +---- +- 覆盖升级前请先完全退出 Squid(关闭窗口只会进入托盘,请在托盘图标 + 上右键退出),否则安装程序无法替换正在运行的程序文件。 +- 本机若装有腾讯电脑管家,其"程序保护"可能拦截未签名安装程序写入 + 临时目录,报"NSIS Error - Error writing temporary file"。三种绕法, + 按方便程度排序: + 1) 双击本目录的「一键安装.bat」——它会把安装包复制到 C 盘临时目录 + 再启动(管家只拦从非系统盘启动的安装程序,此法本机验证有效); + 2) 管家弹窗出现时选"允许",或将安装文件加入信任区后重试; + 3) 使用便携包 Squid-0.1.12-portable-win-x64.zip,解压后把 Squid 目录 + 内的文件覆盖到安装目录(先退出托盘中的 Squid),效果相同。 +- 本版本是导航整合版:空间清理(垃圾清理/重复文件/大文件)与系统维护 + (一键修复/隐私清理/系统优化)各收成一个三页签页面;一级目录 + 「存储空间」移除(与大文件重叠)。所有功能与删除保护不变。 +- 桌面小组件「回到本机跑到桌面中间」彻底修复:改为每秒比对实际位置与 + 应在位置,谁动它都回位(含 RDP 会话切换、系统重摆、第三方软件移动); + 手动拖动会自动宽限,不跟手打架。 +- 「定位」按钮在含空格路径(如 Package Cache)下跳错文件夹的问题已在 + 0.1.11 修复,本版本一并携带。 diff --git a/release/0.1.12/RELEASE_NOTES.md b/release/0.1.12/RELEASE_NOTES.md new file mode 100644 index 0000000..363fdb5 --- /dev/null +++ b/release/0.1.12/RELEASE_NOTES.md @@ -0,0 +1,20 @@ +Squid 0.1.12 更新说明 + +调整 +- 左侧导航大整合,功能一个不少、入口少一半: + · 「空间清理」升级为三页签:垃圾清理 / 重复文件 / 大文件。页签标题带 + 实时计数徽标(不点进去也知道有没有货);切换页签不清结果——扫描状态、 + 勾选、正在跑的任务全部保留;进入哪个页签才扫哪个,不再拖慢首屏。 + · 「系统维护」升级为三页签:一键修复 / 隐私清理 / 系统优化。隐私清理与 + 系统优化从桌面组并入;修复任务运行中切页签,日志照常滚动。 + · 一级目录「存储空间」整页移除(与「大文件」定位重叠),矩形图随之 + 下线;「洞察」组只剩实时监控,「桌面」组只剩桌面小工具。 + · 快捷键重排:1 概览 · 2 空间清理 · 3 应用卸载 · 4 权限管理 · 5 系统维护 + · 6 实时监控 · 7 小工具 · 8 历史 · 9 设置,0 开关小工具。 + +修复 +- 桌面小组件「远程被别人连过之后,回到本机就跑到桌面中间」:上一版只在 + 屏幕分辨率变化时归位,但 RDP 接入时本机会话直接断开、回到本机解锁时 + 分辨率从未变过——是 Windows 在会话重连时按旧几何重新摆放了窗口。现在 + 心跳每秒比对小组件实际位置与应在位置(设定角落或您拖动记住的点), + 任何偏差立即拉回并重新压回桌面层;正在拖动时自动宽限,不会跟手打架。 diff --git a/release/0.1.12/SHA256SUMS.txt b/release/0.1.12/SHA256SUMS.txt new file mode 100644 index 0000000..9a451d3 --- /dev/null +++ b/release/0.1.12/SHA256SUMS.txt @@ -0,0 +1,2 @@ +e086d69d3cbcca7342d72a4c3e51d3515a8f297a963a4575f4936290995c72a4 Squid-Setup-0.1.12-win-x64.exe +060ff9f4231791669192c0f7f6b4ad7a23ad88a1e0c314266c67967e9f7de4b7 Squid-0.1.12-portable-win-x64.zip diff --git a/release/0.1.12/squid-update.json b/release/0.1.12/squid-update.json new file mode 100644 index 0000000..5887dc3 --- /dev/null +++ b/release/0.1.12/squid-update.json @@ -0,0 +1,13 @@ +{ + "schema": 1, + "version": "0.1.12", + "date": "2026-11-24", + "notes": "Squid 0.1.12 更新说明\n\n调整\n- 左侧导航大整合,功能一个不少、入口少一半:\n · 「空间清理」升级为三页签:垃圾清理 / 重复文件 / 大文件。页签标题带\n 实时计数徽标(不点进去也知道有没有货);切换页签不清结果——扫描状态、\n 勾选、正在跑的任务全部保留;进入哪个页签才扫哪个,不再拖慢首屏。\n · 「系统维护」升级为三页签:一键修复 / 隐私清理 / 系统优化。隐私清理与\n 系统优化从桌面组并入;修复任务运行中切页签,日志照常滚动。\n · 一级目录「存储空间」整页移除(与「大文件」定位重叠),矩形图随之\n 下线;「洞察」组只剩实时监控,「桌面」组只剩桌面小工具。\n · 快捷键重排:1 概览 · 2 空间清理 · 3 应用卸载 · 4 权限管理 · 5 系统维护\n · 6 实时监控 · 7 小工具 · 8 历史 · 9 设置,0 开关小工具。\n\n修复\n- 桌面小组件「远程被别人连过之后,回到本机就跑到桌面中间」:上一版只在\n 屏幕分辨率变化时归位,但 RDP 接入时本机会话直接断开、回到本机解锁时\n 分辨率从未变过——是 Windows 在会话重连时按旧几何重新摆放了窗口。现在\n 心跳每秒比对小组件实际位置与应在位置(设定角落或您拖动记住的点),\n 任何偏差立即拉回并重新压回桌面层;正在拖动时自动宽限,不会跟手打架。\n", + "artifacts": { + "windows": { + "file": "Squid-Setup-0.1.12-win-x64.exe", + "sha256": "e086d69d3cbcca7342d72a4c3e51d3515a8f297a963a4575f4936290995c72a4", + "size": 3855299 + } + } +} diff --git a/release/0.1.12/一键安装.bat b/release/0.1.12/一键安装.bat new file mode 100644 index 0000000..13a2d1c --- /dev/null +++ b/release/0.1.12/一键安装.bat @@ -0,0 +1,5 @@ +@echo off +rem 360/QQPC Manager blocks unsigned installers started from non-system +drives when they unpack into TEMP. Copy the setup to %TEMP% on C: first. +copy /Y "%~dp0Squid-Setup-0.1.12-win-x64.exe" "%TEMP%\" >nul +start "" "%TEMP%\Squid-Setup-0.1.12-win-x64.exe" \ No newline at end of file diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 17f4645..387e11e 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -3512,7 +3512,7 @@ dependencies = [ [[package]] name = "squid-core" -version = "0.1.11" +version = "0.1.12" dependencies = [ "anyhow", "chrono", @@ -3531,7 +3531,7 @@ dependencies = [ [[package]] name = "squid-desktop" -version = "0.1.11" +version = "0.1.12" dependencies = [ "base64 0.22.1", "chrono", @@ -3555,7 +3555,7 @@ dependencies = [ [[package]] name = "squid-engine" -version = "0.1.11" +version = "0.1.12" dependencies = [ "anyhow", "chrono", @@ -3578,7 +3578,7 @@ dependencies = [ [[package]] name = "squid-platform" -version = "0.1.11" +version = "0.1.12" dependencies = [ "dirs 6.0.0", "libc", @@ -3597,7 +3597,7 @@ dependencies = [ [[package]] name = "squid-rules" -version = "0.1.11" +version = "0.1.12" dependencies = [ "globset", "serde", @@ -3609,7 +3609,7 @@ dependencies = [ [[package]] name = "squid-stats" -version = "0.1.11" +version = "0.1.12" dependencies = [ "chrono", "libc", diff --git a/src-tauri/src/widget.rs b/src-tauri/src/widget.rs index a894966..754ec3f 100644 --- a/src-tauri/src/widget.rs +++ b/src-tauri/src/widget.rs @@ -61,6 +61,94 @@ mod desktop_layer { const SWP_NOSIZE: u32 = 0x0001; const SWP_NOACTIVATE: u32 = 0x0010; const DWMWA_CLOAKED: u32 = 14; + const MONITOR_DEFAULTTOPRIMARY: u32 = 1; + + #[repr(C)] + struct WinRect { + left: i32, + top: i32, + right: i32, + bottom: i32, + } + #[repr(C)] + struct MonitorInfo { + cb_size: u32, + monitor: WinRect, + work: WinRect, + flags: u32, + } + #[link(name = "user32")] + extern "system" { + fn GetWindowRect(hwnd: *mut c_void, rect: *mut WinRect) -> i32; + fn MonitorFromWindow(hwnd: *mut c_void, flags: u32) -> *mut c_void; + fn GetMonitorInfoW(monitor: *mut c_void, info: *mut MonitorInfo) -> i32; + } + + /// (left, top, right, bottom) of the monitor this window sits on — in + /// true physical pixels, bypassing every DPI translation the webview + /// stack applies. (The Tauri `primary_monitor`/`outer_position` pair + /// disagrees with Win32 by the scale factor on a 150% display, which is + /// what let a mid-screen parking spot read as "correctly docked".) + pub fn monitor_rect(hwnd: *mut c_void) -> Option<(i32, i32, i32, i32)> { + // SAFETY: plain queries; the MONITORINFO carries its own size. + let monitor = unsafe { MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY) }; + if monitor.is_null() { + return None; + } + let mut info = MonitorInfo { + cb_size: std::mem::size_of::() as u32, + monitor: WinRect { + left: 0, + top: 0, + right: 0, + bottom: 0, + }, + work: WinRect { + left: 0, + top: 0, + right: 0, + bottom: 0, + }, + flags: 0, + }; + if unsafe { GetMonitorInfoW(monitor, &mut info) } == 0 { + return None; + } + Some(( + info.monitor.left, + info.monitor.top, + info.monitor.right, + info.monitor.bottom, + )) + } + + /// (x, y, width, height) of the window in physical pixels. + pub fn window_rect(hwnd: *mut c_void) -> Option<(i32, i32, i32, i32)> { + let mut rect = WinRect { + left: 0, + top: 0, + right: 0, + bottom: 0, + }; + // SAFETY: plain query on a live handle. + if unsafe { GetWindowRect(hwnd, &mut rect) } == 0 { + return None; + } + Some(( + rect.left, + rect.top, + rect.right - rect.left, + rect.bottom - rect.top, + )) + } + + /// Move (keep size) and re-park on the desktop layer in one call. + pub fn move_to(hwnd: *mut c_void, x: i32, y: i32) { + // SAFETY: z-order/position edit only; NOACTIVATE keeps focus away. + unsafe { + SetWindowPos(hwnd, HWND_BOTTOM, x, y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE); + } + } pub fn lower(hwnd: *mut c_void) { // SAFETY: hwnd comes from Tauri's own window handle; SetWindowPos with @@ -173,19 +261,19 @@ pub fn spawn_desktop_watch(app: &AppHandle) { let Ok(hwnd) = window.hwnd() else { continue }; let raw: *mut std::ffi::c_void = hwnd.0.cast(); // ── position guard ──────────────────────────────────────────── - let position = window.outer_position().ok().map(|p| (p.x, p.y)); - if let Some(pos) = position { - if last_pos != Some(pos) { + if let Some((x, y, w, h)) = desktop_layer::window_rect(raw) { + if last_pos != Some((x, y)) { // The window just moved: someone (the user, most likely) // is working on it. Re-check shortly instead of yanking // it mid-drag; the loop's cadence doubles as the grace. - last_pos = Some(pos); + last_pos = Some((x, y)); grace = std::time::Instant::now() + std::time::Duration::from_millis(3_500); } else if std::time::Instant::now() >= grace { - let settled = is_settled(&window, &prefs); - if !settled { - place_window(&app, &prefs); - desktop_layer::lower(raw); + if let Some(monitor) = desktop_layer::monitor_rect(raw) { + let want = dock_point(monitor, (w, h), &prefs); + if (x - want.0).abs() > 2 || (y - want.1).abs() > 2 { + desktop_layer::move_to(raw, want.0, want.1); + } } } } @@ -209,25 +297,6 @@ pub fn spawn_desktop_watch(app: &AppHandle) { }); } -/// Whether the widget currently sits where the prefs say it belongs -/// (tolerating a couple of pixels of DPI rounding). False when the geometry -/// cannot be queried — a failed query re-docks rather than strands. -#[cfg(windows)] -fn is_settled(window: &tauri::WebviewWindow, prefs: &WidgetPrefs) -> bool { - let Ok(Some(monitor)) = window.primary_monitor() else { - return false; - }; - let Ok(pos) = window.outer_position() else { - return false; - }; - let outer = window - .outer_size() - .map(|s| (s.width as i32, s.height as i32)) - .unwrap_or((350, 620)); - let want = dock_position((monitor.size().width, monitor.size().height), outer, prefs); - (pos.x - want.0).abs() <= 2 && (pos.y - want.1).abs() <= 2 -} - #[cfg(not(windows))] pub fn spawn_desktop_watch(_app: &AppHandle) {} @@ -261,6 +330,15 @@ pub struct WidgetPrefs { pub collapsed: bool, /// Last user-dragged position; None re-docks to `corner`. pub position: Option<(i32, i32)>, + /// Docking scheme generation. Pre-0.1.12 builds persisted drag positions + /// and docked corners in spaces that disagreed (and an RDP session's + /// small-screen numbers survive as "valid" mid-desktop spots on a big + /// display). The first 0.1.12 boot drops the remembered spot once and + /// sets this flag; corner docking becomes deterministic again, and every + /// drag stored from then on is honoured in the same physical space the + /// guard measures. + #[serde(default)] + pub dock_v2: bool, } impl Default for WidgetPrefs { @@ -278,6 +356,7 @@ impl Default for WidgetPrefs { remember_collapsed: true, collapsed: false, position: None, + dock_v2: true, } } } @@ -346,36 +425,89 @@ fn save_prefs(state: &WidgetState) -> Result<(), String> { fs::write(path, text).map_err(|error| error.to_string()) } -/// Where the widget belongs on a screen of `screen` pixels, all in physical -/// pixels: the remembered drag position clamped into the monitor (an -/// unplugged-secondary position can not hide it off-screen), or the -/// configured corner. Pure so the re-dock maths stays testable headless. -fn dock_position(screen: (u32, u32), outer: (i32, i32), prefs: &WidgetPrefs) -> (i32, i32) { - let margin = 16; +/// Where the widget belongs on a monitor rect `(left, top, right, bottom)` +/// of physical screen pixels — the same space Win32 `GetWindowRect` and the +/// persisted drag position (`outerPosition`, Tauri "physical") live in, so +/// the heartbeat compares like with like. A remembered drag position is +/// clamped onto the monitor (an unplugged-secondary position can not hide +/// it off-screen); otherwise the configured corner + a margin (24 physical +/// px ≈ the old 16 logical px at 150% scale). Pure so the re-dock maths +/// stays testable headless. +fn dock_point(monitor: (i32, i32, i32, i32), size: (i32, i32), prefs: &WidgetPrefs) -> (i32, i32) { + let margin = 24; + let (left, top, right, bottom) = monitor; if let Some((x, y)) = prefs.position { ( - x.clamp(0, (screen.0 as i32 - outer.0).max(0)), - y.clamp(0, (screen.1 as i32 - outer.1).max(0)), + x.clamp(left, (right - size.0).max(left)), + y.clamp(top, (bottom - size.1).max(top)), ) } else { let x = match prefs.corner.as_str() { - "top-left" | "bottom-left" => margin, - _ => screen.0 as i32 - outer.0 - margin, + "top-left" | "bottom-left" => left + margin, + _ => right - size.0 - margin, }; let y = match prefs.corner.as_str() { - "bottom-left" | "bottom-right" => screen.1 as i32 - outer.1 - margin, - _ => margin, + "bottom-left" | "bottom-right" => bottom - size.1 - margin, + _ => top + margin, }; (x, y) } } -/// Dock to a screen corner (widget width ~350px) or restore the remembered -/// drag position. Called on creation, whenever the corner changes, and -/// whenever the heartbeat sees the desktop geometry shift (RDP connect/ -/// disconnect rewrites the console's display: the widget would otherwise sit -/// at the small virtual screen's corner, stranded mid-desktop after the -/// native resolution comes back). +/// Dock to the configured corner or restore the remembered drag position. +/// Called on creation, whenever the corner changes, and whenever the +/// heartbeat sees the window drift off its spot. +/// +/// The drift this guards against is what the user hit: an RDP session runs +/// the console at the peer's resolution (e.g. 1920×1080); Windows re-lays +/// out top-level windows there, and any position persisted from that +/// session is a *small-screen* number. Back at the native 2560×1440 the +/// old code still found that number "valid" (it clamps fine) and re-docked +/// to it — the widget parked ~660 px short of the right edge, mid-desktop. +/// Now the guard compares the window's live Win32 rect with what the prefs +/// demand every beat, and pre-0.1.12 remembered spots are dropped once +/// (`dock_v2`) so corner docking becomes deterministic again. +#[cfg(windows)] +fn place_window(app: &AppHandle, prefs: &WidgetPrefs) { + let Some(window) = app.get_webview_window(WIDGET_LABEL) else { + return; + }; + let Ok(hwnd) = window.hwnd() else { return }; + let raw: *mut std::ffi::c_void = hwnd.0.cast(); + let Some(monitor) = desktop_layer::monitor_rect(raw) else { + return; + }; + let size = desktop_layer::window_rect(raw).map(|(_, _, w, h)| (w, h)); + // A just-created window may still report the builder size before the + // content fit lands; dock_point tolerates that (worst case the next + // auto-resize redocks through the same path). + let size = size.unwrap_or((525, 930)); + let pos = dock_point(monitor, size, prefs); + desktop_layer::move_to(raw, pos.0, pos.1); +} + +/// One-time repair of pre-0.1.12 `position` values: they were persisted +/// while the docking maths (physical px) and the drag readback (DPI-scaled +/// virtual px) disagreed, and an RDP re-layout could even persist a drifted +/// mid-screen spot as if the user had chosen it. Rather than guess, drop the +/// remembered spot once — the widget returns to the configured corner; any +/// drag from now on is persisted in the same physical space this file docks +/// in (the page now sends `outerPosition` through the device scale factor), +/// so it is respected forever. +#[cfg(windows)] +fn migrate_stale_position(state: &WidgetState) -> Result<(), String> { + { + let mut prefs = WidgetState::lock(&state.prefs)?; + if prefs.dock_v2 { + return Ok(()); + } + prefs.position = None; + prefs.dock_v2 = true; + } + save_prefs(state) +} + +#[cfg(not(windows))] fn place_window(app: &AppHandle, prefs: &WidgetPrefs) { let Some(window) = app.get_webview_window(WIDGET_LABEL) else { return; @@ -383,12 +515,17 @@ fn place_window(app: &AppHandle, prefs: &WidgetPrefs) { let Ok(Some(monitor)) = window.primary_monitor() else { return; }; - let screen = (monitor.size().width, monitor.size().height); + let rect = ( + 0, + 0, + monitor.size().width as i32, + monitor.size().height as i32, + ); let outer = window .outer_size() .map(|s| (s.width as i32, s.height as i32)) .unwrap_or((350, 620)); - let pos = dock_position(screen, outer, prefs); + let pos = dock_point(rect, outer, prefs); let _ = window.set_position(tauri::PhysicalPosition::new(pos.0, pos.1)); } @@ -565,6 +702,12 @@ pub fn widget_hide(app: AppHandle) -> Result<(), String> { /// Create the window at startup when enabled; the main page can also open it /// later through widget_set. pub fn spawn_startup(app: &AppHandle) { + #[cfg(windows)] + { + // One-time drop of a pre-dock-v2 remembered position (see the field + // docs) before anything docks off it. + let _ = migrate_stale_position(&app.state::()); + } let prefs = app.state::().current(); sync_window(app, &prefs); } @@ -586,7 +729,7 @@ pub fn toggle_from_tray(app: &AppHandle) -> Result<(), String> { #[cfg(test)] mod tests { - use super::{dock_position, WidgetPatch, WidgetPrefs}; + use super::{dock_point, WidgetPatch, WidgetPrefs}; #[test] fn corner_dock_follows_the_screen_size() { @@ -597,15 +740,19 @@ mod tests { corner: "top-right".into(), ..WidgetPrefs::default() }; - let big = dock_position((2560, 1440), (350, 620), &prefs); - assert_eq!(big, (2560 - 350 - 16, 16)); - let small = dock_position((1280, 720), (350, 620), &prefs); - assert_eq!(small, (1280 - 350 - 16, 16)); + let big = dock_point((0, 0, 2560, 1440), (350, 620), &prefs); + assert_eq!(big, (2560 - 350 - 24, 24)); + let small = dock_point((0, 0, 1280, 720), (350, 620), &prefs); + assert_eq!(small, (1280 - 350 - 24, 24)); let left = WidgetPrefs { corner: "bottom-left".into(), ..WidgetPrefs::default() }; - assert_eq!(dock_position((2560, 1440), (350, 620), &left), (16, 804)); + assert_eq!(dock_point((0, 0, 2560, 1440), (350, 620), &left), (24, 796)); + // A monitor that is not at the origin (extended desktop with the + // primary on the right) docks to *its own* corners. + let offset = dock_point((1920, 0, 1920 + 2560, 1440), (350, 620), &prefs); + assert_eq!(offset, (1920 + 2560 - 350 - 24, 24)); } #[test] @@ -614,11 +761,14 @@ mod tests { position: Some((1200, 300)), ..WidgetPrefs::default() }; - assert_eq!(dock_position((2560, 1440), (350, 620), &prefs), (1200, 300)); + assert_eq!( + dock_point((0, 0, 2560, 1440), (350, 620), &prefs), + (1200, 300) + ); // A remembered spot from a bigger unplugged monitor is clamped back // into the current desktop instead of being lost or hidden. assert_eq!( - dock_position((1280, 720), (350, 620), &prefs), + dock_point((0, 0, 1280, 720), (350, 620), &prefs), (1280 - 350, 720 - 620) ); } diff --git a/src/pages/WidgetWindow.tsx b/src/pages/WidgetWindow.tsx index 9f876dd..50f28d8 100644 --- a/src/pages/WidgetWindow.tsx +++ b/src/pages/WidgetWindow.tsx @@ -111,8 +111,10 @@ export function WidgetWindow() { void windowRef.current ?.setSize(new LogicalSize(350, height)) .then(() => { - // Keep the docked corner anchored after the size change. - if (!lastPos.current) void api.widgetSet({ redock: true }).catch(() => undefined); + // Keep the widget anchored after the size change: redock runs + // the backend maths, which re-corners a corner-docked widget and + // keeps (clamped) a remembered drag spot — correct in both modes. + void api.widgetSet({ redock: true }).catch(() => undefined); }) .catch(() => { lastFit.current = 0; @@ -133,16 +135,32 @@ export function WidgetWindow() { patch({ collapsed: !collapsed }); }; - // Persist a dragged position after the pointer settles (the header is a - // native drag region, so poll once shortly after mouseup). + // Persist a *real* drag after the pointer settles (the header is a native + // drag region, so poll once shortly after mouseup). Mere clicks must not + // pin the position: seed lastPos with the true current spot on mount, and + // only persist once the window has actually moved (≥5 px). An earlier bug + // let a click right after an RDP re-layout save the drifted mid-screen + // spot as "the user's chosen position" forever. useEffect(() => { + void windowRef.current + ?.outerPosition() + .then((position) => { + lastPos.current = position; + }) + .catch(() => undefined); const onUp = () => { window.setTimeout(() => { void windowRef.current ?.outerPosition() .then((position) => { const previous = lastPos.current; - if (previous && previous.x === position.x && previous.y === position.y) return; + if ( + previous && + Math.abs(previous.x - position.x) < 5 && + Math.abs(previous.y - position.y) < 5 + ) { + return; + } lastPos.current = position; patch({ position: [position.x, position.y], keepPosition: true }); })