Skip to content

Commit

Permalink
Qt: Misc. refactor in __init__.py, ElectrumGui class
Browse files Browse the repository at this point in the history
Made the code slightly simpler by detecting macOS version in precisely
only one place. Also updated some comments to use the preferred """
syntax for function comments.
  • Loading branch information
cculianu committed Jan 7, 2021
1 parent 1492b1d commit 654bcdf
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions electroncash_gui/qt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,23 @@ def my_do_in_main_thread_handler(func, *args, **kwargs):
Handlers.do_in_main_thread = my_do_in_main_thread_handler

def _do_in_main_thread_handler_slot(self, func, args, kwargs):
''' Hooked in to util.Handlers.do_in_main_thread via the
"""
Hooked in to util.Handlers.do_in_main_thread via the
do_in_main_thread_signal. This ensures that there is an app-wide
mechanism for posting invocations to the main thread. Currently
CashFusion uses this mechanism, but other code may as well. '''
CashFusion uses this mechanism, but other code may as well.
"""
func(*args, **kwargs)

def _pre_and_post_app_setup(self):
''' Call this before instantiating the QApplication object. It sets up
"""
Call this before instantiating the QApplication object. It sets up
some platform-specific miscellany that need to happen before the
QApplication is constructed.
A function is returned. This function *must* be called after the
QApplication is constructed. '''
QApplication is constructed.
"""
callables = []
def call_callables():
for func in callables:
Expand Down Expand Up @@ -240,51 +244,41 @@ def call_callables():
if hasattr(Qt, "AA_UseHighDpiPixmaps"):
QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)

# Set mac_ver to the appropriate macOS version e.g.: (10,15,1) for Catalina, etc or None if not macOS
mac_ver = None
if sys.platform in ('darwin',):
try:
mac_ver = tuple(int(a) for a in platform.mac_ver()[0].split('.'))
self.print_error("macOS version:", mac_ver)
except (TypeError, ValueError) as e:
self.print_error("WARNING: Cannot parse platform.mac_ver:", repr(e))

# macOS Mojave "font rendering looks terrible on PyQt5.11" workaround.
# See: https://old.reddit.com/r/apple/comments/9leavs/fix_mojave_font_rendering_issues_on_a_perapp_basis/
# This affects PyQt 5.11 (which is what we ship in the macOS El Capitan
# .dmg). We apply the workaround and also warn the user to not use
# the El Capitan compatibility .dmg.
if sys.platform in ('darwin',) and self.qt_version() < (5, 12):
if mac_ver and mac_ver >= (10, 14) and self.qt_version() < (5, 12):
# macOS hacks. On Mojave with PyQt <5.12 the font rendering is terrible.
# As a workaround we need to temporarily set this 'defaults' keys
# which we immediately disable after the QApplication is started.
try:
ver = tuple(int(a) for a in platform.mac_ver()[0].split('.'))
except (TypeError, ValueError):
self.print_error("WARNING: Cannot parse platform.mac_ver", f"'{platform.mac_ver()[0]}'")
ver = None
if ver and ver >= (10, 14):
from electroncash.utils import macos
self.print_error("Mojave+ with PyQt<5.12 detected; applying CGFontRenderingFontSmoothingDisabled workaround...")
bundle = macos.get_bundle_identifier()
os.system(f'defaults write {bundle} CGFontRenderingFontSmoothingDisabled -bool NO')
def undo_hack():
os.system(f'defaults delete {bundle} CGFontRenderingFontSmoothingDisabled')
self.print_error("Mojave+ font rendering workaround applied.")
#msg = _("Mojave or newer system detected, however you are running the "
# "El Capitan compatibility release of Electron Cash. "
# "Font and graphics rendering may be affected."
# "\n\nPlease obtain the latest non-compatibility version "
# "of Electron Cash for MacOS.")
#QMessageBox.warning(None, _("Warning"), msg) # this works even if app is not exec_() yet.

callables.append(undo_hack)
from electroncash.utils import macos
self.print_error("Mojave+ with PyQt<5.12 detected; applying CGFontRenderingFontSmoothingDisabled workaround...")
bundle = macos.get_bundle_identifier()
os.system(f'defaults write {bundle} CGFontRenderingFontSmoothingDisabled -bool NO')
def undo_hack():
os.system(f'defaults delete {bundle} CGFontRenderingFontSmoothingDisabled')
self.print_error("Mojave+ font rendering workaround applied.")

callables.append(undo_hack)

# macOS Big Sur workaround for Qt>=5.13.2 sometimes not working at all
# See: https://bugreports.qt.io/browse/QTBUG-87014
# See also: https://stackoverflow.com/questions/64818879/is-there-any-solution-regarding-to-pyqt-library-doesnt-work-in-mac-os-big-sur
if sys.platform in ('darwin', ):
try:
release_tuple = version.normalize_version(platform.uname().release)
self.print_error("Darwin kernel version:", '.'.join([str(x) for x in release_tuple]))
except Exception as e:
release_tuple = (0, 0, 0)
self.print_error("Error parsing Darwin kernel version:", repr(e))
if release_tuple >= (20, 0, 0) and self.qt_version() > (5, 13, 1):
# Setting this env var causes Qt to use some other macOS API that always works on Big Sur
os.environ['QT_MAC_WANTS_LAYER'] = '1'
self.print_error(f"macOS Big Sur Qt workaround applied")
if mac_ver and mac_ver >= (10, 16) and self.qt_version() >= (5, 13, 2):
# Setting this env var causes Qt to use some other macOS API that always works on Big Sur
os.environ['QT_MAC_WANTS_LAYER'] = '1'
self.print_error(f"macOS Big Sur Qt workaround applied")

def setup_layout_direction():
"""Sets the app layout direction depending on language. To be called
Expand Down

0 comments on commit 654bcdf

Please sign in to comment.