-
Notifications
You must be signed in to change notification settings - Fork 12
/
extension.js
142 lines (119 loc) · 3.73 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
/*
* GNOME Shell Extension: PiP on top
* Developer: Rafostar
*/
import Meta from 'gi://Meta';
import {Extension, gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
export default class PipOnTop extends Extension
{
enable()
{
this._lastWorkspace = null;
this._windowAddedId = 0;
this._windowRemovedId = 0;
this.settings = this.getSettings();
this._settingsChangedId = this.settings.connect(
'changed', this._onSettingsChanged.bind(this));
this._switchWorkspaceId = global.window_manager.connect_after(
'switch-workspace', this._onSwitchWorkspace.bind(this));
this._onSwitchWorkspace();
}
disable()
{
this.settings.disconnect(this._settingsChangedId);
this.settings = null;
global.window_manager.disconnect(this._switchWorkspaceId);
if (this._lastWorkspace) {
this._lastWorkspace.disconnect(this._windowAddedId);
this._lastWorkspace.disconnect(this._windowRemovedId);
}
this._lastWorkspace = null;
this._settingsChangedId = 0;
this._switchWorkspaceId = 0;
this._windowAddedId = 0;
this._windowRemovedId = 0;
let actors = global.get_window_actors();
if (actors) {
for (let actor of actors) {
let window = actor.meta_window;
if (!window) continue;
if (window._isPipAble) {
if (window.above)
window.unmake_above();
if (window.on_all_workspaces)
window.unstick();
}
this._onWindowRemoved(null, window);
}
}
}
_onSettingsChanged(settings, key)
{
switch (key) {
case 'stick':
/* Updates already present windows */
this._onSwitchWorkspace();
break;
default:
break;
}
}
_onSwitchWorkspace()
{
let workspace = global.workspace_manager.get_active_workspace();
let wsWindows = global.display.get_tab_list(Meta.TabList.NORMAL, workspace);
if (this._lastWorkspace) {
this._lastWorkspace.disconnect(this._windowAddedId);
this._lastWorkspace.disconnect(this._windowRemovedId);
}
this._lastWorkspace = workspace;
this._windowAddedId = this._lastWorkspace.connect(
'window-added', this._onWindowAdded.bind(this));
this._windowRemovedId = this._lastWorkspace.connect(
'window-removed', this._onWindowRemoved.bind(this));
/* Update state on already present windows */
if (wsWindows) {
for (let window of wsWindows)
this._onWindowAdded(workspace, window);
}
}
_onWindowAdded(workspace, window)
{
if (!window._notifyPipTitleId) {
window._notifyPipTitleId = window.connect_after(
'notify::title', this._checkTitle.bind(this));
}
this._checkTitle(window);
}
_onWindowRemoved(workspace, window)
{
if (window._notifyPipTitleId) {
window.disconnect(window._notifyPipTitleId);
window._notifyPipTitleId = null;
}
if (window._isPipAble)
window._isPipAble = null;
}
_checkTitle(window)
{
if (!window.title)
return;
/* Check both translated and untranslated string for
* users that prefer running applications in English */
let isPipWin = (window.title == 'Picture-in-Picture'
|| window.title == _('Picture-in-Picture')
|| window.title == 'Picture in picture'
|| window.title == 'Picture-in-picture'
|| window.title.endsWith(' - PiP')
/* Telegram support */
|| window.title == 'TelegramDesktop');
if (isPipWin || window._isPipAble) {
let un = (isPipWin) ? '' : 'un';
window._isPipAble = true;
window[`${un}make_above`]();
/* Change stick if enabled or unstick PipAble windows */
un = (isPipWin && this.settings.get_boolean('stick')) ? '' : 'un';
window[`${un}stick`]();
}
}
}