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
|
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import St from 'gi://St';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
const BUS_NAME = 'org.parley.Transcription1';
const OBJECT_PATH = '/org/parley/Transcription1';
const Interface = `<node>
<interface name="org.parley.Transcription1">
<method name="StartRecording"/>
<method name="StopAndTranscribe"/>
<method name="Toggle"/>
<method name="Cancel"/>
<method name="CopyLastTranscript"/>
<method name="InsertLastTranscript"/>
<method name="OpenTranscriptFolder"/>
<property name="State" type="s" access="read"/>
<property name="LastTranscript" type="s" access="read"/>
<property name="LastTranscriptPath" type="s" access="read"/>
<property name="LastError" type="s" access="read"/>
<property name="InsertionMode" type="s" access="read"/>
<property name="AutoInsert" type="b" access="read"/>
<signal name="StateChanged"><arg name="state" type="s"/></signal>
<signal name="TranscriptReady"><arg name="text" type="s"/><arg name="path" type="s"/></signal>
<signal name="Error"><arg name="code" type="s"/><arg name="message" type="s"/></signal>
<signal name="InsertionFinished"><arg name="backend" type="s"/><arg name="success" type="b"/><arg name="message" type="s"/></signal>
</interface>
</node>`;
const ParleyProxy = Gio.DBusProxy.makeProxyWrapper(Interface);
const PRESENTATION = {
idle: ['audio-input-microphone-symbolic', 'Parley is ready', 'system-status-icon'],
recording: ['media-record-symbolic', 'Recording', 'system-status-icon parley-recording'],
transcribing: ['emblem-synchronizing-symbolic', 'Transcribing locally', 'system-status-icon parley-processing'],
inserting: ['edit-paste-symbolic', 'Inserting transcript', 'system-status-icon parley-processing'],
error: ['dialog-error-symbolic', 'Parley error', 'system-status-icon parley-error'],
};
const Indicator = GObject.registerClass(
class Indicator extends PanelMenu.Button {
_init(extension) {
super._init(0.0, 'Parley Dictation');
this._extension = extension;
this._destroyed = false;
this._proxy = null;
this._dbusSignals = [];
this._gobjectSignals = [];
this._icon = new St.Icon({
icon_name: 'audio-input-microphone-symbolic',
style_class: 'system-status-icon',
});
this.add_child(this._icon);
this._statusItem = new PopupMenu.PopupMenuItem('Connecting to Parley…', {
reactive: false,
});
this.menu.addMenuItem(this._statusItem);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this._toggleItem = new PopupMenu.PopupMenuItem('Start recording');
this._toggleItem.connect('activate', () => this._call('Toggle'));
this.menu.addMenuItem(this._toggleItem);
this._cancelItem = new PopupMenu.PopupMenuItem('Cancel');
this._cancelItem.connect('activate', () => this._call('Cancel'));
this.menu.addMenuItem(this._cancelItem);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this._copyItem = new PopupMenu.PopupMenuItem('Copy last transcript');
this._copyItem.connect('activate', () => this._call('CopyLastTranscript'));
this.menu.addMenuItem(this._copyItem);
this._openItem = new PopupMenu.PopupMenuItem('Open transcript folder');
this._openItem.connect('activate', () => this._call('OpenTranscriptFolder'));
this.menu.addMenuItem(this._openItem);
this._setUnavailable('Connecting to Parley…');
this._connectProxy();
}
_connectProxy() {
this._proxy = new ParleyProxy(
Gio.DBus.session,
BUS_NAME,
OBJECT_PATH,
(proxy, error) => {
if (this._destroyed)
return;
if (error) {
console.error(`Parley D-Bus connection failed: ${error.message}`);
this._setUnavailable('Parley service unavailable');
return;
}
this._dbusSignals.push(
proxy.connectSignal('StateChanged', () => this._refresh()),
proxy.connectSignal('TranscriptReady', () => this._refresh()),
proxy.connectSignal('Error', (_proxy, _sender, [code, message]) => {
console.error(`Parley ${code}: ${message}`);
this._refresh();
})
);
this._gobjectSignals.push(
proxy.connect('g-properties-changed', () => this._refresh()),
proxy.connect('notify::g-name-owner', () => this._refresh())
);
this._refresh();
}
);
}
_refresh() {
if (!this._proxy?.g_name_owner) {
this._setUnavailable('Parley service unavailable');
return;
}
const state = this._proxy.State ?? 'idle';
const [iconName, defaultStatus, styleClass] =
PRESENTATION[state] ?? PRESENTATION.error;
let status = defaultStatus;
if (state === 'error' && this._proxy.LastError)
status = `Error: ${this._proxy.LastError}`;
this._icon.icon_name = iconName;
this._icon.set_style_class_name(styleClass);
this.accessible_name = status;
this._statusItem.label.text = status;
this._toggleItem.label.text = state === 'recording'
? 'Stop and transcribe'
: 'Start recording';
this._toggleItem.setSensitive(['idle', 'recording', 'error'].includes(state));
this._cancelItem.setSensitive(['recording', 'transcribing'].includes(state));
this._copyItem.setSensitive(Boolean(this._proxy.LastTranscriptPath));
this._openItem.setSensitive(true);
}
_setUnavailable(message) {
this._icon.icon_name = 'microphone-disabled-symbolic';
this._icon.set_style_class_name('system-status-icon parley-error');
this.accessible_name = message;
this._statusItem.label.text = message;
this._toggleItem?.setSensitive(false);
this._cancelItem?.setSensitive(false);
this._copyItem?.setSensitive(false);
this._openItem?.setSensitive(false);
}
_call(method) {
if (!this._proxy?.g_name_owner)
return;
this._proxy[`${method}Remote`]((_result, error) => {
if (error) {
console.error(`Parley ${method} failed: ${error.message}`);
}
});
}
destroy() {
this._destroyed = true;
if (this._proxy) {
for (const signal of this._dbusSignals)
this._proxy.disconnectSignal(signal);
for (const signal of this._gobjectSignals)
this._proxy.disconnect(signal);
}
this._dbusSignals = [];
this._gobjectSignals = [];
this._proxy = null;
super.destroy();
}
});
export default class ParleyExtension extends Extension {
enable() {
this._indicator = new Indicator(this);
Main.panel.addToStatusArea(this.uuid, this._indicator);
}
disable() {
this._indicator?.destroy();
this._indicator = null;
}
}
|