Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

perf(webv2): stop the upscale widget re-rendering every field on each edit

joshistoasta272f037749b
diff --git a/invokeai/frontend/webv2/src/app/UpscaleUiAdapter.tsx b/invokeai/frontend/webv2/src/app/UpscaleUiAdapter.tsx
index 54b048b995..de4e5afc94 100644
--- a/invokeai/frontend/webv2/src/app/UpscaleUiAdapter.tsx
+++ b/invokeai/frontend/webv2/src/app/UpscaleUiAdapter.tsx
@@ -8,7 +8,7 @@ import { useQueryClient } from '@tanstack/react-query';
import { useWorkbenchPreferenceSelector } from '@workbench/settings/store';
import { getProjectWidgetValues } from '@workbench/widgetState';
import { useActiveProjectSelector, useWorkbenchCommands } from '@workbench/WorkbenchContext';
-import { useMemo } from 'react';
+import { useCallback, useMemo } from 'react';
/**
* Production binding of Upscale's UI port: maps the upscale widget instance
@@ -37,17 +37,33 @@ export const UpscaleUiAdapterProvider = ({ children }: { children: ReactNode })
);
const commands = useWorkbenchCommands();
const queryClient = useQueryClient();
+ // The port's callbacks are keyed to the project, not to its contents: rebuilding
+ // them whenever `rawValues` changes would hand every consumer new function
+ // identities on each keystroke, re-rendering memoized fields that did not change.
+ const { projectId } = project;
+ const patchPromptDraft = useCallback<UpscaleUiAdapter['patchPromptDraft']>(
+ (values) => commands.generation.patchPromptDraft(values, 'upscale', projectId),
+ [commands, projectId]
+ );
+ const patchValues = useCallback<UpscaleUiAdapter['patchValues']>(
+ (values, origin) => commands.widgets.patchValues('upscale', values, projectId, origin),
+ [commands, projectId]
+ );
+ const reportError = useCallback<UpscaleUiAdapter['reportError']>(
+ (message) => commands.notifications.reportError({ area: 'upscale', message, namespace: 'generation' }),
+ [commands]
+ );
+ const touchGalleryImages = useCallback(() => void invalidateGallery(queryClient), [queryClient]);
const adapter = useMemo<UpscaleUiAdapter>(
() => ({
...project,
- patchPromptDraft: (values) => commands.generation.patchPromptDraft(values, 'upscale', project.projectId),
- patchValues: (values, origin) => commands.widgets.patchValues('upscale', values, project.projectId, origin),
- reportError: (message) =>
- commands.notifications.reportError({ area: 'upscale', message, namespace: 'generation' }),
+ patchPromptDraft,
+ patchValues,
+ reportError,
showPromptSyntaxHighlighting,
- touchGalleryImages: () => void invalidateGallery(queryClient),
+ touchGalleryImages,
}),
- [commands, project, queryClient, showPromptSyntaxHighlighting]
+ [patchPromptDraft, patchValues, project, reportError, showPromptSyntaxHighlighting, touchGalleryImages]
);
return <UpscaleUiProvider adapter={adapter}>{children}</UpscaleUiProvider>;
diff --git a/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleUiContext.tsx b/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleUiContext.tsx
index e197bc0fcf..9a71e76ea1 100644
--- a/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleUiContext.tsx
+++ b/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleUiContext.tsx
@@ -2,7 +2,7 @@ import type { ProjectPromptDraft, ProjectPromptDraftPatch } from '@features/gene
import type { UpscaleWidgetValues } from '@features/upscale/core/types';
import type { ReactNode } from 'react';
-import { createContext, use } from 'react';
+import { createContext, use, useMemo } from 'react';
/**
* Upscale's UI port. The context is a dependency-direction port (the feature
@@ -19,11 +19,34 @@ export interface UpscaleUiAdapter {
touchGalleryImages(): void;
}
+/** The adapter's callbacks, which are stable for the lifetime of a project. */
+export type UpscaleUiActions = Pick<
+ UpscaleUiAdapter,
+ 'patchPromptDraft' | 'patchValues' | 'reportError' | 'touchGalleryImages'
+>;
+
const UpscaleUiContext = createContext<UpscaleUiAdapter | null>(null);
+/**
+ * Actions are published separately from the adapter because the adapter's
+ * identity changes on every value patch (it carries `rawValues`). Components
+ * that only need to *do* something — not read state — subscribe here and so are
+ * not re-rendered by a keystroke elsewhere in the form.
+ */
+const UpscaleUiActionsContext = createContext<UpscaleUiActions | null>(null);
-export const UpscaleUiProvider = ({ adapter, children }: { adapter: UpscaleUiAdapter; children: ReactNode }) => (
- <UpscaleUiContext value={adapter}>{children}</UpscaleUiContext>
-);
+export const UpscaleUiProvider = ({ adapter, children }: { adapter: UpscaleUiAdapter; children: ReactNode }) => {
+ const { patchPromptDraft, patchValues, reportError, touchGalleryImages } = adapter;
+ const actions = useMemo<UpscaleUiActions>(
+ () => ({ patchPromptDraft, patchValues, reportError, touchGalleryImages }),
+ [patchPromptDraft, patchValues, reportError, touchGalleryImages]
+ );
+
+ return (
+ <UpscaleUiActionsContext value={actions}>
+ <UpscaleUiContext value={adapter}>{children}</UpscaleUiContext>
+ </UpscaleUiActionsContext>
+ );
+};
export const useUpscaleUi = (): UpscaleUiAdapter => {
const adapter = use(UpscaleUiContext);
@@ -34,3 +57,13 @@ export const useUpscaleUi = (): UpscaleUiAdapter => {
return adapter;
};
+
+export const useUpscaleUiActions = (): UpscaleUiActions => {
+ const actions = use(UpscaleUiActionsContext);
+
+ if (!actions) {
+ throw new Error('Upscale UI requires an App-composed UpscaleUiProvider.');
+ }
+
+ return actions;
+};
diff --git a/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleWidgetView.tsx b/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleWidgetView.tsx
index 5b0af0d2b7..1aac54edb7 100644
--- a/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/features/upscale/ui/UpscaleWidgetView.tsx
@@ -1,7 +1,7 @@
-/* oxlint-disable react-perf/jsx-no-new-object-as-prop, react-perf/jsx-no-new-function-as-prop, react-perf/jsx-no-new-array-as-prop, react-perf/jsx-no-jsx-as-prop */
+import type { DragEndEvent } from '@dnd-kit/core';
import type { GenerateLora, MainModelConfig, PromptHistoryItem } from '@features/generation/contracts';
import type { ProjectPromptDraft, ProjectPromptDraftPatch } from '@features/generation/settings';
-import type { ModelConfig } from '@features/models';
+import type { ModelConfig, ModelTaxonomyType } from '@features/models';
import type { UpscaleWidgetValues } from '@features/upscale/core/types';
import type { FeatureHintId } from '@platform/ui/hints';
import type { ChangeEvent } from 'react';
@@ -27,6 +27,7 @@ import { galleryImages, galleryTransfers } from '@features/gallery';
import { galleryImageUrls, isGalleryImageDragData, useGalleryImageDroppable } from '@features/gallery/utility';
import { GenerationSettingsSection, NegativePromptField, PositivePromptField } from '@features/generation/components';
import {
+ areProjectPromptDraftsEqual,
SCHEDULER_OPTIONS,
getDefaultLoraWeight,
isLoraCompatibleWithModel,
@@ -67,10 +68,20 @@ import { Button, Combobox, DropZone, Field, IconButton, Select, Slider, Tooltip
import { MiddleTruncate } from '@platform/ui/MiddleTruncate';
import { toaster } from '@platform/ui/toaster';
import { DicesIcon, ImagePlusIcon, Trash2Icon, UploadIcon, XIcon } from 'lucide-react';
-import { useCallback, useMemo, useRef, useState } from 'react';
+import { memo, useCallback, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
-import { useUpscaleUi } from './UpscaleUiContext';
+import { useUpscaleUi, useUpscaleUiActions } from './UpscaleUiContext';
+
+/**
+ * Every prop identity in this file is stable by construction — module-scope
+ * constants for literals, `useCallback`/`useMemo` for anything closing over
+ * state, and `memo` on each section. The widget re-renders on every keystroke
+ * that patches project state, so an inline `{...}`/`() => …` prop anywhere here
+ * re-renders the whole form (prompt editors and model pickers included) for a
+ * change that touched one number. The `react-perf` lint rules enforce this;
+ * they were previously disabled file-wide.
+ */
const DROP_ID = 'upscale-input-image';
const VAE_PRECISION_COLLECTION = createListCollection({
@@ -83,22 +94,89 @@ const LARGE_OUTPUT_MEGAPIXELS = 50;
const DIMENSION_FORMATTER = new Intl.NumberFormat();
const MEGAPIXEL_FORMATTER = new Intl.NumberFormat(undefined, { maximumFractionDigits: 1, minimumFractionDigits: 1 });
+const SPANDREL_MODEL_TYPES: readonly ModelTaxonomyType[] = ['spandrel_image_to_image'];
+const MAIN_MODEL_TYPES: readonly ModelTaxonomyType[] = ['main'];
+const LORA_MODEL_TYPES: readonly ModelTaxonomyType[] = ['lora'];
+const CONTROLNET_MODEL_TYPES: readonly ModelTaxonomyType[] = ['controlnet'];
+const VAE_MODEL_TYPES: readonly ModelTaxonomyType[] = ['vae'];
+
+const SCALE_MARKS = [1, 2, 4, 8, 16];
+const CREATIVITY_MARKS = [UPSCALE_CREATIVITY_MIN, 0, UPSCALE_CREATIVITY_MAX];
+const STRUCTURE_MARKS = [UPSCALE_STRUCTURE_MIN, 0, UPSCALE_STRUCTURE_MAX];
+const TILE_SIZE_MARKS = [UPSCALE_TILE_SIZE_MIN, 1024, UPSCALE_TILE_SIZE_MAX];
+const TILE_OVERLAP_MARKS = [UPSCALE_TILE_OVERLAP_MIN, 128, 256, UPSCALE_TILE_OVERLAP_MAX];
+
+const GENERATION_GRID_COLUMNS = { base: 2, md: 3 };
+const ADVANCED_GRID_COLUMNS = { base: 1, md: 2 };
+const SWITCH_CHECKED_PROPS = { bg: 'accent.solid' };
+const DROP_ZONE_FOCUS_PROPS = {
+ outlineColor: 'accent.focusRing',
+ outlineOffset: '2px',
+ outlineStyle: 'solid',
+ outlineWidth: '2px',
+};
+const DROP_ZONE_DISABLED_PROPS = { cursor: 'wait', opacity: 0.7 };
+// `DropZone` types its props as `BoxProps`, which has no `disabled`; spreading a
+// hoisted object keeps the button attribute without a per-render object.
+const DROP_ZONE_BUSY_PROPS = { disabled: true };
+const DROP_ZONE_HOVER_PROPS = { bg: 'bg.muted', color: 'fg' };
+const UPLOAD_ACCEPT_TYPES = ['image/png', 'image/jpeg', 'image/webp'];
+const PRESET_ENTRIES = Object.entries(UPSCALE_PRESETS);
+
const isSelectableMainModel = (model: ModelConfig): boolean => isSupportedUpscaleMainModel(model);
+const formatScale = (scale: number): string => `${scale}×`;
+
const valuesAreEqual = (left: UpscaleWidgetValues, right: UpscaleWidgetValues): boolean =>
JSON.stringify(left) === JSON.stringify(right);
const getRangeError = (label: string, value: number, min: number, max: number): string | undefined =>
Number.isFinite(value) && value >= min && value <= max ? undefined : `${label} must be between ${min} and ${max}.`;
-const UpscaleOutputPreflight = ({ values }: { values: UpscaleWidgetValues }) => {
+/**
+ * `values` is re-derived from the raw widget state on every patch, so its
+ * nested members arrive with fresh identities even when nothing about them
+ * changed. Sections whose props are those members compare by content instead,
+ * which is what keeps a scale edit from re-rendering the prompt editors.
+ */
+const areLorasEquivalent = (left: readonly GenerateLora[], right: readonly GenerateLora[]): boolean =>
+ left.length === right.length &&
+ left.every((lora, index) => {
+ const other = right[index];
+
+ return (
+ other !== undefined &&
+ lora.model.key === other.model.key &&
+ lora.isEnabled === other.isEnabled &&
+ lora.weight === other.weight
+ );
+ });
+
+const areInputImagesEquivalent = (
+ left: UpscaleWidgetValues['inputImage'],
+ right: UpscaleWidgetValues['inputImage']
+): boolean =>
+ left === right ||
+ (left !== null &&
+ right !== null &&
+ left.image_name === right.image_name &&
+ left.width === right.width &&
+ left.height === right.height);
+
+const UpscaleOutputPreflight = memo(function UpscaleOutputPreflight({
+ inputImage,
+ scale,
+}: {
+ inputImage: UpscaleWidgetValues['inputImage'];
+ scale: number;
+}) {
const { t } = useTranslation();
- if (!values.inputImage) {
+ if (!inputImage) {
return null;
}
- const output = getUpscaleOutputDimensions(values.inputImage, values.scale);
+ const output = getUpscaleOutputDimensions(inputImage, scale);
const outputMegapixels = (output.width * output.height) / 1_000_000;
const isLargeOutput = outputMegapixels >= LARGE_OUTPUT_MEGAPIXELS;
@@ -115,8 +193,7 @@ const UpscaleOutputPreflight = ({ values }: { values: UpscaleWidgetValues }) =>
fontVariantNumeric="tabular-nums"
justifyContent="flex-end"
>
- {DIMENSION_FORMATTER.format(values.inputImage.width)} ×{' '}
- {DIMENSION_FORMATTER.format(values.inputImage.height)}
+ {DIMENSION_FORMATTER.format(inputImage.width)} × {DIMENSION_FORMATTER.format(inputImage.height)}
</DataList.ItemValue>
</DataList.Item>
<DataList.Item>
@@ -129,7 +206,7 @@ const UpscaleOutputPreflight = ({ values }: { values: UpscaleWidgetValues }) =>
fontVariantNumeric="tabular-nums"
justifyContent="flex-end"
>
- {values.scale}×
+ {scale}×
</DataList.ItemValue>
</DataList.Item>
<DataList.Item>
@@ -181,7 +258,7 @@ const UpscaleOutputPreflight = ({ values }: { values: UpscaleWidgetValues }) =>
) : null}
</Stack>
);
-};
+});
const UpscaleModelReconciler = ({
rawValues,
@@ -190,7 +267,7 @@ const UpscaleModelReconciler = ({
rawValues: Record<string, unknown>;
values: UpscaleWidgetValues;
}) => {
- const { patchValues } = useUpscaleUi();
+ const { patchValues } = useUpscaleUiActions();
useMountEffect(() => {
const normalized = normalizeUpscaleWidgetValues(rawValues);
@@ -205,65 +282,103 @@ const UpscaleModelReconciler = ({
return null;
};
-const UpscalePromptFields = ({
- onPatchPromptDraft,
- onPatchValues,
- promptDraft,
- projectId,
- showSyntaxHighlighting,
- values,
-}: {
- onPatchPromptDraft: (patch: ProjectPromptDraftPatch) => void;
- onPatchValues: (patch: Partial<UpscaleWidgetValues>) => void;
- promptDraft: ProjectPromptDraft;
- projectId: string;
- showSyntaxHighlighting: boolean;
- values: UpscaleWidgetValues;
-}) => {
- const { t } = useTranslation();
- const handleUsePrompt = useCallback(
- (prompt: PromptHistoryItem) =>
- onPatchPromptDraft({
- negativePrompt: prompt.negativePrompt ?? '',
- negativePromptEnabled: prompt.negativePrompt ? true : promptDraft.negativePromptEnabled,
- positivePrompt: prompt.positivePrompt,
- }),
- [onPatchPromptDraft, promptDraft.negativePromptEnabled]
- );
+const UpscalePromptFields = memo(
+ function UpscalePromptFields({
+ loras,
+ model,
+ negativePromptHeightPx,
+ onPatchPromptDraft,
+ onPatchValues,
+ positivePromptHeightPx,
+ promptDraft,
+ projectId,
+ showSyntaxHighlighting,
+ }: {
+ loras: GenerateLora[];
+ model: MainModelConfig | null;
+ negativePromptHeightPx: number;
+ onPatchPromptDraft: (patch: ProjectPromptDraftPatch) => void;
+ onPatchValues: (patch: Partial<UpscaleWidgetValues>) => void;
+ positivePromptHeightPx: number;
+ promptDraft: ProjectPromptDraft;
+ projectId: string;
+ showSyntaxHighlighting: boolean;
+ }) {
+ const { t } = useTranslation();
+ const handleUsePrompt = useCallback(
+ (prompt: PromptHistoryItem) =>
+ onPatchPromptDraft({
+ negativePrompt: prompt.negativePrompt ?? '',
+ negativePromptEnabled: prompt.negativePrompt ? true : promptDraft.negativePromptEnabled,
+ positivePrompt: prompt.positivePrompt,
+ }),
+ [onPatchPromptDraft, promptDraft.negativePromptEnabled]
+ );
+ const handlePositiveChange = useCallback(
+ (positivePrompt: string) => onPatchPromptDraft({ positivePrompt }),
+ [onPatchPromptDraft]
+ );
+ const handleNegativeChange = useCallback(
+ (negativePrompt: string) => onPatchPromptDraft({ negativePrompt }),
+ [onPatchPromptDraft]
+ );
+ const handleNegativeEnabledChange = useCallback(
+ (negativePromptEnabled: boolean) => onPatchPromptDraft({ negativePromptEnabled }),
+ [onPatchPromptDraft]
+ );
+ const handlePositiveResizeEnd = useCallback(
+ (positivePromptHeight: number) => onPatchValues({ positivePromptHeightPx: positivePromptHeight }),
+ [onPatchValues]
+ );
+ const handleNegativeResizeEnd = useCallback(
+ (negativePromptHeight: number) => onPatchValues({ negativePromptHeightPx: negativePromptHeight }),
+ [onPatchValues]
+ );
- return (
- <Stack gap="2" p="2">
- <Text color="fg.muted" fontSize="2xs" textWrap="pretty">
- {t('widgets.upscale.sharedPromptDescription')}
- </Text>
- <PositivePromptField
- heightPx={values.positivePromptHeightPx}
- loras={values.loras}
- projectId={projectId}
- selectedModel={values.model ?? undefined}
- showSyntaxHighlighting={showSyntaxHighlighting}
- value={promptDraft.positivePrompt}
- onChange={(positivePrompt) => onPatchPromptDraft({ positivePrompt })}
- onResizeEnd={(positivePromptHeightPx) => onPatchValues({ positivePromptHeightPx })}
- onUsePrompt={handleUsePrompt}
- />
- <NegativePromptField
- heightPx={values.negativePromptHeightPx}
- isEnabled={promptDraft.negativePromptEnabled}
- loras={values.loras}
- projectId={projectId}
- selectedModel={values.model ?? undefined}
- showSyntaxHighlighting={showSyntaxHighlighting}
- value={promptDraft.negativePrompt}
- onChange={(negativePrompt) => onPatchPromptDraft({ negativePrompt })}
- onEnabledChange={(negativePromptEnabled) => onPatchPromptDraft({ negativePromptEnabled })}
- onResizeEnd={(negativePromptHeightPx) => onPatchValues({ negativePromptHeightPx })}
- />
- </Stack>
- );
-};
+ return (
+ <Stack gap="2" p="2">
+ <Text color="fg.muted" fontSize="2xs" textWrap="pretty">
+ {t('widgets.upscale.sharedPromptDescription')}
+ </Text>
+ <PositivePromptField
+ heightPx={positivePromptHeightPx}
+ loras={loras}
+ projectId={projectId}
+ selectedModel={model ?? undefined}
+ showSyntaxHighlighting={showSyntaxHighlighting}
+ value={promptDraft.positivePrompt}
+ onChange={handlePositiveChange}
+ onResizeEnd={handlePositiveResizeEnd}
+ onUsePrompt={handleUsePrompt}
+ />
+ <NegativePromptField
+ heightPx={negativePromptHeightPx}
+ isEnabled={promptDraft.negativePromptEnabled}
+ loras={loras}
+ projectId={projectId}
+ selectedModel={model ?? undefined}
+ showSyntaxHighlighting={showSyntaxHighlighting}
+ value={promptDraft.negativePrompt}
+ onChange={handleNegativeChange}
+ onEnabledChange={handleNegativeEnabledChange}
+ onResizeEnd={handleNegativeResizeEnd}
+ />
+ </Stack>
+ );
+ },
+ (previous, next) =>
+ previous.negativePromptHeightPx === next.negativePromptHeightPx &&
+ previous.onPatchPromptDraft === next.onPatchPromptDraft &&
+ previous.onPatchValues === next.onPatchValues &&
+ previous.positivePromptHeightPx === next.positivePromptHeightPx &&
+ previous.projectId === next.projectId &&
+ areProjectPromptDraftsEqual(previous.promptDraft, next.promptDraft) &&
+ previous.showSyntaxHighlighting === next.showSyntaxHighlighting &&
+ previous.model?.key === next.model?.key &&
+ areLorasEquivalent(previous.loras, next.loras)
+);
-const NumericSliderField = ({
+const NumericSliderField = memo(function NumericSliderField({
error,
formatValue,
helpText,
@@ -291,239 +406,329 @@ const NumericSliderField = ({
sliderMin?: number;
step: number;
value: number;
-}) => (
- <Field error={error} helpText={helpText} hint={hint} label={label}>
- <HStack align="center" gap="3">
- <Slider
- aria-label={[label]}
- flex="1"
- formatValue={(nextValue) => (formatValue ? formatValue(nextValue) : String(nextValue))}
- marks={marks}
- max={sliderMax}
- min={sliderMin}
- size="sm"
- step={step}
- value={[Math.min(sliderMax, Math.max(sliderMin, value))]}
- onValueChange={(details) => {
- const nextValue = details.value[0];
-
- if (nextValue !== undefined) {
- onChange(nextValue);
- }
- }}
- />
- <NumberInput.Root
- max={numberMax}
- min={numberMin}
- size="xs"
- step={step}
- value={String(value)}
- w="20"
- onValueChange={({ valueAsNumber }) => {
- if (Number.isFinite(valueAsNumber)) {
- onChange(valueAsNumber);
- }
- }}
- >
- <NumberInput.Control />
- <NumberInput.Input aria-label={`${label} value`} fontVariantNumeric="tabular-nums" />
- </NumberInput.Root>
- </HStack>
- </Field>
-);
-
-const UpscaleImageField = ({
- inputImage,
- onChange,
-}: {
- inputImage: UpscaleWidgetValues['inputImage'];
- onChange: (image: UpscaleWidgetValues['inputImage']) => void;
-}) => {
- const { t } = useTranslation();
- const { reportError, touchGalleryImages } = useUpscaleUi();
- const fileInputRef = useRef<HTMLInputElement | null>(null);
- const [isLoading, setIsLoading] = useState(false);
- const [errorMessage, setErrorMessage] = useState<string | null>(null);
- const { isOver, setNodeRef } = useGalleryImageDroppable({
- data: { kind: DROP_ID },
- disabled: isLoading,
- id: DROP_ID,
- });
-
- const setGalleryImage = useCallback(
- async (imageName: string) => {
- setErrorMessage(null);
- setIsLoading(true);
-
- try {
- const [image] = await galleryImages.resolveMany([imageName]);
+}) {
+ const ariaLabel = useMemo(() => [label], [label]);
+ const sliderValue = useMemo(() => [Math.min(sliderMax, Math.max(sliderMin, value))], [sliderMax, sliderMin, value]);
+ const handleFormatValue = useCallback(
+ (nextValue: number) => (formatValue ? formatValue(nextValue) : String(nextValue)),
+ [formatValue]
+ );
+ const handleSliderChange = useCallback(
+ (details: { value: number[] }) => {
+ const nextValue = details.value[0];
- if (image) {
- onChange({ height: image.height, image_name: image.imageName, width: image.width });
- }
- } catch (error) {
- const message = error instanceof Error ? error.message : String(error);
- setErrorMessage(message);
- reportError(message);
- } finally {
- setIsLoading(false);
+ if (nextValue !== undefined) {
+ onChange(nextValue);
+ }
+ },
+ [onChange]
+ );
+ const handleNumberChange = useCallback(
+ ({ valueAsNumber }: NumberInput.ValueChangeDetails) => {
+ if (Number.isFinite(valueAsNumber)) {
+ onChange(valueAsNumber);
}
},
- [onChange, reportError]
+ [onChange]
);
- useDndMonitor({
- onDragEnd: (event) => {
- const data = event.active.data.current;
+ return (
+ <Field error={error} helpText={helpText} hint={hint} label={label}>
+ <HStack align="center" gap="3">
+ <Slider
+ aria-label={ariaLabel}
+ flex="1"
+ formatValue={handleFormatValue}
+ marks={marks}
+ max={sliderMax}
+ min={sliderMin}
+ size="sm"
+ step={step}
+ value={sliderValue}
+ onValueChange={handleSliderChange}
+ />
+ <NumberInput.Root
+ max={numberMax}
+ min={numberMin}
+ size="xs"
+ step={step}
+ value={String(value)}
+ w="20"
+ onValueChange={handleNumberChange}
+ >
+ <NumberInput.Control />
+ <NumberInput.Input aria-label={`${label} value`} fontVariantNumeric="tabular-nums" />
+ </NumberInput.Root>
+ </HStack>
+ </Field>
+ );
+});
+
+const UpscaleImageField = memo(
+ function UpscaleImageField({
+ inputImage,
+ onChange,
+ }: {
+ inputImage: UpscaleWidgetValues['inputImage'];
+ onChange: (image: UpscaleWidgetValues['inputImage']) => void;
+ }) {
+ const { t } = useTranslation();
+ const { reportError, touchGalleryImages } = useUpscaleUiActions();
+ const fileInputRef = useRef<HTMLInputElement | null>(null);
+ const [isLoading, setIsLoading] = useState(false);
+ const [errorMessage, setErrorMessage] = useState<string | null>(null);
+ const { isOver, setNodeRef } = useGalleryImageDroppable({
+ data: { kind: DROP_ID },
+ disabled: isLoading,
+ id: DROP_ID,
+ });
- if (!isLoading && event.over?.id === DROP_ID && isGalleryImageDragData(data) && data.items.length === 1) {
- const imageName = data.items[0]?.name;
+ const setGalleryImage = useCallback(
+ async (imageName: string) => {
+ setErrorMessage(null);
+ setIsLoading(true);
- if (imageName) {
- void setGalleryImage(imageName);
+ try {
+ const [image] = await galleryImages.resolveMany([imageName]);
+
+ if (image) {
+ onChange({ height: image.height, image_name: image.imageName, width: image.width });
+ }
+ } catch (error) {
+ const message = error instanceof Error ? error.message : String(error);
+ setErrorMessage(message);
+ reportError(message);
+ } finally {
+ setIsLoading(false);
}
- }
- },
- });
+ },
+ [onChange, reportError]
+ );
- const uploadFile = useCallback(
- async (file: File) => {
- setErrorMessage(null);
+ const handleDragEnd = useCallback(
+ (event: DragEndEvent) => {
+ const data = event.active.data.current;
- if (!['image/png', 'image/jpeg', 'image/webp'].includes(file.type)) {
- setErrorMessage(t('widgets.upscale.unsupportedFile'));
- reportError(t('widgets.upscale.unsupportedFile'));
- return;
- }
+ if (!isLoading && event.over?.id === DROP_ID && isGalleryImageDragData(data) && data.items.length === 1) {
+ const imageName = data.items[0]?.name;
+
+ if (imageName) {
+ void setGalleryImage(imageName);
+ }
+ }
+ },
+ [isLoading, setGalleryImage]
+ );
- const owner = captureAccountScope();
- setIsLoading(true);
+ useDndMonitor({ onDragEnd: handleDragEnd });
- try {
- const image = await galleryTransfers.upload(file, 'none', { signal: owner.signal });
+ const uploadFile = useCallback(
+ async (file: File) => {
+ setErrorMessage(null);
- assertAccountScopeCurrent(owner);
- onChange({ height: image.height, image_name: image.imageName, width: image.width });
- touchGalleryImages();
- } catch (error) {
- if (!isAccountScopeCurrent(owner)) {
+ if (!UPLOAD_ACCEPT_TYPES.includes(file.type)) {
+ setErrorMessage(t('widgets.upscale.unsupportedFile'));
+ reportError(t('widgets.upscale.unsupportedFile'));
return;
}
- const message = error instanceof Error ? error.message : String(error);
- setErrorMessage(message);
- reportError(message);
- } finally {
- setIsLoading(false);
- }
- },
- [onChange, reportError, t, touchGalleryImages]
- );
+ const owner = captureAccountScope();
+ setIsLoading(true);
+
+ try {
+ const image = await galleryTransfers.upload(file, 'none', { signal: owner.signal });
+
+ assertAccountScopeCurrent(owner);
+ onChange({ height: image.height, image_name: image.imageName, width: image.width });
+ touchGalleryImages();
+ } catch (error) {
+ if (!isAccountScopeCurrent(owner)) {
+ return;
+ }
+
+ const message = error instanceof Error ? error.message : String(error);
+ setErrorMessage(message);
+ reportError(message);
+ } finally {
+ setIsLoading(false);
+ }
+ },
+ [onChange, reportError, t, touchGalleryImages]
+ );
- const handleFileChange = useCallback(
- (event: ChangeEvent<HTMLInputElement>) => {
- const file = event.currentTarget.files?.[0];
+ const handleFileChange = useCallback(
+ (event: ChangeEvent<HTMLInputElement>) => {
+ const file = event.currentTarget.files?.[0];
- if (file) {
- void uploadFile(file);
+ if (file) {
+ void uploadFile(file);
+ }
+ event.currentTarget.value = '';
+ },
+ [uploadFile]
+ );
+ const handlePickFile = useCallback(() => fileInputRef.current?.click(), []);
+ const handleClear = useCallback(() => onChange(null), [onChange]);
+
+ return (
+ <Stack gap="2">
+ <DropZone
+ ref={setNodeRef}
+ as="button"
+ aria-busy={isLoading}
+ aria-label={inputImage ? t('widgets.upscale.replaceImage') : t('widgets.upscale.uploadImage')}
+ cursor="pointer"
+ isOver={isOver}
+ {...(isLoading ? DROP_ZONE_BUSY_PROPS : undefined)}
+ minH="24"
+ overflow="hidden"
+ position="relative"
+ _focusVisible={DROP_ZONE_FOCUS_PROPS}
+ _disabled={DROP_ZONE_DISABLED_PROPS}
+ _hover={isLoading ? undefined : DROP_ZONE_HOVER_PROPS}
+ onClick={handlePickFile}
+ >
+ {inputImage ? (
+ <HStack align="stretch" gap="3" h="24" p="2">
+ <Box bg="blackAlpha.300" boxSize="20" flexShrink="0" overflow="hidden" rounded="sm">
+ <Image
+ alt={t('widgets.upscale.inputImageAlt')}
+ boxSize="full"
+ objectFit="contain"
+ outline="1px solid"
+ outlineColor="border.image"
+ outlineOffset="-1px"
+ rounded="sm"
+ src={galleryImageUrls.thumbnail(inputImage.image_name)}
+ />
+ </Box>
+ <Stack align="start" flex="1" gap="1" justify="center" minW="0">
+ <MiddleTruncate color="fg" fontSize="xs" fontWeight="semibold" text={inputImage.image_name} />
+ <Text color="fg.muted" fontSize="2xs" fontVariantNumeric="tabular-nums">
+ {inputImage.width} × {inputImage.height}
+ </Text>
+ <HStack color="fg.muted" gap="1">
+ {isLoading ? <Spinner size="xs" /> : <UploadIcon aria-hidden="true" size="12" />}
+ <Text fontSize="2xs">
+ {isLoading ? t('widgets.upscale.uploadingImage') : t('widgets.upscale.replaceOrDrop')}
+ </Text>
+ </HStack>
+ </Stack>
+ </HStack>
+ ) : (
+ <Stack align="center" color="fg.muted" gap="2" justify="center" minH="24" px="4">
+ {isLoading ? <Spinner size="sm" /> : <ImagePlusIcon aria-hidden="true" size="20" />}
+ <Text fontSize="xs" textAlign="center">
+ {isLoading ? t('widgets.upscale.uploadingImage') : t('widgets.upscale.uploadOrDrop')}
+ </Text>
+ </Stack>
+ )}
+ </DropZone>
+ <HStack justify="end">
+ {inputImage ? (
+ <Button disabled={isLoading} size="xs" variant="ghost" onClick={handleClear}>
+ <XIcon aria-hidden="true" size="12" />
+ {t('widgets.upscale.removeImage')}
+ </Button>
+ ) : null}
+ </HStack>
+ {errorMessage ? (
+ <Text aria-live="polite" color="fg.error" fontSize="2xs" role="alert" textWrap="pretty">
+ {errorMessage}
+ </Text>
+ ) : null}
+ <Input
+ ref={fileInputRef}
+ accept="image/png,image/jpeg,image/webp,.png,.jpg,.jpeg,.webp"
+ aria-hidden="true"
+ display="none"
+ tabIndex={-1}
+ type="file"
+ onChange={handleFileChange}
+ />
+ </Stack>
+ );
+ },
+ (previous, next) =>
+ previous.onChange === next.onChange && areInputImagesEquivalent(previous.inputImage, next.inputImage)
+);
+
+/**
+ * One LoRA row. Split out so editing a single weight re-renders that row rather
+ * than the whole list; the handlers bind the key here instead of at the call
+ * site, where they would be new closures per row per render.
+ */
+const UpscaleLoraRow = memo(function UpscaleLoraRow({
+ lora,
+ onRemove,
+ onUpdate,
+}: {
+ lora: GenerateLora;
+ onRemove: (key: string) => void;
+ onUpdate: (key: string, update: Partial<GenerateLora>) => void;
+}) {
+ const { t } = useTranslation();
+ const modelKey = lora.model.key;
+ const handleToggle = useCallback(
+ (details: { checked: boolean }) => onUpdate(modelKey, { isEnabled: details.checked }),
+ [modelKey, onUpdate]
+ );
+ const handleWeightChange = useCallback(
+ ({ valueAsNumber }: NumberInput.ValueChangeDetails) => {
+ if (Number.isFinite(valueAsNumber)) {
+ onUpdate(modelKey, { weight: valueAsNumber });
}
- event.currentTarget.value = '';
},
- [uploadFile]
+ [modelKey, onUpdate]
);
+ const handleRemove = useCallback(() => onRemove(modelKey), [modelKey, onRemove]);
return (
- <Stack gap="2">
- <DropZone
- ref={setNodeRef}
- as="button"
- aria-busy={isLoading}
- aria-label={inputImage ? t('widgets.upscale.replaceImage') : t('widgets.upscale.uploadImage')}
- cursor="pointer"
- isOver={isOver}
- {...(isLoading ? { disabled: true } : undefined)}
- minH="24"
- overflow="hidden"
- position="relative"
- _focusVisible={{
- outlineColor: 'accent.focusRing',
- outlineOffset: '2px',
- outlineStyle: 'solid',
- outlineWidth: '2px',
- }}
- _disabled={{ cursor: 'wait', opacity: 0.7 }}
- _hover={isLoading ? undefined : { bg: 'bg.muted', color: 'fg' }}
- onClick={() => fileInputRef.current?.click()}
+ <HStack bg="bg.subtle" gap="2" p="2" rounded="md">
+ <Switch.Root aria-label={lora.model.name} checked={lora.isEnabled} size="sm" onCheckedChange={handleToggle}>
+ <Switch.HiddenInput />
+ <Switch.Control _checked={SWITCH_CHECKED_PROPS}>
+ <Switch.Thumb />
+ </Switch.Control>
+ </Switch.Root>
+ <MiddleTruncate flex="1" fontSize="xs" minW="0" text={lora.model.name} />
+ <NumberInput.Root
+ max={10}
+ min={-10}
+ size="xs"
+ step={0.05}
+ value={String(lora.weight)}
+ w="20"
+ onValueChange={handleWeightChange}
>
- {inputImage ? (
- <HStack align="stretch" gap="3" h="24" p="2">
- <Box bg="blackAlpha.300" boxSize="20" flexShrink="0" overflow="hidden" rounded="sm">
- <Image
- alt={t('widgets.upscale.inputImageAlt')}
- boxSize="full"
- objectFit="contain"
- outline="1px solid"
- outlineColor="border.image"
- outlineOffset="-1px"
- rounded="sm"
- src={galleryImageUrls.thumbnail(inputImage.image_name)}
- />
- </Box>
- <Stack align="start" flex="1" gap="1" justify="center" minW="0">
- <MiddleTruncate color="fg" fontSize="xs" fontWeight="semibold" text={inputImage.image_name} />
- <Text color="fg.muted" fontSize="2xs" fontVariantNumeric="tabular-nums">
- {inputImage.width} × {inputImage.height}
- </Text>
- <HStack color="fg.muted" gap="1">
- {isLoading ? <Spinner size="xs" /> : <UploadIcon aria-hidden="true" size="12" />}
- <Text fontSize="2xs">
- {isLoading ? t('widgets.upscale.uploadingImage') : t('widgets.upscale.replaceOrDrop')}
- </Text>
- </HStack>
- </Stack>
- </HStack>
- ) : (
- <Stack align="center" color="fg.muted" gap="2" justify="center" minH="24" px="4">
- {isLoading ? <Spinner size="sm" /> : <ImagePlusIcon aria-hidden="true" size="20" />}
- <Text fontSize="xs" textAlign="center">
- {isLoading ? t('widgets.upscale.uploadingImage') : t('widgets.upscale.uploadOrDrop')}
- </Text>
- </Stack>
- )}
- </DropZone>
- <HStack justify="end">
- {inputImage ? (
- <Button disabled={isLoading} size="xs" variant="ghost" onClick={() => onChange(null)}>
- <XIcon aria-hidden="true" size="12" />
- {t('widgets.upscale.removeImage')}
- </Button>
- ) : null}
- </HStack>
- {errorMessage ? (
- <Text aria-live="polite" color="fg.error" fontSize="2xs" role="alert" textWrap="pretty">
- {errorMessage}
- </Text>
- ) : null}
- <Input
- ref={fileInputRef}
- accept="image/png,image/jpeg,image/webp,.png,.jpg,.jpeg,.webp"
- aria-hidden="true"
- display="none"
- tabIndex={-1}
- type="file"
- onChange={handleFileChange}
- />
- </Stack>
+ <NumberInput.Input aria-label={t('widgets.upscale.loraWeight', { name: lora.model.name })} />
+ </NumberInput.Root>
+ <IconButton
+ aria-label={t('widgets.upscale.removeLora', { name: lora.model.name })}
+ size="xs"
+ variant="ghost"
+ onClick={handleRemove}
+ >
+ <Trash2Icon />
+ </IconButton>
+ </HStack>
);
-};
+});
export const UpscaleWidgetView = () => {
const { t } = useTranslation();
const selection = useUpscaleUi();
const models = useModelsSelector((snapshot) => snapshot.models);
const modelsStatus = useModelsSelector((snapshot) => snapshot.status);
- const normalized = normalizeUpscaleWidgetValues(selection.rawValues) ?? createDefaultUpscaleWidgetValues();
- const values = modelsStatus === 'loaded' ? syncUpscaleWidgetValuesWithModels(normalized, models) : normalized;
+ const { patchPromptDraft: patchDraft, patchValues, projectId, promptDraft, rawValues } = selection;
+ // Normalizing and reconciling against the model list is the widget's most
+ // expensive derivation; it must not run on unrelated re-renders, and a fresh
+ // `values` identity would re-render every section below.
+ const values = useMemo(() => {
+ const normalized = normalizeUpscaleWidgetValues(rawValues) ?? createDefaultUpscaleWidgetValues();
+
+ return modelsStatus === 'loaded' ? syncUpscaleWidgetValuesWithModels(normalized, models) : normalized;
+ }, [models, modelsStatus, rawValues]);
const modelsFingerprint = useMemo(
() =>
models
@@ -534,125 +739,203 @@ export const UpscaleWidgetView = () => {
.join('|'),
[models]
);
- const errors = {
- cfgScale: getRangeError(t('widgets.upscale.cfgScale'), values.cfgScale, 0, 100),
- creativity: getRangeError(
- t('widgets.upscale.creativity'),
- values.creativity,
- UPSCALE_CREATIVITY_MIN,
- UPSCALE_CREATIVITY_MAX
- ),
- scale: getRangeError(t('widgets.upscale.scale'), values.scale, UPSCALE_SCALE_MIN, UPSCALE_SCALE_MAX),
- seed: getRangeError(t('widgets.upscale.seed'), values.seed, 0, SEED_MAX),
- steps: getRangeError(t('widgets.upscale.steps'), values.steps, 1, 1000),
- structure: getRangeError(
- t('widgets.upscale.structure'),
- values.structure,
- UPSCALE_STRUCTURE_MIN,
- UPSCALE_STRUCTURE_MAX
- ),
- tileOverlap: getRangeError(
- t('widgets.upscale.tileOverlap'),
- values.tileOverlap,
- UPSCALE_TILE_OVERLAP_MIN,
- UPSCALE_TILE_OVERLAP_MAX
- ),
- tileSize: getRangeError(
- t('widgets.upscale.tileSize'),
- values.tileSize,
- UPSCALE_TILE_SIZE_MIN,
- UPSCALE_TILE_SIZE_MAX
- ),
- };
- const patch = useCallback((next: Partial<UpscaleWidgetValues>) => selection.patchValues(next), [selection]);
- const patchPromptDraft = useCallback(
- (next: ProjectPromptDraftPatch) => selection.patchPromptDraft(next),
- [selection]
+ const errors = useMemo(
+ () => ({
+ cfgScale: getRangeError(t('widgets.upscale.cfgScale'), values.cfgScale, 0, 100),
+ creativity: getRangeError(
+ t('widgets.upscale.creativity'),
+ values.creativity,
+ UPSCALE_CREATIVITY_MIN,
+ UPSCALE_CREATIVITY_MAX
+ ),
+ scale: getRangeError(t('widgets.upscale.scale'), values.scale, UPSCALE_SCALE_MIN, UPSCALE_SCALE_MAX),
+ seed: getRangeError(t('widgets.upscale.seed'), values.seed, 0, SEED_MAX),
+ steps: getRangeError(t('widgets.upscale.steps'), values.steps, 1, 1000),
+ structure: getRangeError(
+ t('widgets.upscale.structure'),
+ values.structure,
+ UPSCALE_STRUCTURE_MIN,
+ UPSCALE_STRUCTURE_MAX
+ ),
+ tileOverlap: getRangeError(
+ t('widgets.upscale.tileOverlap'),
+ values.tileOverlap,
+ UPSCALE_TILE_OVERLAP_MIN,
+ UPSCALE_TILE_OVERLAP_MAX
+ ),
+ tileSize: getRangeError(
+ t('widgets.upscale.tileSize'),
+ values.tileSize,
+ UPSCALE_TILE_SIZE_MIN,
+ UPSCALE_TILE_SIZE_MAX
+ ),
+ }),
+ [t, values]
);
- const replace = useCallback((next: UpscaleWidgetValues) => selection.patchValues({ ...next }), [selection]);
+ const patch = useCallback((next: Partial<UpscaleWidgetValues>) => patchValues(next), [patchValues]);
+ const patchPromptDraft = useCallback((next: ProjectPromptDraftPatch) => patchDraft(next), [patchDraft]);
useMountEffect(() => {
void ensureModelsLoaded();
});
- const selectMainModel = (model: ModelConfig | null) => {
- if (!isMainModelConfig(model) || !isSelectableMainModel(model)) {
- return;
- }
+ const selectMainModel = useCallback(
+ (model: ModelConfig | null) => {
+ if (!isMainModelConfig(model) || !isSelectableMainModel(model)) {
+ return;
+ }
- const nextValues = syncUpscaleWidgetValuesWithModels({ ...values, model: model as MainModelConfig }, models);
- const notices: string[] = [];
+ const nextValues = syncUpscaleWidgetValuesWithModels({ ...values, model: model as MainModelConfig }, models);
+ const notices: string[] = [];
- if (values.tileControlnetModel?.key !== nextValues.tileControlnetModel?.key) {
- notices.push(
- nextValues.tileControlnetModel
- ? t('widgets.upscale.controlNetChanged', { name: nextValues.tileControlnetModel.name })
- : t('widgets.upscale.controlNetCleared')
- );
- }
- if (values.vae && !nextValues.vae) {
- notices.push(t('widgets.upscale.vaeCleared'));
- }
- const removedLoraCount = values.loras.length - nextValues.loras.length;
+ if (values.tileControlnetModel?.key !== nextValues.tileControlnetModel?.key) {
+ notices.push(
+ nextValues.tileControlnetModel
+ ? t('widgets.upscale.controlNetChanged', { name: nextValues.tileControlnetModel.name })
+ : t('widgets.upscale.controlNetCleared')
+ );
+ }
+ if (values.vae && !nextValues.vae) {
+ notices.push(t('widgets.upscale.vaeCleared'));
+ }
+ const removedLoraCount = values.loras.length - nextValues.loras.length;
- if (removedLoraCount > 0) {
- notices.push(t('widgets.upscale.lorasRemoved', { count: removedLoraCount }));
- }
+ if (removedLoraCount > 0) {
+ notices.push(t('widgets.upscale.lorasRemoved', { count: removedLoraCount }));
+ }
- replace(nextValues);
+ patch({ ...nextValues });
- if (notices.length > 0) {
- toaster.create({
- description: notices.join(' '),
- title: t('widgets.upscale.settingsAdjusted'),
- type: 'info',
- });
- }
- };
+ if (notices.length > 0) {
+ toaster.create({
+ description: notices.join(' '),
+ title: t('widgets.upscale.settingsAdjusted'),
+ type: 'info',
+ });
+ }
+ },
+ [models, patch, t, values]
+ );
- const addLora = (model: ModelConfig | null) => {
- if (!values.model || !isLoraModelConfig(model) || !isLoraCompatibleWithModel(model, values.model)) {
- return;
- }
+ const addLora = useCallback(
+ (model: ModelConfig | null) => {
+ if (!values.model || !isLoraModelConfig(model) || !isLoraCompatibleWithModel(model, values.model)) {
+ return;
+ }
- patch({ loras: [...values.loras, { isEnabled: true, model, weight: getDefaultLoraWeight(model) }] });
- };
+ patch({ loras: [...values.loras, { isEnabled: true, model, weight: getDefaultLoraWeight(model) }] });
+ },
+ [patch, values.loras, values.model]
+ );
+ const updateLora = useCallback(
+ (key: string, update: Partial<GenerateLora>) =>
+ patch({ loras: values.loras.map((lora) => (lora.model.key === key ? { ...lora, ...update } : lora)) }),
+ [patch, values.loras]
+ );
+ const removeLora = useCallback(
+ (key: string) => patch({ loras: values.loras.filter((candidate) => candidate.model.key !== key) }),
+ [patch, values.loras]
+ );
+ const selectedLoraKeys = useMemo(() => new Set(values.loras.map((lora) => lora.model.key)), [values.loras]);
- const updateLora = (key: string, update: Partial<GenerateLora>) =>
- patch({ loras: values.loras.map((lora) => (lora.model.key === key ? { ...lora, ...update } : lora)) });
- const selectedLoraKeys = new Set(values.loras.map((lora) => lora.model.key));
+ const activePresetId = useMemo(
+ () =>
+ PRESET_ENTRIES.find(
+ ([, preset]) => values.creativity === preset.creativity && values.structure === preset.structure
+ )?.[0] ?? null,
+ [values.creativity, values.structure]
+ );
+ const applyPreset = useCallback(
+ ({ value }: { value: string | null }) => {
+ const preset = value ? UPSCALE_PRESETS[value as keyof typeof UPSCALE_PRESETS] : undefined;
- const activePresetId =
- Object.entries(UPSCALE_PRESETS).find(
- ([, preset]) => values.creativity === preset.creativity && values.structure === preset.structure
- )?.[0] ?? null;
+ if (preset) {
+ patch({ creativity: preset.creativity, structure: preset.structure });
+ }
+ },
+ [patch]
+ );
- const applyPreset = ({ value }: { value: string | null }) => {
- const preset = value ? UPSCALE_PRESETS[value as keyof typeof UPSCALE_PRESETS] : undefined;
+ // One setter per field, created once per `patch` identity: inline
+ // `onChange={(x) => patch({ x })}` props would defeat every `memo` below.
+ const set = useMemo(
+ () => ({
+ batchCount: ({ valueAsNumber }: NumberInput.ValueChangeDetails) =>
+ Number.isFinite(valueAsNumber) && patch({ batchCount: valueAsNumber }),
+ cfgScale: ({ valueAsNumber }: NumberInput.ValueChangeDetails) =>
+ Number.isFinite(valueAsNumber) && patch({ cfgScale: valueAsNumber }),
+ clipSkip: ({ valueAsNumber }: NumberInput.ValueChangeDetails) =>
+ Number.isFinite(valueAsNumber) && patch({ clipSkip: valueAsNumber }),
+ creativity: (creativity: number) => patch({ creativity }),
+ inputImage: (inputImage: UpscaleWidgetValues['inputImage']) => patch({ inputImage }),
+ randomizeSeed: (details: { checked: boolean }) => patch({ shouldRandomizeSeed: details.checked }),
+ scale: (scale: number) => patch({ scale }),
+ scheduler: (scheduler: string) => patch({ scheduler }),
+ seed: ({ valueAsNumber }: NumberInput.ValueChangeDetails) =>
+ Number.isFinite(valueAsNumber) && patch({ seed: valueAsNumber }),
+ shuffleSeed: () => patch({ seed: Math.floor(Math.random() * SEED_MAX) }),
+ spandrelModel: (model: ModelConfig | null) =>
+ patch({ upscaleModel: isSpandrelModelConfig(model) ? model : null }),
+ steps: ({ valueAsNumber }: NumberInput.ValueChangeDetails) =>
+ Number.isFinite(valueAsNumber) && patch({ steps: valueAsNumber }),
+ structure: (structure: number) => patch({ structure }),
+ tileOverlap: (tileOverlap: number) => patch({ tileOverlap }),
+ tileSize: (tileSize: number) => patch({ tileSize }),
+ vae: (model: ModelConfig | null) => patch({ vae: isVaeModelConfig(model) ? model : null }),
+ vaePrecision: ({ value }: { value: string[] }) => {
+ const vaePrecision = value[0];
- if (preset) {
- patch({ creativity: preset.creativity, structure: preset.structure });
- }
- };
+ if (vaePrecision === 'fp16' || vaePrecision === 'fp32') {
+ patch({ vaePrecision });
+ }
+ },
+ }),
+ [patch]
+ );
+ // Model filters close over the selected main model, so they change only when
+ // that model does — not on every keystroke elsewhere in the form.
+ const loraFilter = useCallback(
+ (model: ModelConfig) =>
+ Boolean(values.model && isLoraModelConfig(model) && isLoraCompatibleWithModel(model, values.model)),
+ [values.model]
+ );
+ const tileControlNetFilter = useCallback(
+ (model: ModelConfig) => isTileControlNetCandidate(model, values.model),
+ [values.model]
+ );
+ const setTileControlNet = useCallback(
+ (model: ModelConfig | null) =>
+ patch({ tileControlnetModel: isTileControlNetCandidate(model, values.model) ? model : null }),
+ [patch, values.model]
+ );
+ const vaeFilter = useCallback(
+ (model: ModelConfig) => Boolean(values.model && model.base === values.model.base),
+ [values.model]
+ );
+
+ const vaePrecisionValue = useMemo(() => [values.vaePrecision], [values.vaePrecision]);
- const sharedBadge = (
- <Badge fontFamily="mono" size="xs">
- {t('widgets.upscale.shared')}
- </Badge>
+ const sharedBadge = useMemo(
+ () => (
+ <Badge fontFamily="mono" size="xs">
+ {t('widgets.upscale.shared')}
+ </Badge>
+ ),
+ [t]
);
return (
<Stack gap="1" minW="0" p="1">
<UpscaleModelReconciler
- key={`${selection.projectId}:${modelsStatus}:${modelsFingerprint}`}
- rawValues={selection.rawValues}
+ key={`${projectId}:${modelsStatus}:${modelsFingerprint}`}
+ rawValues={rawValues}
values={values}
/>
<GenerationSettingsSection label={t('widgets.upscale.sourceAndTreatment')} defaultOpen>
<Stack gap="3" p="2">
- <UpscaleImageField inputImage={values.inputImage} onChange={(inputImage) => patch({ inputImage })} />
- <UpscaleOutputPreflight values={values} />
+ <UpscaleImageField inputImage={values.inputImage} onChange={set.inputImage} />
+ <UpscaleOutputPreflight inputImage={values.inputImage} scale={values.scale} />
<Field
error={values.upscaleModel ? undefined : t('widgets.upscale.spandrelModelRequired')}
helpText={values.upscaleModel ? t('widgets.upscale.spandrelModelHelp') : undefined}
@@ -661,25 +944,25 @@ export const UpscaleWidgetView = () => {
>
<ModelSelect
invalid={!values.upscaleModel}
- modelTypes={['spandrel_image_to_image']}
+ modelTypes={SPANDREL_MODEL_TYPES}
placeholder={t('widgets.upscale.selectSpandrelModel')}
size="xs"
value={values.upscaleModel?.key ?? null}
- onChange={(model) => patch({ upscaleModel: isSpandrelModelConfig(model) ? model : null })}
+ onChange={set.spandrelModel}
/>
</Field>
<NumericSliderField
error={errors.scale}
- formatValue={(scale) => `${scale}×`}
+ formatValue={formatScale}
helpText={t('widgets.upscale.scaleHelp')}
hint="upscaleScale"
label={t('widgets.upscale.scale')}
- marks={[1, 2, 4, 8, 16]}
+ marks={SCALE_MARKS}
numberMax={UPSCALE_SCALE_MAX}
numberMin={UPSCALE_SCALE_MIN}
step={0.5}
value={values.scale}
- onChange={(scale) => patch({ scale })}
+ onChange={set.scale}
/>
<SegmentGroup.Root
aria-label={t('widgets.upscale.presetsLabel')}
@@ -689,7 +972,7 @@ export const UpscaleWidgetView = () => {
onValueChange={applyPreset}
>
<SegmentGroup.Indicator />
- {Object.entries(UPSCALE_PRESETS).map(([id, preset]) => {
+ {PRESET_ENTRIES.map(([id, preset]) => {
const tooltipContent = `${t(`widgets.upscale.presetDescriptions.${id}`)} ${t(
'widgets.upscale.presetValues',
{ creativity: preset.creativity, structure: preset.structure }
@@ -713,34 +996,37 @@ export const UpscaleWidgetView = () => {
helpText={t('widgets.upscale.creativityHelp')}
hint="creativity"
label={t('widgets.upscale.creativity')}
- marks={[UPSCALE_CREATIVITY_MIN, 0, UPSCALE_CREATIVITY_MAX]}
+ marks={CREATIVITY_MARKS}
numberMax={UPSCALE_CREATIVITY_MAX}
numberMin={UPSCALE_CREATIVITY_MIN}
step={1}
value={values.creativity}
- onChange={(creativity) => patch({ creativity })}
+ onChange={set.creativity}
/>
<NumericSliderField
error={errors.structure}
helpText={t('widgets.upscale.structureHelp')}
hint="structure"
label={t('widgets.upscale.structure')}
- marks={[UPSCALE_STRUCTURE_MIN, 0, UPSCALE_STRUCTURE_MAX]}
+ marks={STRUCTURE_MARKS}
numberMax={UPSCALE_STRUCTURE_MAX}
numberMin={UPSCALE_STRUCTURE_MIN}
step={1}
value={values.structure}
- onChange={(structure) => patch({ structure })}
+ onChange={set.structure}
/>
</Stack>
</GenerationSettingsSection>
<GenerationSettingsSection badges={sharedBadge} label={t('widgets.upscale.detailGuidance')}>
<UpscalePromptFields
- promptDraft={selection.promptDraft}
- projectId={selection.projectId}
+ loras={values.loras}
+ model={values.model}
+ negativePromptHeightPx={values.negativePromptHeightPx}
+ positivePromptHeightPx={values.positivePromptHeightPx}
+ promptDraft={promptDraft}
+ projectId={projectId}
showSyntaxHighlighting={selection.showPromptSyntaxHighlighting}
- values={values}
onPatchPromptDraft={patchPromptDraft}
onPatchValues={patch}
/>
@@ -756,22 +1042,16 @@ export const UpscaleWidgetView = () => {
<ModelSelect
filter={isSelectableMainModel}
invalid={!values.model}
- modelTypes={['main']}
+ modelTypes={MAIN_MODEL_TYPES}
placeholder={t('widgets.upscale.selectMainModel')}
size="xs"
value={values.model?.key ?? null}
onChange={selectMainModel}
/>
</Field>
- <SimpleGrid columns={{ base: 2, md: 3 }} gap="2">
+ <SimpleGrid columns={GENERATION_GRID_COLUMNS} gap="2">
<Field error={errors.steps} hint="steps" label={t('widgets.upscale.steps')}>
- <NumberInput.Root
- max={1000}
- min={1}
- size="xs"
- value={String(values.steps)}
- onValueChange={({ valueAsNumber }) => Number.isFinite(valueAsNumber) && patch({ steps: valueAsNumber })}
- >
+ <NumberInput.Root max={1000} min={1} size="xs" value={String(values.steps)} onValueChange={set.steps}>
<NumberInput.Control />
<NumberInput.Input fontVariantNumeric="tabular-nums" />
</NumberInput.Root>
@@ -783,23 +1063,14 @@ export const UpscaleWidgetView = () => {
size="xs"
step={0.5}
value={String(values.cfgScale)}
- onValueChange={({ valueAsNumber }) =>
- Number.isFinite(valueAsNumber) && patch({ cfgScale: valueAsNumber })
- }
+ onValueChange={set.cfgScale}
>
<NumberInput.Control />
<NumberInput.Input fontVariantNumeric="tabular-nums" />
</NumberInput.Root>
</Field>
<Field hint="batchCount" label={t('widgets.upscale.batchCount')}>
- <NumberInput.Root
- min={1}
- size="xs"
- value={String(values.batchCount)}
- onValueChange={({ valueAsNumber }) =>
- Number.isFinite(valueAsNumber) && patch({ batchCount: valueAsNumber })
- }
- >
+ <NumberInput.Root min={1} size="xs" value={String(values.batchCount)} onValueChange={set.batchCount}>
<NumberInput.Control />
<NumberInput.Input fontVariantNumeric="tabular-nums" />
</NumberInput.Root>
@@ -811,7 +1082,7 @@ export const UpscaleWidgetView = () => {
options={SCHEDULER_OPTIONS}
size="xs"
value={values.scheduler}
- onValueChange={(scheduler) => patch({ scheduler })}
+ onValueChange={set.scheduler}
/>
</Field>
<Field
@@ -827,7 +1098,7 @@ export const UpscaleWidgetView = () => {
size="xs"
value={String(values.seed)}
w="full"
- onValueChange={({ valueAsNumber }) => Number.isFinite(valueAsNumber) && patch({ seed: valueAsNumber })}
+ onValueChange={set.seed}
>
<NumberInput.Input fontVariantNumeric="tabular-nums" />
</NumberInput.Root>
@@ -837,18 +1108,14 @@ export const UpscaleWidgetView = () => {
disabled={values.shouldRandomizeSeed}
size="xs"
variant="outline"
- onClick={() => patch({ seed: Math.floor(Math.random() * SEED_MAX) })}
+ onClick={set.shuffleSeed}
>
<DicesIcon />
</IconButton>
</Tooltip>
- <Switch.Root
- checked={values.shouldRandomizeSeed}
- size="sm"
- onCheckedChange={(event) => patch({ shouldRandomizeSeed: event.checked })}
- >
+ <Switch.Root checked={values.shouldRandomizeSeed} size="sm" onCheckedChange={set.randomizeSeed}>
<Switch.HiddenInput />
- <Switch.Control _checked={{ bg: 'accent.solid' }}>
+ <Switch.Control _checked={SWITCH_CHECKED_PROPS}>
<Switch.Thumb />
</Switch.Control>
<Switch.Label fontSize="xs">{t('widgets.upscale.random')}</Switch.Label>
@@ -858,10 +1125,8 @@ export const UpscaleWidgetView = () => {
<Field hint="concepts" label={t('widgets.upscale.addLora')}>
<ModelSelect
excludeKeys={selectedLoraKeys}
- filter={(model) =>
- Boolean(values.model && isLoraModelConfig(model) && isLoraCompatibleWithModel(model, values.model))
- }
- modelTypes={['lora']}
+ filter={loraFilter}
+ modelTypes={LORA_MODEL_TYPES}
placeholder={t('widgets.upscale.selectLora')}
size="xs"
value={null}
@@ -869,43 +1134,7 @@ export const UpscaleWidgetView = () => {
/>
</Field>
{values.loras.map((lora) => (
- <HStack key={lora.model.key} bg="bg.subtle" gap="2" p="2" rounded="md">
- <Switch.Root
- aria-label={lora.model.name}
- checked={lora.isEnabled}
- size="sm"
- onCheckedChange={(event) => updateLora(lora.model.key, { isEnabled: event.checked })}
- >
- <Switch.HiddenInput />
- <Switch.Control _checked={{ bg: 'accent.solid' }}>
- <Switch.Thumb />
- </Switch.Control>
- </Switch.Root>
- <MiddleTruncate flex="1" fontSize="xs" minW="0" text={lora.model.name} />
- <NumberInput.Root
- max={10}
- min={-10}
- size="xs"
- step={0.05}
- value={String(lora.weight)}
- w="20"
- onValueChange={({ valueAsNumber }) =>
- Number.isFinite(valueAsNumber) && updateLora(lora.model.key, { weight: valueAsNumber })
- }
- >
- <NumberInput.Input aria-label={t('widgets.upscale.loraWeight', { name: lora.model.name })} />
- </NumberInput.Root>
- <IconButton
- aria-label={t('widgets.upscale.removeLora', { name: lora.model.name })}
- size="xs"
- variant="ghost"
- onClick={() =>
- patch({ loras: values.loras.filter((candidate) => candidate.model.key !== lora.model.key) })
- }
- >
- <Trash2Icon />
- </IconButton>
- </HStack>
+ <UpscaleLoraRow key={lora.model.key} lora={lora} onRemove={removeLora} onUpdate={updateLora} />
))}
</Stack>
</GenerationSettingsSection>
@@ -919,15 +1148,13 @@ export const UpscaleWidgetView = () => {
label={t('widgets.upscale.tileControlNet')}
>
<ModelSelect
- filter={(model) => isTileControlNetCandidate(model, values.model)}
+ filter={tileControlNetFilter}
invalid={!values.tileControlnetModel}
- modelTypes={['controlnet']}
+ modelTypes={CONTROLNET_MODEL_TYPES}
placeholder={t('widgets.upscale.selectTileControlNet')}
size="xs"
value={values.tileControlnetModel?.key ?? null}
- onChange={(model) =>
- patch({ tileControlnetModel: isTileControlNetCandidate(model, values.model) ? model : null })
- }
+ onChange={setTileControlNet}
/>
</Field>
<NumericSliderField
@@ -935,39 +1162,39 @@ export const UpscaleWidgetView = () => {
helpText={t('widgets.upscale.tileSizeHelp')}
hint="tileSize"
label={t('widgets.upscale.tileSize')}
- marks={[UPSCALE_TILE_SIZE_MIN, 1024, UPSCALE_TILE_SIZE_MAX]}
+ marks={TILE_SIZE_MARKS}
numberMax={UPSCALE_TILE_SIZE_MAX}
numberMin={UPSCALE_TILE_SIZE_MIN}
step={64}
value={values.tileSize}
- onChange={(tileSize) => patch({ tileSize })}
+ onChange={set.tileSize}
/>
<NumericSliderField
error={errors.tileOverlap}
helpText={t('widgets.upscale.tileOverlapHelp')}
hint="tileOverlap"
label={t('widgets.upscale.tileOverlap')}
- marks={[UPSCALE_TILE_OVERLAP_MIN, 128, 256, UPSCALE_TILE_OVERLAP_MAX]}
+ marks={TILE_OVERLAP_MARKS}
numberMax={UPSCALE_TILE_OVERLAP_MAX}
numberMin={UPSCALE_TILE_OVERLAP_MIN}
step={8}
value={values.tileOverlap}
- onChange={(tileOverlap) => patch({ tileOverlap })}
+ onChange={set.tileOverlap}
/>
- <SimpleGrid columns={{ base: 1, md: 2 }} gap="2">
+ <SimpleGrid columns={ADVANCED_GRID_COLUMNS} gap="2">
<Field
hint="vae"
label={t('widgets.upscale.vae')}
helpText={values.vae ? undefined : t('widgets.upscale.bundledVae')}
>
<ModelSelect
- filter={(model) => Boolean(values.model && model.base === values.model.base)}
+ filter={vaeFilter}
isClearable
- modelTypes={['vae']}
+ modelTypes={VAE_MODEL_TYPES}
placeholder={t('widgets.upscale.bundledVae')}
size="xs"
value={values.vae?.key ?? null}
- onChange={(model) => patch({ vae: isVaeModelConfig(model) ? model : null })}
+ onChange={set.vae}
/>
</Field>
<Field hint="vaePrecision" label={t('widgets.upscale.vaePrecision')}>
@@ -975,28 +1202,14 @@ export const UpscaleWidgetView = () => {
aria-label={t('widgets.upscale.vaePrecision')}
collection={VAE_PRECISION_COLLECTION}
size="xs"
- value={[values.vaePrecision]}
- onValueChange={({ value }) => {
- const vaePrecision = value[0];
-
- if (vaePrecision === 'fp16' || vaePrecision === 'fp32') {
- patch({ vaePrecision });
- }
- }}
+ value={vaePrecisionValue}
+ onValueChange={set.vaePrecision}
/>
</Field>
</SimpleGrid>
{values.model?.base === 'sd-1' ? (
<Field hint="clipSkip" label={t('widgets.upscale.clipSkip')}>
- <NumberInput.Root
- max={12}
- min={0}
- size="xs"
- value={String(values.clipSkip)}
- onValueChange={({ valueAsNumber }) =>
- Number.isFinite(valueAsNumber) && patch({ clipSkip: valueAsNumber })
- }
- >
+ <NumberInput.Root max={12} min={0} size="xs" value={String(values.clipSkip)} onValueChange={set.clipSkip}>
<NumberInput.Control />
<NumberInput.Input fontVariantNumeric="tabular-nums" />
</NumberInput.Root>