forked from rliang/gnome-shell-extension-task-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
145 lines (129 loc) · 4.18 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
const Meta = imports.gi.Meta;
const Clutter = imports.gi.Clutter;
const Gio = imports.gi.Gio;
const St = imports.gi.St;
const Shell = imports.gi.Shell;
const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
let _settings, _iconsBox;
function extProperty(object, key, def) {
key = '__' + Me.uuid + '_' + key;
if (!object[key])
object[key] = def();
return object[key];
}
function extConnect(object, signal, cb) {
extProperty(object, 'handles', () => []).push(object.connect(signal, cb));
}
function extDisconnect(object) {
extProperty(object, 'handles', () => []).forEach(h => object.disconnect(h));
}
function getWorkspaces() {
let wsList = [];
for (let i = 0; i < global.screen.n_workspaces; i++)
wsList.push(global.screen.get_workspace_by_index(i));
return wsList;
}
function windowIcon(win) {
return extProperty(win, 'icon', () => new St.Bin({
style_class: 'taskicons-icon',
child: Shell.WindowTracker.get_default().get_window_app(win)
.create_icon_texture(16),
}));
}
function workspaceBox(ws) {
return extProperty(ws, 'box', () => {
let box = new St.BoxLayout({
style_class: 'panel-button',
reactive: true,
can_focus: true,
track_hover: true,
});
box.connect('button-press-event', () =>
ws.activate(global.get_current_time()));
return box;
});
}
function workspaceLabel(ws) {
return extProperty(ws, 'label', () => new St.Label({
style_class: 'taskicons-label',
y_align: Clutter.ActorAlign.CENTER,
}));
}
function workspaceIcons(ws) {
return ws.list_windows().map(windowIcon).filter(icon => icon !== null)
}
function setupBox(ws, icons, all) {
let isActive = ws.index() === global.screen.get_active_workspace_index();
let isSingle = all.length === 1;
let box = workspaceBox(ws);
box.remove_all_children();
if (_settings.get_boolean('show-workspace-numbers') && (!isSingle || !isActive)) {
let label = workspaceLabel(ws);
label.set_text((ws.index() + 1).toString());
label.reparent(box);
}
box.pseudo_class = null;
if (_settings.get_boolean('highlight-current-workspace') && (!isSingle && isActive))
box.pseudo_class = 'active';
icons.forEach(icon => icon.reparent(box));
box.set_opacity(isActive ? 255 : _settings.get_double('inactive-workspace-opacity'));
return box;
}
function checkBuild() {
let wins = global.get_window_actors()
.map(a => a.meta_window)
.filter(w => w.window_type === Meta.WindowType.NORMAL);
if (wins.length < 1)
return false;
if (wins.length === 1 && wins[0].get_workspace() == global.screen.get_active_workspace())
return false;
return true;
}
function rebuild() {
_iconsBox.remove_all_children();
if (!checkBuild())
return;
getWorkspaces()
.map(ws => [ws, workspaceIcons(ws)])
.filter(([ws, icons]) => icons.length > 0)
.forEach(([ws, icons], _, all) => setupBox(ws, icons, all).reparent(_iconsBox));
}
function init() {
let schema = Me.metadata['settings-schema'];
let source = Gio.SettingsSchemaSource.new_from_directory(Me.dir.get_path(),
Gio.SettingsSchemaSource.get_default(), false)
_settings = new Gio.Settings({
settings_schema: source.lookup(schema, true),
});
}
function enable() {
_iconsBox = new St.BoxLayout({ style_class: 'taskicons-box' });
if (_settings.get_boolean('icons-on-right')) {
Main.panel._rightBox.insert_child_at_index(_iconsBox, 0);
} else {
let appMenu = Main.panel.statusArea.appMenu.actor.get_parent();
let appMenuBox = appMenu.get_parent();
let appMenuIndex = appMenuBox.get_children().indexOf(appMenu);
if (!_settings.get_boolean('icons-before-app-menu'))
appMenuIndex += 1;
appMenuBox.insert_child_at_index(_iconsBox, appMenuIndex);
}
rebuild();
extConnect(global.screen, 'restacked', rebuild);
extConnect(global.window_manager, 'switch-workspace', rebuild);
extConnect(_settings, 'changed', reenable);
}
function disable() {
extDisconnect(global.screen);
extDisconnect(global.window_manager);
extDisconnect(_settings);
_iconsBox.remove_all_children();
_iconsBox.destroy();
}
function reenable() {
disable();
enable();
}