From d660376206977f5a578d66f1d5ac588c04f9b054 Mon Sep 17 00:00:00 2001 From: Ebisu Date: Mon, 19 Jun 2023 00:10:37 +0200 Subject: [PATCH 1/2] get font style from main config --- piker/ui/_style.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/piker/ui/_style.py b/piker/ui/_style.py index 67f14a93d..8c683ad54 100644 --- a/piker/ui/_style.py +++ b/piker/ui/_style.py @@ -28,6 +28,8 @@ from ..log import get_logger +from .. import config + log = get_logger(__name__) _magic_inches = 0.0666 * (1 + 6/16) @@ -49,11 +51,25 @@ class DpiAwareFont: def __init__( self, - # TODO: move to config name: str = 'Hack', font_size: str = 'default', ) -> None: + self._custom_ui = False + # Read preferred font size from main config file if it exists + conf, path = config.load('conf', touch_if_dne=True) + ui_section = conf.get('ui') + if ui_section: + self._custom_ui = True + + font_size = ui_section.get('font_size') + if not font_size: + font_size = 'default' + + name = ui_section.get('name') + if not name: + name = 'Hack' + self.name = name self._qfont = QtGui.QFont(name) self._font_size: str = font_size @@ -99,6 +115,10 @@ def configure_to_dpi(self, screen: QtGui.QScreen | None = None): listed in the script in ``snippets/qt_screen_info.py``. ''' + if self._custom_ui: + self._set_qfont_px_size(self._font_size) + return + if screen is None: screen = self.screen From 4b77de5e2dab110e178e271e785c30a9e0d6def9 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 19 Jun 2023 15:13:01 -0400 Subject: [PATCH 2/2] Fix reading font size from user config Was borked on linux if you didn't provide the setting in `conf.toml` due to some logic errors. Fix that by rejigging `DpiAwareFont` internal variables: - add new `._font_size_calc_key: str` which was the old `._font_size` and is only used when no explicit font size is set by the user in the `conf.toml` config: - this is the "key" that is used to lookup a calculation function which attempts to compute a best fit font size given the measured system displays DPI settings and dimensions. - make the `._font_size: int` the **actual** font size integer that is cached and passed to `Qt` to set the size. - this is overridden by user config now if defined. - change the input kwarg `font_size: str` to the constructor to better change the input kwarg `font_size: str` to the constructor to better named private `_font_size_key: str` which gets set to the new `._font_size_calc_key`. Also, adjust all client code which instantiates `DpiAwareFont` to use the new `_font_size_key` kwarg input so nothing breaks XD --- config/conf.toml | 8 ++++++++ piker/ui/_axes.py | 2 +- piker/ui/_label.py | 2 +- piker/ui/_style.py | 38 ++++++++++++++++++++------------------ 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/config/conf.toml b/config/conf.toml index 6dde7ee6b..a14f6bddc 100644 --- a/config/conf.toml +++ b/config/conf.toml @@ -2,3 +2,11 @@ tsdb.backend = 'marketstore' tsdb.host = 'localhost' tsdb.grpc_port = 5995 + +[ui] +# set custom font + size which will scale entire UI +# font_size = 16 +# font_name = 'Monospaced' + +# colorscheme = 'default' # UNUSED +# graphics.update_throttle = 60 # Hz # TODO diff --git a/piker/ui/_axes.py b/piker/ui/_axes.py index 040d05527..470df3f5e 100644 --- a/piker/ui/_axes.py +++ b/piker/ui/_axes.py @@ -418,7 +418,7 @@ def __init__( self._txt_br: QtCore.QRect = None - self._dpifont = DpiAwareFont(font_size=font_size) + self._dpifont = DpiAwareFont(_font_size_key=font_size) self._dpifont.configure_to_dpi() self.bg_color = pg.mkColor(hcolor(bg_color)) diff --git a/piker/ui/_label.py b/piker/ui/_label.py index 85fbbb8a7..1e010f18b 100644 --- a/piker/ui/_label.py +++ b/piker/ui/_label.py @@ -84,7 +84,7 @@ def __init__( # configure font size based on DPI dpi_font = DpiAwareFont( - font_size=font_size, + _font_size_key=font_size, ) dpi_font.configure_to_dpi() txt.setFont(dpi_font.font) diff --git a/piker/ui/_style.py b/piker/ui/_style.py index 8c683ad54..2d17b62d1 100644 --- a/piker/ui/_style.py +++ b/piker/ui/_style.py @@ -52,28 +52,26 @@ class DpiAwareFont: def __init__( self, name: str = 'Hack', - font_size: str = 'default', + _font_size_key: str = 'default', ) -> None: - self._custom_ui = False + + self._font_size_calc_key: str = _font_size_key + 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) - ui_section = conf.get('ui') - if ui_section: - self._custom_ui = True - - font_size = ui_section.get('font_size') - if not font_size: - font_size = 'default' + if ui_section := conf.get('ui'): + if font_size := ui_section.get('font_size'): + self._font_size = int(font_size) - name = ui_section.get('name') - if not name: - name = 'Hack' + if not (name := ui_section.get('font_name')): + name: str = 'Hack' self.name = name self._qfont = QtGui.QFont(name) - self._font_size: str = font_size self._qfm = QtGui.QFontMetrics(self._qfont) + self._font_inches: float = None self._screen = None @@ -115,10 +113,14 @@ def configure_to_dpi(self, screen: QtGui.QScreen | None = None): listed in the script in ``snippets/qt_screen_info.py``. ''' - if self._custom_ui: + if self._font_size is not None: self._set_qfont_px_size(self._font_size) return + # NOTE: if no font size set either in the [ui] section of the + # config or not yet computed from our magic scaling calcs, + # then attempt to caculate it here! + if screen is None: screen = self.screen @@ -136,10 +138,10 @@ def configure_to_dpi(self, screen: QtGui.QScreen | None = None): scale = round(ldpi/pdpi, ndigits=2) if mx_dpi <= 97: # for low dpi use larger font sizes - inches = _font_sizes['lo'][self._font_size] + inches = _font_sizes['lo'][self._font_size_calc_key] else: # hidpi use smaller font sizes - inches = _font_sizes['hi'][self._font_size] + inches = _font_sizes['hi'][self._font_size_calc_key] dpi = mn_dpi @@ -148,7 +150,7 @@ def configure_to_dpi(self, screen: QtGui.QScreen | None = None): # No implicit DPI scaling was done by the DE so let's engage # some hackery ad-hoc scaling shiat. # dpi is likely somewhat scaled down so use slightly larger font size - if scale >= 1.1 and self._font_size: + if scale >= 1.1 and self._font_size_calc_key: # no idea why if 1.2 <= scale: @@ -204,7 +206,7 @@ def boundingRect(self, value: str) -> QtCore.QRectF: # use inches size to be cross-resolution compatible? _font = DpiAwareFont() -_font_small = DpiAwareFont(font_size='small') +_font_small = DpiAwareFont(_font_size_key='small') def _config_fonts_to_screen() -> None: