-
Notifications
You must be signed in to change notification settings - Fork 13
/
extension.js
296 lines (252 loc) · 8.27 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
import Clutter from "gi://Clutter";
import GObject from "gi://GObject";
import Gio from "gi://Gio";
import GLib from "gi://GLib";
import Meta from "gi://Meta";
import St from "gi://St";
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import * as PanelMenu from "resource:///org/gnome/shell/ui/panelMenu.js";
const workspaceManager = global.workspace_manager;
// workspace switch to previous / next
function workspace_switch(step, scroll_wrap) {
let active_index = workspaceManager.get_active_workspace_index();
let workspace_count = workspaceManager.get_n_workspaces();
let target_index = (active_index + step + workspace_count) % workspace_count;
if (!scroll_wrap) {
if (active_index + step >= workspace_count || active_index + step < 0)
target_index = active_index;
}
workspaceManager
.get_workspace_by_index(target_index)
.activate(global.get_current_time());
}
let WorkspaceIndicator = GObject.registerClass(
class WorkspaceIndicator extends St.Button {
_init(workspace, active, skip_taskbar_mode, change_on_click) {
super._init();
this.active = active;
this.workspace = workspace;
this.skip_taskbar_mode = skip_taskbar_mode;
// setup widgets
this._widget = new St.Widget({
layout_manager: new Clutter.BinLayout(),
x_expand: true,
y_expand: false,
});
this._statusLabel = new St.Label({
style_class: "panel-workspace-indicator",
y_align: Clutter.ActorAlign.CENTER,
text: `${this.workspace.index() + 1}`,
});
if (this.active) {
this._statusLabel.add_style_class_name("workspace-indicator-active");
}
this._widget.add_child(this._statusLabel);
this._thumbnailsBox = new St.BoxLayout({
style_class: "panel-workspace-indicator-box",
y_expand: true,
reactive: true,
});
this._widget.add_child(this._thumbnailsBox);
this.add_child(this._widget);
// Connect signals
this._windowAddedId = this.workspace.connect("window-added", () =>
this.show_or_hide(),
);
this._windowRemovedId = this.workspace.connect("window-removed", () =>
this.show_or_hide(),
);
if (change_on_click) {
this.connect("clicked", () =>
this.workspace.activate(global.get_current_time()),
);
}
this.show_or_hide();
}
show_or_hide() {
if (this.active || this.has_user_window()) {
this.show();
} else {
this.hide();
}
}
has_user_window() {
let windows = this.workspace.list_windows();
if (!this.skip_taskbar_mode) {
return windows.length > 0;
}
return windows.some((w) => {
return !w.is_skip_taskbar();
});
}
destroy() {
this.workspace.disconnect(this._windowRemovedId);
this.workspace.disconnect(this._windowAddedId);
super.destroy();
}
},
);
export default class WorkspaceLayout extends Extension {
constructor(metadata) {
super(metadata);
}
enable() {
this.indicators = [];
this.panel_button = null;
this.box_layout = null;
this.themeContext = St.ThemeContext.get_for_stage(global.stage);
this.settings = this.getSettings();
// Custom CSS file
this.css_file = null;
// Custom CSS stylesheet path
this.custom_css_path = this.settings.get_string("custom-css-path");
if (this.custom_css_path !== "") {
if (GLib.file_test(this.custom_css_path, GLib.FileTest.EXISTS) == true) {
this.themesLoaded = this.themeContext
.get_theme()
.get_custom_stylesheets();
for (let i = 0; i < this.themesLoaded.length; i++) {
this.themeContext.get_theme().unload_stylesheet(this.themesLoaded[i]);
}
this.css_file = Gio.File.new_for_path(this.custom_css_path);
this.themeContext.get_theme().load_stylesheet(this.css_file);
} else {
this.settings.set_string("custom-css-path", "");
}
}
let gschema = Gio.SettingsSchemaSource.new_from_directory(
this.dir.get_child("schemas").get_path(),
Gio.SettingsSchemaSource.get_default(),
false,
);
this._panelPositionChangedId = this.settings.connect(
"changed::panel-position",
() => {
this.add_panel_button();
},
);
this._skipTaskbarModeChangedId = this.settings.connect(
"changed::skip-taskbar-mode",
() => {
this.add_panel_button();
},
);
this._changeOnClickChangedId = this.settings.connect(
"changed::change-on-click",
() => {
this.add_panel_button();
},
);
//scroll to change workspace
this._changeOnScrollChangedId = this.settings.connect(
"changed::change-on-scroll",
() => {
this.add_panel_button();
},
);
//scroll wraparound
this._changeOnScrollChangedId = this.settings.connect(
"changed::wrap-scroll",
() => {
this.add_panel_button();
},
);
this.add_panel_button();
}
disable() {
this.destroy_indicators();
this.destroy_panel_button();
workspaceManager.disconnect(this._workspaceSwitchedId);
workspaceManager.disconnect(this._workspaceAddedId);
workspaceManager.disconnect(this._workspaceRemovedId);
workspaceManager.disconnect(this._workspaceReordered);
this.settings.disconnect(this._panelPositionChangedId);
this.settings.disconnect(this._skipTaskbarModeChangedId);
this.settings.disconnect(this._changeOnClickChangedId);
if (this.css_file !== null) {
this.themeContext.get_theme().unload_stylesheet(this.css_file);
}
this.settings = null;
}
add_panel_button() {
this.destroy_panel_button();
this.panel_button = new PanelMenu.Button(
0.0,
_("Improved Workspace Indicator"),
);
this.box_layout = new St.BoxLayout();
this.panel_button.add_child(this.box_layout);
let change_on_scroll = this.settings.get_boolean("change-on-scroll");
if (change_on_scroll) {
let scroll_wrap = this.settings.get_boolean("wrap-scroll");
this.panel_button.connect("scroll-event", (_, event) => {
let switch_step = 0;
switch (event.get_scroll_direction()) {
case Clutter.ScrollDirection.UP:
switch_step = -1;
break;
case Clutter.ScrollDirection.DOWN:
switch_step = +1;
break;
}
if (switch_step) workspace_switch(switch_step, scroll_wrap);
});
}
let [position] = this.settings.get_value("panel-position").get_string();
Main.panel.addToStatusArea(
"improved-workspace-indicator",
this.panel_button,
0,
position,
);
this._workspaceSwitchedId = workspaceManager.connect_after(
"workspace-switched",
this.add_indicators.bind(this),
);
this._workspaceAddedId = workspaceManager.connect_after(
"workspace-added",
this.add_indicators.bind(this),
);
this._workspaceRemovedId = workspaceManager.connect_after(
"workspace-removed",
this.add_indicators.bind(this),
);
this._workspaceReordered = workspaceManager.connect_after(
"workspaces-reordered",
this.add_indicators.bind(this),
);
this.add_indicators();
}
add_indicators() {
this.destroy_indicators();
let active_index = workspaceManager.get_active_workspace_index();
for (let i = 0; i < workspaceManager.get_n_workspaces(); i++) {
let workspace = workspaceManager.get_workspace_by_index(i);
if (workspace !== null) {
let indicator = new WorkspaceIndicator(
workspace,
i == active_index,
this.settings.get_boolean("skip-taskbar-mode"),
this.settings.get_boolean("change-on-click"),
);
this.box_layout.add_child(indicator);
this.indicators.push(indicator);
}
}
}
destroy_indicators() {
let i = 0;
for (; i < this.indicators.length; i++) {
this.indicators[i].destroy();
}
this.indicators = [];
}
destroy_panel_button() {
this.destroy_indicators();
if (this.box_layout !== null) this.box_layout.destroy();
if (this.panel_button !== null) this.panel_button.destroy();
this.box_layout = null;
this.panel_button = null;
}
}