-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
296 lines (223 loc) · 8.5 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
//
// Web Search is a GNOME Shell extension to search by a click.
// Copyright (C) 2011 Stefano Ciancio
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
const ExtensionUtils = imports.misc.extensionUtils;
const Lang = imports.lang;
const Shell = imports.gi.Shell;
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk
const Gdk = imports.gi.Gdk;
const GLib = imports.gi.GLib;
const St = imports.gi.St;
const Main = imports.ui.main;
const Util = imports.misc.util;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Meta = imports.gi.Meta;
const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
const Prefs = Me.imports.prefs;
const Gettext = imports.gettext.domain('gnome-shell-websearch');
const _ = Gettext.gettext;
const PREFS_DIALOG = 'gnome-shell-extension-prefs [email protected]'
let settings = null;
const SearchButton = new Lang.Class({
Name: 'SearchButton',
Extends: PanelMenu.Button,
_init: function() {
this.parent(0.0, "Search Button");
this._box = new St.BoxLayout();
this._icon = new St.Icon({ gicon: Gio.icon_new_for_string('websearch-symbolic'),
icon_size: 15 });
this._bin = new St.Bin({child: this._icon});
this._box.add(this._bin);
this.actor.add_actor(this._box);
this.actor.add_style_class_name('panel-status-button');
this._clipboard = St.Clipboard.get_default();
this._fromTextBox = false;
// Entry
this.item = new PopupMenu.PopupBaseMenuItem({ activate: false });
this._inputF = new St.Entry(
{
name: "searchEntry",
hint_text: _("Text to search ..."),
track_hover: true,
can_focus: true,
});
this.item.actor.add(this._inputF, { expand: true });
this.menu.addMenuItem(this.item, 1)
this._inputF.clutter_text.connect('activate', Lang.bind(this, this._webSearchFromTextBox));
// Return action on input
this.actor.connect("button-press-event", Lang.bind(this, this._onKeyPress));
// Update search engine label
this._addSearchButton();
// Separator
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem(), 2);
// Setting Menu
let menuPref = new PopupMenu.PopupMenuItem("WebSearch Settings");
menuPref.connect('activate', this._LaunchExtensionPrefs);
this.menu.addMenuItem(menuPref, 3);
},
_LaunchExtensionPrefs: function() {
Util.spawn(["gnome-shell-extension-prefs", Me.metadata.uuid]);
},
/*
* Add Search Button with engine label
*/
_addSearchButton: function() {
// Destroy search button
if (this._searchButton) {this._searchButton.destroy(); }
let engine = Convenience.getSearchEngine()[settings.get_int('ws-searchengine')][2]
this._searchButton = new PopupMenu.PopupMenuItem(engine + " ...");
this._searchButton.connect('activate', Lang.bind(this, this._webSearchFromTextBox));
this.menu.addMenuItem(this._searchButton, 1);
},
/*
* Action on click search icon
*/
_onKeyPress: function(actor, event) {
let button = event.get_button();
// Update search engine label
this._addSearchButton();
let baction;
if (settings.get_boolean('ws-switch-button')) {
baction = !(button === 1);
} else {
baction = (button === 1);
}
if ( baction ) { // Search
this._webSearch();
return;
} else { // Menu open
// Focus on this._inputF
this._inputF.grab_key_focus();
// Select all
this._inputF.clutter_text.set_cursor_visible(true);
this._inputF.clutter_text.set_selection(-1, 0);
}
return;
},
/*
* Callback clipboard get_text and search
*/
_getTextCallbackandSearch: function(clipboard, text) {
this.menu.close();
if (this._fromTextBox) {
text = this._inputF.clutter_text.get_text();
} else {
if (text != null)
this._inputF.set_text(text);
}
if (text == '') {
Main.notify("Nothing in clipboard");
return;
}
let searchurl = Convenience.getSearchEngine()[settings.get_int('ws-searchengine')][3] + text;
Gtk.show_uri(null, searchurl, Gdk.CURRENT_TIME);
},
/*
* Websearch function
*/
_webSearch: function() {
this._fromTextBox = false;
let Typology;
if (settings.get_boolean('ws-clipboard')) {
Typology = St.ClipboardType.PRIMARY
} else {
Typology = St.ClipboardType.CLIPBOARD
}
this._clipboard.get_text(Typology, Lang.bind(this, this._getTextCallbackandSearch));
return true;
},
/*
* Websearch function - priority to text box content
*/
_webSearchFromTextBox: function() {
this._fromTextBox = true;
this._clipboard.get_text(St.ClipboardType.CLIPBOARD, Lang.bind(this, this._getTextCallbackandSearch));
return true;
},
enable: function() {
Main.wm.addKeybinding(
Prefs.SHOWTEXTBOX_SHORTCUT_KEY,
settings,
Meta.KeyBindingFlags.NONE,
Shell.KeyBindingMode.NORMAL |
Shell.KeyBindingMode.MESSAGE_TRAY |
Shell.KeyBindingMode.OVERVIEW,
Lang.bind(this, function () {
this.menu.open();
// Focus on this._inputF
this._inputF.grab_key_focus();
// Select all
this._inputF.clutter_text.set_cursor_visible(true);
this._inputF.clutter_text.set_selection(-1, 0);
})
);
Main.wm.addKeybinding(
Prefs.SEARCH_SHORTCUT_KEY,
settings,
Meta.KeyBindingFlags.NONE,
Shell.KeyBindingMode.NORMAL |
Shell.KeyBindingMode.MESSAGE_TRAY |
Shell.KeyBindingMode.OVERVIEW,
Lang.bind(this, this._webSearch)
);
Main.wm.addKeybinding(
Prefs.SHIFTSEARCHENGINE_SHORTCUT_KEY,
settings,
Meta.KeyBindingFlags.NONE,
Shell.KeyBindingMode.NORMAL |
Shell.KeyBindingMode.MESSAGE_TRAY |
Shell.KeyBindingMode.OVERVIEW,
Lang.bind(this, function () {
let new_search_engine_index = (settings.get_int('ws-searchengine') + 1) %
Convenience.getSearchEngine().length;
settings.set_int('ws-searchengine', new_search_engine_index);
Main.notify("Websearch: search engine changed in \"" +
Convenience.getSearchEngine()[settings.get_int('ws-searchengine')][1] + "\"");
})
);
},
disable: function() {
Main.wm.removeKeybinding(Prefs.SHOWTEXTBOX_SHORTCUT_KEY);
Main.wm.removeKeybinding(Prefs.SEARCH_SHORTCUT_KEY);
Main.wm.removeKeybinding(Prefs.SHIFTSEARCHENGINE_SHORTCUT_KEY);
},
});
let _indicator;
function init(extensionMeta) {
let theme = imports.gi.Gtk.IconTheme.get_default();
theme.append_search_path(extensionMeta.path + "/images/");
}
function enable() {
settings = Convenience.getSettings();
if (!_indicator) {
_indicator = new SearchButton();
_indicator.enable();
Main.panel.addToStatusArea('web-search', _indicator);
}
}
function disable() {
if (_indicator) {
_indicator.disable();
_indicator.destroy();
_indicator = null;
}
settings = null;
}