From 60c087d7aab2cc1d7ad5922d08105bf8cdaba56b Mon Sep 17 00:00:00 2001 From: sorrow404null Date: Fri, 18 Sep 2026 15:15:12 +0800 Subject: [PATCH] fix(webview2): scale native bounds for fractional DPI --- ui/views/wv2_view.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ui/views/wv2_view.py b/ui/views/wv2_view.py index edb646b..fd1b59e 100644 --- a/ui/views/wv2_view.py +++ b/ui/views/wv2_view.py @@ -152,19 +152,32 @@ class WebView2View(QtWidgets.QWidget): # ---------- 几何同步 ---------- def _dpr(self): try: - d = self.dpr() + d = float(self.devicePixelRatioF()) return d if d > 0 else 1.0 except Exception: return 1.0 + @staticmethod + def _physical_bounds(x, y, width, height, dpr): + """按矩形边界换算物理像素,避免分数 DPR 产生相邻白缝。""" + def pixel(value): + scaled = value * dpr + return int(scaled + 0.5) if scaled >= 0 else int(scaled - 0.5) + + left = pixel(x) + top = pixel(y) + right = pixel(x + width) + bottom = pixel(y + height) + return left, top, max(1, right - left), max(1, bottom - top) + def sync_bounds(self): if not self.isVisible() or not self._session.child_hwnd: return top = self.window() p = self.mapTo(top, QtCore.QPoint(0, 0)) d = self._dpr() - L, T = p.x() * d, p.y() * d - W, H = max(1, self.width() * d), max(1, self.height() * d) + L, T, W, H = self._physical_bounds( + p.x(), p.y(), self.width(), self.height(), d) # 去重:值没变就不发跨进程 COM(移动窗口时避免主线程被 WebView2 阻塞 → 整窗黑屏) # 注:旧版 resize hold(放大时钳制旧尺寸→露白边)已移除 —— # 拖动黑边的真凶是假异步 JS 泵(已修真异步),高频 SetBounds 实测 Chromium 完全跟得上;