-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
184 lines (168 loc) · 6.51 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
const Settings = imports.ui.settings;
const Meta = imports.gi.Meta;
const Mainloop = imports.mainloop;
const Lang = imports.lang;
let settings = null;
let maximizeToWorkspace = null;
const WORKSPACE_IS_UNDEFINED = -1;
const STATE_OPENED = 1;
const STATE_MAXIMIZED = 2;
const STATE_UNMAXIMIZED = 3;
const STATE_CLOSED = 4;
function logMessage(message, alwaysLog = false) {
if (alwaysLog || settings.enableLogs) {
global.log(`[Maximize To Workspace] ${message}`);
}
}
function MaximizeToWorkspace() {
this._init();
}
MaximizeToWorkspace.prototype = {
_init: function() {
this._openedEventID = 0;
this._sizeChangeEventID = 0;
this._closedEventID = 0;
},
_opened: function (shellwm, actor) {
if (!actor) {
return;
}
let window = actor.get_meta_window();
window._maximizeToWorkspaceState = STATE_OPENED;
logMessage(`opened: ${window.get_id()} [${window.get_wm_class()}]`);
if (window.get_maximized() !== Meta.MaximizeFlags.BOTH) return;
this._maximize(shellwm, actor);
},
_handleResize: function(shellwm, actor, change) {
logMessage("handle resize");
if (change === Meta.SizeChange.MAXIMIZE) {
this._maximize(shellwm, actor);
} else if (change === Meta.SizeChange.UNMAXIMIZE) {
this._unmaximize(shellwm, actor);
}
},
_maximize: function(shellwm, actor) {
if (!actor) {
return;
}
let window = actor.get_meta_window();
logMessage(`maximized: ${window.get_id()} [${window.get_wm_class()}]`);
let workspace = window.get_workspace();
if (
workspace.index() !== 0
&& workspace.list_windows().filter(w => !w.is_on_all_workspaces()).length === 1
) {
window._previousWorkspaceIndex = WORKSPACE_IS_UNDEFINED;
return;
}
window._previousWorkspaceIndex = workspace.index();
window._maximizeToWorkspaceState = STATE_MAXIMIZED;
let currentTime = global.get_current_time();
let targetWorkspace = global.screen.append_new_workspace(false, currentTime);
Mainloop.timeout_add(500, () => {
logMessage(`maximized (change workspace): ${window.get_id()} [${window.get_wm_class()}]`);
if (!window || window._maximizeToWorkspaceState !== STATE_MAXIMIZED) return;
window.change_workspace(targetWorkspace);
targetWorkspace.activate(currentTime);
});
},
_unmaximize: function(shellwm, actor) {
if (!actor) {
return;
}
let window = actor.get_meta_window();
if (
window._previousWorkspaceIndex === undefined
|| window._previousWorkspaceIndex === WORKSPACE_IS_UNDEFINED
) {
return;
}
let targetWorkspace = window.get_workspace();
let previousWorkspaceIndex = window._previousWorkspaceIndex;
window._maximizeToWorkspaceState = STATE_UNMAXIMIZED;
let currentTime = global.get_current_time();
logMessage(`unmaximized: ${window.get_id()} [${window.get_wm_class()}] workspace #${previousWorkspaceIndex}`);
if (targetWorkspace.list_windows().filter(w => !w.is_on_all_workspaces()).length > 1) {
return;
}
Mainloop.timeout_add(500, () => {
logMessage(`unmaximized (change&remove workspace): ${window.get_id()} [${window.get_wm_class()}]`);
if (
window._maximizeToWorkspaceState !== STATE_UNMAXIMIZED
|| window._previousWorkspaceIndex === WORKSPACE_IS_UNDEFINED
) {
return;
}
window.change_workspace_by_index(previousWorkspaceIndex, false);
window.activate(currentTime);
global.screen.remove_workspace(targetWorkspace, currentTime);
window._previousWorkspaceIndex = WORKSPACE_IS_UNDEFINED;
});
},
_closed: function (shellwm, actor) {
if (!actor) {
return;
}
let window = actor.get_meta_window();
let currentWorkspace = window.get_workspace();
if (currentWorkspace.index() === 0) {
return;
}
if (currentWorkspace.list_windows().filter(w => !w.is_on_all_workspaces()).length !== 0) {
return;
}
logMessage(`closed: ${window.get_id()} [${window.get_wm_class()}]`);
window._previousWorkspaceIndex = WORKSPACE_IS_UNDEFINED;
window._maximizeToWorkspaceState = STATE_CLOSED;
let mainWorkspaceIndex = 0;
Mainloop.timeout_add(300, () => {
if (window._maximizeToWorkspaceState !== STATE_CLOSED) return;
let currentTime = global.get_current_time();
let previousWorkspace = global.screen.get_workspace_by_index(mainWorkspaceIndex);
previousWorkspace.activate(currentTime);
logMessage(`closed (remove workspace): ${window.get_id()} [${window.get_wm_class()}]`);
global.screen.remove_workspace(currentWorkspace, currentTime);
});
},
enable: function() {
logMessage("Enable");
this._openedEventID = global.window_manager.connect("map", Lang.bind(this, this._opened));
this._sizeChangeEventID = global.window_manager.connect("size-change", Lang.bind(this, this._handleResize));
this._closedEventID = global.window_manager.connect("destroy", Lang.bind(this, this._closed));
},
disable: function() {
if (this._openedEventID) {
global.window_manager.disconnect(this._openedEventID);
this._openedEventID = 0;
}
if (this._sizeChangeEventID) {
global.window_manager.disconnect(this._sizeChangeEventID);
this._sizeChangeEventID = 0;
}
if (this._closedEventID) {
global.window_manager.disconnect(this._closedEventID);
this._closedEventID = 0;
}
logMessage("Disable");
}
}
function SettingsMaximizeToWorkspace(uuid) {
this._init(uuid);
}
SettingsMaximizeToWorkspace.prototype = {
_init: function(uuid) {
this.settings = new Settings.ExtensionSettings(this, uuid);
this.settings.bindProperty(Settings.BindingDirection.IN, "enableLogs", "enableLogs", function() {
});
}
}
function init(metadata) {
settings = new SettingsMaximizeToWorkspace(metadata.uuid);
maximizeToWorkspace = new MaximizeToWorkspace();
}
function enable() {
maximizeToWorkspace.enable();
}
function disable() {
maximizeToWorkspace.disable();
}