File indexing completed on 2024-05-12 05:38:48

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Klapetek <mklapetek@kde.org>
0003     SPDX-FileCopyrightText: 2016 Sebastian Kügler <sebas@kde.org>
0004     SPDX-FileCopyrightText: 2023 Natalie Clarius <natalie.clarius@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "osd.h"
0010 
0011 #include <LayerShellQt/Window>
0012 
0013 #include <KWindowSystem>
0014 #include <KX11Extras>
0015 
0016 #include <QCursor>
0017 #include <QGuiApplication>
0018 #include <QQuickItem>
0019 #include <QScreen>
0020 #include <QStandardPaths>
0021 #include <QTimer>
0022 
0023 #include "osdaction.h"
0024 
0025 using namespace PowerDevil;
0026 
0027 void Osd::showActionSelector(const QString &currentProfile)
0028 {
0029     if (!m_osdActionSelector) {
0030         m_osdActionSelector = std::make_unique<QQuickView>(&m_engine, nullptr);
0031         m_osdActionSelector->setInitialProperties({{QLatin1String("actions"), QVariant::fromValue(OsdAction::availableActions())}, {QLatin1String("currentProfile"), currentProfile}});
0032         m_osdActionSelector->setSource(QUrl(QStringLiteral("qrc:/qml/OsdSelector.qml")));
0033         m_osdActionSelector->setColor(Qt::transparent);
0034         m_osdActionSelector->setFlag(Qt::FramelessWindowHint);
0035 
0036         if (m_osdActionSelector->status() != QQuickView::Ready) {
0037             qWarning() << "Failed to load OSD QML file";
0038             m_osdActionSelector.reset();
0039             return;
0040         }
0041 
0042         auto rootObject = m_osdActionSelector->rootObject();
0043         connect(rootObject, SIGNAL(clicked(QString)), this, SLOT(onOsdActionSelected(QString)));
0044     }
0045 
0046     auto screen = qGuiApp->primaryScreen();
0047 
0048     if (KWindowSystem::isPlatformWayland()) {
0049         auto layerWindow = LayerShellQt::Window::get(m_osdActionSelector.get());
0050         layerWindow->setScope(QStringLiteral("on-screen-display"));
0051         layerWindow->setLayer(LayerShellQt::Window::LayerOverlay);
0052         layerWindow->setAnchors({});
0053         layerWindow->setKeyboardInteractivity(LayerShellQt::Window::KeyboardInteractivityOnDemand);
0054         m_osdActionSelector->setScreen(screen);
0055     } else {
0056         auto newGeometry = m_osdActionSelector->geometry();
0057         newGeometry.moveCenter(screen->geometry().center());
0058         m_osdActionSelector->setGeometry(newGeometry);
0059         KX11Extras::setState(m_osdActionSelector->winId(), NET::SkipPager | NET::SkipSwitcher | NET::SkipTaskbar);
0060         KX11Extras::setType(m_osdActionSelector->winId(), NET::OnScreenDisplay);
0061         m_osdActionSelector->requestActivate();
0062     }
0063     m_osdActionSelector->setVisible(true);
0064 }
0065 
0066 void Osd::onOsdActionSelected(const QString &action)
0067 {
0068     Q_EMIT osdActionSelected(action);
0069     hideOsd();
0070 }
0071 
0072 void Osd::hideOsd()
0073 {
0074     if (m_osdActionSelector) {
0075         m_osdActionSelector->setVisible(false);
0076     }
0077 }
0078 
0079 #include "moc_osd.cpp"