Commit 927e16a6 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧
Browse files

Use KWin's tablet mode to decide on the mobility UX

There are certain aspects where we decide to use slightly different
settings for a tablety device (e.g. Plasma Mobile) and the rest. So far
this was getting set using the QT_QUICK_CONTROLS_MOBILE environment
variable, which makes it harder for us as it carries tons of graphical
connotations that we don't care for in here. Instead use KWin's
TabletMode, which fits much better what we are trying to do here.

This applies to:
- the power button configuration (on tablet it puts the device to sleep,
on non-tablet it shows the logout page)
- profile generation. Since profile generation is static, we'll use the
availability of the feature to decide how to generate it. This way the
device won't just start behaving completely different when it stops
acting as a tablet.
parent 5668ded3
......@@ -24,7 +24,7 @@ include(KDECompilerSettings NO_POLICY_SCOPE)
include(KDEClangFormat)
find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS Widgets DBus X11Extras)
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS Activities Auth IdleTime Config DBusAddons Solid I18n GlobalAccel KIO NotifyConfig Wayland DocTools Crash Notifications)
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS Activities Auth IdleTime Config DBusAddons Solid I18n GlobalAccel KIO NotifyConfig Wayland DocTools Crash Notifications Kirigami2)
find_package(KF5Screen CONFIG REQUIRED)
find_package(LibKWorkspace CONFIG REQUIRED)
......
......@@ -22,7 +22,6 @@ set(powerdevil_bundled_actions_SRCS
actions/bundled/powerprofile.cpp
)
if(HAVE_WIRELESS_SUPPORT)
set(powerdevil_bundled_actions_SRCS ${powerdevil_bundled_actions_SRCS}
actions/bundled/wirelesspowersaving.cpp
......@@ -96,6 +95,7 @@ target_link_libraries(powerdevilcore
KF5::Notifications
KF5::XmlGui
KF5::WaylandClient
KF5::Kirigami2
PW::KWorkspace
XCB::DPMS
)
......
......@@ -43,6 +43,7 @@
#include <KLocalizedString>
#include <KPluginFactory>
#include <KSharedConfig>
#include <TabletModeWatcher>
namespace PowerDevil {
namespace BundledActions {
......@@ -69,19 +70,11 @@ DPMS::DPMS(QObject* parent)
// inhibitions persist over kded module unload/load
m_inhibitScreen = PowerDevil::PolicyAgent::instance()->unavailablePolicies() & PowerDevil::PolicyAgent::ChangeScreenSettings;
KGlobalAccel *accel = KGlobalAccel::self();
KActionCollection *actionCollection = new KActionCollection( this );
actionCollection->setComponentDisplayName(i18nc("Name for powerdevil shortcuts category", "Power Management"));
QAction *globalAction = actionCollection->addAction(QLatin1String("Turn Off Screen"));
globalAction->setText(i18nc("@action:inmenu Global shortcut", "Turn Off Screen"));
const bool mobile = !qEnvironmentVariableIsEmpty("QT_QUICK_CONTROLS_MOBILE");
if (!mobile) {
accel->setGlobalShortcut(globalAction, QList<QKeySequence>());
} else {
accel->setGlobalShortcut(globalAction, Qt::Key_PowerOff);
}
connect(globalAction, &QAction::triggered, this, [this] {
if (m_helper) {
if (m_lockBeforeTurnOff) {
......@@ -90,6 +83,17 @@ DPMS::DPMS(QObject* parent)
m_helper->trigger(QStringLiteral("TurnOff"));
}
});
auto powerButtonMode = [globalAction] (bool isTablet) {
if (!isTablet) {
KGlobalAccel::self()->setGlobalShortcut(globalAction, QList<QKeySequence>());
} else {
KGlobalAccel::self()->setGlobalShortcut(globalAction, Qt::Key_PowerOff);
}
};
auto interface = Kirigami::TabletModeWatcher::self();
connect(interface, &Kirigami::TabletModeWatcher::tabletModeChanged, globalAction, powerButtonMode);
powerButtonMode(interface->isTabletMode());
}
DPMS::~DPMS() = default;
......
......@@ -65,6 +65,7 @@ private:
QScopedPointer<AbstractDpmsHelper> m_helper;
bool m_lockBeforeTurnOff = false;
bool m_tabletMode = false;
void lockScreen();
};
......
......@@ -39,6 +39,7 @@
#include <KScreen/Output>
#include <KGlobalAccel>
#include <TabletModeWatcher>
namespace PowerDevil {
namespace BundledActions {
......@@ -47,7 +48,6 @@ HandleButtonEvents::HandleButtonEvents(QObject *parent)
: Action(parent)
, m_screenConfiguration(nullptr)
{
const bool mobile = !qEnvironmentVariableIsEmpty("QT_QUICK_CONTROLS_MOBILE");
new HandleButtonEventsAdaptor(this);
// We enforce no policies here - after all, we just call other actions - which have their policies.
setRequiredPolicies(PowerDevil::PolicyAgent::None);
......@@ -71,10 +71,16 @@ HandleButtonEvents::HandleButtonEvents(QObject *parent)
globalAction = actionCollection->addAction("PowerOff");
globalAction->setText(i18nc("@action:inmenu Global shortcut", "Power Off"));
if (!mobile) {
accel->setGlobalShortcut(globalAction, Qt::Key_PowerOff);
}
connect(globalAction, &QAction::triggered, this, &HandleButtonEvents::powerOffButtonTriggered);
auto powerButtonMode = [globalAction] (bool isTablet) {
if (!isTablet) {
KGlobalAccel::self()->setGlobalShortcut(globalAction, Qt::Key_PowerOff);
} else {
KGlobalAccel::self()->setGlobalShortcut(globalAction, QList<QKeySequence>());
}
};
auto interface = Kirigami::TabletModeWatcher::self();
connect(interface, &Kirigami::TabletModeWatcher::tabletModeChanged, globalAction, powerButtonMode);
powerButtonMode(interface->isTabletMode());
globalAction = actionCollection->addAction("PowerDown");
globalAction->setText(i18nc("@action:inmenu Global shortcut, used for long presses of the power button", "Power Down"));
......
......@@ -49,6 +49,7 @@
#include <QDebug>
#include <algorithm>
#include <TabletModeWatcher>
#ifdef Q_OS_LINUX
#include <sys/timerfd.h>
......@@ -121,7 +122,11 @@ void Core::onBackendReady()
qCDebug(POWERDEVIL) << "Generating a default configuration";
bool toRam = m_backend->supportedSuspendMethods() & PowerDevil::BackendInterface::ToRam;
bool toDisk = m_backend->supportedSuspendMethods() & PowerDevil::BackendInterface::ToDisk;
ProfileGenerator::generateProfiles(toRam, toDisk);
// These are generated profiles,
const bool mobile = Kirigami::TabletModeWatcher::self()->isTabletModeAvailable();
ProfileGenerator::generateProfiles(mobile, toRam, toDisk);
m_profilesConfig->reparseConfiguration();
}
......
......@@ -29,7 +29,7 @@
namespace PowerDevil {
void ProfileGenerator::generateProfiles(bool toRam, bool toDisk)
void ProfileGenerator::generateProfiles(bool mobile, bool toRam, bool toDisk)
{
// Change critical action if default (hibernate) is unavailable
if (!toDisk) {
......@@ -42,8 +42,6 @@ void ProfileGenerator::generateProfiles(bool toRam, bool toDisk)
PowerDevilSettings::self()->save();
}
const bool mobile = !qEnvironmentVariableIsEmpty("QT_QUICK_CONTROLS_MOBILE");
// Ok, let's get our config file.
KSharedConfigPtr profilesConfig = KSharedConfig::openConfig("powermanagementprofilesrc", KConfig::SimpleConfig);
......
......@@ -36,7 +36,7 @@ namespace ProfileGenerator
ToggleScreenOnOffMode = 128
};
void generateProfiles(bool toRam, bool toDisk);
void generateProfiles(bool isMobile, bool toRam, bool toDisk);
}
}
......
......@@ -17,6 +17,7 @@ target_link_libraries(kcm_powerdevilprofilesconfig
KF5::KIOWidgets
KF5::ConfigGui
KF5::ConfigWidgets
KF5::Kirigami2
powerdevilui
powerdevilconfigcommonprivate
)
......
......@@ -27,6 +27,7 @@
#include <powerdevilpowermanagement.h>
#include <powerdevil_debug.h>
#include <TabletModeWatcher>
#include <QCheckBox>
#include <QFormLayout>
......@@ -77,8 +78,10 @@ EditPage::EditPage(QWidget *parent, const QVariantList &args)
m_profilesConfig = KSharedConfig::openConfig("powermanagementprofilesrc", KConfig::SimpleConfig | KConfig::CascadeConfig);
if (m_profilesConfig->groupList().isEmpty()) {
// Use the generator
auto interface = Kirigami::TabletModeWatcher::self();
PowerDevil::ProfileGenerator::generateProfiles(
interface->isTabletModeAvailable(),
PowerDevil::PowerManagement::instance()->canSuspend(),
PowerDevil::PowerManagement::instance()->canHibernate()
);
......@@ -194,7 +197,10 @@ void EditPage::restoreDefaultProfiles()
"Are you sure you want to continue?"), i18n("Restore Default Profiles"));
if (ret == KMessageBox::Continue) {
qCDebug(POWERDEVIL) << "Restoring defaults.";
auto interface = Kirigami::TabletModeWatcher::self();
PowerDevil::ProfileGenerator::generateProfiles(
interface->isTabletModeAvailable(),
PowerDevil::PowerManagement::instance()->canSuspend(),
PowerDevil::PowerManagement::instance()->canHibernate()
);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment