Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SnapShotSettings.logic.ts

168 lines6.5 KB

1import type { ClientSettingsPatch, DesktopSnapShotState, SnapShotSound } from "@t3tools/contracts";
2import { msg } from "gt-react";
3​
4import { m, type MessageResolver } from "../../i18n/messages";
5import {
6 captureSetupBackend,
7 captureSetupDesktopName,
8 captureSetupAccessReady,
9 captureSetupMacPermissionsReady,
10} from "./SnapShotSetupDialog.logic";
11​
12export function snapShotStatus(
13 state: DesktopSnapShotState | null,
14 enabled: boolean,
15 resolve: MessageResolver = m,
16): string {
17 if (!state) return resolve(msg("Checking snapshots…"));
18 if (state.mode === "unavailable")
19 return state.message ?? resolve(msg("Not supported on this platform."));
20 if (!enabled) return resolve(msg("Turn this on to set up snapshots."));
21 return snapShotSetupSummary(state, enabled, resolve);
23​
24export function snapShotSetupSummary(
25 state: DesktopSnapShotState,
26 enabled: boolean,
27 resolve: MessageResolver = m,
28): string {
29 if (state.message) return resolve(msg("Capture needs attention"));
30 if (state.linuxBackend === "hyprland" && state.hyprlandHelper?.status !== "ready")
31 return state.hyprlandHelper?.status === "error"
32 ? resolve(msg("Check capture access in setup"))
33 : resolve(msg("Install the capture helper to continue"));
34 if (captureSetupBackend(state) === "gnome" && state.gnomeExtension?.status !== "enabled")
35 return resolve(msg("Set up active-window snapshots"));
36 if (captureSetupBackend(state) === "kde" && state.kdeHelper?.status !== "ready")
37 return state.kdeHelper?.status === "error"
38 ? resolve(msg("Check capture access in setup"))
39 : resolve(msg("Install the capture helper to continue"));
40 if (captureSetupBackend(state) === "picker")
41 return resolve(msg("Manual capture only. You'll choose a window each time"));
42 if (!enabled) return resolve(msg("Enable capture to continue"));
43 if (state.shortcutPending)
44 return state.linuxBackend === "hyprland"
45 ? resolve(msg("Connecting your shortcut…"))
46 : resolve(msg("Waiting for shortcut permission"));
47 if (state.shortcutVerified) return resolve(msg("Ready to capture"));
48 if (state.linuxBackend === "niri" && state.shortcutBinding)
49 return resolve(msg("Use your shortcut from another app"));
50 if (state.linuxBackend === "hyprland" && state.shortcutActionRegistered)
51 return resolve(msg("Use your shortcut from another app"));
52 if (state.shortcutRegistered)
53 return state.shortcutLabel ? resolve(msg("Ready to capture")) : resolve(msg("Shortcut saved"));
54 return resolve(msg("Finish shortcut setup"));
56​
57export function snapShotShortcutStatus(
58 state: DesktopSnapShotState | null,
59 resolve: MessageResolver = m,
60): string | null {
61 if (!state) return null;
62 if (state.linuxBackend === "hyprland") return state.shortcutMessage;
63 if (state.shortcutPending)
64 return resolve(msg("Approve the shortcut permission prompt to continue."));
65 if (state.shortcutRegistered)
66 return state.mode === "portal" ? null : resolve(msg("Shortcut saved."));
67 return state.shortcutMessage;
69​
70export function snapShotSetupButtonLabel(
71 state: DesktopSnapShotState | null,
72 resolve: MessageResolver = m,
73): string {
74 if (!state) return resolve(msg("Continue setup"));
75 if (captureSetupAccessReady(state)) return resolve(msg("Manage capture"));
76 const desktop = captureSetupDesktopName(state);
77 return desktop ? resolve(msg`Set up ${{ desktop }} capture`) : resolve(msg("Continue setup"));
79​
80export function snapShotSetupComplete(
81 state: DesktopSnapShotState | null,
82 includeAccessibility: boolean,
83): boolean {
84 if (state?.windows) return true;
85 return (
86 state?.macPermissions !== undefined &&
87 captureSetupAccessReady(state) &&
88 captureSetupMacPermissionsReady(state, includeAccessibility) &&
89 state.shortcutRegistered
90 );
92​
93export type SnapShotSoundSelection = SnapShotSound | "off";
94​
95export function snapShotFeedbackUnavailableMessage(
96 state: DesktopSnapShotState | null,
97 resolve: MessageResolver = m,
98): string | undefined {
99 if (state?.mode !== "portal" || state.linuxFeedbackAvailable) return undefined;
100 if (state.linuxBackend === "hyprland")
101 return state.hyprlandHelper?.status === "ready"
102 ? resolve(msg("Capture effects aren't available on this desktop."))
103 : resolve(msg("Install or update the capture helper to enable effects."));
104 if (state.linuxBackend === "niri")
105 return resolve(msg("Capture effects aren't available on Niri."));
106 if (state.linuxBackend === "kde")
107 return state.kdeHelper?.status === "ready"
108 ? resolve(msg("Capture effects aren't available on this desktop."))
109 : resolve(msg("Install or update the capture helper to enable effects."));
110 return state.linuxBackend === "gnome-extension"
111 ? resolve(msg("Update the GNOME extension, then sign out and back in to enable effects."))
112 : captureSetupBackend(state) === "gnome"
113 ? resolve(msg("Finish extension setup to enable effects."))
114 : resolve(msg("Capture effects aren't available on this desktop."));
116​
117export function snapShotDescription(
118 state: DesktopSnapShotState | null,
119 resolve: MessageResolver = m,
120): string {
121 return state?.mode === "portal" && captureSetupBackend(state) === "picker"
122 ? resolve(msg("Automatic capture isn't available here. Choose a window instead."))
123 : resolve(msg("Capture a window and attach it to your current draft."));
125​
126export function snapShotAccessibilityUnavailableMessage(
127 state: DesktopSnapShotState | null,
128 resolve: MessageResolver = m,
129): string | undefined {
130 if (state?.mode !== "portal") return undefined;
131 if (state.linuxBackend === "picker" || state.linuxBackend === "screenshot-portal")
132 return resolve(msg("This desktop only provides a screenshot."));
133 return undefined;
135​
136export function snapShotUnavailableMessage(
137 hasBridge: boolean,
138 resolve: MessageResolver = m,
139): string | undefined {
140 if (hasBridge) return undefined;
141 return typeof window !== "undefined" && window.desktopBridge
142 ? resolve(msg("Update the desktop app to use snapshots."))
143 : resolve(msg("Only available in the desktop app."));
145​
146export function snapShotSoundPatch(sound: SnapShotSoundSelection): ClientSettingsPatch {
147 return sound === "off"
148 ? { snapShotPlaySound: false }
149 : { snapShotPlaySound: true, snapShotSound: sound };
151​
152export function createRecordingRequestTracker() {
153 let currentRequest: symbol | null = null;
154​
155 return {
156 tryBegin() {
157 if (currentRequest) return null;
158 currentRequest = Symbol();
159 return currentRequest;
160 },
161 clear() {
162 currentRequest = null;
163 },
164 owns(request: symbol) {
165 return currentRequest === request;
166 },
167 };