2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2012-11-20 16:26:50 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2012 Martin Gräßlin <mgraesslin@kde.org>
|
2012-11-20 16:26:50 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2012-11-20 16:26:50 +00:00
|
|
|
#include "focuschain.h"
|
2022-04-22 17:39:12 +00:00
|
|
|
#include "window.h"
|
2021-08-28 18:58:29 +00:00
|
|
|
#include "workspace.h"
|
2012-11-20 16:26:50 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2013-04-05 07:41:25 +00:00
|
|
|
KWIN_SINGLETON_FACTORY_VARIABLE(FocusChain, s_manager)
|
2012-11-20 16:26:50 +00:00
|
|
|
|
|
|
|
FocusChain::FocusChain(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_separateScreenFocus(false)
|
2022-04-23 08:33:23 +00:00
|
|
|
, m_activeWindow(nullptr)
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FocusChain::~FocusChain()
|
|
|
|
{
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
s_manager = nullptr;
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 08:33:23 +00:00
|
|
|
void FocusChain::remove(Window *window)
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2018-07-27 19:43:09 +00:00
|
|
|
for (auto it = m_desktopFocusChains.begin();
|
2022-03-23 10:13:38 +00:00
|
|
|
it != m_desktopFocusChains.end();
|
|
|
|
++it) {
|
2022-04-23 08:33:23 +00:00
|
|
|
it.value().removeAll(window);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
2022-04-23 08:33:23 +00:00
|
|
|
m_mostRecentlyUsed.removeAll(window);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 12:07:43 +00:00
|
|
|
void FocusChain::addDesktop(VirtualDesktop *desktop)
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2021-08-16 12:07:43 +00:00
|
|
|
m_desktopFocusChains.insert(desktop, Chain());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FocusChain::removeDesktop(VirtualDesktop *desktop)
|
|
|
|
{
|
|
|
|
if (m_currentDesktop == desktop) {
|
|
|
|
m_currentDesktop = nullptr;
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
2021-08-16 12:07:43 +00:00
|
|
|
m_desktopFocusChains.remove(desktop);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 17:39:12 +00:00
|
|
|
Window *FocusChain::getForActivation(VirtualDesktop *desktop) const
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2021-08-28 18:58:29 +00:00
|
|
|
return getForActivation(desktop, workspace()->activeOutput());
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 17:39:12 +00:00
|
|
|
Window *FocusChain::getForActivation(VirtualDesktop *desktop, Output *output) const
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2018-07-27 19:43:09 +00:00
|
|
|
auto it = m_desktopFocusChains.constFind(desktop);
|
2012-11-20 16:26:50 +00:00
|
|
|
if (it == m_desktopFocusChains.constEnd()) {
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
return nullptr;
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
2015-03-05 09:24:54 +00:00
|
|
|
const auto &chain = it.value();
|
2012-11-20 16:26:50 +00:00
|
|
|
for (int i = chain.size() - 1; i >= 0; --i) {
|
2015-03-05 09:24:54 +00:00
|
|
|
auto tmp = chain.at(i);
|
2022-04-23 08:33:23 +00:00
|
|
|
// TODO: move the check into Window
|
2021-11-23 12:13:56 +00:00
|
|
|
if (!tmp->isShade() && tmp->isShown() && tmp->isOnCurrentActivity()
|
2022-03-23 10:13:38 +00:00
|
|
|
&& (!m_separateScreenFocus || tmp->output() == output)) {
|
2015-03-12 12:43:39 +00:00
|
|
|
return tmp;
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
return nullptr;
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 08:33:23 +00:00
|
|
|
void FocusChain::update(Window *window, FocusChain::Change change)
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2022-04-23 08:33:23 +00:00
|
|
|
if (!window->wantsTabFocus()) {
|
2012-11-20 16:26:50 +00:00
|
|
|
// Doesn't want tab focus, remove
|
2022-04-23 08:33:23 +00:00
|
|
|
remove(window);
|
2012-11-20 16:26:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-23 08:33:23 +00:00
|
|
|
if (window->isOnAllDesktops()) {
|
2012-11-20 16:26:50 +00:00
|
|
|
// Now on all desktops, add it to focus chains it is not already in
|
2018-07-27 19:43:09 +00:00
|
|
|
for (auto it = m_desktopFocusChains.begin();
|
2022-03-23 10:13:38 +00:00
|
|
|
it != m_desktopFocusChains.end();
|
|
|
|
++it) {
|
2015-03-05 09:24:54 +00:00
|
|
|
auto &chain = it.value();
|
2012-11-20 16:26:50 +00:00
|
|
|
// Making first/last works only on current desktop, don't affect all desktops
|
|
|
|
if (it.key() == m_currentDesktop
|
2022-03-23 10:13:38 +00:00
|
|
|
&& (change == MakeFirst || change == MakeLast)) {
|
2012-11-20 16:26:50 +00:00
|
|
|
if (change == MakeFirst) {
|
2022-04-23 08:33:23 +00:00
|
|
|
makeFirstInChain(window, chain);
|
2012-11-20 16:26:50 +00:00
|
|
|
} else {
|
2022-04-23 08:33:23 +00:00
|
|
|
makeLastInChain(window, chain);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-23 08:33:23 +00:00
|
|
|
insertWindowIntoChain(window, chain);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Now only on desktop, remove it anywhere else
|
2018-07-27 19:43:09 +00:00
|
|
|
for (auto it = m_desktopFocusChains.begin();
|
2022-03-23 10:13:38 +00:00
|
|
|
it != m_desktopFocusChains.end();
|
|
|
|
++it) {
|
2015-03-05 09:24:54 +00:00
|
|
|
auto &chain = it.value();
|
2022-04-23 08:33:23 +00:00
|
|
|
if (window->isOnDesktop(it.key())) {
|
|
|
|
updateWindowInChain(window, change, chain);
|
2012-11-20 16:26:50 +00:00
|
|
|
} else {
|
2022-04-23 08:33:23 +00:00
|
|
|
chain.removeAll(window);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add for most recently used chain
|
2022-04-23 08:33:23 +00:00
|
|
|
updateWindowInChain(window, change, m_mostRecentlyUsed);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 08:33:23 +00:00
|
|
|
void FocusChain::updateWindowInChain(Window *window, FocusChain::Change change, Chain &chain)
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
|
|
|
if (change == MakeFirst) {
|
2022-04-23 08:33:23 +00:00
|
|
|
makeFirstInChain(window, chain);
|
2012-11-20 16:26:50 +00:00
|
|
|
} else if (change == MakeLast) {
|
2022-04-23 08:33:23 +00:00
|
|
|
makeLastInChain(window, chain);
|
2012-11-20 16:26:50 +00:00
|
|
|
} else {
|
2022-04-23 08:33:23 +00:00
|
|
|
insertWindowIntoChain(window, chain);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 08:33:23 +00:00
|
|
|
void FocusChain::insertWindowIntoChain(Window *window, Chain &chain)
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2022-04-23 08:33:23 +00:00
|
|
|
if (chain.contains(window)) {
|
2012-11-20 16:26:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-23 08:33:23 +00:00
|
|
|
if (m_activeWindow && m_activeWindow != window && !chain.empty() && chain.last() == m_activeWindow) {
|
|
|
|
// Add it after the active window
|
|
|
|
chain.insert(chain.size() - 1, window);
|
2012-11-20 16:26:50 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise add as the first one
|
2022-04-23 08:33:23 +00:00
|
|
|
chain.append(window);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 08:33:23 +00:00
|
|
|
void FocusChain::moveAfterWindow(Window *window, Window *reference)
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2022-04-23 08:33:23 +00:00
|
|
|
if (!window->wantsTabFocus()) {
|
2012-11-20 16:26:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-27 19:43:09 +00:00
|
|
|
for (auto it = m_desktopFocusChains.begin();
|
2022-03-23 10:13:38 +00:00
|
|
|
it != m_desktopFocusChains.end();
|
|
|
|
++it) {
|
2022-04-23 08:33:23 +00:00
|
|
|
if (!window->isOnDesktop(it.key())) {
|
2012-11-20 16:26:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-04-23 08:33:23 +00:00
|
|
|
moveAfterWindowInChain(window, reference, it.value());
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
2022-04-23 08:33:23 +00:00
|
|
|
moveAfterWindowInChain(window, reference, m_mostRecentlyUsed);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 08:33:23 +00:00
|
|
|
void FocusChain::moveAfterWindowInChain(Window *window, Window *reference, Chain &chain)
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
|
|
|
if (!chain.contains(reference)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-23 08:33:23 +00:00
|
|
|
if (Window::belongToSameApplication(reference, window)) {
|
|
|
|
chain.removeAll(window);
|
|
|
|
chain.insert(chain.indexOf(reference), window);
|
2012-11-20 16:26:50 +00:00
|
|
|
} else {
|
2022-04-23 08:33:23 +00:00
|
|
|
chain.removeAll(window);
|
2012-11-20 16:26:50 +00:00
|
|
|
for (int i = chain.size() - 1; i >= 0; --i) {
|
2022-04-22 17:39:12 +00:00
|
|
|
if (Window::belongToSameApplication(reference, chain.at(i))) {
|
2022-04-23 08:33:23 +00:00
|
|
|
chain.insert(i, window);
|
2012-11-20 16:26:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:39:12 +00:00
|
|
|
Window *FocusChain::firstMostRecentlyUsed() const
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
|
|
|
if (m_mostRecentlyUsed.isEmpty()) {
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
return nullptr;
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
return m_mostRecentlyUsed.first();
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:39:12 +00:00
|
|
|
Window *FocusChain::nextMostRecentlyUsed(Window *reference) const
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
|
|
|
if (m_mostRecentlyUsed.isEmpty()) {
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
return nullptr;
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
const int index = m_mostRecentlyUsed.indexOf(reference);
|
2014-11-06 21:58:53 +00:00
|
|
|
if (index == -1) {
|
|
|
|
return m_mostRecentlyUsed.first();
|
|
|
|
}
|
|
|
|
if (index == 0) {
|
2012-11-20 16:26:50 +00:00
|
|
|
return m_mostRecentlyUsed.last();
|
|
|
|
}
|
|
|
|
return m_mostRecentlyUsed.at(index - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// copied from activation.cpp
|
2022-04-22 17:39:12 +00:00
|
|
|
bool FocusChain::isUsableFocusCandidate(Window *c, Window *prev) const
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2022-03-23 10:13:38 +00:00
|
|
|
return c != prev && !c->isShade() && c->isShown() && c->isOnCurrentDesktop() && c->isOnCurrentActivity() && (!m_separateScreenFocus || c->isOnOutput(prev ? prev->output() : workspace()->activeOutput()));
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 17:39:12 +00:00
|
|
|
Window *FocusChain::nextForDesktop(Window *reference, VirtualDesktop *desktop) const
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2018-07-27 19:43:09 +00:00
|
|
|
auto it = m_desktopFocusChains.constFind(desktop);
|
|
|
|
if (it == m_desktopFocusChains.constEnd()) {
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
return nullptr;
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
2015-03-05 09:24:54 +00:00
|
|
|
const auto &chain = it.value();
|
2012-11-20 16:26:50 +00:00
|
|
|
for (int i = chain.size() - 1; i >= 0; --i) {
|
2022-04-23 08:33:23 +00:00
|
|
|
auto window = chain.at(i);
|
|
|
|
if (isUsableFocusCandidate(window, reference)) {
|
|
|
|
return window;
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
return nullptr;
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 08:33:23 +00:00
|
|
|
void FocusChain::makeFirstInChain(Window *window, Chain &chain)
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2022-04-23 08:33:23 +00:00
|
|
|
chain.removeAll(window);
|
2020-10-20 20:15:46 +00:00
|
|
|
if (options->moveMinimizedWindowsToEndOfTabBoxFocusChain()) {
|
2022-04-23 08:33:23 +00:00
|
|
|
if (window->isMinimized()) { // add it before the first minimized ...
|
2022-03-23 10:13:38 +00:00
|
|
|
for (int i = chain.count() - 1; i >= 0; --i) {
|
2020-10-20 20:15:46 +00:00
|
|
|
if (chain.at(i)->isMinimized()) {
|
2022-04-23 08:33:23 +00:00
|
|
|
chain.insert(i + 1, window);
|
2020-10-20 20:15:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-20 20:05:33 +00:00
|
|
|
}
|
2022-04-23 08:33:23 +00:00
|
|
|
chain.prepend(window); // ... or at end of chain
|
2020-10-20 20:15:46 +00:00
|
|
|
} else {
|
2022-04-23 08:33:23 +00:00
|
|
|
chain.append(window);
|
2020-10-20 20:05:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-23 08:33:23 +00:00
|
|
|
chain.append(window);
|
2020-10-20 20:05:33 +00:00
|
|
|
}
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 08:33:23 +00:00
|
|
|
void FocusChain::makeLastInChain(Window *window, Chain &chain)
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2022-04-23 08:33:23 +00:00
|
|
|
chain.removeAll(window);
|
|
|
|
chain.prepend(window);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 08:33:23 +00:00
|
|
|
bool FocusChain::contains(Window *window, VirtualDesktop *desktop) const
|
2012-11-20 16:26:50 +00:00
|
|
|
{
|
2018-07-27 19:43:09 +00:00
|
|
|
auto it = m_desktopFocusChains.constFind(desktop);
|
|
|
|
if (it == m_desktopFocusChains.constEnd()) {
|
2012-11-20 16:26:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-04-23 08:33:23 +00:00
|
|
|
return it.value().contains(window);
|
2012-11-20 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|