t3-code-android-nightly/apps/desktop/gnome-extension/captureService.js
Bilal Bakr 299404a754
feat(desktop): add cross-platform window capture (#8103)
Co-authored-by: Dara Adedeji <daraadedeji07@gmail.com>
Co-authored-by: Dara Adedeji <76637177+SunkenInTime@users.noreply.github.com>
Co-authored-by: Wout Stiens <71498452+StiensWout@users.noreply.github.com>
Co-authored-by: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com>
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-07 18:21:53 -07:00

63 lines
1.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// This policy is independent of Shell so lifecycle and authorization can be tested without a desktop.
export const CLIENT_NAMES = [
"com.t3tools.T3Code.SnapShot",
"com.t3tools.T3Code.Development.SnapShot",
];
export function isWaylandSession(meta) {
// GNOME 50 removed the X11 compositor and this API. GNOME 45–49 still need the check.
return typeof meta.is_wayland_compositor !== "function" || meta.is_wayland_compositor();
}
export class CaptureService {
constructor({ getNameOwner, isAvailable, takeSnapshot, getProcessId, beginFeedback }) {
this._getNameOwner = getNameOwner;
this._isAvailable = isAvailable;
this._takeSnapshot = takeSnapshot;
this._getProcessId = getProcessId;
this._beginFeedback = beginFeedback;
this._enabled = true;
this._busy = false;
}
disable() {
this._enabled = false;
}
_checkSession() {
if (!this._enabled || !this._isAvailable()) {
throw new Error("SnapShots are unavailable in this session.");
}
}
async capture(sender, options) {
this._checkSession();
if (this._busy) throw new Error("A snapshot is already in progress.");
this._busy = true;
try {
let allowed = false;
for (const name of CLIENT_NAMES) {
const owner = await this._getNameOwner(name);
if (owner === sender) {
allowed = true;
break;
}
}
if (!allowed) throw new Error("Only T3 Code may request a snapshot.");
const pid = options ? await this._getProcessId(sender) : undefined;
this._checkSession();
const snapshot = await this._takeSnapshot(options?.animate ?? false);
// Never deliver pixels after locking the session or disabling the extension.
this._checkSession();
if (options) {
return {
...snapshot,
animationStarted: this._beginFeedback(sender, pid, snapshot, options),
};
}
return snapshot;
} finally {
this._busy = false;
}
}
}