-
Notifications
You must be signed in to change notification settings - Fork 6
/
focuschain.cpp
262 lines (236 loc) · 7.38 KB
/
focuschain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
KWin - the KDE window manager
This file is part of the KDE project.
SPDX-FileCopyrightText: 2012 Martin Gräßlin <mgraesslin@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "focuschain.h"
#include "abstract_client.h"
#include "screens.h"
namespace KWin
{
KWIN_SINGLETON_FACTORY_VARIABLE(FocusChain, s_manager)
FocusChain::FocusChain(QObject *parent)
: QObject(parent)
, m_separateScreenFocus(false)
, m_activeClient(nullptr)
, m_currentDesktop(0)
{
}
FocusChain::~FocusChain()
{
s_manager = nullptr;
}
void FocusChain::remove(AbstractClient *client)
{
for (auto it = m_desktopFocusChains.begin();
it != m_desktopFocusChains.end();
++it) {
it.value().removeAll(client);
}
m_mostRecentlyUsed.removeAll(client);
}
void FocusChain::resize(uint previousSize, uint newSize)
{
for (uint i = previousSize + 1; i <= newSize; ++i) {
m_desktopFocusChains.insert(i, Chain());
}
for (uint i = previousSize; i > newSize; --i) {
m_desktopFocusChains.remove(i);
}
}
AbstractClient *FocusChain::getForActivation(uint desktop) const
{
return getForActivation(desktop, screens()->current());
}
AbstractClient *FocusChain::getForActivation(uint desktop, int screen) const
{
auto it = m_desktopFocusChains.constFind(desktop);
if (it == m_desktopFocusChains.constEnd()) {
return nullptr;
}
const auto &chain = it.value();
for (int i = chain.size() - 1; i >= 0; --i) {
auto tmp = chain.at(i);
// TODO: move the check into Client
if (tmp->isShown(false) && tmp->isOnCurrentActivity()
&& ( !m_separateScreenFocus || tmp->screen() == screen)) {
return tmp;
}
}
return nullptr;
}
void FocusChain::update(AbstractClient *client, FocusChain::Change change)
{
if (!client->wantsTabFocus()) {
// Doesn't want tab focus, remove
remove(client);
return;
}
if (client->isOnAllDesktops()) {
// Now on all desktops, add it to focus chains it is not already in
for (auto it = m_desktopFocusChains.begin();
it != m_desktopFocusChains.end();
++it) {
auto &chain = it.value();
// Making first/last works only on current desktop, don't affect all desktops
if (it.key() == m_currentDesktop
&& (change == MakeFirst || change == MakeLast)) {
if (change == MakeFirst) {
makeFirstInChain(client, chain);
} else {
makeLastInChain(client, chain);
}
} else {
insertClientIntoChain(client, chain);
}
}
} else {
// Now only on desktop, remove it anywhere else
for (auto it = m_desktopFocusChains.begin();
it != m_desktopFocusChains.end();
++it) {
auto &chain = it.value();
if (client->isOnDesktop(it.key())) {
updateClientInChain(client, change, chain);
} else {
chain.removeAll(client);
}
}
}
// add for most recently used chain
updateClientInChain(client, change, m_mostRecentlyUsed);
}
void FocusChain::updateClientInChain(AbstractClient *client, FocusChain::Change change, Chain &chain)
{
if (change == MakeFirst) {
makeFirstInChain(client, chain);
} else if (change == MakeLast) {
makeLastInChain(client, chain);
} else {
insertClientIntoChain(client, chain);
}
}
void FocusChain::insertClientIntoChain(AbstractClient *client, Chain &chain)
{
if (chain.contains(client)) {
return;
}
if (m_activeClient && m_activeClient != client &&
!chain.empty() && chain.last() == m_activeClient) {
// Add it after the active client
chain.insert(chain.size() - 1, client);
} else {
// Otherwise add as the first one
chain.append(client);
}
}
void FocusChain::moveAfterClient(AbstractClient *client, AbstractClient *reference)
{
if (!client->wantsTabFocus()) {
return;
}
for (auto it = m_desktopFocusChains.begin();
it != m_desktopFocusChains.end();
++it) {
if (!client->isOnDesktop(it.key())) {
continue;
}
moveAfterClientInChain(client, reference, it.value());
}
moveAfterClientInChain(client, reference, m_mostRecentlyUsed);
}
void FocusChain::moveAfterClientInChain(AbstractClient *client, AbstractClient *reference, Chain &chain)
{
if (!chain.contains(reference)) {
return;
}
if (AbstractClient::belongToSameApplication(reference, client)) {
chain.removeAll(client);
chain.insert(chain.indexOf(reference), client);
} else {
chain.removeAll(client);
for (int i = chain.size() - 1; i >= 0; --i) {
if (AbstractClient::belongToSameApplication(reference, chain.at(i))) {
chain.insert(i, client);
break;
}
}
}
}
AbstractClient *FocusChain::firstMostRecentlyUsed() const
{
if (m_mostRecentlyUsed.isEmpty()) {
return nullptr;
}
return m_mostRecentlyUsed.first();
}
AbstractClient *FocusChain::nextMostRecentlyUsed(AbstractClient *reference) const
{
if (m_mostRecentlyUsed.isEmpty()) {
return nullptr;
}
const int index = m_mostRecentlyUsed.indexOf(reference);
if (index == -1) {
return m_mostRecentlyUsed.first();
}
if (index == 0) {
return m_mostRecentlyUsed.last();
}
return m_mostRecentlyUsed.at(index - 1);
}
// copied from activation.cpp
bool FocusChain::isUsableFocusCandidate(AbstractClient *c, AbstractClient *prev) const
{
return c != prev &&
c->isShown(false) && c->isOnCurrentDesktop() && c->isOnCurrentActivity() &&
(!m_separateScreenFocus || c->isOnScreen(prev ? prev->screen() : screens()->current()));
}
AbstractClient *FocusChain::nextForDesktop(AbstractClient *reference, uint desktop) const
{
auto it = m_desktopFocusChains.constFind(desktop);
if (it == m_desktopFocusChains.constEnd()) {
return nullptr;
}
const auto &chain = it.value();
for (int i = chain.size() - 1; i >= 0; --i) {
auto client = chain.at(i);
if (isUsableFocusCandidate(client, reference)) {
return client;
}
}
return nullptr;
}
void FocusChain::makeFirstInChain(AbstractClient *client, Chain &chain)
{
chain.removeAll(client);
if (options->moveMinimizedWindowsToEndOfTabBoxFocusChain()) {
if (client->isMinimized()) { // add it before the first minimized ...
for (int i = chain.count()-1; i >= 0; --i) {
if (chain.at(i)->isMinimized()) {
chain.insert(i+1, client);
return;
}
}
chain.prepend(client); // ... or at end of chain
} else {
chain.append(client);
}
} else {
chain.append(client);
}
}
void FocusChain::makeLastInChain(AbstractClient *client, Chain &chain)
{
chain.removeAll(client);
chain.prepend(client);
}
bool FocusChain::contains(AbstractClient *client, uint desktop) const
{
auto it = m_desktopFocusChains.constFind(desktop);
if (it == m_desktopFocusChains.constEnd()) {
return false;
}
return it.value().contains(client);
}
} // namespace