File indexing completed on 2024-05-12 05:32:09

0001 /*
0002     SPDX-FileCopyrightText: 2021 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "desktopbackgrounditem.h"
0008 #include "core/output.h"
0009 #include "window.h"
0010 #if KWIN_BUILD_ACTIVITIES
0011 #include "activities.h"
0012 #endif
0013 #include "core/outputbackend.h"
0014 #include "main.h"
0015 #include "scripting_logging.h"
0016 #include "virtualdesktops.h"
0017 #include "workspace.h"
0018 
0019 namespace KWin
0020 {
0021 
0022 DesktopBackgroundItem::DesktopBackgroundItem(QQuickItem *parent)
0023     : WindowThumbnailItem(parent)
0024 {
0025 }
0026 
0027 void DesktopBackgroundItem::componentComplete()
0028 {
0029     WindowThumbnailItem::componentComplete();
0030     updateWindow();
0031 }
0032 
0033 QString DesktopBackgroundItem::outputName() const
0034 {
0035     return m_output ? m_output->name() : QString();
0036 }
0037 
0038 void DesktopBackgroundItem::setOutputName(const QString &name)
0039 {
0040     setOutput(kwinApp()->outputBackend()->findOutput(name));
0041 }
0042 
0043 Output *DesktopBackgroundItem::output() const
0044 {
0045     return m_output;
0046 }
0047 
0048 void DesktopBackgroundItem::setOutput(Output *output)
0049 {
0050     if (m_output != output) {
0051         m_output = output;
0052         updateWindow();
0053         Q_EMIT outputChanged();
0054     }
0055 }
0056 
0057 VirtualDesktop *DesktopBackgroundItem::desktop() const
0058 {
0059     return m_desktop;
0060 }
0061 
0062 void DesktopBackgroundItem::setDesktop(VirtualDesktop *desktop)
0063 {
0064     if (m_desktop != desktop) {
0065         m_desktop = desktop;
0066         updateWindow();
0067         Q_EMIT desktopChanged();
0068     }
0069 }
0070 
0071 QString DesktopBackgroundItem::activity() const
0072 {
0073     return m_activity;
0074 }
0075 
0076 void DesktopBackgroundItem::setActivity(const QString &activity)
0077 {
0078     if (m_activity != activity) {
0079         m_activity = activity;
0080         updateWindow();
0081         Q_EMIT activityChanged();
0082     }
0083 }
0084 
0085 void DesktopBackgroundItem::updateWindow()
0086 {
0087     if (!isComponentComplete()) {
0088         return;
0089     }
0090 
0091     if (Q_UNLIKELY(!m_output)) {
0092         qCWarning(KWIN_SCRIPTING) << "DesktopBackgroundItem.output is required";
0093         return;
0094     }
0095 
0096     VirtualDesktop *desktop = m_desktop;
0097     if (!desktop) {
0098         desktop = VirtualDesktopManager::self()->currentDesktop();
0099     }
0100 
0101     QString activity = m_activity;
0102     if (activity.isEmpty()) {
0103 #if KWIN_BUILD_ACTIVITIES
0104         activity = Workspace::self()->activities()->current();
0105 #endif
0106     }
0107 
0108     Window *clientCandidate = nullptr;
0109 
0110     const auto clients = workspace()->windows();
0111     for (Window *client : clients) {
0112         if (client->isDesktop() && client->isOnOutput(m_output) && client->isOnDesktop(desktop) && client->isOnActivity(activity)) {
0113             // In the unlikely event there are multiple desktop windows (e.g. conky's floating panel is of type "desktop")
0114             // choose the one which matches the ouptut size, if possible.
0115             if (!clientCandidate || client->size() == m_output->geometry().size()) {
0116                 clientCandidate = client;
0117             }
0118         }
0119     }
0120 
0121     setClient(clientCandidate);
0122 }
0123 
0124 } // namespace KWin
0125 
0126 #include "moc_desktopbackgrounditem.cpp"