feat(webui): support custom background images (#6674)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-16 22:08:55 +08:00
1 parent 92d23d2d2b
commit e91906bc3a
29 files changed
+1259 -3

No files matched your search

@@ -151,7 +151,7 @@ export function RemoteInstances({ children }: { children: ReactNode }) {
openHostIds: frames.map((frame) => frame.connection.id),
directoryError: hosts.directoryError, refresh: hosts.refresh, watchDirectory: hosts.watchDirectory, rename: hosts.rename, connect, disconnect: hosts.disconnect, remove: hosts.remove, cancel: hosts.cancel }}>
<HostNavigationContext.Provider value={bridge.embedded || (available || selected ? picker : null)}>
<div className="flex h-full min-h-0 flex-col bg-background">
<div data-theme-surface className="flex h-full min-h-0 flex-col bg-background">
<div className="relative min-h-0 flex-1 overflow-hidden">
<div ref={localPanel} data-host-view="local" aria-hidden={!!selected} {...(selected ? { inert: "" } : {})}
style={{ visibility: selected ? "hidden" : "visible" }}
@@ -6,6 +6,7 @@ import { LanguageSwitcher } from "@/components/LanguageSwitcher";
import { SettingsGroup, SettingsRow, SettingsSectionTitle } from "@/components/settings/shared/SettingsControls";
import { SettingsHint } from "@/components/settings/shared/SettingsHint";
import { ToggleButton } from "@/components/ui/toggle-button";
import { BackgroundSettings } from "./BackgroundSettings";
import { SegmentedControl } from "@/components/ui/segmented-control";
import type { ThemePreference } from "@/hooks/useTheme";
import type { FileEditDisplayMode, LocalActivityMode, LocalPreferences } from "@/lib/local-preferences";
@@ -71,6 +72,7 @@ export function AppearanceSettingsView({ themePreference, onChangeTheme, localPr
<SettingsSectionTitle>{t("settings.sections.interface")}</SettingsSectionTitle>
<SettingsGroup>
<SettingsRow title={t("settings.rows.language")}><LanguageSwitcher /></SettingsRow>
<BackgroundSettings />
<SettingsRow title={t("settings.rows.brandLogos")} description={t("settings.legal.thirdPartyBrands")}>
<ToggleButton checked={localPrefs.brandLogos}
onChange={(brandLogos) => onChangeLocalPrefs((previous) => ({ ...previous, brandLogos }))}
@@ -0,0 +1,131 @@
import { useId, useRef, useState, type CSSProperties } from "react";
import { ImagePlus } from "lucide-react";
import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogFooter, DialogTitle } from "@/components/ui/dialog";
import { useBackground } from "@/providers/BackgroundProvider";
import { useObjectUrl } from "@/hooks/useObjectUrl";
export function BackgroundSettings() {
const { t } = useTranslation();
const background = useBackground();
const input = useRef<HTMLInputElement>(null);
const trigger = useRef<HTMLButtonElement>(null);
const blurId = useId();
const veilId = useId();
const [open, setOpen] = useState(false);
const [file, setFile] = useState<File | null>(null);
const [removed, setRemoved] = useState(false);
const [fileError, setFileError] = useState(false);
const [imageSaved, setImageSaved] = useState(false);
const [selectedBlur, setSelectedBlur] = useState(5);
const [selectedVeil, setSelectedVeil] = useState(88);
const image = removed ? null : file ?? background?.image ?? null;
const preview = useObjectUrl(image);
if (!background) return null;
const hasPreview = !!file || (background.hasBackground && !removed);
const styleDirty = selectedBlur !== background.blur || selectedVeil !== background.veil;
const dirty = !!file || (removed && background.hasBackground) || styleDirty;
const setDialogOpen = (next: boolean) => {
if (background.busy) return;
setOpen(next);
setFile(null);
setRemoved(false);
setFileError(false);
setImageSaved(false);
setSelectedBlur(background.blur);
setSelectedVeil(background.veil);
background.clearError();
};
const save = async () => {
const imageChanged = !!file || removed;
const saved = file ? await background.upload(file) : removed ? await background.remove() : true;
if (!saved) return;
if (imageChanged) {
setFile(null);
setRemoved(false);
setImageSaved(true);
}
const styleSaved = styleDirty ? await background.setStyle(selectedBlur, selectedVeil) : true;
if (styleSaved) setDialogOpen(false);
};
return <>
<button ref={trigger} type="button" className="settings-disclosure-row background-settings-row w-full text-left transition-colors settings-hover"
aria-label={t("settings.appearance.background")} disabled={!background.ready && !background.loadError}
onClick={() => background.loadError ? background.retry() : setDialogOpen(true)}>
<span className="text-control-label text-foreground">{t("settings.appearance.background")}</span>
<span className="background-settings-trigger">
{background.loadError ? <span className="text-destructive">{t("common.retry")}</span>
: background.hasBackground ? <span className="background-settings-thumbnail" aria-hidden />
: <span className="text-muted-foreground">{t("settings.appearance.backgroundChoose")}</span>}
</span>
</button>
<Dialog open={open} onOpenChange={setDialogOpen}>
<DialogContent className="background-settings-dialog" aria-describedby={undefined} showCloseButton={false}
onCloseAutoFocus={event => {
if (trigger.current?.isConnected) { event.preventDefault(); trigger.current.focus(); }
}}>
<DialogTitle className="sr-only">{t("settings.appearance.background")}</DialogTitle>
<button type="button" className="background-settings-preview" data-has-image={hasPreview || undefined}
disabled={background.busy} onClick={() => input.current?.click()}
aria-label={t(hasPreview ? "settings.appearance.backgroundReplace" : "settings.appearance.backgroundChoose")}
style={{
...(preview ? { backgroundImage: `url("${preview}")` } : {}),
"--custom-background-blur": `${selectedBlur}px`,
"--custom-background-tint": `${selectedVeil / 100}`,
} as CSSProperties}>
{hasPreview ? <span className="background-settings-preview-action">
<ImagePlus className="h-3.5 w-3.5" aria-hidden />
{t("settings.appearance.backgroundReplace")}
</span> : <span className="background-settings-empty">
<ImagePlus className="h-6 w-6" aria-hidden />
<span>{t("settings.appearance.backgroundChoose")}</span>
</span>}
</button>
<input ref={input} type="file" accept="image/png,image/jpeg,image/webp" hidden onChange={(event) => {
const next = event.currentTarget.files?.[0];
event.currentTarget.value = "";
if (next) {
if (next.size > 6 * 1024 * 1024) { setFileError(true); return; }
setFile(next); setRemoved(false); setFileError(false); setImageSaved(false); background.clearError();
}
}} />
{hasPreview && <div className="background-blur-control">
<div className="flex items-center justify-between gap-3">
<label htmlFor={blurId} className="text-control-label">{t("settings.appearance.backgroundBlur")}</label>
<output htmlFor={blurId} className="text-xs tabular-nums text-muted-foreground">{selectedBlur} px</output>
</div>
<input id={blurId} type="range" min={0} max={20} step={1} value={selectedBlur} disabled={background.busy}
style={{ "--slider-progress": `${selectedBlur * 5}%` } as CSSProperties}
onChange={event => setSelectedBlur(Number(event.currentTarget.value))} />
</div>}
{hasPreview && <div className="background-blur-control">
<div className="flex items-center justify-between gap-3">
<label htmlFor={veilId} className="text-control-label">{t("settings.appearance.backgroundFade")}</label>
<output htmlFor={veilId} className="text-xs tabular-nums text-muted-foreground">{selectedVeil}%</output>
</div>
<input id={veilId} type="range" min={0} max={100} step={1} value={selectedVeil} disabled={background.busy}
style={{ "--slider-progress": `${selectedVeil}%` } as CSSProperties}
onChange={event => setSelectedVeil(Number(event.currentTarget.value))} />
</div>}
{background.loadError && <div role="alert" className="flex items-center gap-2 text-xs text-destructive">
<span>{t("settings.status.loadError")}</span>
<Button type="button" variant="ghost" size="sm" onClick={background.retry}>{t("common.retry")}</Button>
</div>}
{fileError && <p role="alert" className="text-xs text-destructive">{t("settings.appearance.backgroundTooLarge")}</p>}
{background.error && <p role="alert" className="text-xs text-destructive">{t(imageSaved ? "settings.appearance.backgroundPartialError" : "settings.appearance.backgroundError")}</p>}
<DialogFooter className="background-settings-footer">
{hasPreview && <Button type="button" variant="ghost" size="sm" className="background-settings-remove" disabled={background.busy}
onClick={() => { setFile(null); setRemoved(true); setFileError(false); setImageSaved(false); background.clearError(); }}>
{t("settings.appearance.backgroundRemove")}
</Button>}
<Button type="button" variant="ghost" size="sm" disabled={background.busy} onClick={() => setDialogOpen(false)}>{t("settings.actions.cancel")}</Button>
<Button type="button" size="sm" disabled={!dirty || background.busy || !background.ready || (hasPreview && !preview)} onClick={() => void save()}>{t(background.busy ? "settings.actions.saving" : "settings.actions.save")}</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>;
}
@@ -71,3 +71,28 @@
@media (prefers-reduced-motion: reduce) {
.appearance-theme-card, .appearance-custom-option { transition: none; }
}
html[data-custom-background] .appearance-theme-card,
html[data-custom-background] .appearance-reading-card { background: hsl(var(--settings-surface) / 0.65); backdrop-filter: blur(12px); }
html[data-custom-background] .appearance-reading-preview { background: hsl(var(--background) / 0.35); }
.background-settings-trigger { display: inline-flex; min-height: 32px; align-items: center; justify-content: flex-end; gap: 8px; font-size: 13px; }
.background-settings-row:focus-visible { outline: 2px solid hsl(var(--ring)); outline-offset: -2px; }
.background-settings-thumbnail { width: 52px; height: 32px; border-radius: 7px; background: var(--custom-background-image) center / cover; box-shadow: inset 0 0 0 1px hsl(var(--border) / 0.7); }
.background-settings-dialog { width: min(440px, calc(100vw - 32px)); gap: 16px; }
.background-settings-preview { position: relative; display: flex; width: 100%; min-width: 0; max-width: 100%; aspect-ratio: 16 / 9; min-height: 180px; align-items: center; justify-content: center; overflow: hidden; border: 1px solid hsl(var(--border) / 0.65); border-radius: var(--radius-panel); background-color: hsl(var(--muted) / 0.5); background-position: center; background-size: cover; cursor: pointer; }
.background-settings-preview:focus-visible { outline: 2px solid hsl(var(--ring)); outline-offset: 3px; }
.background-settings-preview-action { position: absolute; z-index: 1; right: 10px; bottom: 10px; display: inline-flex; align-items: center; gap: 5px; min-height: 32px; padding: 0 10px; border: 1px solid hsl(var(--border) / 0.7); border-radius: var(--radius-control); background: hsl(var(--background) / 0.9); color: hsl(var(--foreground)); font-size: 12px; box-shadow: 0 1px 4px hsl(var(--foreground) / 0.1); }
.background-settings-footer { display: flex; flex-direction: row; align-items: center; justify-content: flex-end; gap: 8px; }
.background-settings-remove { margin-right: auto; }
.background-settings-preview[data-has-image]::after { content: ""; position: absolute; inset: 0; background: hsl(var(--background) / var(--custom-background-tint, 0.88)); backdrop-filter: blur(var(--custom-background-blur, 5px)); pointer-events: none; }
.background-blur-control { display: grid; gap: 8px; }
.background-blur-control input[type="range"] { appearance: none; width: 100%; height: 28px; margin: 0; background: transparent; cursor: pointer; }
.background-blur-control input[type="range"]::-webkit-slider-runnable-track { height: 4px; border-radius: 999px; background: linear-gradient(to right, hsl(var(--foreground) / 0.65) var(--slider-progress), hsl(var(--muted-foreground) / 0.22) var(--slider-progress)); }
.background-blur-control input[type="range"]::-moz-range-track { height: 4px; border-radius: 999px; background: linear-gradient(to right, hsl(var(--foreground) / 0.65) var(--slider-progress), hsl(var(--muted-foreground) / 0.22) var(--slider-progress)); }
.background-blur-control input[type="range"]::-webkit-slider-thumb { appearance: none; width: 16px; height: 16px; margin-top: -6px; border: 1px solid hsl(var(--foreground) / 0.45); border-radius: 50%; background: hsl(var(--background)); box-shadow: 0 1px 3px hsl(var(--foreground) / 0.18); }
.background-blur-control input[type="range"]::-moz-range-thumb { width: 14px; height: 14px; border: 1px solid hsl(var(--foreground) / 0.45); border-radius: 50%; background: hsl(var(--background)); box-shadow: 0 1px 3px hsl(var(--foreground) / 0.18); }
.background-blur-control input[type="range"]:focus-visible { outline: 2px solid hsl(var(--ring)); outline-offset: 3px; border-radius: 4px; }
.background-blur-control input[type="range"]:disabled { cursor: default; opacity: 0.5; }
.background-settings-empty { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: hsl(var(--muted-foreground)); font-size: 13px; }
.background-settings-preview:hover .background-settings-empty { color: hsl(var(--foreground)); }