Skip to content

armgit / BUI

Public Imported from GitHub
0

Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI

fix(tkinter): stop scaling fitted windows twice on hidpi

Krish Kalariabdea18c0f737
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ea1b0ea..ab7aaaf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -202,6 +202,7 @@ The newest work. Details for some items follow in the sections below.
- **GPU text:** `BUI_gpu_slug()` read the environment on every text run, which pushed the GPU raster from 1.6 to 9.4 ms per frame at 3,000 cells. It now reads it once.
- **`WindowHandle::update`:** `cx.notify()` inside the closure didn't re-render that window.
- **Python, hidden Tk root:** BUI's hidden Tk root (started before the first window on macOS, and by the Tk host) became `tkinter._default_root`, so a real tkinter program's `StringVar()` bound to the hidden interpreter and its widgets never saw the value.
+- **tkinter on HiDPI:** a window fitting its content (and `simpledialog` windows) was scaled twice, so it came out twice too large on Retina displays and other scaled screens.
### Changed
- **Text looks like Blender's:** Inter, Blender's UI font, is embedded and is the default face on every platform. The portable stb_truetype engine is now the default on Windows too. `-DBUI_TEXT_GDI=ON` (Rust: the `gdi-text` feature) restores GDI and system fonts. `BUI_TEXT_STB` and `portable-text` are now no-ops.
diff --git a/bindings/python/bui/tksyntax/_core.py b/bindings/python/bui/tksyntax/_core.py
index be9e881..bf6a910 100644
--- a/bindings/python/bui/tksyntax/_core.py
+++ b/bindings/python/bui/tksyntax/_core.py
@@ -19,6 +19,7 @@ import collections
import enum
import functools
import itertools
+import math
import re
import sys
import time
@@ -2227,16 +2228,22 @@ class _WindowHost:
self.destroy()
return False
+ def _fit_size(self):
+ """Logical size fitting the content (+ menu bar). natural() measures raster
+ pixels, which include the UI scale; set_size and geometry scale again."""
+ w, h = self._node.natural()
+ if self._menubar is not None and self._menubar._bar is not None:
+ bw, bh = self._menubar._bar.natural()
+ w, h = max(w, bw), h + bh
+ scale = buipy.kit_scale()
+ return max(1, math.ceil(w / scale)), max(1, math.ceil(h / scale))
+
def _show(self):
if self._shown_window or self._destroyed:
return
self._shown_window = True
- if self._geometry is None: # Tk: the window fits its content (+ menu bar)
- w, h = self._node.natural()
- if self._menubar is not None and self._menubar._bar is not None:
- bw, bh = self._menubar._bar.natural()
- w, h = max(w, bw), h + bh
- self._window.set_size(max(1, w), max(1, h))
+ if self._geometry is None: # Tk: the window fits its content
+ self._window.set_size(*self._fit_size())
master = self._wm_store.get("transient")
if master and self._wm_store.get("_placed") is False:
self._wm_store["_placed"] = True
diff --git a/bindings/python/bui/tksyntax/simpledialog.py b/bindings/python/bui/tksyntax/simpledialog.py
index 283aff5..74ebb30 100644
--- a/bindings/python/bui/tksyntax/simpledialog.py
+++ b/bindings/python/bui/tksyntax/simpledialog.py
@@ -17,7 +17,7 @@ def _place_window(w, parent=None):
"""Center `w` over its parent (or the screen) and show it."""
w.wm_withdraw()
w.update_idletasks()
- rw, rh = w._node.natural()
+ rw, rh = w._fit_size()
w.geometry("%dx%d" % (max(rw, 240), max(rh, 60)))
if parent is not None and parent.winfo_ismapped():
px, py = parent.winfo_rootx(), parent.winfo_rooty()
diff --git a/bindings/python/tests/test_tk_compat.py b/bindings/python/tests/test_tk_compat.py
index d121795..6750601 100644
--- a/bindings/python/tests/test_tk_compat.py
+++ b/bindings/python/tests/test_tk_compat.py
@@ -9,6 +9,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
"""
import os
+import subprocess
import sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
@@ -557,8 +558,37 @@ def test_hidden_root_is_not_default_root():
assert seen == "linked", "StringVar went to the hidden root: Entry shows %r" % seen
+_FIT_AT_SCALE = """
+import bui, buipy
+from bui import host
+from bui.tksyntax import Label, Listbox, Tk, Toplevel, simpledialog
+if host.native_available():
+ root = Tk()
+ Listbox(root, height=5).pack()
+ root.update()
+ top = Toplevel(root)
+ for _ in range(4): # larger than the dialog's minimum size
+ Label(top, text="wider than the minimum " * 5).pack()
+ simpledialog._place_window(top)
+ root.update()
+ for win in (root, top):
+ need, got = win._node.natural(), win._window.size
+ assert 0 <= got[0] - need[0] <= 1 and 0 <= got[1] - need[1] <= 1, (need, got)
+ assert buipy.kit_scale() == 2.0
+ root.destroy()
+"""
+
+
+def test_window_fits_content_at_ui_scale():
+ """HiDPI: a window fitting its content isn't scaled twice (natural() is in raster
+ pixels). In a child process: the UI scale is read when the first window opens."""
+ env = dict(os.environ, BUI_UI_SCALE="2", PYTHONPATH=os.pathsep.join(sys.path))
+ subprocess.run([sys.executable, "-c", _FIT_AT_SCALE], env=env, check=True, timeout=60)
+
+
def main() -> int:
test_hidden_root_is_not_default_root()
+ test_window_fits_content_at_ui_scale()
test_programs_match_stdlib()
test_install_runs_unchanged_scripts()
print("tkinter compatibility tests passed")