-
Notifications
You must be signed in to change notification settings - Fork 3
/
extension.js
988 lines (834 loc) · 34 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
const Clutter = imports.gi.Clutter;
const Config = imports.misc.config;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Lang = imports.lang;
const Mainloop = imports.mainloop;
const Meta = imports.gi.Meta;
const Shell = imports.gi.Shell;
const St = imports.gi.St;
const PolicyType = imports.gi.Gtk.PolicyType;
const Util = imports.misc.util;
const MessageTray = imports.ui.messageTray;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const CheckBox = imports.ui.checkBox.CheckBox;
const Gettext = imports.gettext;
const _ = Gettext.domain('clipboard-indicator').gettext;
const Clipboard = St.Clipboard.get_default();
const CLIPBOARD_TYPE = St.ClipboardType.CLIPBOARD;
const SETTING_KEY_CLEAR_HISTORY = "clear-history";
const SETTING_KEY_PREV_ENTRY = "prev-entry";
const SETTING_KEY_NEXT_ENTRY = "next-entry";
const SETTING_KEY_TOGGLE_MENU = "toggle-menu";
const INDICATOR_ICON = 'edit-paste-symbolic';
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const ConfirmDialog = Me.imports.confirmDialog;
const Prefs = Me.imports.prefs;
const prettyPrint = Utils.prettyPrint;
const writeRegistry = Utils.writeRegistry;
const readRegistry = Utils.readRegistry;
const writeImage = Utils.writeImage;
const getImage = Utils.getImage;
const deleteImage = Utils.deleteImage;
let TIMEOUT_MS = 1000;
let MAX_REGISTRY_LENGTH = 15;
let MAX_ENTRY_LENGTH = 50;
let CACHE_ONLY_FAVORITE = false;
let DELETE_ENABLED = true;
let MOVE_ITEM_FIRST = false;
let ENABLE_KEYBINDING = true;
let PRIVATEMODE = false;
let NOTIFY_ON_COPY = true;
let MAX_TOPBAR_LENGTH = 15;
let TOPBAR_DISPLAY_MODE = 1; //0 - only icon, 1 - only clipbord content, 2 - both
let DISABLE_DOWN_ARROW = false;
let STRIP_TEXT = false;
let SAVE_IMAGE = false;
let SUPPORT_PNG = false;
const ClipboardIndicator = Lang.Class({
Name: 'ClipboardIndicator',
Extends: PanelMenu.Button,
_settingsChangedId: null,
_clipboardTimeoutId: null,
_selectionOwnerChangedId: null,
_historyLabelTimeoutId: null,
_historyLabel: null,
_buttonText: null,
_disableDownArrow: null,
destroy: function () {
this._disconnectSettings();
this._unbindShortcuts();
this._clearClipboardTimeout();
this._disconnectSelectionListener();
this._clearLabelTimeout();
this._clearDelayedSelectionTimeout();
// Call parent
this.parent();
},
_init: function() {
this.parent(0.0, "ClipboardIndicator");
this._shortcutsBindingIds = [];
this.clipItemsRadioGroup = [];
let hbox = new St.BoxLayout({ style_class: 'panel-status-menu-box clipboard-indicator-hbox' });
this.icon = new St.Icon({ icon_name: INDICATOR_ICON,
style_class: 'system-status-icon clipboard-indicator-icon' });
hbox.add_child(this.icon);
this._buttonText = new St.Label({
text: _('Text will be here'),
y_align: Clutter.ActorAlign.CENTER
});
hbox.add_child(this._buttonText);
this._downArrow = PopupMenu.arrowIcon(St.Side.BOTTOM);
hbox.add(this._downArrow);
this.add_child(hbox);
this._createHistoryLabel();
this._loadSettings();
this._buildMenu();
this._updateTopbarLayout();
this._setupListener();
},
_updateButtonText: function(content){
if (!content || PRIVATEMODE){
this._buttonText.set_text("...")
} else {
this._buttonText.set_text(this._truncate(content, MAX_TOPBAR_LENGTH));
}
},
_buildMenu: function () {
let that = this;
this._getCache(function (clipHistory) {
let lastIdx = clipHistory.length - 1;
let clipItemsArr = that.clipItemsRadioGroup;
/* This create the search entry, which is add to a menuItem.
The searchEntry is connected to the function for research.
The menu itself is connected to some shitty hack in order to
grab the focus of the keyboard. */
that._entryItem = new PopupMenu.PopupBaseMenuItem({
reactive: false,
can_focus: false
});
that.searchEntry = new St.Entry({
name: 'searchEntry',
style_class: 'search-entry',
can_focus: true,
hint_text: _('Type here to search...'),
track_hover: true,
x_expand: true,
y_expand: true
});
that.searchEntry.get_clutter_text().connect(
'text-changed',
Lang.bind(that, that._onSearchTextChanged)
);
that._entryItem.add(that.searchEntry);
that.menu.addMenuItem(that._entryItem);
that.menu.connect('open-state-changed', Lang.bind(this, function(self, open){
let a = Mainloop.timeout_add(50, Lang.bind(this, function() {
if (open) {
that.searchEntry.set_text('');
global.stage.set_key_focus(that.searchEntry);
}
Mainloop.source_remove(a);
}));
}));
// Create menu sections for items
// Favorites
that.favoritesSection = new PopupMenu.PopupMenuSection();
that.scrollViewFavoritesMenuSection = new PopupMenu.PopupMenuSection();
let favoritesScrollView = new St.ScrollView({
style_class: 'ci-history-menu-section',
overlay_scrollbars: true
});
favoritesScrollView.add_actor(that.favoritesSection.actor);
that.scrollViewFavoritesMenuSection.actor.add_actor(favoritesScrollView);
that.menu.addMenuItem(that.scrollViewFavoritesMenuSection);
that.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// History
that.historySection = new PopupMenu.PopupMenuSection();
that.scrollViewMenuSection = new PopupMenu.PopupMenuSection();
let historyScrollView = new St.ScrollView({
style_class: 'ci-history-menu-section',
overlay_scrollbars: true
});
historyScrollView.add_actor(that.historySection.actor);
that.scrollViewMenuSection.actor.add_actor(historyScrollView);
that.menu.addMenuItem(that.scrollViewMenuSection);
// Add cached items
clipHistory.forEach(function (buffer) {
const { contents, favorite, type } = buffer;
that._addEntry(contents, favorite, true, false, type);
});
// Add separator
that.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// Private mode switch
that.privateModeMenuItem = new PopupMenu.PopupSwitchMenuItem(
_("Private mode"), PRIVATEMODE, { reactive: true });
that.privateModeMenuItem.connect('toggled',
Lang.bind(that, that._onPrivateModeSwitch));
that.menu.addMenuItem(that.privateModeMenuItem);
that._onPrivateModeSwitch();
// Add 'Clear' button which removes all items from cache
let clearMenuItem = new PopupMenu.PopupMenuItem(_('Clear history'));
that.menu.addMenuItem(clearMenuItem);
clearMenuItem.connect('activate', Lang.bind(that, that._removeAll));
// Add 'Settings' menu item to open settings
let settingsMenuItem = new PopupMenu.PopupMenuItem(_('Settings'));
that.menu.addMenuItem(settingsMenuItem);
settingsMenuItem.connect('activate', Lang.bind(that, that._openSettings));
if (lastIdx >= 0) {
that._selectMenuItem(clipItemsArr[lastIdx]);
}
});
},
/* When text change, this function will check, for each item of the
historySection and favoritesSestion, if it should be visible or not (based on words contained
in the clipContents attribute of the item). It doesn't destroy or create
items. It the entry is empty, the section is restored with all items
set as visible. */
_onSearchTextChanged: function() {
let searchedText = this.searchEntry.get_text().toLowerCase();
if(searchedText === '') {
this._getAllIMenuItems().forEach(function(mItem){
mItem.actor.visible = true;
});
}
else {
this._getAllIMenuItems().forEach(function(mItem){
// prevent broken menu on search
if (mItem.type === 'image') {
mItem.actor.visible = false;
return;
}
let text = mItem.clipContents.toLowerCase();
let isMatching = text.indexOf(searchedText) >= 0;
mItem.actor.visible = isMatching
});
}
},
_truncate: function(string, length) {
let shortened = string.replace(/\s+/g, ' ');
if (shortened.length > length) {
shortened = shortened.substring(0,length-1) + '...';
}
return shortened;
},
_setEntryLabel: function (menuItem) {
const buffer = menuItem.clipContents;
menuItem.label.set_text(this._truncate(buffer, MAX_ENTRY_LENGTH));
},
_addEntry: function (buffer, favorite, autoSelect, autoSetClip, type, bufferBytes = false) {
let menuItem = new PopupMenu.PopupMenuItem('');
if(type !== 'text') {
const icon = new St.Icon({
icon_size: 72,
style_class: 'imagem-icon'
});
icon.set_gicon(Gio.BytesIcon.new(bufferBytes === false ? getImage(buffer) : bufferBytes))
const icofavBtn = new St.Button({
style_class: 'ci-action-btn',
can_focus: true,
child: icon,
x_align: Clutter.ActorAlign.START,
x_expand: true,
y_expand: true
});
menuItem.actor.add_child(icofavBtn);
menuItem.icofavBtn = icofavBtn;
}
menuItem.menu = this.menu;
menuItem.clipContents = buffer;
menuItem.clipFavorite = favorite;
menuItem.type = type;
menuItem.radioGroup = this.clipItemsRadioGroup;
menuItem.buttonPressId = menuItem.connect('activate', Lang.bind(menuItem, this._onMenuItemSelectedAndMenuClose));
if (type === "text") {
this._setEntryLabel(menuItem);
}
this.clipItemsRadioGroup.push(menuItem);
// Favorite button
let icon_name = favorite ? 'starred-symbolic' : 'non-starred-symbolic';
let iconfav = new St.Icon({
icon_name: icon_name,
style_class: 'system-status-icon'
});
let icofavBtn = new St.Button({
style_class: 'ci-action-btn',
can_focus: true,
child: iconfav,
x_align: Clutter.ActorAlign.END,
x_expand: true,
y_expand: true
});
menuItem.actor.add_child(icofavBtn);
menuItem.icofavBtn = icofavBtn;
menuItem.favoritePressId = icofavBtn.connect('button-press-event',
Lang.bind(this, function () {
this._favoriteToggle(menuItem);
})
);
// Delete button
let icon = new St.Icon({
icon_name: 'edit-delete-symbolic', //'mail-attachment-symbolic',
style_class: 'system-status-icon'
});
let icoBtn = new St.Button({
style_class: 'ci-action-btn',
can_focus: true,
child: icon,
x_align: Clutter.ActorAlign.END,
x_expand: false,
y_expand: true
});
menuItem.actor.add_child(icoBtn);
menuItem.icoBtn = icoBtn;
menuItem.deletePressId = icoBtn.connect('button-press-event',
Lang.bind(this, function () {
this._removeEntry(menuItem, 'delete');
})
);
if (favorite) {
this.favoritesSection.addMenuItem(menuItem, 0);
}
else {
this.historySection.addMenuItem(menuItem, 0);
}
if (autoSelect === true) {
this._selectMenuItem(menuItem, autoSetClip);
}
if (TOPBAR_DISPLAY_MODE === 1 || TOPBAR_DISPLAY_MODE === 2) {
this._updateButtonText(buffer);
}
this._updateCache();
},
_favoriteToggle: function (menuItem) {
menuItem.clipFavorite = menuItem.clipFavorite ? false : true;
this._moveItemFirst(menuItem);
this._updateCache();
},
_removeAll: function () {
const title = _("Clear all?");
const message = _("Are you sure you want to delete all clipboard items?");
const sub_message = _("This operation cannot be undone.");
ConfirmDialog.openConfirmDialog(title, message, sub_message, _("Clear"), _("Cancel"), () => {
let that = this;
// We can't actually remove all items, because the clipboard still
// has data that will be re-captured on next refresh, so we remove
// all except the currently selected item
// Don't remove favorites here
that.historySection._getMenuItems().forEach(function (mItem) {
if (!mItem.currentlySelected) {
if (mItem.type === "image") deleteImage(mItem.clipContents);
let idx = that.clipItemsRadioGroup.indexOf(mItem);
mItem.destroy();
that.clipItemsRadioGroup.splice(idx,1);
}
});
that._updateCache();
that._showNotification(_("Clipboard history cleared"));
});
},
_removeEntry: function (menuItem, event) {
let itemIdx = this.clipItemsRadioGroup.indexOf(menuItem);
if(event === 'delete' && menuItem.currentlySelected) {
Clipboard.set_text(CLIPBOARD_TYPE, "");
}
if (event === 'delete' && menuItem.type === "image") deleteImage(menuItem.clipContents);
menuItem.destroy();
this.clipItemsRadioGroup.splice(itemIdx,1);
this._updateCache();
},
_removeOldestEntries: function () {
let that = this;
let clipItemsRadioGroupNoFavorite = that.clipItemsRadioGroup.filter(
item => item.clipFavorite === false);
while (clipItemsRadioGroupNoFavorite.length > MAX_REGISTRY_LENGTH) {
let oldestNoFavorite = clipItemsRadioGroupNoFavorite.shift();
that._removeEntry(oldestNoFavorite);
clipItemsRadioGroupNoFavorite = that.clipItemsRadioGroup.filter(
item => item.clipFavorite === false);
}
that._updateCache();
},
_onMenuItemSelected: function (autoSet) {
var that = this;
that.radioGroup.forEach(function (menuItem) {
let clipContents = that.clipContents;
const type = menuItem.type;
if (menuItem === that && clipContents) {
that.setOrnament(PopupMenu.Ornament.DOT);
that.currentlySelected = true;
if (autoSet !== false) {
if (type === "text") {
Clipboard.set_text(CLIPBOARD_TYPE, clipContents);
}
else {
Clipboard.set_content(CLIPBOARD_TYPE, SUPPORT_PNG ? "image/png" : "image/jpeg", getImage(clipContents));
}
}
}
else {
menuItem.setOrnament(PopupMenu.Ornament.NONE);
menuItem.currentlySelected = false;
}
});
},
_selectMenuItem: function (menuItem, autoSet) {
let fn = Lang.bind(menuItem, this._onMenuItemSelected);
fn(autoSet);
},
_onMenuItemSelectedAndMenuClose: function (autoSet) {
var that = this;
that.radioGroup.forEach(function (menuItem) {
let clipContents = that.clipContents;
const type = that.type;
if (menuItem === that && clipContents) {
that.setOrnament(PopupMenu.Ornament.DOT);
that.currentlySelected = true;
if (autoSet !== false) {
if (type === "text") {
Clipboard.set_text(CLIPBOARD_TYPE, clipContents);
}
else {
Clipboard.set_content(CLIPBOARD_TYPE, SUPPORT_PNG ? "image/png" : "image/jpeg", getImage(clipContents));
}
}
}
else {
menuItem.setOrnament(PopupMenu.Ornament.NONE);
menuItem.currentlySelected = false;
}
});
that.menu.close();
},
_getCache: function (cb) {
return readRegistry(cb);
},
_updateCache: function () {
let registry = this.clipItemsRadioGroup.map(function (menuItem) {
return {
"contents" : menuItem.clipContents,
"favorite" : menuItem.clipFavorite,
"type" : menuItem.type
};
});
writeRegistry(registry.filter(function (menuItem) {
if (CACHE_ONLY_FAVORITE) {
if (menuItem["favorite"]) {
return menuItem;
}
} else {
return menuItem;
}
}));
},
_onSelectionChange (selection, selectionType, selectionSource) {
if (selectionType === Meta.SelectionType.SELECTION_CLIPBOARD) {
this._refreshIndicator();
}
},
_refreshIndicator: function () {
if (PRIVATEMODE) return; // Private mode, do not.
let that = this;
Clipboard.get_text(CLIPBOARD_TYPE, function (clipboard, text) {
if (text === null) {
if (SAVE_IMAGE) {
Clipboard.get_content(CLIPBOARD_TYPE, SUPPORT_PNG ? 'image/png' : 'image/jpeg', function (clipboard, content) {
that._processClipboardContent(content.get_data(), 'image', content.hash());
});
}
else {
// do nothing
}
}
else {
const items = text.split('\n');
let blockText = false;
let hasText = false;
items.map((values) => {
// Dolphin debugger
if (!values.length) return;
// ebp === webp
// vif === avif
// peg === jpeg
const support_image_type = ['png', 'jpg', 'peg', 'gif', 'ico', 'ebp', 'svg', 'vif'];
const type = values.substring(values.length - 3);
if (support_image_type.indexOf(type) !== -1) {
// Dolphin return file:// + $PATH.
const path = values.substring(0, 7) === 'file://' ? values.substring(7) : values;
const file = Gio.file_new_for_path(path);
if (!file.query_exists(null)) {
hasText = true;
}
else {
const bytes = file.load_bytes(null)[0]
that._processClipboardContent(bytes, 'image', bytes.hash());
}
}
else {
hasText = true;
}
if (hasText && !blockText) {
blockText = true
that._processClipboardContent(text, "text");
}
})
}
});
},
// type most be image or text
_processClipboardContent (byteContent, type, name) {
const that = this;
let content;
if (type === "text") {
if (!STRIP_TEXT) {
content = byteContent;
}
else {
content = byteContent.trim();
}
}
else {
content = writeImage(byteContent, name);
}
if (content !== "" && content) {
let registry = that.clipItemsRadioGroup.map(function (menuItem) {
return menuItem.clipContents;
});
const itemIndex = registry.indexOf(content);
if (itemIndex < 0) {
that._addEntry(content, false, true, false, type, type === "image" && byteContent);
that._removeOldestEntries();
if (NOTIFY_ON_COPY) {
that._showNotification(_("Copied to clipboard"), notif => {
notif.addAction(_('Cancel'), Lang.bind(that, that._cancelNotification));
});
}
}
else if (itemIndex >= 0 && itemIndex < registry.length - 1) {
const item = that._findItem(content, type);
that._selectMenuItem(item, false);
if (!item.clipFavorite && MOVE_ITEM_FIRST) {
that._moveItemFirst(item, type);
}
}
}
},
_moveItemFirst: function (item, type = null) {
this._removeEntry(item);
this._addEntry(item.clipContents, item.clipFavorite, item.currentlySelected, false, type ?? item.type);
},
_findItem: function (content, type) {
return this.clipItemsRadioGroup.filter(
item => item.clipContents === content)[0];
},
_getCurrentlySelectedItem () {
return this.clipItemsRadioGroup.find(item => item.currentlySelected);
},
_getAllIMenuItems: function (text) {
return this.historySection._getMenuItems().concat(this.favoritesSection._getMenuItems());
},
_setupListener () {
const metaDisplay = Shell.Global.get().get_display();
if (typeof metaDisplay.get_selection === 'function') {
const selection = metaDisplay.get_selection();
this._setupSelectionTracking(selection);
}
else {
this._setupTimeout();
}
},
_setupSelectionTracking (selection) {
this.selection = selection;
this._selectionOwnerChangedId = selection.connect('owner-changed', (selection, selectionType, selectionSource) => {
this._onSelectionChange(selection, selectionType, selectionSource);
});
},
_setupTimeout: function (reiterate) {
let that = this;
reiterate = typeof reiterate === 'boolean' ? reiterate : true;
this._clipboardTimeoutId = Mainloop.timeout_add(TIMEOUT_MS, function () {
that._refreshIndicator();
// If the timeout handler returns `false`, the source is
// automatically removed, so we reset the timeout-id so it won't
// be removed on `.destroy()`
if (reiterate === false)
that._clipboardTimeoutId = null;
// As long as the timeout handler returns `true`, the handler
// will be invoked again and again as an interval
return reiterate;
});
},
_openSettings: function () {
if (typeof ExtensionUtils.openPrefs === 'function') {
ExtensionUtils.openPrefs();
} else {
Util.spawn([
"gnome-shell-extension-prefs",
Me.uuid
]);
}
},
_initNotifSource: function () {
if (!this._notifSource) {
this._notifSource = new MessageTray.Source('ClipboardIndicator',
INDICATOR_ICON);
this._notifSource.connect('destroy', Lang.bind(this, function() {
this._notifSource = null;
}));
Main.messageTray.add(this._notifSource);
}
},
_cancelNotification: function() {
if (this.clipItemsRadioGroup.length >= 2) {
let clipSecond = this.clipItemsRadioGroup.length - 2;
let previousClip = this.clipItemsRadioGroup[clipSecond];
Clipboard.set_text(CLIPBOARD_TYPE, previousClip.clipContents);
previousClip.setOrnament(PopupMenu.Ornament.DOT);
previousClip.icoBtn.visible = false;
previousClip.currentlySelected = true;
} else {
Clipboard.set_text(CLIPBOARD_TYPE, "");
}
let clipFirst = this.clipItemsRadioGroup.length - 1;
this._removeEntry(this.clipItemsRadioGroup[clipFirst]);
},
_showNotification: function (message, transformFn) {
let notification = null;
this._initNotifSource();
if (this._notifSource.count === 0) {
notification = new MessageTray.Notification(this._notifSource, message);
}
else {
notification = this._notifSource.notifications[0];
notification.update(message, '', { clear: true });
}
if (typeof transformFn === 'function') {
transformFn(notification);
}
notification.setTransient(true);
if (Config.PACKAGE_VERSION < '3.38')
this._notifSource.notify(notification);
else
this._notifSource.showNotification(notification);
},
_createHistoryLabel: function () {
this._historyLabel = new St.Label({
style_class: 'ci-notification-label',
text: ''
});
global.stage.add_actor(this._historyLabel);
this._historyLabel.hide();
},
_onPrivateModeSwitch: function() {
let that = this;
PRIVATEMODE = this.privateModeMenuItem.state;
// We hide the history in private ModeTypee because it will be out of sync (selected item will not reflect clipboard)
this.scrollViewMenuSection.actor.visible = !PRIVATEMODE;
this.scrollViewFavoritesMenuSection.actor.visible = !PRIVATEMODE;
// If we get out of private mode then we restore the clipboard to old state
if (!PRIVATEMODE) {
let selectList = this.clipItemsRadioGroup.filter((item) => !!item.currentlySelected);
Clipboard.get_text(CLIPBOARD_TYPE, function (clipBoard, text) {
that._updateButtonText(text);
});
if (selectList.length) {
this._selectMenuItem(selectList[0]);
} else {
// Nothing to return to, let's empty it instead
Clipboard.set_text(CLIPBOARD_TYPE, "");
}
this.icon.remove_style_class_name('private-mode');
} else {
this._buttonText.set_text('...');
this.icon.add_style_class_name('private-mode');
}
},
_loadSettings: function () {
this._settings = Prefs.SettingsSchema;
this._settingsChangedId = this._settings.connect('changed',
Lang.bind(this, this._onSettingsChange));
this._fetchSettings();
if (ENABLE_KEYBINDING)
this._bindShortcuts();
},
_fetchSettings: function () {
TIMEOUT_MS = this._settings.get_int(Prefs.Fields.INTERVAL);
MAX_REGISTRY_LENGTH = this._settings.get_int(Prefs.Fields.HISTORY_SIZE);
MAX_ENTRY_LENGTH = this._settings.get_int(Prefs.Fields.PREVIEW_SIZE);
CACHE_ONLY_FAVORITE = this._settings.get_boolean(Prefs.Fields.CACHE_ONLY_FAVORITE);
DELETE_ENABLED = this._settings.get_boolean(Prefs.Fields.DELETE);
MOVE_ITEM_FIRST = this._settings.get_boolean(Prefs.Fields.MOVE_ITEM_FIRST);
NOTIFY_ON_COPY = this._settings.get_boolean(Prefs.Fields.NOTIFY_ON_COPY);
ENABLE_KEYBINDING = this._settings.get_boolean(Prefs.Fields.ENABLE_KEYBINDING);
MAX_TOPBAR_LENGTH = this._settings.get_int(Prefs.Fields.TOPBAR_PREVIEW_SIZE);
TOPBAR_DISPLAY_MODE = this._settings.get_int(Prefs.Fields.TOPBAR_DISPLAY_MODE_ID);
DISABLE_DOWN_ARROW = this._settings.get_boolean(Prefs.Fields.DISABLE_DOWN_ARROW);
STRIP_TEXT = this._settings.get_boolean(Prefs.Fields.STRIP_TEXT);
SAVE_IMAGE = this._settings.get_boolean(Prefs.Fields.SAVE_IMAGE);
SUPPORT_PNG = this._settings.get_boolean(Prefs.Fields.SUPPORT_PNG);
},
_onSettingsChange: function () {
var that = this;
// Load the settings into variables
that._fetchSettings();
// Remove old entries in case the registry size changed
that._removeOldestEntries();
// Re-set menu-items lables in case preview size changed
this._getAllIMenuItems().forEach(function (mItem) {
if (mItem.type === "text") that._setEntryLabel(mItem);
});
//update topbar
this._updateTopbarLayout();
if(TOPBAR_DISPLAY_MODE === 1 || TOPBAR_DISPLAY_MODE === 2) {
Clipboard.get_text(CLIPBOARD_TYPE, function (clipBoard, text) {
that._updateButtonText(text);
});
}
// Bind or unbind shortcuts
if (ENABLE_KEYBINDING)
that._bindShortcuts();
else
that._unbindShortcuts();
},
_bindShortcuts: function () {
this._unbindShortcuts();
this._bindShortcut(SETTING_KEY_CLEAR_HISTORY, this._removeAll);
this._bindShortcut(SETTING_KEY_PREV_ENTRY, this._previousEntry);
this._bindShortcut(SETTING_KEY_NEXT_ENTRY, this._nextEntry);
this._bindShortcut(SETTING_KEY_TOGGLE_MENU, this._toggleMenu);
},
_unbindShortcuts: function () {
this._shortcutsBindingIds.forEach(
(id) => Main.wm.removeKeybinding(id)
);
this._shortcutsBindingIds = [];
},
_bindShortcut: function(name, cb) {
var ModeType = Shell.hasOwnProperty('ActionMode') ?
Shell.ActionMode : Shell.KeyBindingMode;
Main.wm.addKeybinding(
name,
this._settings,
Meta.KeyBindingFlags.NONE,
ModeType.ALL,
Lang.bind(this, cb)
);
this._shortcutsBindingIds.push(name);
},
_updateTopbarLayout: function(){
if(TOPBAR_DISPLAY_MODE === 0){
this.icon.visible = true;
this._buttonText.visible = false;
}
if(TOPBAR_DISPLAY_MODE === 1){
this.icon.visible = false;
this._buttonText.visible = true;
}
if(TOPBAR_DISPLAY_MODE === 2){
this.icon.visible = true;
this._buttonText.visible = true;
}
if(!DISABLE_DOWN_ARROW) {
this._downArrow.visible = true;
} else {
this._downArrow.visible = false;
}
},
_disconnectSettings: function () {
if (!this._settingsChangedId)
return;
this._settings.disconnect(this._settingsChangedId);
this._settingsChangedId = null;
},
_clearClipboardTimeout: function () {
if (!this._clipboardTimeoutId)
return;
Mainloop.source_remove(this._clipboardTimeoutId);
this._clipboardTimeoutId = null;
},
_disconnectSelectionListener () {
if (!this._selectionOwnerChangedId)
return;
this.selection.disconnect(this._selectionOwnerChangedId);
},
_clearLabelTimeout: function () {
if (!this._historyLabelTimeoutId)
return;
Mainloop.source_remove(this._historyLabelTimeoutId);
this._historyLabelTimeoutId = null;
},
_clearDelayedSelectionTimeout: function () {
if (this._delayedSelectionTimeoutId) {
Mainloop.source_remove(this._delayedSelectionTimeoutId);
}
},
_selectEntryWithDelay: function (entry) {
let that = this;
that._selectMenuItem(entry, false);
that._delayedSelectionTimeoutId = Mainloop.timeout_add(
TIMEOUT_MS * 0.75, function () {
that._selectMenuItem(entry); //select the item
that._delayedSelectionTimeoutId = null;
return false;
});
},
_previousEntry: function() {
let that = this;
that._clearDelayedSelectionTimeout();
this._getAllIMenuItems().some(function (mItem, i, menuItems){
if (mItem.currentlySelected) {
i--; //get the previous index
if (i < 0) i = menuItems.length - 1; //cycle if out of bound
let index = i + 1; //index to be displayed
that._showNotification(index + ' / ' + menuItems.length + ': ' + menuItems[i].label.text);
if (MOVE_ITEM_FIRST) {
that._selectEntryWithDelay(menuItems[i]);
}
else {
that._selectMenuItem(menuItems[i]);
}
return true;
}
return false;
});
},
_nextEntry: function() {
let that = this;
that._clearDelayedSelectionTimeout();
this._getAllIMenuItems().some(function (mItem, i, menuItems){
if (mItem.currentlySelected) {
i++; //get the next index
if (i === menuItems.length) i = 0; //cycle if out of bound
let index = i + 1; //index to be displayed
that._showNotification(index + ' / ' + menuItems.length + ': ' + menuItems[i].label.text);
if (MOVE_ITEM_FIRST) {
that._selectEntryWithDelay(menuItems[i]);
}
else {
that._selectMenuItem(menuItems[i]);
}
return true;
}
return false;
});
},
_toggleMenu: function(){
this.menu.toggle();
}
});
function init () {
let localeDir = Me.dir.get_child('locale');
Gettext.bindtextdomain('clipboard-indicator', localeDir.get_path());
}
let clipboardIndicator;
function enable () {
clipboardIndicator = new ClipboardIndicator();
Main.panel.addToStatusArea('clipboardIndicator', clipboardIndicator, 1);
}
function disable () {
clipboardIndicator.destroy();
}