-
Notifications
You must be signed in to change notification settings - Fork 9
/
extension.js
114 lines (99 loc) · 3.01 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
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const { Container, WorkspaceIndicator } = Me.imports.ui;
const WorkspaceManager = global.workspace_manager;
const Display = global.display;
const Main = imports.ui.main;
// Initial state
let indicators;
let container;
const handlers = [];
const getWorkspaceWindowCount = (workspace) => {
if (!workspace.n_windows) {
return 0;
}
const primary = Display.get_primary_monitor();
return workspace.list_windows().reduce((agg, window) => {
if (window.get_monitor() !== primary) {
return agg;
}
return agg + 1;
}, 0);
};
const initUI = () => {
const workspaceCount = ExtensionUtils.getSettings(
"org.gnome.desktop.wm.preferences"
).get_int("num-workspaces");
const currentWorkspace = WorkspaceManager.get_active_workspace_index();
indicators = new Array(workspaceCount).fill(null).map((_, i) => {
const active = i === currentWorkspace;
const workspace = WorkspaceManager.get_workspace_by_index(i);
return WorkspaceIndicator({
label: `${i + 1}`,
active,
windowCount: getWorkspaceWindowCount(workspace),
});
});
container = Container({ indicators });
};
const attachHandlers = () => {
// Listen for workspace switch
const changeHandler = WorkspaceManager.connect(
"active-workspace-changed",
() => {
const activeWorkspace = WorkspaceManager.get_active_workspace_index();
indicators.forEach((indicator, index) => {
index === activeWorkspace
? indicator.setActive()
: indicator.setInactive();
});
}
);
handlers.push(() => WorkspaceManager.disconnect(changeHandler));
const workspaceHandlers = indicators.map((instance, i) => {
const workspace = WorkspaceManager.get_workspace_by_index(i);
const updateWindowCount = () =>
instance.setWindowCount(getWorkspaceWindowCount(workspace));
const windowAdded = workspace.connect("window-added", updateWindowCount);
const windowRemoved = workspace.connect(
"window-removed",
updateWindowCount
);
const nodeClick = instance.node.connect("clicked", () => {
workspace.activate(global.get_current_time());
});
return () => {
workspace.disconnect(windowAdded);
workspace.disconnect(windowRemoved);
instance.node.disconnect(nodeClick);
};
});
handlers.push(...workspaceHandlers);
};
const detachHandlers = () => {
while (handlers.length > 0) {
handlers.pop()();
}
};
var init = () => {};
var enable = () => {
if (
ExtensionUtils.getSettings("org.gnome.mutter").get_boolean(
"dynamic-workspaces"
)
) {
Main.notifyError(
"Simply Workspaces Extension",
"Dynamic workspaces are not supported"
);
logError(Error("Dynamic workspaces are not supported."));
return;
}
initUI();
attachHandlers();
Main.panel._leftBox.insert_child_at_index(container.node, 0);
};
var disable = () => {
detachHandlers();
Main.panel._leftBox.remove_child(container.node);
};