Commit 08c90e7e authored by David Redondo's avatar David Redondo 🏎
Browse files

osd: Make OsdAction a Q_GADGET, move signal to manager

Simplifies the design a bit
parent bcc0353c
......@@ -118,6 +118,7 @@ void KScreenDaemon::init()
new KScreenAdaptor(this);
// Initialize OSD manager to register its dbus interface
m_osdManager = new KScreen::OsdManager(this);
connect(m_osdManager, &KScreen::OsdManager::selected, this, &KScreenDaemon::applyOsdAction);
m_changeCompressor->setInterval(10);
m_changeCompressor->setSingleShot(true);
......@@ -319,8 +320,7 @@ void KScreenDaemon::applyIdealConfig()
if (showOsd) {
qCDebug(KSCREEN_KDED) << "Getting ideal config from user via OSD...";
auto action = m_osdManager->showActionSelector();
connect(action, &KScreen::OsdAction::selected, this, &KScreenDaemon::applyOsdAction);
m_osdManager->showActionSelector();
} else {
m_osdManager->hideOsd();
}
......@@ -538,8 +538,7 @@ void KScreenDaemon::displayButton()
{
qCDebug(KSCREEN_KDED) << "displayBtn triggered";
auto action = m_osdManager->showActionSelector();
connect(action, &KScreen::OsdAction::selected, this, &KScreenDaemon::applyOsdAction);
m_osdManager->showActionSelector();
}
void KScreenDaemon::lidClosedChanged(bool lidIsClosed)
......
......@@ -63,6 +63,7 @@ void Osd::showActionSelector()
rootObject->setProperty("screenGeometry", m_output->geometry());
}
rootObject->setProperty("visible", true);
rootObject->setProperty("actions", QVariant::fromValue(OsdAction::availableActions()));
} else {
qCWarning(KSCREEN_KDED) << "Could not get root object for action selector.";
}
......
......@@ -4,6 +4,9 @@
Work sponsored by the LiMux project of the city of Munich:
SPDX-FileCopyrightText: 2018 Kai Uwe Broulik <kde@broulik.de>
SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redondo.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -13,55 +16,14 @@
using namespace KScreen;
OsdAction::OsdAction(QObject *parent)
: QObject(parent)
QVector<OsdAction> OsdAction::availableActions()
{
}
QVector<int> OsdAction::actionOrder() const
{
return {SwitchToExternal, SwitchToInternal, Clone, ExtendLeft, ExtendRight, NoAction};
}
QString OsdAction::actionLabel(OsdAction::Action action) const
{
switch (action) {
// this is built by both daemon and plasmoid, needs explicit translation domain here
case SwitchToExternal:
return i18nd("kscreen", "Switch to external screen");
case SwitchToInternal:
return i18nd("kscreen", "Switch to laptop screen");
case Clone:
return i18nd("kscreen", "Unify outputs");
case ExtendLeft:
return i18nd("kscreen", "Extend to left");
case ExtendRight:
return i18nd("kscreen", "Extend to right");
case NoAction:
return i18nd("kscreen", "Leave unchanged");
}
Q_UNREACHABLE();
return QString();
}
QString OsdAction::actionIconName(OsdAction::Action action) const
{
switch (action) {
case SwitchToExternal:
return QStringLiteral("osd-shutd-laptop");
case SwitchToInternal:
return QStringLiteral("osd-shutd-screen");
case Clone:
return QStringLiteral("osd-duplicate");
case ExtendLeft:
return QStringLiteral("osd-sbs-left");
case ExtendRight:
return QStringLiteral("osd-sbs-sright");
case NoAction:
return QStringLiteral("dialog-cancel");
}
Q_UNREACHABLE();
return QString();
return {
{SwitchToExternal, i18nd("kscreen", "Switch to external screen"), QStringLiteral("osd-shutd-laptop")},
{SwitchToInternal, i18nd("kscreen", "Switch to laptop screen"), QStringLiteral("osd-shutd-screen")},
{Clone, i18nd("kscreen", "Unify outputs"), QStringLiteral("osd-duplicate")},
{ExtendLeft, i18nd("kscreen", "Extend to left"), QStringLiteral("osd-sbs-left")},
{ExtendRight, i18nd("kscreen", "Extend to right"), QStringLiteral("osd-sbs-sright")},
{NoAction, i18nd("kscreen", "Leave unchanged"), QStringLiteral("dialog-cancel")},
};
}
......@@ -4,6 +4,9 @@
Work sponsored by the LiMux project of the city of Munich:
SPDX-FileCopyrightText: 2018 Kai Uwe Broulik <kde@broulik.de>
SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redondo.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -15,9 +18,11 @@
namespace KScreen
{
class OsdAction : public QObject
{
Q_OBJECT
struct OsdAction {
Q_GADGET
Q_PROPERTY(QString label MEMBER label CONSTANT)
Q_PROPERTY(QString iconName MEMBER iconName CONSTANT)
Q_PROPERTY(Action action MEMBER action CONSTANT)
public:
enum Action {
NoAction,
......@@ -29,14 +34,11 @@ public:
};
Q_ENUM(Action)
explicit OsdAction(QObject *parent = nullptr);
Q_INVOKABLE QVector<int> actionOrder() const;
Q_INVOKABLE QString actionLabel(KScreen::OsdAction::Action action) const;
Q_INVOKABLE QString actionIconName(KScreen::OsdAction::Action action) const;
Action action;
QString label;
QString iconName;
Q_SIGNALS:
void selected(KScreen::OsdAction::Action action);
static QVector<OsdAction> availableActions();
};
} // namespace KScreen
/*
SPDX-FileCopyrightText: 2016 Sebastian Kügler <sebas@kde.org>
SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redondo.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -14,36 +15,19 @@
#include <KScreen/Output>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QQmlEngine>
namespace KScreen
{
class OsdActionImpl : public OsdAction
{
Q_OBJECT
public:
OsdActionImpl(QObject *parent = nullptr)
: OsdAction(parent)
{
}
void setOsd(Osd *osd)
{
connect(osd, &Osd::osdActionSelected, this, [this](Action action) {
Q_EMIT selected(action);
deleteLater();
});
}
};
OsdManager::OsdManager(QObject *parent)
: QObject(parent)
, m_cleanupTimer(new QTimer(this))
{
qmlRegisterSingletonType<KScreen::OsdAction>("org.kde.KScreen", 1, 0, "OsdAction", [](QQmlEngine *, QJSEngine *) -> QObject * {
return new KScreen::OsdAction();
});
qmlRegisterUncreatableType<KScreen::OsdAction>("org.kde.KScreen", 1, 0, "OsdAction", QStringLiteral("Can't create OsdAction"));
// free up memory when the osd hasn't been used for more than 1 minute
m_cleanupTimer->setInterval(60000);
......@@ -67,17 +51,11 @@ OsdManager::~OsdManager()
{
}
OsdAction *OsdManager::showActionSelector()
void OsdManager::showActionSelector()
{
hideOsd();
OsdActionImpl *action = new OsdActionImpl(this);
connect(action, &OsdActionImpl::selected, this, [this]() {
for (auto osd : qAsConst(m_osds)) {
osd->hideOsd();
}
});
connect(new KScreen::GetConfigOperation(), &KScreen::GetConfigOperation::finished, this, [this, action](const KScreen::ConfigOperation *op) {
connect(new KScreen::GetConfigOperation(), &KScreen::GetConfigOperation::finished, this, [this](const KScreen::ConfigOperation *op) {
if (op->hasError()) {
qCWarning(KSCREEN_KDED) << op->errorString();
return;
......@@ -124,15 +102,17 @@ OsdAction *OsdManager::showActionSelector()
} else {
osd = new KScreen::Osd(osdOutput, this);
m_osds.insert(osdOutput->name(), osd);
connect(osd, &Osd::osdActionSelected, this, [this](OsdAction::Action action) {
for (auto osd : qAsConst(m_osds)) {
osd->hideOsd();
}
Q_EMIT selected(action);
});
}
action->setOsd(osd);
osd->showActionSelector();
m_cleanupTimer->start();
});
return action;
}
}
#include "osdmanager.moc"
......@@ -31,7 +31,9 @@ public:
public Q_SLOTS:
void hideOsd();
KScreen::OsdAction *showActionSelector();
void showActionSelector();
Q_SIGNALS:
void selected(OsdAction::Action action);
private:
void slotIdentifyOutputs(KScreen::ConfigOperation *op);
......
......@@ -20,6 +20,7 @@ PlasmaCore.Dialog {
type: PlasmaCore.Dialog.Normal
property string infoText
property var screenGeometry
property var actions
onScreenGeometryChanged: {
root.x = screenGeometry.x + (screenGeometry.width - mainItem.width) / 2
......@@ -33,17 +34,13 @@ PlasmaCore.Dialog {
Repeater {
id: actionRepeater
property int currentIndex: 0
model: OsdAction.actionOrder().map(layout => ({
iconSource: OsdAction.actionIconName(layout),
label: OsdAction.actionLabel(layout),
action: layout,
}))
model: root.actions
delegate: PlasmaComponents.Button {
property int actionId: modelData.action
Accessible.name: modelData.label
icon.name: modelData.iconSource
icon.name: modelData.iconName
icon.height: PlasmaCore.Units.gridUnit * 8
icon.width: PlasmaCore.Units.gridUnit * 8
......
......@@ -18,8 +18,6 @@
#include <KScreen/GetConfigOperation>
#include <KScreen/Output>
#include "../kded/osdaction.h"
#include <algorithm>
KScreenApplet::KScreenApplet(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
......@@ -31,10 +29,7 @@ KScreenApplet::~KScreenApplet() = default;
void KScreenApplet::init()
{
qmlRegisterSingletonType<KScreen::OsdAction>("org.kde.private.kscreen", 1, 0, "OsdAction", [](QQmlEngine *, QJSEngine *) -> QObject * {
return new KScreen::OsdAction();
});
qmlRegisterUncreatableType<KScreen::OsdAction>("org.kde.private.kscreen", 1, 0, "OsdAction", QStringLiteral("Can't create OsdAction"));
connect(new KScreen::GetConfigOperation(KScreen::GetConfigOperation::NoEDID),
&KScreen::ConfigOperation::finished,
this,
......@@ -91,6 +86,12 @@ void KScreenApplet::checkOutputs()
}
}
QVariant KScreenApplet::availableActions()
{
// Need to wrap it in a QVariant, otherwise QML doesn't like the return type
return QVariant::fromValue(KScreen::OsdAction::availableActions());
}
K_PLUGIN_CLASS_WITH_JSON(KScreenApplet, "metadata.json")
#include "kscreenapplet.moc"
......@@ -11,6 +11,8 @@
#include <KScreen/Types>
#include "../kded/osdaction.h"
class KScreenApplet : public Plasma::Applet
{
Q_OBJECT
......@@ -39,6 +41,8 @@ public:
Q_INVOKABLE void applyLayoutPreset(KScreenApplet::Action action);
Q_INVOKABLE QVariant availableActions();
Q_SIGNALS:
void connectedOutputCountChanged();
......
......@@ -27,16 +27,6 @@ Item {
readonly property bool presentationModeEnabled: presentationModeCookie > 0
property int presentationModeCookie: -1
readonly property var screenLayouts:
OsdAction.actionOrder()
// We don't want the "No action" item in the plasmoid
.filter(layout => layout !== OsdAction.NoAction)
.map(layout => ({
iconName: OsdAction.actionIconName(layout),
label: OsdAction.actionLabel(layout),
action: layout,
}))
function action_configure() {
KCMShell.openSystemSettings(kcmName);
}
......@@ -86,7 +76,7 @@ Item {
ScreenLayoutSelection {
Layout.leftMargin: PlasmaCore.Units.smallSpacing
Layout.fillWidth: true
screenLayouts: root.screenLayouts
screenLayouts: Plasmoid.nativeInterface.availableActions()
}
PresentationModeItem {
......
......@@ -44,8 +44,8 @@ void OsdTest::setUseDBus(bool yesno)
void OsdTest::showActionSelector()
{
if (!m_useDBus) {
auto action = getOsdManager()->showActionSelector();
connect(action, &KScreen::OsdAction::selected, [](KScreen::OsdAction::Action action) {
getOsdManager()->showActionSelector();
connect(getOsdManager(), &KScreen::OsdManager::selected, [](KScreen::OsdAction::Action action) {
qCDebug(KSCREEN_KDED) << "Selected action:" << action;
qApp->quit();
});
......
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