-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
172 lines (147 loc) · 5.39 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
import GObject from 'gi://GObject';
import St from 'gi://St';
import Clutter from 'gi://Clutter';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import {Extension, gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
// D-Bus interface XML
const ifaceXml = `
<node>
<interface name="com.github.keyswift.WindowMonitor">
<method name="GetActiveWindow">
<arg type="s" direction="out" name="windowInfo"/>
</method>
<signal name="StatusChanged">
<arg type="b" name="isConnected"/>
</signal>
</interface>
</node>`;
const StatusIndicator = GObject.registerClass(
class StatusIndicator extends PanelMenu.Button {
constructor() {
super(0.0, 'Status Indicator');
this.icon = new St.Icon({
icon_name: 'face-smile-symbolic',
style_class: 'system-status-icon'
});
this.add_child(this.icon);
}
setSuccess() {
this.icon.icon_name = 'face-smile-symbolic';
}
setError() {
this.icon.icon_name = 'face-sad-symbolic';
}
});
export default class QuickSettingsExampleExtension extends Extension {
enable() {
// Initialize current window info
this._currentWindowInfo = {
class: '',
title: ''
};
// Create and add the status indicator to the panel
this._statusIndicator = new StatusIndicator();
Main.panel.addToStatusArea('status-indicator', this._statusIndicator, 0, 'right');
// Set up D-Bus interface
this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(ifaceXml, {
GetActiveWindow: () => {
// Show smile face when method is called
this._statusIndicator.setSuccess();
return JSON.stringify(this._currentWindowInfo);
}
});
this._dbusImpl.export(Gio.DBus.session, '/com/github/keyswift/WindowMonitor');
this._handlerId = global.display.connect('notify::focus-window', () => {
let window = global.display.focus_window;
let wmClass = '';
let title = '';
if (window) {
wmClass = window.get_wm_class();
title = window.get_title();
}
// Update current window info
this._currentWindowInfo = {
class: wmClass || '',
title: title || ''
};
// Send the wmClass and title to D-Bus server
this._sendWindowInfoToDbus(wmClass, title);
});
// Set initial state
const focusWindow = global.display.focus_window;
if (focusWindow) {
const wmClass = focusWindow.get_wm_class();
const title = focusWindow.get_title();
this._currentWindowInfo = {
class: wmClass || '',
title: title || ''
};
this._sendWindowInfoToDbus(wmClass, title);
}
}
_sendWindowInfoToDbus(wmClass, title) {
try {
// D-Bus parameters defined in the comment at the top of the file
const busName = 'com.github.keyswift.WinInfoReceiver';
const busPath = '/com/github/keyswift/WinInfoReceiver';
const busInterface = 'com.github.keyswift.WinInfoReceiver';
const methodName = 'UpdateActiveWindow';
// Create JSON object with both wmClass and title
const windowInfo = JSON.stringify({
class: wmClass || '',
title: title || ''
});
// Create a D-Bus proxy for the method call
// Use GLib.Variant to create the parameter for the method
let variant = new GLib.Variant('(s)', [windowInfo]);
// Send the window info to the D-Bus server
Gio.DBus.session.call(
busName,
busPath,
busInterface,
methodName,
variant,
null,
Gio.DBusCallFlags.NONE,
-1,
null,
(connection, result) => {
try {
connection.call_finish(result);
this._statusIndicator.setSuccess();
} catch (e) {
// Show cry icon on failure
this._statusIndicator.setError();
console.error(`Failed to send window info to D-Bus: ${e.message}`);
}
}
);
} catch (e) {
// Show cry icon on error
this._statusIndicator.setError();
console.error(`Error setting up D-Bus call: ${e.message}`);
}
}
disable() {
// Disconnect the signal handler
if (this._handlerId !== undefined) {
global.display.disconnect(this._handlerId);
this._handlerId = undefined;
}
// Unexport D-Bus interface
if (this._dbusImpl) {
this._dbusImpl.unexport();
this._dbusImpl = null;
}
// Remove and destroy the status indicator
if (this._statusIndicator) {
this._statusIndicator.destroy();
this._statusIndicator = null;
}
// Clear current window info
this._currentWindowInfo = null;
}
}