This repository was archived by the owner on Mar 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.qml
More file actions
373 lines (322 loc) · 11.5 KB
/
Main.qml
File metadata and controls
373 lines (322 loc) · 11.5 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
import QtQuick
import Quickshell
import Quickshell.Io
import qs.Services.UI
import "I18n.js" as I18n
Item {
id: root
required property var pluginApi
visible: false
width: 0
height: 0
readonly property var defaults: pluginApi && pluginApi.manifest && pluginApi.manifest.metadata
? (pluginApi.manifest.metadata.defaultSettings || {})
: ({})
readonly property var pluginSettings: pluginApi ? (pluginApi.pluginSettings || {}) : ({})
readonly property string obsctlPath: pluginApi ? (pluginApi.pluginDir + "/scripts/obsctl") : ""
readonly property string openPathHelper: pluginApi ? (pluginApi.pluginDir + "/scripts/open-path") : ""
readonly property string configuredVideosPath: pluginSettings.videosPath !== undefined
? String(pluginSettings.videosPath).trim()
: String(defaults.videosPath !== undefined ? defaults.videosPath : "")
readonly property string videosPath: configuredVideosPath !== ""
? configuredVideosPath
: (Quickshell.env("XDG_VIDEOS_DIR") || ((Quickshell.env("HOME") || "") + "/Videos"))
readonly property int pollIntervalMs: Math.max(
750,
Number(pluginSettings.pollIntervalMs !== undefined
? pluginSettings.pollIntervalMs
: (defaults.pollIntervalMs !== undefined ? defaults.pollIntervalMs : 2500))
)
readonly property string leftClickAction: pluginSettings.leftClickAction !== undefined
? String(pluginSettings.leftClickAction)
: String(defaults.leftClickAction !== undefined ? defaults.leftClickAction : "panel")
readonly property bool showBarWhenRecording: pluginSettings.showBarWhenRecording !== undefined
? Boolean(pluginSettings.showBarWhenRecording)
: Boolean(defaults.showBarWhenRecording)
readonly property bool showBarWhenReplay: pluginSettings.showBarWhenReplay !== undefined
? Boolean(pluginSettings.showBarWhenReplay)
: Boolean(defaults.showBarWhenReplay)
readonly property bool showControlCenterWhenRecording: pluginSettings.showControlCenterWhenRecording !== undefined
? Boolean(pluginSettings.showControlCenterWhenRecording)
: Boolean(defaults.showControlCenterWhenRecording)
readonly property bool showControlCenterWhenReplay: pluginSettings.showControlCenterWhenReplay !== undefined
? Boolean(pluginSettings.showControlCenterWhenReplay)
: Boolean(defaults.showControlCenterWhenReplay)
readonly property bool showControlCenterWhenReady: pluginSettings.showControlCenterWhenReady !== undefined
? Boolean(pluginSettings.showControlCenterWhenReady)
: Boolean(defaults.showControlCenterWhenReady)
property bool obsRunning: false
property bool websocket: false
property bool recording: false
property bool replayBuffer: false
property int recordDurationMs: 0
property int displayRecordDurationMs: 0
readonly property bool connected: obsRunning && websocket
readonly property bool showInBar: (recording && showBarWhenRecording)
|| (replayBuffer && showBarWhenReplay)
readonly property bool showInControlCenter: (recording && showControlCenterWhenRecording)
|| (replayBuffer && showControlCenterWhenReplay)
|| (connected && showControlCenterWhenReady)
readonly property string primaryActionText: leftClickAction === "toggle-record"
? tr("actions.primary.toggle_record", "toggles recording")
: tr("actions.primary.open_controls", "opens controls")
function tr(key, fallback, interpolations) {
return I18n.tr(pluginApi, key, fallback, interpolations);
}
function applyStatus(payload) {
obsRunning = Boolean(payload && payload.obsRunning);
websocket = Boolean(payload && payload.websocket);
recording = Boolean(payload && payload.recording);
replayBuffer = Boolean(payload && payload.replayBuffer);
recordDurationMs = Math.max(0, Number(payload && payload.recordDurationMs ? payload.recordDurationMs : 0));
displayRecordDurationMs = recording ? recordDurationMs : 0;
displayTimer.running = recording;
}
function resetStatus() {
applyStatus({
"obsRunning": false,
"websocket": false,
"recording": false,
"replayBuffer": false,
"recordDurationMs": 0
});
}
function refresh() {
if (!statusProcess.running) {
statusProcess.running = true;
}
}
function runAction(action) {
if (!actionProcess.running) {
pendingAction = action;
actionProcess.running = true;
}
}
function launchObs() {
runAction("launch");
}
function toggleRecord() {
runAction("toggle-record");
}
function toggleReplay() {
runAction("toggle-replay");
}
function saveReplay() {
runAction("save-replay");
}
function openVideos() {
if (videosPath !== "") {
Quickshell.execDetached([openPathHelper, videosPath]);
}
}
function showActionToast(payload) {
if (!payload) {
return;
}
const translated = translatedActionPayload(payload);
if (!translated.title) {
return;
}
const actionLabel = payload.openVideos ? tr("toast.actions.open_videos", "Open Videos") : "";
const actionCallback = payload.openVideos ? function () { root.openVideos(); } : null;
ToastService.showNotice(translated.title, translated.body, "", 3200, actionLabel, actionCallback);
}
function translatedActionPayload(payload) {
if (!payload) {
return { title: "", body: "" };
}
switch (payload.event) {
case "record-started":
return {
title: tr("toast.record_started.title", "OBS recording started"),
body: tr("toast.record_started.body", "Local recording is running."),
};
case "record-started-launch":
return {
title: tr("toast.record_started_launch.title", "OBS recording started"),
body: tr("toast.record_started_launch.body", "OBS launched in the tray."),
};
case "record-stopped":
return {
title: tr("toast.record_stopped.title", "OBS recording stopped"),
body: tr("toast.record_stopped.body", "Recording saved to Videos."),
};
case "replay-started":
return {
title: tr("toast.replay_started.title", "OBS replay buffer started"),
body: tr("toast.replay_started.body", "Replay buffer is active."),
};
case "replay-started-launch":
return {
title: tr("toast.replay_started_launch.title", "OBS replay buffer started"),
body: tr("toast.replay_started_launch.body", "OBS launched in the tray."),
};
case "replay-stopped":
return {
title: tr("toast.replay_stopped.title", "OBS replay buffer stopped"),
body: tr("toast.replay_stopped.body", "Instant replay is off."),
};
case "replay-saved":
return {
title: tr("toast.replay_saved.title", "OBS replay saved"),
body: tr("toast.replay_saved.body", "Saved the last replay buffer to Videos."),
};
case "offline":
return {
title: tr("toast.offline.title", "OBS is offline"),
body: tr("toast.offline.body", "Launch OBS to use recording controls."),
};
default:
return {
title: payload.title || "",
body: payload.body || "",
};
}
}
function showProcessErrorToast(detail) {
const body = detail && detail !== ""
? detail
: tr("toast.error.body", "Check the OBS helper output.");
ToastService.showNotice(
tr("toast.error.title", "OBS control failed"),
body,
"",
4200
);
}
function openControls(screen, anchorItem) {
if (pluginApi && screen) {
pluginApi.togglePanel(screen, anchorItem);
}
}
function togglePanelFromIpc() {
pluginApi?.withCurrentScreen(function(screen) {
root.openControls(screen, null);
});
}
function runPrimaryAction(screen, anchorItem) {
if (leftClickAction === "toggle-record") {
runSecondaryAction();
return;
}
openControls(screen, anchorItem);
}
function runSecondaryAction() {
if (!obsRunning) {
launchObs();
} else if (connected) {
toggleRecord();
} else {
refresh();
}
}
function runMiddleAction() {
if (connected) {
toggleReplay();
}
}
Component.onCompleted: refresh()
property string pendingAction: ""
Timer {
id: actionRefreshTimer
interval: 900
running: false
repeat: false
onTriggered: root.refresh()
}
Timer {
id: pollTimer
interval: root.pollIntervalMs
running: true
repeat: true
onTriggered: root.refresh()
}
Timer {
id: displayTimer
interval: 1000
running: false
repeat: true
onTriggered: {
if (!root.recording) {
root.displayRecordDurationMs = 0;
running = false;
return;
}
root.displayRecordDurationMs += 1000;
}
}
IpcHandler {
target: "plugin:obs-control"
function togglePanel() {
root.togglePanelFromIpc();
}
function refreshStatus() {
root.refresh();
}
function launchObs() {
root.launchObs();
}
function toggleRecord() {
root.toggleRecord();
}
function toggleReplay() {
root.toggleReplay();
}
function saveReplay() {
root.saveReplay();
}
function openVideos() {
root.openVideos();
}
function primaryAction() {
if (root.leftClickAction === "toggle-record") {
root.runSecondaryAction();
return;
}
root.togglePanelFromIpc();
}
function secondaryAction() {
root.runSecondaryAction();
}
function middleAction() {
root.runMiddleAction();
}
}
Process {
id: actionProcess
running: false
command: pendingAction === "" ? [] : [root.obsctlPath, pendingAction]
stdout: StdioCollector {}
stderr: StdioCollector {}
onExited: function(exitCode) {
const output = String(stdout.text || "").trim();
const errorOutput = String(stderr.text || "").trim();
if (exitCode === 0 && output !== "") {
try {
root.showActionToast(JSON.parse(output));
} catch (e) {}
} else if (exitCode !== 0) {
root.showProcessErrorToast(errorOutput);
}
pendingAction = "";
actionRefreshTimer.restart();
}
}
Process {
id: statusProcess
running: false
command: [root.obsctlPath, "status"]
stdout: StdioCollector {}
onExited: function(exitCode) {
if (exitCode !== 0) {
root.resetStatus();
return;
}
try {
root.applyStatus(JSON.parse(String(stdout.text || "").trim() || "{}"));
} catch (e) {
root.resetStatus();
}
}
}
}