Repository navigation
Expand file tree
/
Copy pathworkspaceManager.js
More file actions
382 lines (335 loc) · 16 KB
/
Copy pathworkspaceManager.js
File metadata and controls
382 lines (335 loc) · 16 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Customised Workspaces extension for Gnome 3
* This file is part of the Customised Workspaces Gnome Extension for Gnome 3
* Copyright (C) http://github.com/blipk
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// External imports
import Gio from "gi://Gio"
import GLib from "gi://GLib"
// import Meta from "gi://Meta"
import Shell from "gi://Shell"
import * as Main from "resource:///org/gnome/shell/ui/main.js"
// import * as workspace from "resource:///org/gnome/shell/ui/workspace.js"
import * as extensionUtils from "resource:///org/gnome/shell/misc/extensionUtils.js"
// Internal imports
import { WorksetsInstance as Me } from "./extension.js"
import * as dev from "./dev.js"
import * as utils from "./utils.js"
import * as workspaceIsolater from "./workspaceIsolater.js"
export class WorkspaceManager {
constructor() {
try {
Me.workspaceManager = this
this.signals = new utils.SignalHandler()
this.signals.add( global.window_manager, "switch-workspace", this._activeWorkspaceChanged.bind( this ) )
this.loadDefaults = true
this.noUpdate = false
this.activateIsolater( true )
this._workspaceUpdate()
this.loadDefaultWorksets()
this._workspaceUpdate()
} catch ( e ) { dev.log( e ) }
}
destroy() {
try {
this.switchToWorkspace( 0 )
this._workspaceUpdate( true )
this.signals.destroy()
} catch ( e ) { dev.log( e ) }
}
_workspaceUpdate( destroyClean = false ) {
if ( this.noUpdate ) return
try {
let min_workspaces = 1
let currents = []
utils.forEachEntry( Me.session.workspaceMaps, function ( workspaceMapKey, workspaceMapValues, i ) {
// Remove any worksets that are set to current on more than one workspace
if ( workspaceMapValues.currentWorkset != "" ) {
if ( currents.indexOf( workspaceMapValues.currentWorkset ) > -1 ) {
Me.session.workspaceMaps[workspaceMapKey].currentWorkset = ""
}
currents.push( workspaceMapValues.currentWorkset )
}
// Minimum workspaces should be one more than the workspace index that the last active workset is on
if ( !destroyClean ) {
let index = parseInt( workspaceMapKey.replace( "Workspace", "" ) )
if ( workspaceMapValues.currentWorkset != "" && index > min_workspaces - 2 ) {
min_workspaces = index + 2
}
}
}, this )
if ( min_workspaces < 2 ) min_workspaces = 2
const currentWorkspaces = []
const n_global_workspaces = Me.gWorkspaceManager.n_workspaces
// Make all workspaces non-persistent
for ( let i = n_global_workspaces - 1; i >= 0; i-- ) {
currentWorkspaces[i] = Me.gWorkspaceManager.get_workspace_by_index( i )
currentWorkspaces[i]._keepAliveId = false
}
for ( let i = 0; i < min_workspaces - 1; i++ ) {
if ( !currentWorkspaces[i] ) {
dev.log( `Could not find expected workspace at index ${i}` )
return
}
// If we have less than the minimum workspaces create new ones
if ( n_global_workspaces < min_workspaces - 1 ) {
if ( i >= n_global_workspaces ) {
Me.gWorkspaceManager.append_new_workspace( false, global.get_current_time() )
}
}
// Make all workspaces persistent again
currentWorkspaces[i]._keepAliveId = true
}
// Update the workspace view
Main.wm._workspaceTracker._checkWorkspaces()
} catch ( e ) { dev.log( e ) }
}
_activeWorkspaceChanged() {
try {
this._workspaceUpdate()
let foundActive = false
//Loop through worksets and load the one which is set to current
Me.session.Worksets.forEach( function ( workset, worksetIndex ) {
if ( Me.session.workspaceMaps["Workspace" + this.activeWorkspaceIndex].currentWorkset == workset.WorksetName ) {
foundActive = workset.WorksetName
Me.session.displayWorkset( Me.session.Worksets[worksetIndex] )
}
}, this )
// If there's not any active on the workspace,
// load any that are set to default here, or just display the background/favourites from the default
if ( foundActive === false ) this.loadDefaultWorksets()
this._workspaceUpdate()
// Update the overlay
if ( Me.workspaceViewManager ) Me.workspaceViewManager.refreshOverview()
} catch ( e ) { dev.log( e ) }
}
loadDefaultWorksets() {
try {
if ( !this.loadDefaults ) return
let found = false
Me.session.Worksets.forEach( function ( workset, worksetIndex ) {
let map = Me.session.workspaceMaps["Workspace" + this.activeWorkspaceIndex]
if ( map.currentWorkset == workset.WorksetName ||
( map.currentWorkset == "" && map.defaultWorkset == workset.WorksetName ) ) {
Me.session.displayWorkset( Me.session.Worksets[worksetIndex] )
Me.session.workspaceMaps["Workspace" + this.activeWorkspaceIndex].currentWorkset = workset.WorksetName
found = true
}
}, this )
if ( !found ) Me.session.displayWorkset( Me.session.DefaultWorkset, false, true )
} catch ( e ) { dev.log( e ) }
}
getWorkspaceWindows( workspaceIndex ) {
try {
if ( utils.isEmpty( workspaceIndex ) ) workspaceIndex = Me.gWorkspaceManager.get_active_workspace_index()
const workspace = Me.gWorkspaceManager.get_workspace_by_index( workspaceIndex )
const windows = workspace
.list_windows()
.filter( function ( w ) { return !w.is_skip_taskbar() && !w.is_on_all_workspaces() } )
return windows
} catch ( e ) { dev.log( e ) }
}
get activeWorkspace() {
this._workspaceUpdate()
return Me.gWorkspaceManager.get_active_workspace()
}
get activeWorkspaceIndex() {
this._workspaceUpdate()
return Me.gWorkspaceManager.get_active_workspace_index()
}
get NumGlobalWorkspaces() {
this._workspaceUpdate()
return Me.gWorkspaceManager.n_workspaces
}
get activeWorksetName() {
try {
this._workspaceUpdate()
if ( Me.session.workspaceMaps["Workspace" + this.activeWorkspaceIndex] == undefined ) {
let obj = { ["Workspace" + this.activeWorkspaceIndex]: { "defaultWorkset": "", "currentWorkset": "" } }
Object.assign( Me.session.workspaceMaps, obj )
Me.session.saveSession()
}
return Me.session.workspaceMaps["Workspace" + this.activeWorkspaceIndex].currentWorkset
} catch ( e ) { dev.log( e ) }
}
set activeWorksetName( workset ) {
try {
let name = workset.WorksetName || workset
this._workspaceUpdate()
if ( Me.session.workspaceMaps["Workspace" + this.activeWorkspaceIndex] == undefined ) {
let obj = { ["Workspace" + this.activeWorkspaceIndex]: { "defaultWorkset": "", "currentWorkset": name } }
Object.assign( Me.session.workspaceMaps, obj )
} else {
Me.session.workspaceMaps["Workspace" + this.activeWorkspaceIndex].currentWorkset = name
}
Me.session.saveSession()
} catch ( e ) { dev.log( e ) }
}
set lastWorkspaceActiveWorksetName( workset ) {
try {
let name = workset.WorksetName || workset
this._workspaceUpdate()
if ( Me.session.workspaceMaps["Workspace" + ( this.NumGlobalWorkspaces - 1 )] == undefined ) {
let obj = { ["Workspace" + ( this.NumGlobalWorkspaces - 1 )]: { "defaultWorkset": "", "currentWorkset": name } }
Object.assign( Me.session.workspaceMaps, obj )
} else {
Me.session.workspaceMaps["Workspace" + ( this.NumGlobalWorkspaces - 1 )].currentWorkset = name
}
Me.session.saveSession()
} catch ( e ) { dev.log( e ) }
}
_readProcEnviron( pid ) {
try {
let envFile = Gio.file_new_for_path( "/proc/" + pid + "/environ" )
let [success, contents] = envFile.load_contents( null )
if ( !success ) return ""
// /proc/[pid]/environ uses null bytes as separators
return new TextDecoder().decode( contents ).replace( /\0/g, "\n" ).toLowerCase()
} catch ( e ) {
return ""
}
}
_bamfHintCache = {}
_getBamfHint( pid ) {
if ( this._bamfHintCache[pid] ) return this._bamfHintCache[pid]
let env = this._readProcEnviron( pid )
let bamfIdx = env.indexOf( "bamf_desktop_file_hint=" )
if ( bamfIdx === -1 ) return ""
let hintValue = env.substring( bamfIdx + 23 )
let endIdx = hintValue.indexOf( "\n" )
if ( endIdx > -1 ) hintValue = hintValue.substring( 0, endIdx )
let result = GLib.path_get_basename( hintValue )
this._bamfHintCache[pid] = result
return result
}
getWorkspaceAppIds( workspaceIndex, excludeFavorites = true ) {
try {
if ( utils.isEmpty( workspaceIndex ) ) workspaceIndex = Me.gWorkspaceManager.get_active_workspace_index()
const windowTracker = Shell.WindowTracker.get_default()
const windows = this.getWorkspaceWindows( workspaceIndex )
let appIDs = []
windows.forEach( function ( w ) {
let id = windowTracker.get_window_app( w ).get_id()
// Snap installed applications are launched as window backed,
// so need to get the hint that is set for ubuntus BAMF daemon from the apps environment vars
// Possible alternative if the BAMF method proves unreliable: Shell.AppSystem.search(w.get_wm_class_instance())
if ( id.indexOf( "window:" ) > -1 ) {
let bamfId = this._getBamfHint( w.get_pid() )
if ( bamfId ) id = bamfId
}
appIDs.push( id )
}, this )
//remove duplicates from apps with multiple windows
appIDs = appIDs.filter( function ( item, pos ) {
return appIDs.indexOf( item ) === pos
}, this )
//remove un-apped windows
appIDs = appIDs.filter( function ( item, pos ) {
if ( item.match( "window:" ) ) return false
return true
}, this )
if ( excludeFavorites ) {
let favApps = global.settings.get_strv( "favorite-apps" )
appIDs = appIDs.filter( ( item, pos ) => {
let ret = true
favApps.forEach( function ( favItem ) {
if ( item.match( favItem ) ) ret = false
}, this )
return ret
}, this )
}
return appIDs
} catch ( e ) { dev.log( e ) }
}
switchToWorkspace( index = 0 ) {
try {
index = parseInt( index, 10 )
this._workspaceUpdate()
Me.gWorkspaceManager.get_workspace_by_index( index ).activate( 0 )
this._workspaceUpdate()
} catch ( e ) { dev.log( e ) }
}
_moveWindowsToWorkspace() {
//TO DO
}
activateIsolater( init = false ) {
try {
if ( !init )
Me.session.activeSession.Options.IsolateWorkspaces = !Me.session.activeSession.Options.IsolateWorkspaces
if ( Me.session.activeSession.Options.IsolateWorkspaces ) {
// Defer to isolaters in other extensions
const hasDash2Panel = Boolean( Me.gExtensions.dash2panel() ) && Boolean( Me.gExtensions.dash2panelSettings() )
const enabledDash2Panel = hasDash2Panel && Me.gExtensions.dash2panel().state === extensionUtils.ExtensionState.ENABLED
const hasDash2Dock = Boolean( Me.gExtensions.dash2dock() ) && Boolean( Me.gExtensions.dash2dockSettings() )
const enabledDash2Dock = hasDash2Dock && Me.gExtensions.dash2dock().state === extensionUtils.ExtensionState.ENABLED
// The .state check above doesn't actually work as it's held on Main.extensionManager
// dev.log(
// `hasDash2Panel ${hasDash2Panel}`,
// `enabledDash2Panel ${enabledDash2Panel}`,
// `hasDash2Dock ${hasDash2Dock}`,
// `enabledDash2Dock ${enabledDash2Dock}`
// )
if ( ( hasDash2Panel || hasDash2Dock ) && Me.workspaceIsolater ) {
Me.workspaceIsolater.destroy()
delete Me.workspaceIsolater
}
if ( hasDash2Panel )
Me.gExtensions.dash2panelSettings().set_boolean( "isolate-workspaces", true )
if ( hasDash2Dock )
Me.gExtensions.dash2dockSettings().set_boolean( "isolate-workspaces", true )
if ( !enabledDash2Panel && !enabledDash2Dock ) {
Me.workspaceIsolater = new workspaceIsolater.WorkspaceIsolator()
workspaceIsolater.WorkspaceIsolator.refresh()
}
} else {
if ( Me.gExtensions.dash2panelSettings() )
Me.gExtensions.dash2panelSettings().set_boolean( "isolate-workspaces", false )
if ( Me.gExtensions.dash2dockSettings() )
Me.gExtensions.dash2dockSettings().set_boolean( "isolate-workspaces", false )
if ( Me.workspaceIsolater ) {
Me.workspaceIsolater.destroy()
delete Me.workspaceIsolater
}
}
} catch ( e ) { dev.log( e ) }
Me.session.applySession()
}
spawnOnSwitch( workset ) {
const cmd = Me.session.activeSession.Options.CliSwitch.replaceAll( "$CWORKSPACE", GLib.shell_quote( workset.WorksetName ) )
const args = [
"/usr/bin/env",
"bash",
"-c",
cmd
]
utils.spawnWithCallback(
null, args, GLib.get_environ(), 0, null,
( stdout ) => {
try {
if ( !stdout ) return
dev.log( "Workspace switched command output:", stdout )
} catch ( e ) { dev.log( e ) }
}
)
}
}