-
Notifications
You must be signed in to change notification settings - Fork 462
/
appIconsDecorator.js
168 lines (146 loc) · 5.74 KB
/
appIconsDecorator.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
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
import {
Docking,
AppIconIndicators,
Utils,
} from './imports.js';
import {
Gio,
} from './dependencies/gi.js';
import {
AppMenu,
AppDisplay,
Main,
PopupMenu,
} from './dependencies/shell/ui.js';
const Labels = Object.freeze({
GENERIC: Symbol('generic'),
ICONS: Symbol('icons'),
});
export class AppIconsDecorator {
constructor() {
this._signals = new Utils.GlobalSignalsHandler();
this._methodInjections = new Utils.InjectionsHandler();
this._propertyInjections = new Utils.PropertyInjectionsHandler(
null, {allowNewProperty: true});
this._indicators = new Set();
this._patchAppIcons();
this._decorateIcons();
}
destroy() {
this._signals?.destroy();
delete this._signals;
this._methodInjections?.destroy();
delete this._methodInjections;
this._propertyInjections?.destroy();
delete this._propertyInjections;
this._indicators?.forEach(i => i.destroy());
this._indicators?.clear();
delete this._indicators;
}
_decorateIcon(parentIcon, signalLabel = Labels.GENERIC) {
const indicator = new AppIconIndicators.UnityIndicator(parentIcon);
this._indicators.add(indicator);
this._signals.addWithLabel(signalLabel, parentIcon, 'destroy', () => {
this._indicators.delete(indicator);
indicator.destroy();
});
return indicator;
}
_decorateIcons() {
const {appDisplay} = Docking.DockManager.getDefault().overviewControls;
const decorateAppIcons = () => {
this._indicators.forEach(i => i.destroy());
this._indicators.clear();
this._signals.removeWithLabel(Labels.ICONS);
const decorateViewIcons = view => {
const items = view.getAllItems();
items.forEach(i => {
if (i instanceof AppDisplay.AppIcon) {
this._decorateIcon(i, Labels.ICONS);
} else if (i instanceof AppDisplay.FolderIcon) {
decorateViewIcons(i.view);
this._signals.addWithLabel(Labels.ICONS, i.view,
'view-loaded', () => decorateAppIcons());
}
});
};
decorateViewIcons(appDisplay);
};
this._signals.add(appDisplay, 'view-loaded', () => decorateAppIcons());
decorateAppIcons();
}
_patchAppIcons() {
const self = this;
this._methodInjections.add(AppDisplay.AppSearchProvider.prototype,
'createResultObject', function (originalFunction, ...args) {
/* eslint-disable no-invalid-this */
const result = originalFunction.call(this, ...args);
if (result instanceof AppDisplay.AppIcon)
self._decorateIcon(result);
return result;
/* eslint-enable no-invalid-this */
});
this._methodInjections.add(AppDisplay.AppIcon.prototype,
'activate', function (originalFunction, ...args) {
/* eslint-disable no-invalid-this */
if (this.updating) {
const icon = Gio.Icon.new_for_string('action-unavailable-symbolic');
Main.osdWindowManager.show(-1, icon,
_('%s is updating, try again later').format(this.name),
null);
return;
}
originalFunction.call(this, ...args);
/* eslint-enable no-invalid-this */
});
const appIconsTypes = [
AppDisplay.AppSearchProvider,
AppDisplay.AppIcon,
];
appIconsTypes.forEach(type =>
this._propertyInjections.add(type.prototype, 'updating', {
get() {
return !!this.__d2dUpdating;
},
set(updating) {
if (this.updating === updating)
return;
this.__d2dUpdating = updating;
if (updating)
this.add_style_class_name('updating');
else
this.remove_style_class_name('updating');
},
}));
this._methodInjections.add(AppMenu.AppMenu.prototype,
'open', function (originalFunction, ...args) {
/* eslint-disable no-invalid-this */
if (!this.sourceActor.updating) {
originalFunction.call(this, ...args);
return;
}
if (this.isOpen)
return;
if (this.isEmpty())
return;
// Temporarily hide all the menu items a part the Pinning and
// the details one while we're updating.
const validItems = [
this._toggleFavoriteItem,
this._detailsItem,
];
const items = this._getMenuItems().filter(
i => !validItems.includes(i)).map(i =>
i instanceof PopupMenu.PopupMenuBase ? i.actor : i);
const itemsVisibility = items.map(i => i.visible);
items.forEach(i => (i.visible = false));
const menuClosedId = this.connect('menu-closed', () => {
this.disconnect(menuClosedId);
items.forEach((i, idx) => (i.visible = itemsVisibility[idx]));
});
originalFunction.call(this, ...args);
/* eslint-enable no-invalid-this */
});
}
}