From 99b84a321acea3e91ec973542e7bea45bafc7a5f Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Sun, 23 May 2021 21:16:19 +0200 Subject: [PATCH] Don't show virtual keyboard SNI when no virtual keyboard is selected in the KCM Not choosing any virtual keyboard is a valid option. In this case do not show the SNI since it isn't very useful then and takes up some space in the system tray. BUG: 437171 --- src/inputmethod.cpp | 70 +++++++++++++++++++++++---------------------- src/inputmethod.h | 2 +- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/inputmethod.cpp b/src/inputmethod.cpp index 06a8a48b39..7b9bbf6e9e 100644 --- a/src/inputmethod.cpp +++ b/src/inputmethod.cpp @@ -81,27 +81,8 @@ void InputMethod::init() ); } - qCDebug(KWIN_VIRTUALKEYBOARD) << "Registering the SNI"; - - QMenu *sniMenu = new QMenu; - sniMenu->addAction(i18n("Configure virtual keyboards..."), this, [] { - QProcess::startDetached("systemsettings5", {"kcm_virtualkeyboard"}); - }); - - m_sni = new KStatusNotifierItem(QStringLiteral("kwin-virtual-keyboard"), this); - m_sni->setStandardActionsEnabled(false); - m_sni->setCategory(KStatusNotifierItem::Hardware); - m_sni->setStatus(KStatusNotifierItem::Passive); - m_sni->setTitle(i18n("Virtual Keyboard")); - m_sni->setToolTipTitle(i18n("Whether to show the virtual keyboard on demand.")); - m_sni->setStandardActionsEnabled(false); - m_sni->setContextMenu(sniMenu); updateSni(); - connect(m_sni, &KStatusNotifierItem::activateRequested, this, - [this] { - setEnabled(!m_enabled); - } - ); + connect(this, &InputMethod::enabledChanged, this, &InputMethod::updateSni); new VirtualKeyboardDBus(this); @@ -516,21 +497,42 @@ void InputMethod::adoptInputMethodContext() void InputMethod::updateSni() { - if (!m_sni) { - return; - } - if (m_inputMethodCommand.isEmpty()) { - m_sni->setIconByName(QStringLiteral("input-keyboard-virtual-on")); - m_sni->setOverlayIconByName(QStringLiteral("emblem-unavailable")); - m_sni->setTitle(i18n("No Virtual Keyboard configured")); - } else if (m_enabled) { - m_sni->setIconByName(QStringLiteral("input-keyboard-virtual-on")); - m_sni->setTitle(i18n("Virtual Keyboard: enabled")); - m_sni->setOverlayIconByName({}); + if (!m_inputMethodCommand.isEmpty()) { + if (!m_sni) { + qCDebug(KWIN_VIRTUALKEYBOARD) << "Registering the SNI"; + m_sni.reset(new KStatusNotifierItem(QStringLiteral("kwin-virtual-keyboard"), this)); + + connect(m_sni.get(), &KStatusNotifierItem::activateRequested, this, + [this] { + setEnabled(!m_enabled); + } + ); + } + m_sni->setStandardActionsEnabled(false); + m_sni->setCategory(KStatusNotifierItem::Hardware); + m_sni->setStatus(KStatusNotifierItem::Passive); + m_sni->setTitle(i18n("Virtual Keyboard")); + m_sni->setToolTipTitle(i18n("Whether to show the virtual keyboard on demand.")); + m_sni->setStandardActionsEnabled(false); + + QMenu *sniMenu = new QMenu; + sniMenu->addAction(i18n("Configure virtual keyboards..."), this, [] { + QProcess::startDetached("systemsettings5", {"kcm_virtualkeyboard"}); + }); + + m_sni->setContextMenu(sniMenu); + + if (m_enabled) { + m_sni->setIconByName(QStringLiteral("input-keyboard-virtual-on")); + m_sni->setTitle(i18n("Virtual Keyboard: enabled")); + m_sni->setOverlayIconByName({}); + } else { + m_sni->setIconByName(QStringLiteral("input-keyboard-virtual-off")); + m_sni->setTitle(i18n("Virtual Keyboard: disabled")); + m_sni->setOverlayIconByName({}); + } } else { - m_sni->setIconByName(QStringLiteral("input-keyboard-virtual-off")); - m_sni->setTitle(i18n("Virtual Keyboard: disabled")); - m_sni->setOverlayIconByName({}); + m_sni.reset(); } } diff --git a/src/inputmethod.h b/src/inputmethod.h index 13cebf355b..0104ca05b6 100644 --- a/src/inputmethod.h +++ b/src/inputmethod.h @@ -91,7 +91,7 @@ private: bool m_enabled = false; bool m_active = false; - KStatusNotifierItem *m_sni = nullptr; + QScopedPointer m_sni; QPointer m_inputClient; QPointer m_trackedClient;