-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlink_preview_dialog.js
325 lines (278 loc) · 9.5 KB
/
link_preview_dialog.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
const St = imports.gi.St;
const Lang = imports.lang;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Clutter = imports.gi.Clutter;
const Main = imports.ui.main;
const Animation = imports.ui.animation;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const PrefsKeys = Me.imports.prefs_keys;
const PopupDialog = Me.imports.popup_dialog;
const LinkPreviewers = Me.imports.link_previewers;
const URI_TYPES = {
PATH: 0,
URL: 1,
NOTE: 2,
UNDEFINED: 3
};
const StatusBox = new Lang.Class({
Name: 'StatusBox',
_init: function() {
this.actor = new St.BoxLayout();
this._label = new St.Label({
text: 'Loading...'
});
let spinner_icon = Gio.File.new_for_uri(
'resource:///org/gnome/shell/theme/process-working.svg'
);
this._spinner = new Animation.AnimatedIcon(spinner_icon, 24);
this.actor.add_child(this._spinner.actor);
this.actor.add_child(this._label);
this.actor.hide();
},
show: function() {
this._spinner.play();
this.actor.show();
},
hide: function() {
this._spinner.stop();
this.actor.hide();
},
destroy: function() {
this.actor.destroy();
}
});
const LinkPreviewDialog = new Lang.Class({
Name: 'LinkPreviewDialog',
Extends: PopupDialog.PopupDialog,
_init: function() {
this.parent();
this._box = new St.BoxLayout({
style_class: 'note-links-preview-dialog'
});
this._status_box = new StatusBox();
this._box.add_child(this._status_box.actor);
this._previewer = null;
this.actor.add_child(this._box);
this.actor.z_position = 0.1;
this.connect('hided', Lang.bind(this, this.clear));
},
_on_captured_event: function(object, event) {
if(event.type() === Clutter.EventType.MOTION) {
this._reposition();
}
this.parent(object, event);
},
_reposition: function() {
let [x, y] = global.get_pointer();
let cursor_indent = 10;
let margin = 20;
let monitor = Main.layoutManager.currentMonitor;
let available_width =
(monitor.width + monitor.x) - x - cursor_indent - margin;
let available_height =
(monitor.height + monitor.y) - y - cursor_indent - margin;
let offset_x = 10;
let offset_y = 10;
if(this.actor.width > available_width) {
offset_x =
(monitor.width + monitor.x) - (this.actor.width + x + margin);
}
if(this.actor.height > available_height) {
offset_y =
(monitor.height + monitor.y) - (this.actor.height + y + margin);
}
let dialog_x = x + cursor_indent + offset_x;
let dialog_y = y + cursor_indent + offset_y;
if(x > dialog_x && y > dialog_y) {
dialog_x = x - this.actor.width - cursor_indent;
}
this.actor.x = dialog_x;
this.actor.y = dialog_y;
},
_is_url: function(str) {
let url_regexp = imports.misc.util._urlRegexp;
return str.match(url_regexp) !== null;
},
_get_uri_for_note_link: function(link) {
let uri, type;
if(Utils.get_client().is_valid_uri(link)) {
uri = link;
type = URI_TYPES.NOTE;
}
else if(this._is_url(link)) {
type = URI_TYPES.URL;
if(link.indexOf(':') === -1) {
uri = 'http://' + link;
}
else {
uri = link;
}
}
else if(Utils.starts_with(link, '/') || Utils.starts_with(link, '~')) {
link = Utils.expand_path(link);
uri = 'file://' + link;
type = URI_TYPES.PATH;
}
else {
uri = link;
type = URI_TYPES.UNDEFINED;
}
return [uri, type];
},
_preview_file: function(uri, uri_type) {
function on_query_complete(object, res) {
let info;
try {
info = object.query_info_finish(res);
}
catch(e) {
log('LinkPreviewDialog:_preview_file(): %s'.format(e));
this._show_message(e.message);
return;
}
let content_type = info.get_content_type();
let thumbnail_path = info.get_attribute_byte_string('thumbnail::path');
let previewer;
if(
Utils.starts_with(content_type, 'image')
&& Utils.SETTINGS.get_boolean(PrefsKeys.PREVIEW_IMAGES_KEY)
) {
let size = info.get_size();
let max_local_size = Utils.SETTINGS.get_int(
PrefsKeys.PREVIEW_MAX_LOCAL_SIZE_KB_KEY
) * 1024;
let max_remote_size = Utils.SETTINGS.get_int(
PrefsKeys.PREVIEW_MAX_NET_SIZE_KB_KEY
) * 1024;
let dont_preview_local =
uri_type === URI_TYPES.PATH
&& max_local_size !== 0
&& size > max_local_size;
let dont_preview_net =
uri_type === URI_TYPES.URL
&& max_remote_size !== 0
&& size > max_remote_size;
if(dont_preview_local || dont_preview_net) {
this._show_message(
'The size of the image(%s) is '.format(
GLib.format_size(size)
) +
'larger than the limit(%s)'.format(
GLib.format_size(
dont_preview_local
? max_local_size
: max_remote_size
)
)
);
return;
}
else {
previewer = LinkPreviewers.ImagePreviewer;
}
}
else if(
content_type === 'text/html'
&& Utils.SETTINGS.get_boolean(PrefsKeys.PREVIEW_WEBPAGES_KEY)
) {
previewer = LinkPreviewers.WebpagePreviewer;
}
else if(
!Utils.starts_with(content_type, 'image')
&& content_type !== 'text/html'
&& thumbnail_path !== null
&& Utils.SETTINGS.get_boolean(PrefsKeys.PREVIEW_FILES_KEY)
) {
uri = 'file://' + thumbnail_path;
previewer = LinkPreviewers.ImagePreviewer;
}
else {
this._show_message('No preview');
return;
}
this._show_previewer(uri, previewer);
}
let file = Gio.file_new_for_uri(uri);
file.query_info_async(
'standard::content-type,standard::size,thumbnail::path',
Gio.FileQueryInfoFlags.NONE,
GLib.PRIORITY_DEFAULT,
null,
Lang.bind(this, on_query_complete)
);
},
_show_message: function(message) {
this.clear();
this._previewer = new LinkPreviewers.MessagePreviewer(message, {
max_width: Utils.SETTINGS.get_int(PrefsKeys.PREVIEW_MAX_WIDTH_KEY),
max_height: Utils.SETTINGS.get_int(PrefsKeys.PREVIEW_MAX_HEIGHT_KEY)
});
this.show();
this._previewer.load(Lang.bind(this, function(ok) {
if(ok !== true) {
this.hide();
return;
}
this._status_box.hide();
this._box.add_child(this._previewer.actor);
this._reposition();
}));
},
_show_previewer: function(uri, previewer) {
if(this._previewer !== null) this.clear();
this._previewer = new previewer(uri, {
max_width: Utils.SETTINGS.get_int(PrefsKeys.PREVIEW_MAX_WIDTH_KEY),
max_height: Utils.SETTINGS.get_int(PrefsKeys.PREVIEW_MAX_HEIGHT_KEY)
});
this.show();
this._status_box.show();
this._previewer.load(Lang.bind(this, function(ok) {
if(ok !== true) {
this.hide();
return;
}
this._status_box.hide();
this._box.add_child(this._previewer.actor);
this._reposition();
}));
},
preview: function(link) {
link = link.trim();
let [uri, type] = this._get_uri_for_note_link(link);
this.show();
this._status_box.show();
if(
type === URI_TYPES.NOTE
&& Utils.SETTINGS.get_boolean(PrefsKeys.PREVIEW_NOTES_KEY)
) {
this._show_previewer(uri, LinkPreviewers.NotePreviewer);
}
else if(type === URI_TYPES.UNDEFINED) {
this._show_message('No preview');
}
else if(type !== URI_TYPES.NOTE) {
if(
Utils.SETTINGS.get_boolean(PrefsKeys.PREVIEW_ONLY_LOCAL_KEY)
&& type === URI_TYPES.URL
) {
this._show_message('Preview is disabled for remote images');
}
else {
this._preview_file(uri, type);
}
}
},
clear: function() {
if(this._previewer !== null) {
this._previewer.destroy();
this._previewer = null;
}
this._status_box.hide();
},
hide: function() {
this.parent();
}
});