-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
181 lines (144 loc) · 5.72 KB
/
extension.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
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
//
// Title: [email protected]
// Version: 0.0.1
// File: extension.js
// Description: Gnome shell extension to search through active windows.
// Search is done by application name and window title and integrates
// nicely into the search display
// Todo: Add live previews of windows and optimize
//
// Copyright (C) 2011 - Marcus Dillavou <[email protected]>
//
// 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 2, 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, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
const St = imports.gi.St;
const Mainloop = imports.mainloop;
const Gettext = imports.gettext.domain('gnome-shell');
const _ = Gettext.gettext;
const Shell = imports.gi.Shell;
const Main = imports.ui.main;
const Search = imports.ui.search;
function WindowSearchProvider() {
this._init();
}
WindowSearchProvider.prototype = {
__proto__: Search.SearchProvider.prototype,
_init: function() {
Search.SearchProvider.prototype._init.call(this, _("WINDOWS"));
},
getResultMeta: function(resultId) {
let apps = this.getRunningApps();
for (let i = 0; i < apps.length; i++) {
let app = apps[i];
let windows = app.get_windows();
for (let j = 0; j < windows.length; j++) {
let window = windows[j];
let title = app.get_name() + ' - ' + window.get_title();
if (resultId == title) {
return { 'id': resultId,
'name': window.get_title(),
'createIcon': function(size) {
return app.create_icon_texture(size);
}
};
}
}
}
// !mwd - should never get here!
return { 'id': resultId,
'name': resultId,
'createIcon': function(size) {
return null;
}
};
},
activateResult: function(id, params) {
let apps = this.getRunningApps();
for (let i = 0; i < apps.length; i++) {
let app = apps[i];
let windows = app.get_windows();
for (let j = 0; j < windows.length; j++) {
let window = windows[j];
let title = app.get_name() + ' - ' + window.get_title();
if (id == title) {
// !mwd - we do this manually instead of calling
// Main.activateWindow(window) because activateWindow
// toggles the overview when it shouldn't and causes
// weird focus and keyboard issues
//Main.activateWindow(window);
let activeWorkspaceNum = global.screen.get_active_workspace_index();
let windowWorkspaceNum = window.get_workspace().index();
time = global.get_current_time();
if (windowWorkspaceNum != activeWorkspaceNum) {
let workspace = global.screen.get_workspace_by_index(windowWorkspaceNum);
workspace.activate_with_focus(window, time);
} else {
window.activate(time);
}
}
}
}
},
getInitialResultSet: function(terms) {
let results = [];
let apps = this.getRunningApps();
terms = terms.map(String.toLowerCase);
if (!apps.length)
return results;
for (let i = 0; i < apps.length; i++) {
let app = apps[i];
let windows = app.get_windows();
for (let j = 0; j < windows.length; j++) {
let window = windows[j];
let mtype = Search.MatchType.NONE;
let title = app.get_name() + ' - ' + window.get_title();
let titleLower = String.toLowerCase(title);
for (let k = 0; k < terms.length; k++) {
let idx = titleLower.indexOf(terms[k]);
if (idx == 0) {
mtype = Search.MatchType.PREFIX;
} else if (idx > 0) {
if (mtype == Search.MatchType.NONE)
mtype = Search.MatchType.SUBSTRING;
} else {
mtype = Search.MatchType.NONE;
break;
}
}
if (mtype != Search.MatchType.NONE) {
results.push(title);
}
}
}
return results;
},
getSubsearchResultSet: function(previousResults, terms) {
//!mwd - not too effecient here!
return this.getInitialResultSet(terms);
},
getRunningApps: function() {
return Shell.AppSystem.get_default().get_running();
}
};
let searchProvider;
function init(extensionMeta) {
searchProvider = new WindowSearchProvider();
}
function enable() {
Main.overview._viewSelector.addSearchProvider(searchProvider);
}
function disable() {
Main.overview._viewSelector.removeSearchProvider(searchProvider);
}