File indexing completed on 2024-03-24 17:23:12

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2017 - 2020 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 <WindowList.hxx>
0022 #include <PopupMenu.hxx>
0023 
0024 #include <QIcon>
0025 #include <QStyle>
0026 #include <QStyleOptionButton>
0027 #include <QPainter>
0028 
0029 #include <KWinCompat.hxx>
0030 
0031 //--------------------------------------------------------------------------------
0032 
0033 WindowList::WindowList(QWidget *parent)
0034   : QPushButton(parent)
0035 {
0036   setIcon(QIcon::fromTheme("arrow-up"));
0037   setMaximumWidth(22);
0038   setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
0039 
0040   setMenu(new PopupMenu(this));
0041   connect(menu(), &PopupMenu::aboutToShow, this, &WindowList::fillMenu);
0042 }
0043 
0044 //--------------------------------------------------------------------------------
0045 
0046 void WindowList::fillMenu()
0047 {
0048   menu()->clear();
0049 
0050   QList<WId> windows = KWinCompat::windows();
0051 
0052   for (int i = 1; i <= KWinCompat::numberOfDesktops(); i++)
0053   {
0054     menu()->addSection(KWinCompat::desktopName(i).isEmpty() ?
0055                        QString::number(i) : KWinCompat::desktopName(i));
0056 
0057     for (WId wid : windows)
0058     {
0059       KWindowInfo win(wid, NET::WMDesktop | NET::WMWindowType | NET::WMState | NET::WMName | NET::WMIcon);
0060       if ( win.valid(true) && win.isOnDesktop(i) &&
0061           (win.windowType(NET::DesktopMask) != NET::Desktop) &&
0062           (win.windowType(NET::DockMask) != NET::Dock) &&
0063            !(win.state() & NET::SkipTaskbar) )
0064       {
0065         QAction *action =
0066             menu()->addAction(KWinCompat::icon(wid, 22, 22, true), win.name(),
0067                               [wid]() { KWinCompat::forceActiveWindow(wid); });
0068 
0069         action->setData(static_cast<int>(wid));
0070       }
0071     }
0072   }
0073 }
0074 
0075 //--------------------------------------------------------------------------------
0076 
0077 void WindowList::paintEvent(QPaintEvent *event)
0078 {
0079   Q_UNUSED(event);
0080 
0081   QPainter painter(this);
0082 
0083   QStyleOptionButton option;
0084   initStyleOption(&option);
0085   option.features = QStyleOptionButton::None;  // avoid menu arrow
0086 
0087   style()->drawControl(QStyle::CE_PushButton, &option, &painter, this);
0088 }
0089 
0090 //--------------------------------------------------------------------------------