Skip to content

Commit 19d47c3

Browse files
committed
improve global shortcuts configuration
1 parent 24193f9 commit 19d47c3

6 files changed

+46
-11
lines changed

desktop/qml/SettingsAccessebilityPage.qml

+11-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ ColumnLayout {
4444
}
4545

4646
Button {
47-
visible: _settings.hotkeys_enabled && app.feature_hotkeys
47+
id: gsConfButton
48+
49+
visible: _settings.hotkeys_enabled && app.feature_hotkeys && (_settings.hotkeys_type === Settings.HotkeysTypeX11 || _settings.is_kde())
4850
text: qsTranslate("SettingsPage", "Configure global keyboard shortcuts")
4951
Layout.leftMargin: appWin.padding
5052
onClicked: {
@@ -59,6 +61,14 @@ ColumnLayout {
5961
}
6062
}
6163

64+
TipMessage {
65+
color: palette.text
66+
indends: 1
67+
visible: _settings.hotkeys_enabled && app.feature_hotkeys && !gsConfButton.visible
68+
text: qsTranslate("SettingsPage", "Global keyboard shortcuts are managed through the %1.").arg("<i>XDG Desktop Portal</i>") + " " +
69+
qsTranslate("SettingsPage", "Use your desktop environment configuration tool to change key bindings.")
70+
}
71+
6272
Component {
6373
id: hotkeysEditDialog
6474

src/dsnote_app.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -5713,6 +5713,6 @@ QString dsnote_app::stt_auto_lang_name() const {
57135713

57145714
void dsnote_app::open_hotkeys_editor() {
57155715
#ifdef USE_DESKTOP
5716-
m_gs_manager.set_portal_bindings();
5716+
m_gs_manager.reset_portal_connection();
57175717
#endif
57185718
}

src/global_hotkeys_manager.cpp

+24-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "global_hotkeys_manager.hpp"
99

1010
#include <QDebug>
11+
#include <QRandomGenerator>
1112

1213
#include "config.h"
1314
#include "dbus_portal_request_inf.h"
@@ -103,12 +104,16 @@ void global_hotkeys_manager::handle_portal_activated(
103104
emit hotkey_activated(action_id, {});
104105
}
105106

106-
void global_hotkeys_manager::create_portal_session() {
107+
void global_hotkeys_manager::create_portal_session(bool force_bind) {
107108
LOGD("[dbus] call CreateSession");
108109

110+
auto handle_token = QStringLiteral(APP_ID "_%1")
111+
.arg(QRandomGenerator::global()->generate());
112+
m_force_bind = force_bind;
113+
109114
auto reply = m_portal_inf.CreateSession({
110-
{QLatin1String("session_handle_token"), APP_ID},
111-
{QLatin1String("handle_token"), APP_ID},
115+
{QLatin1String("session_handle_token"), handle_token},
116+
{QLatin1String("handle_token"), handle_token},
112117
});
113118

114119
reply.waitForFinished();
@@ -152,7 +157,7 @@ void global_hotkeys_manager::fetch_portal_shortcuts() {
152157
return;
153158
}
154159

155-
auto req = new OrgFreedesktopPortalRequestInterface(
160+
auto *req = new OrgFreedesktopPortalRequestInterface(
156161
DBUS_SERVICE_NAME, reply.value().path(), QDBusConnection::sessionBus(),
157162
this);
158163

@@ -178,7 +183,6 @@ void global_hotkeys_manager::handle_list_shortcuts_response(
178183
const auto arg = results["shortcuts"].value<QDBusArgument>();
179184
arg >> s;
180185

181-
QString desc;
182186
for (auto it = s.cbegin(), it_end = s.cend(); it != it_end; ++it) {
183187
LOGD("portal hotkey: " << it->first << " "
184188
<< it->second["description"].toString() << " "
@@ -187,6 +191,7 @@ void global_hotkeys_manager::handle_list_shortcuts_response(
187191

188192
if (s.isEmpty()) {
189193
set_portal_bindings();
194+
m_force_bind = false;
190195
return;
191196
}
192197

@@ -204,11 +209,13 @@ void global_hotkeys_manager::handle_list_shortcuts_response(
204209
return true;
205210
}();
206211

207-
if (all_shortcuts_configured) {
212+
if (all_shortcuts_configured && !m_force_bind) {
208213
LOGD("portal global shortcuts already configured");
209214
} else {
210215
set_portal_bindings();
211216
}
217+
218+
m_force_bind = false;
212219
}
213220

214221
void global_hotkeys_manager::handle_bind_shortcuts_response(
@@ -329,4 +336,15 @@ void global_hotkeys_manager::handle_x11_activated() {
329336
emit hotkey_activated(action_id, {});
330337
}
331338

339+
void global_hotkeys_manager::reset_portal_connection() {
340+
if (!is_portal_supported()) {
341+
LOGW("portal not supported");
342+
return;
343+
}
344+
345+
disable_portal();
346+
347+
create_portal_session(/*force_bind=*/true);
348+
}
349+
332350
#endif // USE_X11_FEATURES

src/global_hotkeys_manager.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class global_hotkeys_manager : public QObject {
3131
bool is_x11_supported() const;
3232
bool is_portal_supported() const;
3333
void set_portal_bindings();
34+
void reset_portal_connection();
3435

3536
Q_SIGNALS:
3637
void hotkey_activated(const QString& action_id, const QString& extra);
@@ -54,13 +55,14 @@ class global_hotkeys_manager : public QObject {
5455
#undef X
5556
};
5657
x11_hotkeys_t m_x11_hotkeys;
58+
bool m_force_bind = false;
5759

5860
void enable_x11();
5961
void disable_x11();
6062
void handle_x11_activated();
6163
#endif
6264

63-
void create_portal_session();
65+
void create_portal_session(bool force_bind = false);
6466
void fetch_portal_shortcuts();
6567
QString get_portal_request_token();
6668
void enable_or_disable();

src/settings.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ QStringList settings::qt_styles() const {
13211321
bool settings::is_native_style() const { return m_native_style; }
13221322

13231323
#ifdef USE_DESKTOP
1324-
static bool use_default_qt_style() {
1324+
bool settings::use_default_qt_style() {
13251325
const auto* desk_name_str = getenv("XDG_CURRENT_DESKTOP");
13261326
if (!desk_name_str) {
13271327
LOGD("no XDG_CURRENT_DESKTOP");
@@ -1332,7 +1332,9 @@ static bool use_default_qt_style() {
13321332

13331333
QString desk_name{desk_name_str};
13341334

1335-
return desk_name.contains("KDE") || desk_name.contains("XFCE");
1335+
m_kde = desk_name.contains("KDE");
1336+
1337+
return m_kde || desk_name.contains("XFCE");
13361338
}
13371339

13381340
void settings::update_qt_style(QQmlApplicationEngine* engine) {

src/settings.h

+3
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ class settings : public QSettings, public singleton<settings> {
669669
void disable_py_scan();
670670
#ifdef USE_DESKTOP
671671
void update_qt_style(QQmlApplicationEngine *engine);
672+
bool use_default_qt_style();
672673
#endif
673674
// app
674675
#define X(name, _) \
@@ -848,6 +849,7 @@ class settings : public QSettings, public singleton<settings> {
848849
Q_INVOKABLE bool is_wayland() const;
849850
Q_INVOKABLE bool is_xcb() const;
850851
Q_INVOKABLE bool is_flatpak() const;
852+
Q_INVOKABLE bool is_kde() const { return m_kde; }
851853
Q_INVOKABLE QStringList qt_styles() const;
852854
Q_INVOKABLE bool file_exists(const QString &file_path) const;
853855
Q_INVOKABLE QString
@@ -1095,6 +1097,7 @@ class settings : public QSettings, public singleton<settings> {
10951097
unsigned int m_error_flags = error_flags_t::ErrorNoError;
10961098
unsigned int m_system_flags = system_flags_t::SystemNone;
10971099
bool m_native_style = false;
1100+
bool m_kde = false;
10981101

10991102
static QString settings_filepath();
11001103
void set_restart_required(bool value);

0 commit comments

Comments
 (0)