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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
feat(ui): share the canvas segmented-control look via SegmentedControl
Josh Corbettdddfd679d6a5
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.tsx b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.tsxindex 1f2f9a1c34..894806e4e5 100644--- a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.tsx+++ b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.tsx@@ -1,7 +1,8 @@import type { GalleryView } from '@features/gallery/core/types';-import { SegmentGroup, Text } from '@chakra-ui/react';-import { useCallback } from 'react';+import { Text } from '@chakra-ui/react';+import { SegmentedControl } from '@platform/ui';+import { useCallback, useMemo } from 'react';import { useTranslation } from 'react-i18next';import { getGalleryCountForView } from './galleryBoardLabels';@@ -11,7 +12,6 @@ const GALLERY_VIEW_TABS = [{ labelKey: 'common.media', value: 'images' },{ labelKey: 'common.assets', value: 'assets' },] satisfies { labelKey: string; value: GalleryView }[];-const CHECKED_VIEW_TAB_STYLES = { bg: 'accent.solid', color: 'accent.contrast' } as const;/*** Media / Assets, each carrying the selected board's count for that view so@@ -27,30 +27,16 @@ export const GalleryViewTabs = () => {const { actions, gallery } = useGalleryWidget();const selectedBoard = gallery.boards.find((board) => board.id === gallery.selectedBoardId);- const handleViewChange = useCallback(- (event: { value: string | null }) => {- if (event.value) {- actions.setView(event.value as GalleryView);- }- },- [actions]- );+ const handleViewChange = useCallback((value: string) => actions.setView(value as GalleryView), [actions]);- return (- <SegmentGroup.Root- aria-label={t('common.view')}- size="xs"- value={gallery.galleryView}- onValueChange={handleViewChange}- >- <SegmentGroup.Indicator />- {GALLERY_VIEW_TABS.map(({ labelKey, value }) => {+ const options = useMemo(+ () =>+ GALLERY_VIEW_TABS.map(({ labelKey, value }) => {const count = selectedBoard ? getGalleryCountForView(selectedBoard, value) : null;- return (- <SegmentGroup.Item key={value} value={value} _checked={CHECKED_VIEW_TAB_STYLES}>- <SegmentGroup.ItemHiddenInput />- <SegmentGroup.ItemText display="flex" fontSize="xs" gap="1.5">+ return {+ label: (+ <Text as="span" display="flex" gap="1.5">{t(labelKey)}{count === null ? null : (// Dimmed from the item's own text colour rather than pinned to@@ -61,10 +47,21 @@ export const GalleryViewTabs = () => {{count}</Text>)}- </SegmentGroup.ItemText>- </SegmentGroup.Item>- );- })}- </SegmentGroup.Root>+ </Text>+ ),+ value,+ };+ }),+ [selectedBoard, t]+ );++ return (+ <SegmentedControl+ ariaLabel={t('common.view')}+ isFullWidth={false}+ options={options}+ value={gallery.galleryView}+ onChange={handleViewChange}+ />);};diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsButton.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsButton.tsxindex dd8c8e6fb4..bd512c04f5 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsButton.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/DynamicPromptsButton.tsx@@ -1,12 +1,13 @@import type { DynamicPromptsFieldConfig } from '@features/generation/ui/promptFields/DynamicPromptsPanel';-import { Popover, Portal, SegmentGroup, Stack, Text } from '@chakra-ui/react';+import { Popover, Portal, Stack, Text } from '@chakra-ui/react';import { DynamicPromptsPanel } from '@features/generation/ui/promptFields/DynamicPromptsPanel';import { WildcardsPanel } from '@features/generation/ui/promptFields/WildcardsPanel';import { useDynamicPrompts } from '@features/generation/ui/useDynamicPrompts';import { useWildcards } from '@features/generation/ui/useWildcards';import { IconButton } from '@platform/ui/Button';import { PopoverContent } from '@platform/ui/Popover';+import { SegmentedControl } from '@platform/ui/SegmentedControl';import { Tooltip } from '@platform/ui/Tooltip';import { BracesIcon } from 'lucide-react';import { useCallback, useId, useMemo, useState } from 'react';@@ -41,10 +42,7 @@ export const DynamicPromptsButton = ({const popoverIds = useMemo(() => ({ trigger: triggerId }), [triggerId]);const handleOpenChange = useCallback((event: { open: boolean }) => setIsOpen(event.open), []);- const handleTabChange = useCallback(- (event: { value: string | null }) => setTab(event.value === 'wildcards' ? 'wildcards' : 'preview'),- []- );+ const handleTabChange = useCallback((value: string) => setTab(value === 'wildcards' ? 'wildcards' : 'preview'), []);const closeWith = useCallback((apply: (value: string) => void) => (value: string) => {apply(value);@@ -109,12 +107,15 @@ export const DynamicPromptsButton = ({<PopoverContent w="26rem"><Popover.Body p="2.5"><Stack gap="2.5">- {/* `alignSelf` keeps the tabs to their content width; stretched across- the popover they read as a header band rather than a control. */}- <SegmentGroup.Root alignSelf="start" size="xs" value={tab} onValueChange={handleTabChange}>- <SegmentGroup.Indicator />- <SegmentGroup.Items items={tabItems} />- </SegmentGroup.Root>+ {/* Content width (`isFullWidth={false}` + `alignSelf`): stretched across+ the popover the tabs read as a header band rather than a control. */}+ <SegmentedControl+ alignSelf="start"+ isFullWidth={false}+ options={tabItems}+ value={tab}+ onChange={handleTabChange}+ />{tab === 'preview' ? (<DynamicPromptsPanelbatchCount={batchCount}diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/graph-preview/GraphPreviewDialog.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/graph-preview/GraphPreviewDialog.tsxindex 2a370d5f35..1f701b884a 100644--- a/invokeai/frontend/webv2/src/features/workflow/ui/graph-preview/GraphPreviewDialog.tsx+++ b/invokeai/frontend/webv2/src/features/workflow/ui/graph-preview/GraphPreviewDialog.tsx@@ -2,9 +2,9 @@ import type { GraphPreviewSourceState, WorkflowInvocationSourceId } from '@featuimport type { ReactFlowInstance } from '@xyflow/react';import type { ReactNode } from 'react';-import { Box, Dialog, Icon, Portal, SegmentGroup, Stack, Text } from '@chakra-ui/react';+import { Box, Dialog, Icon, Portal, Stack, Text } from '@chakra-ui/react';import { useWorkflowGraphPreview } from '@features/workflow/ui/WorkflowUiContext';-import { Button, JsonPreview, toaster } from '@platform/ui';+import { Button, JsonPreview, SegmentedControl, toaster } from '@platform/ui';import { CheckIcon, ChevronUpIcon, CopyIcon, TriangleAlertIcon } from 'lucide-react';import { useCallback, useMemo, useRef, useState } from 'react';import { useTranslation } from 'react-i18next';@@ -110,9 +110,9 @@ export const GraphPreviewDialog = ({);const handleOpenChange = useCallback((event: { open: boolean }) => closeAndReset(event.open), [closeAndReset]);- const handleModeChange = useCallback((event: { value: string | null }) => {- if (isPreviewMode(event.value)) {- setMode(event.value);+ const handleModeChange = useCallback((value: string) => {+ if (isPreviewMode(value)) {+ setMode(value);}}, []);const closeDialog = useCallback(() => closeAndReset(false), [closeAndReset]);@@ -194,6 +194,7 @@ export const GraphPreviewDialog = ({.catch(() => toaster.create({ title: t('graphPreview.copyFailed'), type: 'error' }));}, [graph, t]);+ const modeOptions = useMemo(() => modeItems.map((item) => ({ label: t(item.labelKey), value: item.value })), [t]);const jsonLabel = useMemo(() => t('graphPreview.graphJsonLabel', { title: sourceLabel }), [t, sourceLabel]);const subtitle = useMemo(() => {const compiledFrom = t('graphPreview.compiledFrom', { source: sourceLabel });@@ -223,15 +224,7 @@ export const GraphPreviewDialog = ({{subtitle}</Text></Stack>- <SegmentGroup.Root size="xs" value={mode} onValueChange={handleModeChange}>- <SegmentGroup.Indicator />- {modeItems.map((item) => (- <SegmentGroup.Item key={item.value} value={item.value}>- <SegmentGroup.ItemHiddenInput />- <SegmentGroup.ItemText>{t(item.labelKey)}</SegmentGroup.ItemText>- </SegmentGroup.Item>- ))}- </SegmentGroup.Root>+ <SegmentedControl isFullWidth={false} options={modeOptions} value={mode} onChange={handleModeChange} /></Dialog.Header><Dialog.Body display="flex" flex="1" flexDirection="column" gap="3" minH="0">{hasInvalidReasons ? (diff --git a/invokeai/frontend/webv2/src/platform/ui/SegmentedControl.tsx b/invokeai/frontend/webv2/src/platform/ui/SegmentedControl.tsxnew file mode 100644index 0000000000..5815a05978--- /dev/null+++ b/invokeai/frontend/webv2/src/platform/ui/SegmentedControl.tsx@@ -0,0 +1,72 @@+import type { ReactNode } from 'react';++import { SegmentGroup } from '@chakra-ui/react';+import { useCallback } from 'react';++// Axe measures the checked label against the item's own background, not the+// moving indicator sibling painted behind it, so the item carries the fill too.+const CHECKED_ITEM_STYLES = { bg: 'accent.solid', color: 'accent.contrast' } as const;++export interface SegmentedControlOption {+ disabled?: boolean;+ label: ReactNode;+ value: string;+}++export interface SegmentedControlProps extends Omit<SegmentGroup.RootProps, 'onChange' | 'onValueChange' | 'value'> {+ ariaLabel?: string;+ disabled?: boolean;+ /** Default true: the control fills its container, split into equal segments. */+ isFullWidth?: boolean;+ onChange: (value: string) => void;+ options: readonly SegmentedControlOption[];+ value: string | null;+}++/** The house segmented control: an `xs` group of equal centered segments with `2xs` labels. */+export const SegmentedControl = ({+ ariaLabel,+ disabled,+ isFullWidth = true,+ onChange,+ options,+ value,+ ...rest+}: SegmentedControlProps) => {+ const handleValueChange = useCallback(+ ({ value: next }: SegmentGroup.ValueChangeDetails) => {+ if (next !== null) {+ onChange(next);+ }+ },+ [onChange]+ );++ return (+ <SegmentGroup.Root+ aria-label={ariaLabel}+ disabled={disabled}+ size="xs"+ value={value}+ w={isFullWidth ? 'full' : undefined}+ onValueChange={handleValueChange}+ {...rest}+ >+ <SegmentGroup.Indicator />+ {options.map((option) => (+ <SegmentGroup.Item+ key={option.value}+ disabled={option.disabled}+ flex={isFullWidth ? '1' : undefined}+ justifyContent={isFullWidth ? 'center' : undefined}+ minW={isFullWidth ? '0' : undefined}+ value={option.value}+ _checked={CHECKED_ITEM_STYLES}+ >+ <SegmentGroup.ItemHiddenInput />+ <SegmentGroup.ItemText fontSize="2xs">{option.label}</SegmentGroup.ItemText>+ </SegmentGroup.Item>+ ))}+ </SegmentGroup.Root>+ );+};diff --git a/invokeai/frontend/webv2/src/platform/ui/index.ts b/invokeai/frontend/webv2/src/platform/ui/index.tsindex a519c44e7a..1bbbee6183 100644--- a/invokeai/frontend/webv2/src/platform/ui/index.ts+++ b/invokeai/frontend/webv2/src/platform/ui/index.ts@@ -16,6 +16,7 @@ export * from './RenameDialog';export * from './ResizableTextarea';export * from './Row';export * from './Scrollable';+export * from './SegmentedControl';export * from './SegmentTabs';export * from './Select';export * from './Slider';diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-presentation/PropertyPrimitives.tsx b/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-presentation/PropertyPrimitives.tsxindex 319ce1195d..1883f09473 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-presentation/PropertyPrimitives.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-presentation/PropertyPrimitives.tsx@@ -1,6 +1,7 @@import type { ReactNode } from 'react';-import { Badge, chakra, Flex, Grid, Icon, SegmentGroup, Stack, Switch, Text } from '@chakra-ui/react';+import { Badge, chakra, Flex, Grid, Icon, Stack, Switch, Text } from '@chakra-ui/react';+import { SegmentedControl } from '@platform/ui/SegmentedControl';import { ChevronDownIcon } from 'lucide-react';import { useCallback } from 'react';import { useTranslation } from 'react-i18next';@@ -131,8 +132,8 @@ export const PropertySegmentedRow = <Value extends string>({value: Value;}) => {const handleChange = useCallback(- ({ value: next }: SegmentGroup.ValueChangeDetails) => {- if (next !== null && next !== value) {+ (next: string) => {+ if (next !== value) {onValueChange(next as Value);}},@@ -140,23 +141,14 @@ export const PropertySegmentedRow = <Value extends string>({);return (<PropertyControlRow label={label}>- <SegmentGroup.Root- aria-label={label}+ <SegmentedControl+ ariaLabel={label}disabled={disabled}gridColumn="2 / -1"- size="xs"+ options={options}value={value}- w="full"- onValueChange={handleChange}- >- <SegmentGroup.Indicator />- {options.map((option) => (- <SegmentGroup.Item key={option.value} flex="1" justifyContent="center" value={option.value}>- <SegmentGroup.ItemText fontSize="2xs">{option.label}</SegmentGroup.ItemText>- <SegmentGroup.ItemHiddenInput />- </SegmentGroup.Item>- ))}- </SegmentGroup.Root>+ onChange={handleChange}+ /></PropertyControlRow>);};