-
Notifications
You must be signed in to change notification settings - Fork 27
/
extension.js
147 lines (125 loc) · 4.73 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
/*
Workspaces Bar
Copyright Francois Thirioux 2021
GitHub contributors: @fthx
License GPL v3
*/
const { Clutter, Gio, GObject, Shell, St } = imports.gi;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
var WORKSPACES_SCHEMA = "org.gnome.desktop.wm.preferences";
var WORKSPACES_KEY = "workspace-names";
var WorkspacesBar = GObject.registerClass(
class WorkspacesBar extends PanelMenu.Button {
_init() {
super._init(0.0, 'Workspaces bar');
this.track_hover = false;
// define gsettings schema for workspaces names, get workspaces names, signal for settings key changed
this.workspaces_settings = new Gio.Settings({ schema: WORKSPACES_SCHEMA });
this.workspaces_names_changed = this.workspaces_settings.connect(`changed::${WORKSPACES_KEY}`, this._update_workspaces_names.bind(this));
// hide Activities button
this._show_activities(false);
// bar creation
this.ws_bar = new St.BoxLayout({});
this._update_workspaces_names();
this.add_child(this.ws_bar);
// signals for workspaces state: active workspace, number of workspaces
this._ws_active_changed = global.workspace_manager.connect('active-workspace-changed', this._update_ws.bind(this));
this._ws_number_changed = global.workspace_manager.connect('notify::n-workspaces', this._update_ws.bind(this));
this._restacked = global.display.connect('restacked', this._update_ws.bind(this));
this._windows_changed = Shell.WindowTracker.get_default().connect('tracked-windows-changed', this._update_ws.bind(this));
}
// remove signals, restore Activities button, destroy workspaces bar
_destroy() {
this._show_activities(true);
if (this._ws_active_changed) {
global.workspace_manager.disconnect(this._ws_active_changed);
}
if (this._ws_number_changed) {
global.workspace_manager.disconnect(this._ws_number_changed);
}
if (this._restacked) {
global.display.disconnect(this._restacked);
}
if (this._windows_changed) {
Shell.WindowTracker.get_default().disconnect(this._windows_changed);
}
if (this.workspaces_names_changed) {
this.workspaces_settings.disconnect(this.workspaces_names_changed);
}
this.ws_bar.destroy();
super.destroy();
}
// hide Activities button
_show_activities(show) {
this.activities_button = Main.panel.statusArea['activities'];
if (this.activities_button) {
if (show && !Main.sessionMode.isLocked) {
this.activities_button.container.show();
} else {
this.activities_button.container.hide();
}
}
}
// update workspaces names
_update_workspaces_names() {
this.workspaces_names = this.workspaces_settings.get_strv(WORKSPACES_KEY);
this._update_ws();
}
// update the workspaces bar
_update_ws() {
// destroy old workspaces bar buttons
this.ws_bar.destroy_all_children();
// get number of workspaces
this.ws_count = global.workspace_manager.get_n_workspaces();
this.active_ws_index = global.workspace_manager.get_active_workspace_index();
// display all current workspaces buttons
for (let ws_index = 0; ws_index < this.ws_count; ++ws_index) {
this.ws_box = new St.Bin({visible: true, reactive: true, can_focus: true, track_hover: true});
this.ws_box.label = new St.Label({y_align: Clutter.ActorAlign.CENTER});
if (ws_index == this.active_ws_index) {
if (global.workspace_manager.get_workspace_by_index(ws_index).n_windows > 0) {
this.ws_box.label.style_class = 'desktop-label-nonempty-active';
} else {
this.ws_box.label.style_class = 'desktop-label-empty-active';
}
} else {
if (global.workspace_manager.get_workspace_by_index(ws_index).n_windows > 0) {
this.ws_box.label.style_class = 'desktop-label-nonempty-inactive';
} else {
this.ws_box.label.style_class = 'desktop-label-empty-inactive';
}
}
if (this.workspaces_names[ws_index]) {
this.ws_box.label.set_text(" " + this.workspaces_names[ws_index] + " ");
} else {
this.ws_box.label.set_text(" " + (ws_index + 1) + " ");
}
this.ws_box.set_child(this.ws_box.label);
this.ws_box.connect('button-release-event', () => this._toggle_ws(ws_index) );
this.ws_bar.add_actor(this.ws_box);
}
}
// activate workspace or show overview
_toggle_ws(ws_index) {
if (global.workspace_manager.get_active_workspace_index() == ws_index) {
Main.overview.toggle();
} else {
global.workspace_manager.get_workspace_by_index(ws_index).activate(global.get_current_time());
}
}
});
class Extension {
constructor() {
}
enable() {
this.workspaces_bar = new WorkspacesBar();
Main.panel.addToStatusArea('workspaces-bar', this.workspaces_bar, 0, 'left');
}
disable() {
this.workspaces_bar._destroy();
}
}
function init() {
return new Extension();
}