┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/disabledactionnotifier.cpp
diff options
context:
space:
mode:
authorJin Liu <[email protected]>2024-04-03 12:34:50 +0000
committerMéven Car <[email protected]>2024-04-03 12:34:50 +0000
commit7d49cb567b595570f59156965332428c9490a04d (patch)
treef2a9bf6334aa299cc50f30b2f1fc96811f7ceb1a /src/disabledactionnotifier.cpp
parente75bfea1cb9f6b4cab4334e29bb9d29bb09711ba (diff)
DolphinMainWindow: show a banner when the user presses the shortcut of a disabled action
Currently, there's no feedback when the user presses a shortcut of a disabled action, e.g. cut / paste in a read-only directory. This patch shows a banner in that case. It's implemented by enabling a QShortcut for each disabled action. the QShortcut is deleted when the action is enabled again. The following actions are included: cut paste rename moveToTrash deleteWithTrashShortcut deleted createDir copyToOtherView moveToOtherView
Diffstat (limited to 'src/disabledactionnotifier.cpp')
-rw-r--r--src/disabledactionnotifier.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/disabledactionnotifier.cpp b/src/disabledactionnotifier.cpp
new file mode 100644
index 000000000..844e66228
--- /dev/null
+++ b/src/disabledactionnotifier.cpp
@@ -0,0 +1,51 @@
+/*
+ * SPDX-FileCopyrightText: 2024 Jin Liu <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "disabledactionnotifier.h"
+
+DisabledActionNotifier::DisabledActionNotifier(QObject *parent)
+ : QObject(parent)
+{
+}
+
+void DisabledActionNotifier::setDisabledReason(QAction *action, QStringView reason)
+{
+ if (action->isEnabled()) {
+ return;
+ }
+
+ if (m_shortcuts.contains(action)) {
+ m_shortcuts.take(action)->deleteLater();
+ }
+
+ QShortcut *shortcut = new QShortcut(action->shortcut(), parent());
+ m_shortcuts.insert(action, shortcut);
+
+ connect(action, &QAction::enabledChanged, this, [this, action](bool enabled) {
+ if (enabled) {
+ m_shortcuts.take(action)->deleteLater();
+ }
+ });
+
+ // Don't capture QStringView, as it may reference a temporary QString
+ QString reasonString = reason.toString();
+ connect(shortcut, &QShortcut::activated, this, [this, action, reasonString]() {
+ Q_EMIT disabledActionTriggered(action, reasonString);
+ });
+}
+
+void DisabledActionNotifier::clearDisabledReason(QAction *action)
+{
+ if (action->isEnabled()) {
+ return;
+ }
+
+ if (m_shortcuts.contains(action)) {
+ m_shortcuts.take(action)->deleteLater();
+ }
+}
+
+#include "moc_disabledactionnotifier.cpp"