Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get font style from main config #523

Merged
merged 3 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ebisu4 tellin me there's issues getting this to work on windows @sirdinkus, so if you could test it a bit it'd be handy 😉


# colorscheme = 'default' # UNUSED
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are just "wishlist" stuff rn since nothing implemented but figured i might as well toss em in for a keen piker to take a swing at 😉

# graphics.update_throttle = 60 # Hz # TODO
2 changes: 1 addition & 1 deletion piker/ui/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion piker/ui/_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 29 additions & 7 deletions piker/ui/_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

from ..log import get_logger

from .. import config

log = get_logger(__name__)

_magic_inches = 0.0666 * (1 + 6/16)
Expand All @@ -49,15 +51,27 @@ class DpiAwareFont:

def __init__(
self,
# TODO: move to config
name: str = 'Hack',
font_size: str = 'default',
_font_size_key: str = 'default',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main init change was this, font_size is not really what it was 😂 ; more of a name to map to a font size calculator..

anyway, changed all dep code as well so should be fine to land without regression.


) -> None:

self._font_size_calc_key: str = _font_size_key
self._font_size: int | None = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this now gets cached (after first calc) if not defined in the user config conf.toml

TODO: ideally in the future we can actually build an entire "dynamic resizing system" + controls so that a user can do the classic ctl and +/- to change the font sizing (and thus all related graphics) interactively from the GUI. Not sure yet how difficult that's going to be to implement but in theory not that bad since pretty much all widgets have resize handlers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm maybe i'll make a follow up task-issue for this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, another thing was checking out that https://github.com/samuelcolvin/watchfiles lib to see if we could also get a "changes-in-conf.toml-triggers-live-update" type thing (like alacritty) workin 😎

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioned in #221 since we're more or less officially exposing a top level config now.


# Read preferred font size from main config file if it exists
conf, path = config.load('conf', touch_if_dne=True)
if ui_section := conf.get('ui'):
if font_size := ui_section.get('font_size'):
self._font_size = int(font_size)

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

Expand Down Expand Up @@ -99,6 +113,14 @@ def configure_to_dpi(self, screen: QtGui.QScreen | None = None):
listed in the script in ``snippets/qt_screen_info.py``.

'''
if self._font_size is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm maybe it's handy to eventually add a .reset() meth as well in case the window gets moved to another display and we want to re-compute the dynamic-by-DPI font size?

(will stick in follow up issue as per above comments)

self._set_qfont_px_size(self._font_size)
return

# NOTE: if no font size set either in the [ui] section of the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe document this impl detail: the dynamic size calculating by DPI, in the class doc string?

# config or not yet computed from our magic scaling calcs,
# then attempt to caculate it here!

if screen is None:
screen = self.screen

Expand All @@ -116,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

Expand All @@ -128,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:
Expand Down Expand Up @@ -184,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:
Expand Down