Skip to content

Commit

Permalink
Backport PR #22826: PR: Try to set the monospace font size up to six …
Browse files Browse the repository at this point in the history
…times in `SpyderApplication` (Utils)
  • Loading branch information
ccordoba12 authored and meeseeksmachine committed Nov 3, 2024
1 parent 558e55b commit 0ee1173
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions spyder/utils/qthelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,8 @@ def __init__(self, *args):
# This is filled at startup in spyder.app.utils.create_window
self._main_window: QMainWindow = None

# ---- Qt methods
# -------------------------------------------------------------------------
def event(self, event):

if sys.platform == 'darwin' and event.type() == QEvent.FileOpen:
Expand All @@ -803,6 +805,8 @@ def event(self, event):

return QApplication.event(self, event)

# ---- Public API
# -------------------------------------------------------------------------
def set_font(self):
"""Set font for the entire application."""
# This selects the system font by default
Expand All @@ -821,10 +825,24 @@ def set_font(self):
if size > 0:
app_font.setPointSize(size)

self.set_monospace_interface_font(app_font)
self._set_monospace_interface_font(app_font)
self.setFont(app_font)

def set_monospace_interface_font(self, app_font):
def get_mainwindow_position(self) -> QPoint:
"""Get main window position."""
return self._main_window.pos()

def get_mainwindow_width(self) -> int:
"""Get main window width."""
return self._main_window.width()

def get_mainwindow_height(self) -> int:
"""Get main window height."""
return self._main_window.height()

# ---- Private API
# -------------------------------------------------------------------------
def _set_monospace_interface_font(self, app_font):
"""
Set monospace interface font in our config system according to the app
one.
Expand All @@ -835,18 +853,25 @@ def set_monospace_interface_font(self, app_font):
plain_font.setPointSize(size)

# Select a size that matches the app font one, so that the UI looks
# consistent. We only check three point sizes above and below the app
# font to avoid getting stuck in an infinite loop.
# consistent.
attempts = 0
monospace_size = size
while (
# Keep going until the xHeight's of both fonts match
QFontMetrics(plain_font).xHeight() != x_height
# We only check three point sizes above and below the app font to
# avoid getting stuck in an infinite loop.
and ((size - 4) < monospace_size < (size + 4))
# Do this up to six times to not get stuck in an infinite loop.
# Fixes spyder-ide/spyder#22661
and attempts < 6
):
if QFontMetrics(plain_font).xHeight() > x_height:
monospace_size -= 1
else:
monospace_size += 1
plain_font.setPointSize(monospace_size)
attempts += 1

# There are some fonts (e.g. MS Serif) for which it seems that Qt
# can't detect their xHeight's as expected. So, we check below
Expand All @@ -861,18 +886,6 @@ def set_monospace_interface_font(self, app_font):
self.set_conf('monospace_app_font/size', monospace_size,
section='appearance')

def get_mainwindow_position(self) -> QPoint:
"""Get main window position."""
return self._main_window.pos()

def get_mainwindow_width(self) -> int:
"""Get main window width."""
return self._main_window.width()

def get_mainwindow_height(self) -> int:
"""Get main window height."""
return self._main_window.height()


def restore_launchservices():
"""Restore LaunchServices to the previous state"""
Expand Down

0 comments on commit 0ee1173

Please sign in to comment.