2026-10-21 00:54:54 +00:00
|
|
|
import { isTauri } from "@tauri-apps/api/core";
|
2026-10-25 04:24:05 +00:00
|
|
|
import { listen } from "@tauri-apps/api/event";
|
2026-10-21 00:54:54 +00:00
|
|
|
import { writeImage } from "@tauri-apps/plugin-clipboard-manager";
|
|
|
|
|
import { Copy, X } from "lucide-react";
|
2026-10-25 04:24:05 +00:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
2026-10-21 00:54:54 +00:00
|
|
|
import { useI18n } from "../i18n";
|
|
|
|
|
import { api } from "../lib/api";
|
|
|
|
|
|
2026-10-25 04:24:05 +00:00
|
|
|
interface PinnedPreview {
|
|
|
|
|
id: number;
|
|
|
|
|
path: string;
|
|
|
|
|
dataUrl: string;
|
|
|
|
|
width: number;
|
|
|
|
|
height: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 00:54:54 +00:00
|
|
|
function decodePath(value: string | null): string | null {
|
|
|
|
|
if (!value || value.length % 2 !== 0 || !/^[0-9a-f]+$/i.test(value)) return null;
|
|
|
|
|
try {
|
|
|
|
|
const bytes = new Uint8Array((value.match(/../g) ?? []).map((pair) => parseInt(pair, 16)));
|
|
|
|
|
return new TextDecoder().decode(bytes);
|
|
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function PinnedImage() {
|
|
|
|
|
const { t } = useI18n();
|
2026-10-25 04:24:05 +00:00
|
|
|
const [path, setPath] = useState(() =>
|
|
|
|
|
decodePath(new URLSearchParams(window.location.search).get("path")),
|
|
|
|
|
);
|
2026-10-21 00:54:54 +00:00
|
|
|
const [src, setSrc] = useState<string | null>(null);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
2026-10-25 04:24:05 +00:00
|
|
|
const latestId = useRef(0);
|
|
|
|
|
const renderedId = useRef(0);
|
2026-10-21 00:54:54 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isTauri()) {
|
|
|
|
|
setSrc("/demo-preview.svg");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-10-25 04:24:05 +00:00
|
|
|
let disposed = false;
|
|
|
|
|
const apply = (preview: PinnedPreview | null) => {
|
|
|
|
|
if (disposed) return;
|
|
|
|
|
if (!preview) {
|
|
|
|
|
if (latestId.current === 0) setSrc(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (preview.id < latestId.current) return;
|
|
|
|
|
if (preview.id === renderedId.current) return;
|
|
|
|
|
latestId.current = preview.id;
|
|
|
|
|
setPath(preview.path);
|
|
|
|
|
const image = new Image();
|
|
|
|
|
image.onload = () => {
|
|
|
|
|
if (disposed) return;
|
|
|
|
|
renderedId.current = preview.id;
|
|
|
|
|
setSrc(preview.dataUrl);
|
|
|
|
|
setError(null);
|
|
|
|
|
// Let React commit the loaded image before the native window is shown.
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
|
if (!disposed) void api.pinReady(preview.id);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
image.onerror = () => {
|
|
|
|
|
if (disposed) return;
|
|
|
|
|
renderedId.current = preview.id;
|
|
|
|
|
setError(t("pinLoadFailed"));
|
|
|
|
|
void api.pinReady(preview.id);
|
|
|
|
|
};
|
|
|
|
|
image.src = preview.dataUrl;
|
|
|
|
|
};
|
|
|
|
|
const refresh = () => {
|
|
|
|
|
void api
|
|
|
|
|
.getPinnedImage()
|
|
|
|
|
.then(apply)
|
|
|
|
|
.catch((reason) => {
|
|
|
|
|
if (disposed) return;
|
|
|
|
|
setError(String(reason).replace(/^Error:\s*/, ""));
|
|
|
|
|
void api.pinReady(null);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
const refreshWhenVisible = () => {
|
|
|
|
|
if (document.visibilityState === "visible") refresh();
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener("focus", refresh);
|
|
|
|
|
document.addEventListener("visibilitychange", refreshWhenVisible);
|
|
|
|
|
let unlisten: Promise<() => void> | null = null;
|
|
|
|
|
// Register first: a reused pin window can receive a new image immediately
|
|
|
|
|
// after the previous pin is closed. Querying first would lose that event.
|
|
|
|
|
unlisten = listen<PinnedPreview>("pin://show", (event) => apply(event.payload)).then(
|
|
|
|
|
(stopListening) => {
|
|
|
|
|
if (!disposed) refresh();
|
|
|
|
|
return stopListening;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
return () => {
|
|
|
|
|
disposed = true;
|
|
|
|
|
window.removeEventListener("focus", refresh);
|
|
|
|
|
document.removeEventListener("visibilitychange", refreshWhenVisible);
|
|
|
|
|
void unlisten?.then((release) => release());
|
|
|
|
|
};
|
|
|
|
|
// Language changes only affect error text, not the image payload.
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []);
|
2026-10-21 00:54:54 +00:00
|
|
|
|
|
|
|
|
async function copy() {
|
|
|
|
|
if (!path) return;
|
|
|
|
|
await writeImage(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return <div className="pin-error">{error}</div>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="pin-shell">
|
|
|
|
|
<div
|
|
|
|
|
className="pin-surface"
|
|
|
|
|
data-tauri-drag-region
|
2026-10-25 04:24:05 +00:00
|
|
|
onDoubleClick={() => void api.closePin()}
|
2026-10-21 00:54:54 +00:00
|
|
|
>
|
|
|
|
|
{src ? (
|
2026-10-25 04:24:05 +00:00
|
|
|
<img
|
|
|
|
|
src={src}
|
|
|
|
|
alt=""
|
|
|
|
|
draggable={false}
|
|
|
|
|
data-tauri-drag-region
|
|
|
|
|
onError={() => setError(t("pinLoadFailed"))}
|
|
|
|
|
/>
|
2026-10-21 00:54:54 +00:00
|
|
|
) : (
|
|
|
|
|
<div className="pin-loading" />
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="pin-actions">
|
|
|
|
|
<button type="button" title={t("copy")} onClick={() => void copy()}>
|
|
|
|
|
<Copy size={13} />
|
|
|
|
|
</button>
|
2026-10-25 04:24:05 +00:00
|
|
|
<button type="button" title={t("close")} onClick={() => void api.closePin()}>
|
2026-10-21 00:54:54 +00:00
|
|
|
<X size={13} />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|