Make Background a native feature
- Background replaces the Background Slideshow plugin and the background image of Appearance. It shows a folder as a slideshow, or one image. - It is on by default. Its module loads at launch only when a folder or an image is chosen. - Rust keeps the one folder in background-folder.json. The interface never sends a path. Eight background_* commands replace the six deprecated backgrounds_* commands. - The first start moves the slideshow folder and settings, and keeps the saved image as the image source. - One blur worker with a cache, fewer folder reads, and a decode before each fade. The hub writes only the changed properties. - Remove the deprecated folder methods of api.backgrounds. set, get, and subscribe stay for the Project Management plugin. - Show the view buttons in the workbench test without text, after UI-056. - Update the tests, the measurement tool, the docs, and UI-057.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
// The Background feature. `loader.ts` imports this module when a folder or an image is selected (`launch.ts`),
|
||||
// at a palette command, or when Appearance → Background shows its settings.
|
||||
// It shows the images through `backgroundHub`, so the workarea, browser tabs, and Project Management boards get the frames.
|
||||
import { mount, unmount } from "svelte";
|
||||
import { tr } from "../../i18n/index.ts";
|
||||
import { backgroundHub } from "../../plugins/backgrounds.ts";
|
||||
import { readAppearance } from "../../theme/appearance.ts";
|
||||
import { hasImage, normalizeLayout, onBackgroundChange, readBackground, writeBackground } from "../backgroundSettings.ts";
|
||||
import type { FeatureContext, FeatureInstance } from "../types.ts";
|
||||
import { BlurWorker } from "./blur.ts";
|
||||
import { Slideshow, type ImageSource } from "./engine.ts";
|
||||
import { browserRuntime, folderHost, folderSource, imageSource, onBackgroundCommand, sendBackgroundCommand } from "./sources.ts";
|
||||
import BackgroundSettings from "./BackgroundSettings.svelte";
|
||||
|
||||
export function start(_context: FeatureContext): FeatureInstance {
|
||||
const owner = Symbol("background");
|
||||
const blur = new BlurWorker();
|
||||
const engine = new Slideshow({ set: (target, frame) => backgroundHub.set(owner, target, frame) }, browserRuntime(blur));
|
||||
/** The host folder for display. `undefined` until the first read. */
|
||||
let folder: string | null | undefined;
|
||||
let generation = 0;
|
||||
let disposed = false;
|
||||
/** The last source and settings. A change of another appearance setting, such as the theme, gives the same value. */
|
||||
let applied = "";
|
||||
|
||||
/** Give the engine the current source and settings. `reread` asks the host for its folder again. */
|
||||
async function apply(reread: boolean) {
|
||||
const run = ++generation;
|
||||
const settings = readBackground();
|
||||
const appearance = readAppearance();
|
||||
let source: ImageSource | null = null;
|
||||
let layout = normalizeLayout(settings);
|
||||
if (settings.source === "folder") {
|
||||
if (reread || folder === undefined) {
|
||||
try { folder = await folderHost.current(); } catch { folder = null; }
|
||||
if (disposed || run !== generation) return;
|
||||
}
|
||||
source = folder ? folderSource(folder) : null;
|
||||
} else if (settings.source === "image" && hasImage(appearance)) {
|
||||
source = imageSource(appearance.newTabBackground!);
|
||||
layout = normalizeLayout(appearance.newTabBackground);
|
||||
}
|
||||
const next = { ...settings, ...layout };
|
||||
const signature = JSON.stringify([source?.key ?? null, next]);
|
||||
if (!reread && signature === applied) return;
|
||||
applied = signature;
|
||||
await engine.configure(source, next);
|
||||
}
|
||||
|
||||
const stopSettings = onBackgroundChange(() => { void apply(false); });
|
||||
const stopCommands = onBackgroundCommand(command => {
|
||||
if (command === "next") void engine.next();
|
||||
else { engine.invalidate(); void apply(true); }
|
||||
});
|
||||
// Another window or program can change the folder. A focus reads the folder, the list, and the sidecar again.
|
||||
const focus = () => { engine.invalidate(); void apply(true); };
|
||||
window.addEventListener("focus", focus);
|
||||
void apply(true);
|
||||
|
||||
return {
|
||||
mount() { throw new Error(tr("This view is unavailable.")); },
|
||||
async run(command) {
|
||||
if (command === "next") await sendBackgroundCommand("next");
|
||||
else if (command === "pause") {
|
||||
const settings = readBackground();
|
||||
await writeBackground({ ...settings, paused: !settings.paused });
|
||||
} else throw new Error(tr("This command is unavailable."));
|
||||
},
|
||||
dispose() {
|
||||
disposed = true; generation++;
|
||||
stopSettings(); stopCommands();
|
||||
window.removeEventListener("focus", focus);
|
||||
engine.dispose();
|
||||
blur.dispose();
|
||||
backgroundHub.removeOwner(owner);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** The settings of Appearance → Background. They work also in a window where Background does not run. */
|
||||
export function mountSettings(element: HTMLElement) {
|
||||
const component = mount(BackgroundSettings, { target: element });
|
||||
return () => { void unmount(component); };
|
||||
}
|
||||
Reference in New Issue
Block a user