File indexing completed on 2024-06-09 05:31:01

0001 /*
0002     SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "systemtraycontainer.h"
0008 #include "debug.h"
0009 
0010 #include <Plasma/Corona>
0011 #include <PlasmaQuick/AppletQuickItem>
0012 #include <QAction>
0013 #include <kactioncollection.h>
0014 
0015 SystemTrayContainer::SystemTrayContainer(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
0016     : Plasma::Applet(parent, data, args)
0017 {
0018 }
0019 
0020 SystemTrayContainer::~SystemTrayContainer()
0021 {
0022 }
0023 
0024 void SystemTrayContainer::init()
0025 {
0026     Applet::init();
0027 
0028     // in the first creation we immediately create the systray: so it's accessible during desktop scripting
0029     uint id = config().readEntry("SystrayContainmentId", 0);
0030 
0031     if (id == 0) {
0032         ensureSystrayExists();
0033     }
0034 }
0035 
0036 void SystemTrayContainer::ensureSystrayExists()
0037 {
0038     if (m_innerContainment) {
0039         return;
0040     }
0041 
0042     Plasma::Containment *cont = containment();
0043     if (!cont) {
0044         return;
0045     }
0046 
0047     Plasma::Corona *c = cont->corona();
0048     if (!c) {
0049         return;
0050     }
0051 
0052     uint id = config().readEntry("SystrayContainmentId", 0);
0053     if (id > 0) {
0054         foreach (Plasma::Containment *candidate, c->containments()) {
0055             if (candidate->id() == id) {
0056                 m_innerContainment = candidate;
0057                 break;
0058             }
0059         }
0060         qCDebug(SYSTEM_TRAY_CONTAINER) << "Containment id" << id << "that used to be a system tray was deleted";
0061         // id = 0;
0062     }
0063 
0064     if (!m_innerContainment) {
0065         m_innerContainment = c->createContainment(QStringLiteral("org.kde.plasma.private.systemtray"), QVariantList() << "org.kde.plasma:force-create");
0066         config().writeEntry("SystrayContainmentId", m_innerContainment->id());
0067     }
0068 
0069     if (!m_innerContainment) {
0070         return;
0071     }
0072 
0073     m_innerContainment->setParent(this);
0074     connect(containment(), &Plasma::Containment::screenChanged, m_innerContainment.data(), &Plasma::Containment::reactToScreenChange);
0075 
0076     if (formFactor() == Plasma::Types::Horizontal || formFactor() == Plasma::Types::Vertical) {
0077         m_innerContainment->setFormFactor(formFactor());
0078     } else {
0079         m_innerContainment->setFormFactor(Plasma::Types::Horizontal);
0080     }
0081 
0082     m_innerContainment->setLocation(location());
0083 
0084     auto oldInternalSystray = m_internalSystray;
0085     m_internalSystray = PlasmaQuick::AppletQuickItem::itemForApplet(m_innerContainment);
0086 
0087     if (m_internalSystray != oldInternalSystray) {
0088         Q_EMIT internalSystrayChanged();
0089     }
0090 
0091     setInternalAction("configure", m_innerContainment->internalAction("configure"));
0092     connect(m_innerContainment.data(), &Plasma::Containment::configureRequested, this, [this](Plasma::Applet *applet) {
0093         Q_EMIT containment()->configureRequested(applet);
0094     });
0095 
0096     if (m_internalSystray) {
0097         // don't let internal systray manage context menus
0098         m_internalSystray->setAcceptedMouseButtons(Qt::NoButton);
0099     }
0100 
0101     // replace internal remove action with ours
0102     m_innerContainment->setInternalAction("remove", internalAction("remove"));
0103 
0104     // Sync the display hints
0105     m_innerContainment->setContainmentDisplayHints(containmentDisplayHints() | Plasma::Types::ContainmentDrawsPlasmoidHeading
0106                                                    | Plasma::Types::ContainmentForcesSquarePlasmoids);
0107     connect(cont, &Plasma::Containment::containmentDisplayHintsChanged, this, [this]() {
0108         m_innerContainment->setContainmentDisplayHints(containmentDisplayHints() | Plasma::Types::ContainmentDrawsPlasmoidHeading
0109                                                        | Plasma::Types::ContainmentForcesSquarePlasmoids);
0110     });
0111 }
0112 
0113 void SystemTrayContainer::constraintsEvent(Plasma::Applet::Constraints constraints)
0114 {
0115     if (constraints & Plasma::Applet::LocationConstraint) {
0116         if (m_innerContainment) {
0117             m_innerContainment->setLocation(location());
0118         }
0119     }
0120 
0121     if (constraints & Plasma::Applet::FormFactorConstraint) {
0122         if (m_innerContainment) {
0123             if (formFactor() == Plasma::Types::Horizontal || formFactor() == Plasma::Types::Vertical) {
0124                 m_innerContainment->setFormFactor(formFactor());
0125             } else {
0126                 m_innerContainment->setFormFactor(Plasma::Types::Horizontal);
0127             }
0128         }
0129     }
0130 
0131     if (constraints & Plasma::Applet::UiReadyConstraint) {
0132         ensureSystrayExists();
0133     }
0134 }
0135 
0136 QQuickItem *SystemTrayContainer::internalSystray()
0137 {
0138     return m_internalSystray;
0139 }
0140 
0141 K_PLUGIN_CLASS(SystemTrayContainer)
0142 
0143 #include "systemtraycontainer.moc"