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
This commit is contained in:
llama 2025-01-13 11:24:21 +08:00 committed by GitHub
parent e98a597b4d
commit 38821372dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View file

@ -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)};
}}
"""

View file

@ -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()