From 38821372ddaf68a314ab75dda2e706a0ed13d338 Mon Sep 17 00:00:00 2001 From: llama <100429699+iamllama@users.noreply.github.com> Date: Mon, 13 Jan 2025 11:24:21 +0800 Subject: [PATCH] Use platform-native button layout in dialogs and messageboxes (#3725) * set button-layout prop in stylesheet * fix lint * check for and use non-default layout on linux before falling back --- qt/aqt/stylesheets.py | 18 +++++++++++++++++- qt/aqt/theme.py | 5 +++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/qt/aqt/stylesheets.py b/qt/aqt/stylesheets.py index 265f03ddd..35e47ef0d 100644 --- a/qt/aqt/stylesheets.py +++ b/qt/aqt/stylesheets.py @@ -1,7 +1,7 @@ # Copyright: Ankitects Pty Ltd and contributors # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -from anki.utils import is_win +from anki.utils import is_mac, is_win from aqt import colors, props from aqt.theme import ThemeManager @@ -28,6 +28,19 @@ qlineargradient( """ +def button_layout(tm: ThemeManager): + # https://doc.qt.io/qt-6/stylesheet-reference.html#button-layout + if is_win: + return 0 + elif is_mac: + return 1 + # on linux, use non-default layout if available + if tm._default_button_layout: + return tm._default_button_layout + # fallback to GnomeLayout + return 3 + + class CustomStyles: def general(self, tm: ThemeManager) -> str: return f""" @@ -201,6 +214,9 @@ class CustomStyles: }} QPushButton:flat {{ border: none; + }} + QDialogButtonBox {{ + button-layout: {button_layout(tm)}; }} """ diff --git a/qt/aqt/theme.py b/qt/aqt/theme.py index c43cf8f86..e06cf71c2 100644 --- a/qt/aqt/theme.py +++ b/qt/aqt/theme.py @@ -21,6 +21,7 @@ from aqt.qt import ( QPainter, QPalette, QPixmap, + QStyle, QStyleFactory, Qt, qtmajor, @@ -62,6 +63,7 @@ class ThemeManager: _dark_mode_available: bool | None = None _default_style: str | None = None _current_widget_style: WidgetStyle | None = None + _default_button_layout: int | None = None def rtl(self) -> bool: return is_rtl(anki.lang.current_lang) @@ -234,6 +236,9 @@ class ThemeManager: style = app.style() assert style is not None self._default_style = style.objectName() + self._default_button_layout = style.styleHint( + QStyle.StyleHint.SH_DialogButtonLayout + ) self._apply_palette(app) self._apply_style(app) gui_hooks.theme_did_change()