From a32ffb75f2cfbd08323377d7b54c1f9d41b32249 Mon Sep 17 00:00:00 2001 From: goodboy Date: Wed, 11 Mar 2026 19:07:38 -0400 Subject: [PATCH] Improve styling and logging for UI font-size zoom Refine zoom methods in `MainWindow` and font helpers in `_style` to return `px_size` up the call chain and log detailed zoom state on each change. Deats, - make `_set_qfont_px_size()` return `self.px_size`. - make `configure_to_dpi()` and `_config_fonts_to_screen()` return the new `px_size` up through the call chain. - add `font_size` to `log.info()` in `zoom_in()`, `zoom_out()`, and `reset_zoom()` alongside `zoom_step` and `zoom_level(%)`. - reformat `has_ctrl`/`_has_shift` bitwise checks and key-match tuples to multiline style. - comment out `Shift` modifier requirement for zoom hotkeys (now `Ctrl`-only). - comment out unused `mn_dpi` and `dpi` locals. Also, - convert all single-line docstrings to `'''` multiline style across zoom and font methods. - rewrap `configure_to_dpi()` docstring to 67 chars. - move `from . import _style` to module-level import in `_window.py`. - drop unused `screen` binding in `boundingRect()`. (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 | 41 ++++++++++----- piker/ui/_window.py | 118 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 120 insertions(+), 39 deletions(-) diff --git a/piker/ui/_style.py b/piker/ui/_style.py index f310dc34..e1da8b7d 100644 --- a/piker/ui/_style.py +++ b/piker/ui/_style.py @@ -79,9 +79,13 @@ class DpiAwareFont: self._font_inches: float = None self._screen = None - def _set_qfont_px_size(self, px_size: int) -> None: + def _set_qfont_px_size( + self, + px_size: int, + ) -> int: self._qfont.setPixelSize(int(px_size)) self._qfm = QtGui.QFontMetrics(self._qfont) + return self.px_size @property def screen(self) -> QtGui.QScreen: @@ -128,17 +132,18 @@ class DpiAwareFont: self, screen: QtGui.QScreen | None = None, zoom_level: float = 1.0, - ): + ) -> int: ''' - Set an appropriately sized font size depending on the screen DPI. + Set an appropriately sized font size depending on the screen DPI + or scale the size according to `zoom_level`. - If we end up needing to generalize this more here there are resources - listed in the script in ``snippets/qt_screen_info.py``. + If we end up needing to generalize this more here there are + resources listed in the script in + ``snippets/qt_screen_info.py``. ''' if self._font_size is not None: - self._set_qfont_px_size(self._font_size * zoom_level) - return + return self._set_qfont_px_size(self._font_size * zoom_level) # NOTE: if no font size set either in the [ui] section of the # config or not yet computed from our magic scaling calcs, @@ -157,7 +162,7 @@ class DpiAwareFont: ldpi = pdpi mx_dpi = max(pdpi, ldpi) - mn_dpi = min(pdpi, ldpi) + # mn_dpi = min(pdpi, ldpi) scale = round(ldpi/pdpi, ndigits=2) if mx_dpi <= 97: # for low dpi use larger font sizes @@ -166,7 +171,7 @@ class DpiAwareFont: else: # hidpi use smaller font sizes inches = _font_sizes['hi'][self._font_size_calc_key] - dpi = mn_dpi + # dpi = mn_dpi mult = 1.0 @@ -213,10 +218,10 @@ class DpiAwareFont: f"\nOur best guess font size is {font_size}\n" ) # apply the size - self._set_qfont_px_size(font_size) + return self._set_qfont_px_size(font_size) def boundingRect(self, value: str) -> QtCore.QRectF: - if (screen := self.screen) is None: + if self.screen is None: raise RuntimeError("You must call .configure_to_dpi() first!") unscaled_br: QtCore.QRectF = self._qfm.boundingRect(value) @@ -233,12 +238,22 @@ _font = DpiAwareFont() _font_small = DpiAwareFont(_font_size_key='small') -def _config_fonts_to_screen(zoom_level: float = 1.0) -> None: - 'configure global DPI aware font sizes' +def _config_fonts_to_screen( + zoom_level: float = 1.0 +) -> int: + ''' + Configure global DPI aware font size(s). + If `zoom_level` is provided we apply it to auto-calculated + DPI-aware font. + + Return the new `DpiAwareFont.px_size`. + + ''' global _font, _font_small _font.configure_to_dpi(zoom_level=zoom_level) _font_small.configure_to_dpi(zoom_level=zoom_level) + return _font.px_size def get_fonts() -> tuple[ diff --git a/piker/ui/_window.py b/piker/ui/_window.py index 20e1f5ff..865b65c9 100644 --- a/piker/ui/_window.py +++ b/piker/ui/_window.py @@ -43,7 +43,11 @@ from piker.ui.qt import ( QObject, ) from ..log import get_logger -from ._style import _font_small, hcolor +from . import _style +from ._style import ( + _font_small, + hcolor, +) from ._widget import GodWidget @@ -73,6 +77,7 @@ class GlobalZoomEventFilter(QObject): Filter keyboard events for global zoom shortcuts. Returns True to filter out (consume) the event, False to pass through. + ''' if event.type() == QEvent.Type.KeyPress: key = event.key() @@ -82,28 +87,49 @@ class GlobalZoomEventFilter(QObject): mods = mods & ~Qt.KeyboardModifier.KeypadModifier # Check if we have Ctrl+Shift (both required) - has_ctrl = bool(mods & Qt.KeyboardModifier.ControlModifier) - has_shift = bool(mods & Qt.KeyboardModifier.ShiftModifier) + has_ctrl = bool( + mods + & + Qt.KeyboardModifier.ControlModifier + ) + _has_shift = bool( + mods + & + Qt.KeyboardModifier.ShiftModifier + ) # Only handle UI zoom if BOTH Ctrl and Shift are pressed # For Plus key: user presses Cmd+Shift+Equal (which makes Plus) # For Minus key: user presses Cmd+Shift+Minus - if has_ctrl and has_shift: + if ( + has_ctrl + # and + # has_shift + ): # Zoom in: Ctrl+Shift+Plus # Note: Plus key usually comes as Key_Equal with Shift modifier - if key in (Qt.Key.Key_Plus, Qt.Key.Key_Equal): + if key in ( + Qt.Key.Key_Plus, + Qt.Key.Key_Equal, + ): self.main_window.zoom_in() return True # consume event # Zoom out: Ctrl+Shift+Minus # Note: On some keyboards Shift+Minus produces '_' (Underscore) - elif key in (Qt.Key.Key_Minus, Qt.Key.Key_Underscore): + elif key in ( + Qt.Key.Key_Minus, + Qt.Key.Key_Underscore, + ): self.main_window.zoom_out() return True # consume event # Reset zoom: Ctrl+Shift+0 # Note: On some keyboards Shift+0 produces ')' (ParenRight) - elif key in (Qt.Key.Key_0, Qt.Key.Key_ParenRight): + elif key in ( + Qt.Key.Key_0, + Qt.Key.Key_ParenRight, + ): self.main_window.reset_zoom() return True # consume event @@ -444,34 +470,69 @@ class MainWindow(QMainWindow): event.accept() def zoom_in(self) -> None: - '''Increase UI zoom level.''' - new_zoom = min(self._zoom_level + self._zoom_step, self._max_zoom) - if new_zoom != self._zoom_level: - self._zoom_level = new_zoom - self._apply_zoom() - log.info(f'Zoomed in to {self._zoom_level:.1%}') + ''' + Increase overall UI-widgets zoom level by scaling it the + global font sizes. - def zoom_out(self) -> None: - '''Decrease UI zoom level.''' - new_zoom = max(self._zoom_level - self._zoom_step, self._min_zoom) + ''' + new_zoom: float = min( + self._zoom_level + self._zoom_step, + self._max_zoom, + ) if new_zoom != self._zoom_level: self._zoom_level = new_zoom - self._apply_zoom() - log.info(f'Zoomed out to {self._zoom_level:.1%}') + font_size: int = self._apply_zoom() + log.info( + f'Zoomed in UI\n' + f'zoom_step: {self._zoom_step!r}\n' + f'zoom_level(%): {self._zoom_level:.1%}\n' + f'font_size: {font_size!r}' + ) + + def zoom_out(self) -> float: + ''' + Decrease UI zoom level. + + ''' + new_zoom: float = max(self._zoom_level - self._zoom_step, self._min_zoom) + if new_zoom != self._zoom_level: + self._zoom_level = new_zoom + font_size: int = self._apply_zoom() + log.info( + f'Zoomed out UI\n' + f'zoom_step: {self._zoom_step!r}\n' + f'zoom_level(%): {self._zoom_level:.1%}\n' + f'font_size: {font_size!r}' + ) + + return new_zoom def reset_zoom(self) -> None: - '''Reset UI zoom to 100%.''' + ''' + Reset UI zoom to 100%. + + ''' if self._zoom_level != 1.0: self._zoom_level = 1.0 - self._apply_zoom() - log.info('Reset zoom to 100%') + font_size: int = self._apply_zoom() + log.info( + f'Reset zoom level\n' + f'zoom_step: {self._zoom_step!r}\n' + f'zoom_level(%): {self._zoom_level:.1%}\n' + f'font_size: {font_size!r}' + ) - def _apply_zoom(self) -> None: - '''Apply current zoom level to all UI elements.''' - from . import _style + return self._zoom_level + def _apply_zoom(self) -> int: + ''' + Apply current zoom level to all UI elements. + + ''' # reconfigure fonts with zoom multiplier - _style._config_fonts_to_screen(zoom_level=self._zoom_level) + font_size: int = _style._config_fonts_to_screen( + zoom_level=self._zoom_level + ) # update status bar styling with new font size if self._status_bar: @@ -500,8 +561,13 @@ class MainWindow(QMainWindow): self._refresh_widget_fonts(self.godwidget) self.godwidget.update() + return font_size + def _update_chart_order_panes(self) -> None: - '''Update order entry panels in all charts.''' + ''' + Update order entry panels in all charts. + + ''' if not self.godwidget: return