|
| 1 | +// https://unix.stackexchange.com/a/706478/379240 |
| 2 | + |
| 3 | +function print(str) { |
| 4 | + console.info('Nyrna: ' + str); |
| 5 | +} |
| 6 | + |
| 7 | +let windows = workspace.windowList(); |
| 8 | +print('Found ' + windows.length + ' windows'); |
| 9 | + |
| 10 | +function updateWindowsOnDBus(windows) { |
| 11 | + let windowsList = []; |
| 12 | + |
| 13 | + for (let window of windows) { |
| 14 | + windowsList.push({ |
| 15 | + caption: window.caption, |
| 16 | + pid: window.pid, |
| 17 | + internalId: window.internalId, |
| 18 | + onCurrentDesktop: isWindowOnCurrentDesktop(window), |
| 19 | + }); |
| 20 | + } |
| 21 | + |
| 22 | + callDBus( |
| 23 | + 'codes.merritt.Nyrna', |
| 24 | + '/', |
| 25 | + 'codes.merritt.Nyrna', |
| 26 | + 'updateWindows', |
| 27 | + JSON.stringify(windowsList), |
| 28 | + (result) => { |
| 29 | + if (result) { |
| 30 | + print('Successfully updated windows on DBus'); |
| 31 | + } else { |
| 32 | + print('Failed to update windows on DBus'); |
| 33 | + } |
| 34 | + } |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +function isWindowOnCurrentDesktop(window) { |
| 39 | + let windowDesktops = Object.values(window.desktops); |
| 40 | + let windowIsOnCurrentDesktop = window.onAllDesktops; |
| 41 | + |
| 42 | + if (!windowIsOnCurrentDesktop) { |
| 43 | + for (let windowDesktop of windowDesktops) { |
| 44 | + if (windowDesktop.id === workspace.currentDesktop.id) { |
| 45 | + windowIsOnCurrentDesktop = true; |
| 46 | + break; |
| 47 | + } else { |
| 48 | + windowIsOnCurrentDesktop = false; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return windowIsOnCurrentDesktop; |
| 54 | +} |
| 55 | + |
| 56 | +function updateCurrentDesktopOnDBus() { |
| 57 | + print('Current desktop id: ' + workspace.currentDesktop.id); |
| 58 | + |
| 59 | + callDBus( |
| 60 | + 'codes.merritt.Nyrna', |
| 61 | + '/', |
| 62 | + 'codes.merritt.Nyrna', |
| 63 | + 'updateCurrentDesktop', |
| 64 | + workspace.currentDesktop, |
| 65 | + (result) => { |
| 66 | + if (result) { |
| 67 | + print('Successfully updated current desktop on DBus'); |
| 68 | + } else { |
| 69 | + print('Failed to update current desktop on DBus'); |
| 70 | + } |
| 71 | + } |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +updateCurrentDesktopOnDBus(); |
| 76 | +updateWindowsOnDBus(windows); |
| 77 | + |
| 78 | +workspace.currentDesktopChanged.connect(() => { |
| 79 | + print('Current desktop changed'); |
| 80 | + updateCurrentDesktopOnDBus(); |
| 81 | + updateWindowsOnDBus(windows); |
| 82 | +}); |
| 83 | + |
| 84 | +workspace.windowAdded.connect(window => { |
| 85 | + print('Window added: ' + window.caption); |
| 86 | + windows.push(window); |
| 87 | + updateWindowsOnDBus(windows); |
| 88 | +}); |
| 89 | + |
| 90 | +workspace.windowRemoved.connect(window => { |
| 91 | + print('Window removed: ' + window.caption); |
| 92 | + windows = windows.filter(w => w.internalId !== window.internalId); |
| 93 | + updateWindowsOnDBus(windows); |
| 94 | +}); |
0 commit comments