From f96c8c2ac899251f6b0ff2b49c85e889aa0219c3 Mon Sep 17 00:00:00 2001 From: Adnane Taghi Date: Thu, 15 May 2025 07:52:39 +0200 Subject: [PATCH] Make URL schemes dialog more ergonomic (#4002) (originally merged into a PR branch) * Make URL schemes dialog more ergonomic * add name to contributors list * Title Case * Tweak build instructions so Cursor picks them up * Use a warning icon for the URL scheme pop-up * Default to cancelling --- .cursor/rules/building.md | 2 ++ .cursor/rules/testing.md | 2 -- CONTRIBUTORS | 1 + ftl/core/preferences.ftl | 2 ++ qt/aqt/about.py | 1 + qt/aqt/profiles.py | 8 ++++++++ qt/aqt/url_schemes.py | 25 +++++++++++++++++++------ 7 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 .cursor/rules/building.md delete mode 100644 .cursor/rules/testing.md diff --git a/.cursor/rules/building.md b/.cursor/rules/building.md new file mode 100644 index 000000000..15326d9fa --- /dev/null +++ b/.cursor/rules/building.md @@ -0,0 +1,2 @@ +- To build and check the project, use ./check in the root folder (or check.bat on Windows) +- This will format files, then run lints and unit tests. diff --git a/.cursor/rules/testing.md b/.cursor/rules/testing.md deleted file mode 100644 index 47a530219..000000000 --- a/.cursor/rules/testing.md +++ /dev/null @@ -1,2 +0,0 @@ -- To build and check the project, use ./check(.bat) -- This will format files, then run lints and unit tests. diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 40bda393b..2297786ae 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -227,6 +227,7 @@ JL710 Matt Brubeck Yaoliang Chen KolbyML +Adnane Taghi ******************** diff --git a/ftl/core/preferences.ftl b/ftl/core/preferences.ftl index a0983dd6c..7c75904eb 100644 --- a/ftl/core/preferences.ftl +++ b/ftl/core/preferences.ftl @@ -89,6 +89,8 @@ preferences-url-scheme-prompt = Allowed { preferences-url-schemes } (space-separ preferences-url-scheme-warning = Blocked attempt to open `{ $link }`, which may be a security issue. If you trust the deck author and wish to proceed, you can add `{ $scheme }` to your allowed { preferences-url-schemes }. +preferences-url-scheme-allow-once = Allow Once +preferences-url-scheme-always-allow = Always Allow ## NO NEED TO TRANSLATE. This text is no longer used by Anki, and will be removed in the future. diff --git a/qt/aqt/about.py b/qt/aqt/about.py index d06b13ad5..bf34e4395 100644 --- a/qt/aqt/about.py +++ b/qt/aqt/about.py @@ -221,6 +221,7 @@ def show(mw: aqt.AnkiQt) -> QDialog: "Yuki", "🦙 (siid)", "Mukunda Madhav Dey", + "Adnane Taghi", ) ) diff --git a/qt/aqt/profiles.py b/qt/aqt/profiles.py index 0fd85ca8f..273e6df3a 100644 --- a/qt/aqt/profiles.py +++ b/qt/aqt/profiles.py @@ -750,3 +750,11 @@ create table if not exists profiles def set_allowed_url_schemes(self, schemes: list[str]) -> None: self.profile["allowedUrlSchemes"] = schemes + + def always_allow_scheme(self, scheme: str) -> None: + schemes = self.allowed_url_schemes() + + if scheme not in schemes: + schemes.append(scheme) + + self.set_allowed_url_schemes(schemes) diff --git a/qt/aqt/url_schemes.py b/qt/aqt/url_schemes.py index f5ee1110d..ea05ee319 100644 --- a/qt/aqt/url_schemes.py +++ b/qt/aqt/url_schemes.py @@ -5,8 +5,8 @@ from __future__ import annotations from markdown import markdown -from aqt.qt import Qt, QUrl -from aqt.utils import ask_user_dialog, getText, openLink, tr +from aqt.qt import QMessageBox, Qt, QUrl +from aqt.utils import MessageBox, getText, openLink, tr def show_url_schemes_dialog() -> None: @@ -32,6 +32,13 @@ def is_supported_scheme(url: QUrl) -> bool: return scheme in allowed_schemes or scheme in ["http", "https"] +def always_allow_scheme(url: QUrl) -> None: + from aqt import mw + + scheme = url.scheme().lower() + mw.pm.always_allow_scheme(scheme) + + def open_url_if_supported_scheme(url: QUrl) -> None: from aqt import mw @@ -41,18 +48,24 @@ def open_url_if_supported_scheme(url: QUrl) -> None: def on_button(idx: int) -> None: if idx == 0: - show_url_schemes_dialog() + openLink(url) + elif idx == 1: + always_allow_scheme(url) + openLink(url) msg = markdown( tr.preferences_url_scheme_warning(link=url.toString(), scheme=url.scheme()) ) - ask_user_dialog( + MessageBox( msg, buttons=[ - tr.actions_with_ellipsis(action=tr.preferences_url_schemes()), - tr.actions_close(), + tr.preferences_url_scheme_allow_once(), + tr.preferences_url_scheme_always_allow(), + (tr.actions_cancel(), QMessageBox.ButtonRole.RejectRole), ], parent=mw, callback=on_button, textFormat=Qt.TextFormat.RichText, + default_button=2, + icon=QMessageBox.Icon.Warning, )