forked from zpydr/gnome-shell-extension-taskbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows34.js
194 lines (170 loc) · 6.09 KB
/
windows34.js
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
// GNOME Shell Extension TaskBar
// Copyright (C) 2014 zpydr
//
// Version 40
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// zpydr@openmailbox.org
const Lang = imports.lang;
function Windows(callBackThis, callbackWindowsListChanged, callbackWindowChanged)
{
this.init(callBackThis, callbackWindowsListChanged, callbackWindowChanged);
}
Windows.prototype =
{
workspace: null,
windowsList: new Array(),
callBackThis: null,
callbackWindowsListChanged: null,
callbackWindowChanged: null,
workspaceSwitchSignal: null,
windowAddedSignal: null,
windowRemovedSignal: null,
windowsSignals: new Array(),
init: function(callBackThis, callbackWindowsListChanged, callbackWindowChanged)
{
//Set User Callback
this.callBackThis = callBackThis;
this.callbackWindowsListChanged = callbackWindowsListChanged;
this.callbackWindowChanged = callbackWindowChanged;
//Init WindowsList
this.onWorkspaceChanged();
//Add window manager signals
this.workspaceSwitchSignal = global.screen.connect('workspace-switched', Lang.bind(this, this.onWorkspaceChanged));
},
destruct: function()
{
//Remove window manager signals
global.screen.disconnect(this.workspaceSwitchSignal);
//Remove workspace signals
this.workspace.disconnect(this.windowAddedSignal);
this.workspace.disconnect(this.windowRemovedSignal);
//Clean windows list
this.cleanWindowsList();
},
onWorkspaceChanged: function()
{
//Remove workspace signals if necessary
if (this.windowAddedSignal != null)
this.workspace.disconnect(this.windowAddedSignal);
if (this.windowRemovedSignal != null)
this.workspace.disconnect(this.windowRemovedSignal);
//Clean windows list
this.cleanWindowsList();
//Get current workspace
this.workspace = global.screen.get_active_workspace();
//Build windows list
this.workspace.list_windows().reverse().forEach(
function(window)
{
this.addWindowInList(window);
},
this
);
//Call User Callback
this.callbackWindowsListChanged.call(this.callBackThis, this.windowsList, 0, null);
//Add workspace signals
this.windowAddedSignal = this.workspace.connect_after('window-added', Lang.bind(this, this.onWindowAdded));
this.windowRemovedSignal = this.workspace.connect_after('window-removed', Lang.bind(this, this.onWindowRemoved));
},
onWindowChanged: function(window, object, type)
{
if (type == 0) //Focus changed
{
if (window.appears_focused)
this.callbackWindowChanged.call(this.callBackThis, window, 0);
}
else if (type == 1) //Title changed
this.callbackWindowChanged.call(this.callBackThis, window, 1);
else if (type == 2) //Minimized
this.callbackWindowChanged.call(this.callBackThis, window, 2);
},
onWindowAdded: function(workspace, window)
{
if (this.addWindowInList(window))
this.callbackWindowsListChanged.call(this.callBackThis, this.windowsList, 1, window);
},
onWindowRemoved: function(workspace, window)
{
if (this.removeWindowInList(window))
this.callbackWindowsListChanged.call(this.callBackThis, this.windowsList, 2, window);
},
searchWindowInList: function(window)
{
let index = null;
for (let indexWindow in this.windowsList)
{
if (this.windowsList[indexWindow] == window)
{
index = indexWindow;
break;
}
}
return index;
},
addWindowInList: function(window)
{
let index = this.searchWindowInList(window);
if (index == null && ! window.is_skip_taskbar())
{
this.windowsList.push(window);
//Add window signals
let objectAndSignals = [
window, [
window.connect('notify::appears-focused', Lang.bind(this, this.onWindowChanged, 0)),
window.connect('notify::title', Lang.bind(this, this.onWindowChanged, 1)),
window.connect('notify::minimized', Lang.bind(this, this.onWindowChanged, 2))
]
];
this.windowsSignals.push(objectAndSignals);
return true;
}
else
return false;
},
removeWindowInList: function(window)
{
let index = this.searchWindowInList(window);
if (index != null)
{
this.windowsList.splice(index, 1);
//Remove window signals
for (let indexSignal in this.windowsSignals)
{
let [object, signals] = this.windowsSignals[indexSignal];
if (object == window)
{
signals.forEach(
function(signal)
{
object.disconnect(signal);
},
this
);
this.windowsSignals.splice(indexSignal, 1);
break;
}
}
return true;
}
else
return false;
},
cleanWindowsList: function()
{
for (let i = this.windowsList.length -1 ; i>=0 ; i--)
this.removeWindowInList(this.windowsList[i]);
}
}