From 88732a67d5d0b59cbdd741abc33b0a0b0bb4e4d8 Mon Sep 17 00:00:00 2001 From: goodboy Date: Wed, 28 Jan 2026 15:34:57 -0500 Subject: [PATCH] Add `get_fonts()` API and fix `.px_size` for non-Qt ctxs Add a public `.ui._style.get_fonts()` helper to retrieve the `_font[_small]: DpiAwareFont` singleton pair. Adjust `DpiAwareFont.px_size` to return `conf.toml` value when Qt returns `-1` (no active Qt app). Also, - raise `ValueError` with detailed msg if both Qt and a conf-lookup fail - add some more type union whitespace cleanups: `int | None` -> `int|None` (this commit-msg was generated in some part by [`claude-code`][claude-code-gh]) [claude-code-gh]: https://github.com/anthropics/claude-code --- piker/ui/_style.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/piker/ui/_style.py b/piker/ui/_style.py index b6c47817..893308b0 100644 --- a/piker/ui/_style.py +++ b/piker/ui/_style.py @@ -61,7 +61,7 @@ class DpiAwareFont: ) -> None: self._font_size_calc_key: str = _font_size_key - self._font_size: int | None = None + self._font_size: int|None = None # Read preferred font size from main config file if it exists conf, path = config.load('conf', touch_if_dne=True) @@ -107,7 +107,22 @@ class DpiAwareFont: @property def px_size(self) -> int: - return self._qfont.pixelSize() + size: int = self._qfont.pixelSize() + + # XXX, when no Qt app has been spawned this will always be + # invalid.. + # SO, just return any conf.toml value. + if size == -1: + if (conf_size := self._font_size) is None: + raise ValueError( + f'No valid `{type(_font).__name__}.px_size` set?\n' + f'\n' + f'-> `ui.font_size` is NOT set in `conf.toml`\n' + f'-> no Qt app is active ??\n' + ) + return conf_size + + return size def configure_to_dpi(self, screen: QtGui.QScreen | None = None): ''' @@ -221,6 +236,20 @@ def _config_fonts_to_screen() -> None: _font_small.configure_to_dpi() +def get_fonts() -> tuple[ + DpiAwareFont, + DpiAwareFont, +]: + ''' + Get the singleton font pair (of instances) from which all other + UI/UX should be "scaled around". + + See `DpiAwareFont` for (internal) deats. + + ''' + return _font, _font_small + + # TODO: re-compute font size when main widget switches screens? # https://forum.qt.io/topic/54136/how-do-i-get-the-qscreen-my-widget-is-on-qapplication-desktop-screen-returns-a-qwidget-and-qobject_cast-qscreen-returns-null/3