Repository navigation
Expand file tree
/
Copy pathui.ts
More file actions
336 lines (319 loc) · 11.8 KB
/
Copy pathui.ts
File metadata and controls
336 lines (319 loc) · 11.8 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
// The views shared by the commands: cameras, their controls, and the snapshots.
import {
type Action,
action,
type Color,
copy,
field,
form,
Icons,
type Item,
list,
type ListSpec,
meta,
open,
pop,
refresh,
submenu,
toast,
} from "@backslash/api";
import {
autoPartner,
clamp,
coarseStep,
type Control,
displayValue,
formatSummary,
formatTime,
isInactive,
isReadOnly,
nextMenuValue,
niceName,
parseValue,
ratio,
sortControls,
} from "./lib.ts";
import {
type Camera,
controls,
DEFAULT_FOLDER,
setControls,
type Shot,
shots,
snapshot,
tilde,
trash,
} from "./system.ts";
export type Prefs = Record<string, unknown>;
export const folderOf = (prefs: Prefs) => String(prefs.folder || DEFAULT_FOLDER);
const formatBytes = (n: number) =>
n >= 1024 * 1024 ? `${(n / 1024 / 1024).toFixed(1)} MB` : `${Math.max(1, Math.round(n / 1024))} KB`;
// ---------------------------------------------------------------------------
// Snapshots
export async function takeAndShow(cam: Camera, prefs: Prefs) {
await toast(`Smile! Taking a picture with ${cam.device.name}…`);
const path = await snapshot(cam, folderOf(prefs), prefs);
return snapshotsView(prefs, cam, path);
}
export function snapshotAction(cam: Camera, prefs: Prefs, o: { shortcut?: string; section?: string } = {}): Action {
return action("Take Snapshot", () => takeAndShow(cam, prefs), { icon: Icons.camera, ...o });
}
function shotRow(s: Shot, prefs: Prefs, cam?: Camera): Item {
return {
id: s.path,
title: s.name,
subtitle: formatTime(s.mtime),
icon: { source: Icons.image, tint: "blue" },
accessories: [{ text: formatBytes(s.size) }],
detail: {
image: s.path,
metadata: [
meta.text("Taken", formatTime(s.mtime), { icon: Icons.clock }),
meta.text("Size", formatBytes(s.size)),
meta.text("File", tilde(s.path), { icon: Icons.file }),
],
},
actions: [
open(s.path, "Open Picture"),
...(cam ? [snapshotAction(cam, prefs, { shortcut: "<Ctrl>n" })] : []),
open(s.path.slice(0, s.path.lastIndexOf("/")), "Open Folder", { icon: Icons.folder, shortcut: "<Ctrl>o" }),
copy(s.path, "Copy Path", { shortcut: "<Ctrl><Shift>c" }),
action("Move to Trash", async () => {
await trash(s.path);
await toast(`${s.name} moved to the trash`, { style: "success" });
return refresh();
}, { icon: Icons.trash, destructive: true, confirm: `Move ${s.name} to the trash?`, shortcut: "<Ctrl>Delete" }),
],
};
}
/** The pictures in the snapshot folder, newest first (the one just taken selected). */
export function snapshotsView(prefs: Prefs, cam?: Camera, taken?: string): ListSpec {
return list({
title: taken ? "Snapshot Taken" : "Webcam Snapshots",
placeholder: "Filter snapshots…",
empty: {
title: "No snapshot yet",
description: cam
? "Ctrl+N takes one"
: `Take one with Take Webcam Snapshot; they are saved in ${folderOf(prefs)}`,
icon: Icons.camera,
},
status: (items) => `${items.length} in ${folderOf(prefs)}`,
actions: cam ? [snapshotAction(cam, prefs, { shortcut: "<Ctrl>n" })] : [],
async items() {
const list = await shots(folderOf(prefs));
// The new picture first, even if the clock went backwards.
if (taken) list.sort((a, b) => Number(b.path === taken) - Number(a.path === taken));
return list.map((s) => shotRow(s, prefs, cam));
},
});
}
// ---------------------------------------------------------------------------
// Controls
async function apply(cam: Camera, c: Control, value: number, all: Control[]) {
const v = clamp(c, value);
const partner = autoPartner(c.name, all);
// A manual value only sticks once the automatic mode is off.
const pre: [string, number][] = partner && isInactive(c) && partner.control.value !== partner.manual
? [[partner.control.name, partner.manual]]
: [];
if (pre.length) await setControls(cam.node, pre);
await setControls(cam.node, [[c.name, v]]);
if (pre.length) await toast(`${niceName(partner!.control.name)} turned off to set ${niceName(c.name)}`);
return refresh();
}
function valueForm(cam: Camera, c: Control, all: Control[]) {
return form({
title: niceName(c.name),
submitTitle: "Set",
fields: [
field.text("value", c.min !== undefined ? `Value (${c.min} to ${c.max})` : "Value", {
value: String(c.value ?? ""),
required: true,
placeholder: c.default !== undefined ? `Default ${c.default}` : undefined,
}),
],
async submit(values) {
await apply(cam, c, parseValue(c, String(values.value ?? "")), all);
return pop();
},
});
}
function controlActions(cam: Camera, c: Control, all: Control[], prefs: Prefs): Action[] {
if (isReadOnly(c)) return [copy(String(c.value ?? ""), "Copy Value")];
const reset = c.default !== undefined && c.value !== c.default
? [action("Reset to Default", () => apply(cam, c, c.default!, all), {
icon: Icons.history,
shortcut: "<Ctrl>r",
section: "Value",
})]
: [];
const snap = snapshotAction(cam, prefs, { shortcut: "<Ctrl>n", section: "Camera" });
if (c.type === "bool") {
return [
action(c.value ? "Turn Off" : "Turn On", () => apply(cam, c, c.value ? 0 : 1, all), { icon: Icons.power }),
...reset,
snap,
];
}
if (c.type === "menu" || c.type === "intmenu") {
const next = nextMenuValue(c);
const label = (v: number) => c.menu.find((m) => m.value === v)?.label ?? String(v);
return [
...(next !== undefined
? [action(`Switch to ${label(next)}`, () => apply(cam, c, next, all), { icon: Icons.cycle })]
: []),
submenu(
"Choose…",
c.menu.map((m) =>
action(m.label, () => apply(cam, c, m.value, all), { icon: m.value === c.value ? Icons.check : Icons.dot })
),
{ icon: Icons.chevronRight, shortcut: "<Ctrl>l" },
),
...reset,
snap,
];
}
if (c.type === "int" || c.type === "int64") {
const step = coarseStep(c);
return [
action(`Increase by ${step}`, () => apply(cam, c, (c.value ?? 0) + step, all), { icon: Icons.plus }),
action(`Decrease by ${step}`, () => apply(cam, c, (c.value ?? 0) - step, all), { icon: Icons.minus }),
action("Set Value…", () => valueForm(cam, c, all), {
icon: Icons.settings,
shortcut: "<Ctrl>e",
section: "Value",
}),
...(c.max !== undefined
? [action("Set to Maximum", () => apply(cam, c, c.max!, all), { icon: Icons.arrowUp, section: "Value" })]
: []),
...(c.min !== undefined
? [action("Set to Minimum", () => apply(cam, c, c.min!, all), { icon: Icons.arrowDown, section: "Value" })]
: []),
...reset,
snap,
];
}
if (c.type === "button") return [action("Press", () => apply(cam, c, 1, all), { icon: Icons.bolt }), snap];
return [copy(String(c.value ?? ""), "Copy Value"), snap];
}
function controlRow(cam: Camera, c: Control, all: Control[], prefs: Prefs): Item {
const inactive = isInactive(c);
const partner = autoPartner(c.name, all);
const r = ratio(c);
const changed = c.default !== undefined && c.value !== undefined && c.value !== c.default;
return {
id: c.name,
title: niceName(c.name),
subtitle: c.name,
icon: {
source: c.type === "bool"
? (c.value ? Icons.squareCheck : Icons.square)
: c.menu.length
? Icons.layers
: Icons.sun,
tint: inactive ? "gray" : "blue",
},
section: c.section,
keywords: [c.name, c.type, ...c.menu.map((m) => m.label)],
accessories: [
...(inactive
? [{
tag: "auto",
color: "gray" as Color,
tooltip: partner ? `Set by ${niceName(partner.control.name)}` : "Inactive",
}]
: []),
...(changed ? [{ icon: { source: Icons.dot, tint: "orange" as Color }, tooltip: `Default: ${c.default}` }] : []),
{ text: displayValue(c) },
],
detail: {
metadata: [
meta.text("Value", displayValue(c), { icon: Icons.sun }),
...(r !== undefined ? [meta.progress("Range", r, `${c.min} … ${c.max}`)] : []),
...(c.default !== undefined
? [meta.text("Default", c.menu.find((m) => m.value === c.default)?.label ?? String(c.default))]
: []),
...(c.step && c.step > 1 ? [meta.text("Step", c.step)] : []),
...(c.menu.length
? [
meta.tags(
"Choices",
c.menu.map((m) => ({ text: m.label, color: m.value === c.value ? "blue" as Color : undefined })),
),
]
: []),
...(inactive
? [meta.text(
"Inactive",
partner ? `${niceName(partner.control.name)} is on: changing this turns it off` : "Set by the camera",
)]
: []),
meta.separator(),
meta.text("Control", c.name),
meta.text("Camera", `${cam.device.name} (${cam.node})`, { icon: Icons.camera }),
],
},
actions: controlActions(cam, c, all, prefs),
};
}
export function controlsView(cam: Camera, prefs: Prefs): ListSpec {
return list({
title: cam.device.name,
placeholder: "Filter controls: brightness, exposure, focus, zoom…",
empty: { title: "This camera has no adjustable controls", icon: Icons.camera },
status: (items) => `${items.length} controls · ${cam.node}`,
actions: [
snapshotAction(cam, prefs, { shortcut: "<Ctrl>n" }),
action("Reset All to Defaults", async () => {
const all = await controls(cam.node);
const values = all.filter((c) =>
!isReadOnly(c) && c.type !== "button" && c.default !== undefined && c.value !== c.default
)
// Automatic modes first, so manual values they override aren't refused.
.sort((a, b) => Number(!!autoPartner(a.name, all)) - Number(!!autoPartner(b.name, all)))
.map((c) => [c.name, c.default!] as [string, number]);
for (const v of values) await setControls(cam.node, [v]).catch(() => {});
await toast(`${values.length} controls reset`, { style: "success" });
return refresh();
}, { icon: Icons.history, destructive: true, confirm: "Reset every control of this camera to its default?" }),
],
async items({ signal }) {
const all = sortControls(await controls(cam.node, signal));
return all.map((c) => controlRow(cam, c, all, prefs));
},
});
}
// ---------------------------------------------------------------------------
// Cameras
export function cameraRow(cam: Camera, prefs: Prefs, isDefault: boolean): Item {
const formats = formatSummary(cam.formats);
return {
id: cam.node,
title: cam.device.name,
subtitle: cam.node,
icon: { source: Icons.camera, tint: "blue" },
keywords: [...cam.device.nodes, cam.info.driver ?? "", cam.device.bus],
accessories: [
...(isDefault ? [{ tag: "default", color: "blue" as Color }] : []),
...(cam.info.driver ? [{ text: cam.info.driver }] : []),
],
detail: {
metadata: [
meta.text("Device", cam.node, { icon: Icons.camera }),
meta.text("Other nodes", cam.device.nodes.filter((n) => n !== cam.node).join(", ") || "—"),
...(cam.info.driver ? [meta.text("Driver", cam.info.driver)] : []),
...(cam.device.bus ? [meta.text("Bus", cam.device.bus, { icon: Icons.plug })] : []),
...(formats ? [meta.text("Largest pictures", formats, { icon: Icons.image })] : []),
meta.separator(),
meta.text("Snapshots", folderOf(prefs), { icon: Icons.folder }),
],
},
actions: [
action("Show Controls", () => controlsView(cam, prefs), { icon: Icons.settings }),
snapshotAction(cam, prefs),
action("Show Snapshots", () => snapshotsView(prefs, cam), { icon: Icons.image, shortcut: "<Ctrl>s" }),
copy(cam.node, "Copy Device Path", { shortcut: "<Ctrl><Shift>c" }),
],
};
}