From 75c0c415cc27583f3014ba4fbb7d5a371ce94d59 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Sun, 24 Mar 2019 23:11:54 +0100 Subject: [PATCH] Don't show the OrientationSensor if it's not supported Test Plan: I don't get the OrientationSensor SNI on my laptop, which is fine, because it was useless. Reviewers: #kwin, #plasma, zzag Reviewed By: #kwin, #plasma, zzag Subscribers: kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D19955 --- orientation_sensor.cpp | 53 +++++++++++++++++++++--------------------- orientation_sensor.h | 2 +- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/orientation_sensor.cpp b/orientation_sensor.cpp index d88d422049..539e92d960 100644 --- a/orientation_sensor.cpp +++ b/orientation_sensor.cpp @@ -66,20 +66,44 @@ OrientationSensor::OrientationSensor(QObject *parent) connect(m_sensor, &QOrientationSensor::activeChanged, this, &OrientationSensor::refresh); } +void OrientationSensor::activate() +{ + m_userEnabled = !m_userEnabled; + startStopSensor(); + emit userEnabledChanged(m_userEnabled); +} + void OrientationSensor::refresh() { - if (!m_sni) { + if (m_orientation == Orientation::Undefined) { + delete m_sni; + m_sni = nullptr; return; } + + if (!m_sni) { + m_sni = new KStatusNotifierItem(QStringLiteral("kwin-automatic-rotation"), this); + m_sni->setStandardActionsEnabled(false); + m_sni->setCategory(KStatusNotifierItem::Hardware); + m_sni->setStatus(KStatusNotifierItem::Passive); + // TODO: proper icon with state + m_sni->setIconByName(QStringLiteral("preferences-desktop-display")); + // we start disabled, it gets updated when the sensor becomes active + + connect(m_sni, &KStatusNotifierItem::activateRequested, this, &OrientationSensor::activate); + } + if (m_sensor->isActive()) { m_sni->setTitle(i18n("Allow Rotation")); m_sni->setToolTipTitle(i18n("Automatic screen rotation is enabled")); } else { QString text; switch(m_orientation) { + case Orientation::Undefined: + Q_UNREACHABLE(); //we don't want an sni if it's undefined + break; case Orientation::FaceUp: case Orientation::FaceDown: - case Orientation::Undefined: text = i18n("Undefined"); break; case Orientation::TopUp: @@ -106,7 +130,7 @@ void OrientationSensor::setEnabled(bool enabled) m_enabled = enabled; if (m_enabled) { loadConfig(); - setupStatusNotifier(); + refresh(); m_adaptor = new OrientationSensorAdaptor(this); } else { delete m_sni; @@ -125,29 +149,6 @@ void OrientationSensor::loadConfig() m_userEnabled = m_config->group("OrientationSensor").readEntry("Enabled", true); } -void OrientationSensor::setupStatusNotifier() -{ - if (m_sni) { - return; - } - m_sni = new KStatusNotifierItem(QStringLiteral("kwin-automatic-rotation"), this); - m_sni->setStandardActionsEnabled(false); - m_sni->setCategory(KStatusNotifierItem::Hardware); - m_sni->setStatus(KStatusNotifierItem::Passive); - // TODO: proper icon with state - m_sni->setIconByName(QStringLiteral("preferences-desktop-display")); - // we start disabled, it gets updated when the sensor becomes active - - refresh(); - connect(m_sni, &KStatusNotifierItem::activateRequested, this, - [this] { - m_userEnabled = !m_userEnabled; - startStopSensor(); - emit userEnabledChanged(m_userEnabled); - } - ); -} - void OrientationSensor::startStopSensor() { if (m_enabled && m_userEnabled) { diff --git a/orientation_sensor.h b/orientation_sensor.h index 2edba32125..131fb88063 100644 --- a/orientation_sensor.h +++ b/orientation_sensor.h @@ -76,10 +76,10 @@ Q_SIGNALS: void userEnabledChanged(bool); private: - void setupStatusNotifier(); void startStopSensor(); void loadConfig(); void refresh(); + void activate(); QOrientationSensor *m_sensor; bool m_enabled = false;