forked from d-go/quick-settings-avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
249 lines (208 loc) · 8.81 KB
/
extension.js
File metadata and controls
249 lines (208 loc) · 8.81 KB
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
import AccountsService from 'gi://AccountsService';
import Clutter from 'gi://Clutter';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Shell from 'gi://Shell';
import St from 'gi://St';
import { Avatar } from 'resource:///org/gnome/shell/ui/userWidget.js';
import { QuickSettingsItem, SystemIndicator } from 'resource:///org/gnome/shell/ui/quickSettings.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import { PACKAGE_VERSION } from 'resource:///org/gnome/shell/misc/config.js';
const QuickSettingsMenu = Main.panel.statusArea.quickSettings;
const [major, minor] = PACKAGE_VERSION.split('.').map(v => Number(v));
const SETTINGS = [
'avatar-mode',
'avatar-position',
'avatar-size',
'avatar-realname',
'avatar-username',
'avatar-hostname',
'avatar-nobackground',
];
let sourceId = null;
const AvatarItem = GObject.registerClass(
class AvatarItem extends QuickSettingsItem {
_init(settings) {
super._init({
style_class: 'icon-button avatar-button',
canFocus: true,
hasMenu: false,
});
if (settings.avatarNoBackground) {
this.add_style_class_name('no-bg');
}
this._user = AccountsService.UserManager.get_default().get_user(GLib.get_user_name());
// Main box container
this._container = new St.BoxLayout({
style_class: 'avatar-name-box',
y_align: Clutter.ActorAlign.CENTER,
x_align: Clutter.ActorAlign.CENTER,
vertical: false,
});
this.set_y_align(Clutter.ActorAlign.CENTER);
this.set_child(this._container);
// Avatar picture
const iconSize = settings.avatarSize % 2 === 0 ? settings.avatarSize + 1 : settings.avatarSize;
this._avatarPicture = new Avatar(this._user, {
iconSize,
styleClass: 'avatar-picture',
});
this._avatarPicture.style = `icon-size: ${iconSize}px;`;
// Avatar real name label
const { avatarRealname, avatarUsername, avatarHostname } = settings;
const isOnRight = settings.avatarPosition === 0;
this._realNameLabel = new St.Label({
style_class: 'avatar-realname-label',
y_align: Clutter.ActorAlign.CENTER,
x_align: isOnRight ? Clutter.ActorAlign.END : Clutter.ActorAlign.START,
text: this._user.get_real_name() || GLib.get_real_name(),
});
// Avatar user name + host name label
this._userNameLabel = new St.Label({
style_class: 'avatar-username-label',
y_align: Clutter.ActorAlign.CENTER,
x_align: isOnRight ? Clutter.ActorAlign.END : Clutter.ActorAlign.START,
text: (avatarUsername ? GLib.get_user_name() : '') + (avatarHostname ? `@${GLib.get_host_name()}` : ''),
});
// Labels container
const labelsContainer = new St.BoxLayout({
style_class: 'avatar-labels-box',
y_align: Clutter.ActorAlign.CENTER,
vertical: true,
});
if (avatarRealname) {
labelsContainer.add_child(this._realNameLabel);
this._userNameLabel.add_style_class_name('with-real-name');
}
if (avatarUsername || avatarHostname) {
labelsContainer.add_child(this._userNameLabel);
}
// Depending on avatar position, add username first and then the avatar picture or viceversa
if (isOnRight) {
this._container.add_child(labelsContainer);
this._container.add_child(this._avatarPicture);
} else {
this._container.add_child(this._avatarPicture);
this._container.add_child(labelsContainer);
}
this._bindModeActions();
// Listen to changes when avatar pic or real name is changed via settings
this._user.connectObject('changed', this._updateAvatar.bind(this), this);
}
_updateAvatar() {
this._avatarPicture.update();
this._realNameLabel.text = this._user.get_real_name();
}
_bindModeActions() {
let userSettings = 'gnome-user-accounts-panel.desktop';
if (major >= 46) {
userSettings = 'gnome-users-panel.desktop';
}
this._settingsApp = Shell.AppSystem.get_default().lookup_app(userSettings);
if (!this._settingsApp) {
console.log('Missing users settings core component, expect trouble…');
}
this.accessible_name = this._settingsApp?.get_name() ?? null;
// Links to the Users settings on click
this.connect('clicked', () => {
Main.overview.hide();
Main.panel.closeQuickSettings();
this._settingsApp.activate();
});
}
}
);
const Indicator = GObject.registerClass(
class Indicator extends SystemIndicator {
_init(settings) {
super._init();
this.settings = settings;
this._load();
}
_load() {
this._avatarItem = new AvatarItem(this.settings);
// Container of the system toggles
this.systemItemsBox = QuickSettingsMenu._system._systemItem.child;
if (this.systemItemsBox) {
this._addAvatar();
}
// Destroy the avatar toggle on plugin deactivation
this.connect('destroy', () => {
this._avatarItem.destroy();
});
}
_addAvatar() {
const tmpSystemItems = [];
this.systemItemsBox.get_children().forEach(item => {
tmpSystemItems.push({ item, isVisible: item.visible, yAlign: item.get_y_align() });
});
this.systemItemsBox.remove_all_children();
// 0: right, 1: left
if (this.settings.avatarPosition === 0) {
this._addSystemItems(tmpSystemItems);
this.systemItemsBox.add_child(this._avatarItem);
} else {
this.systemItemsBox.add_child(this._avatarItem);
this._addSystemItems(tmpSystemItems);
}
}
_addSystemItems(items) {
items.forEach(({ item, isVisible }) => {
item.visible = isVisible;
// Remove the Y axis expansion on current buttons
item.set_y_align(Clutter.ActorAlign.CENTER);
// Battery/Power button doesn't have a fixed height,
// so it shrinks when specifying y_align to CENTER
if (item.constructor?.name === 'PowerToggle') {
item.set_style('height: 41px;');
}
this.systemItemsBox.add_child(item);
});
}
});
export default class QSAvatar extends Extension {
enable() {
console.log(`[QSA] Enabling extension`);
this.settings = this.getSettings('org.gnome.shell.extensions.quick-settings-avatar');
this.handlerIds = SETTINGS.map(setting => (
this.settings.connect(`changed::${setting}`, () => {
this.disable();
this.enable();
})
));
if (QuickSettingsMenu._system) {
this._indicator = new Indicator(this._mapSettings());
} else {
sourceId = GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
if (!QuickSettingsMenu._system)
return GLib.SOURCE_CONTINUE;
this._indicator = new Indicator(this._mapSettings());
return GLib.SOURCE_REMOVE;
});
}
}
disable() {
console.log(`[QSA] Disabling extension`);
this.handlerIds.forEach((handler) => this.settings.disconnect(handler));
this._indicator.destroy();
this.settings = null;
this.handlerIds = null;
this._indicator = null;
if (sourceId) {
GLib.Source.remove(sourceId);
sourceId = null;
}
}
_mapSettings() {
return {
avatarMode: this.settings.get_int('avatar-mode'),
avatarPosition: this.settings.get_int('avatar-position'),
avatarSize: this.settings.get_int('avatar-size'),
avatarRealname: this.settings.get_boolean('avatar-realname'),
avatarUsername: this.settings.get_boolean('avatar-username'),
avatarHostname: this.settings.get_boolean('avatar-hostname'),
avatarNoBackground: this.settings.get_boolean('avatar-nobackground'),
}
}
}