File indexing completed on 2024-04-14 05:39:39

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2017 Martin Koller, kollix@aon.at
0004 
0005   This file is part of liquidshell.
0006 
0007   liquidshell is free software: you can redistribute it and/or modify
0008   it under the terms of the GNU General Public License as published by
0009   the Free Software Foundation, either version 3 of the License, or
0010   (at your option) any later version.
0011 
0012   liquidshell is distributed in the hope that it will be useful,
0013   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015   GNU General Public License for more details.
0016 
0017   You should have received a copy of the GNU General Public License
0018   along with liquidshell.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <TaskBar.hxx>
0022 #include <TaskBarButton.hxx>
0023 
0024 #include <KWinCompat.hxx>
0025 
0026 #include <QVariant>
0027 #include <QSpacerItem>
0028 #include <QDebug>
0029 
0030 #include <cmath>
0031 
0032 //--------------------------------------------------------------------------------
0033 
0034 TaskBar::TaskBar(DesktopPanel *parent)
0035   : QWidget(parent)
0036 {
0037   grid = new QGridLayout(this);
0038   grid->setSpacing(2);
0039   grid->setContentsMargins(QMargins());
0040 
0041   fill();
0042 
0043   connect(parent, &DesktopPanel::rowsChanged, this, &TaskBar::fill);
0044 
0045   connect(KWinCompat::self(), &KWinCompat::currentDesktopChanged, this, &TaskBar::fill);
0046   connect(KWinCompat::self(), &KWinCompat::windowAdded, this, &TaskBar::fill);
0047   connect(KWinCompat::self(), &KWinCompat::windowRemoved, this, &TaskBar::fill);
0048 
0049   connect(KWinCompat::self(), SIGNAL(windowChanged(WId, NET::Properties, NET::Properties2)),
0050           this, SLOT(windowChanged(WId, NET::Properties, NET::Properties2)));
0051 }
0052 
0053 //--------------------------------------------------------------------------------
0054 
0055 void TaskBar::fill()
0056 {
0057   QLayoutItem *child;
0058   while ( (child = grid->takeAt(0)) )
0059   {
0060     delete child->widget();
0061     delete child;
0062   }
0063 
0064   QList<WId> windowsToShow;
0065   const int MAX_ROWS = qobject_cast<DesktopPanel *>(parentWidget())->getRows();
0066 
0067   for (WId wid : KWinCompat::windows())
0068   {
0069     KWindowInfo win(wid, NET::WMDesktop | NET::WMWindowType | NET::WMState);
0070     if ( win.valid(true) && win.isOnCurrentDesktop() &&
0071         (win.windowType(NET::DesktopMask) != NET::Desktop) &&
0072         (win.windowType(NET::DockMask) != NET::Dock) &&
0073         (win.windowType(NET::PopupMenuMask) != NET::PopupMenu) &&
0074         (win.windowType(NET::UtilityMask) != NET::Utility) &&
0075          !(win.state() & NET::SkipTaskbar) )
0076       windowsToShow.append(wid);
0077   }
0078 
0079   if ( windowsToShow.count() == 0 )
0080   {
0081     grid->addItem(new QSpacerItem(0, 0,  QSizePolicy::Expanding), 0, 0);
0082     return;
0083   }
0084 
0085   int row = 0, col = 0, actualRows;
0086   const int MAX_COLUMNS = std::max(2, static_cast<int>(std::ceil(windowsToShow.count() / float(MAX_ROWS))));
0087   actualRows = static_cast<int>(std::ceil(windowsToShow.count() / float(MAX_COLUMNS)));
0088   const int ICON_SIZE = height() / actualRows >= 36 ? 32 : 22;
0089 
0090   for (WId wid : windowsToShow)
0091   {
0092     TaskBarButton *b = new TaskBarButton(wid);
0093     b->setIconSize(ICON_SIZE);
0094     grid->addWidget(b, row, col);
0095 
0096     col = (col + 1) % MAX_COLUMNS;
0097     if ( col == 0 ) row++;
0098   }
0099 }
0100 
0101 //--------------------------------------------------------------------------------
0102 
0103 void TaskBar::windowChanged(WId wid, NET::Properties props, NET::Properties2 props2)
0104 {
0105   Q_UNUSED(wid)
0106   Q_UNUSED(props2)
0107 
0108   if ( props & NET::WMDesktop )
0109     fill();
0110 }
0111 
0112 //--------------------------------------------------------------------------------