add maskPreview

This commit is contained in:
Qing
2022-12-11 19:54:47 +08:00
parent e4664e2d58
commit 03965c69e6
2 changed files with 55 additions and 15 deletions

View File

@@ -32,3 +32,12 @@ header {
gap: 6px; gap: 6px;
justify-self: end; justify-self: end;
} }
.mask-preview {
max-height: 400px;
max-width: 400px;
margin-top: 30px;
margin-left: 20px;
border: 1px solid var(--border-color);
border-radius: 8px;
}

View File

@@ -2,12 +2,13 @@ import { ArrowUpTrayIcon } from '@heroicons/react/24/outline'
import { PlayIcon } from '@radix-ui/react-icons' import { PlayIcon } from '@radix-ui/react-icons'
import React, { useState } from 'react' import React, { useState } from 'react'
import { useRecoilState, useRecoilValue } from 'recoil' import { useRecoilState, useRecoilValue } from 'recoil'
import * as PopoverPrimitive from '@radix-ui/react-popover'
import { import {
fileState, fileState,
interactiveSegClicksState,
isInpaintingState, isInpaintingState,
isSDState, isSDState,
maskState, maskState,
runManuallyState,
} from '../../store/Atoms' } from '../../store/Atoms'
import Button from '../shared/Button' import Button from '../shared/Button'
import Shortcuts from '../Shortcuts/Shortcuts' import Shortcuts from '../Shortcuts/Shortcuts'
@@ -16,14 +17,18 @@ import SettingIcon from '../Settings/SettingIcon'
import PromptInput from './PromptInput' import PromptInput from './PromptInput'
import CoffeeIcon from '../CoffeeIcon/CoffeeIcon' import CoffeeIcon from '../CoffeeIcon/CoffeeIcon'
import emitter, { EVENT_CUSTOM_MASK } from '../../event' import emitter, { EVENT_CUSTOM_MASK } from '../../event'
import { useImage } from '../../utils'
const Header = () => { const Header = () => {
const isInpainting = useRecoilValue(isInpaintingState) const isInpainting = useRecoilValue(isInpaintingState)
const [file, setFile] = useRecoilState(fileState) const [file, setFile] = useRecoilState(fileState)
const [mask, setMask] = useRecoilState(maskState) const [mask, setMask] = useRecoilState(maskState)
const [maskImage, maskImageLoaded] = useImage(mask)
const [uploadElemId] = useState(`file-upload-${Math.random().toString()}`) const [uploadElemId] = useState(`file-upload-${Math.random().toString()}`)
const [maskUploadElemId] = useState(`mask-upload-${Math.random().toString()}`) const [maskUploadElemId] = useState(`mask-upload-${Math.random().toString()}`)
const isSD = useRecoilValue(isSDState) const isSD = useRecoilValue(isSDState)
const runManually = useRecoilValue(runManuallyState)
const [openMaskPopover, setOpenMaskPopover] = useState(false)
const renderHeader = () => { const renderHeader = () => {
return ( return (
@@ -88,10 +93,11 @@ const Header = () => {
onChange={ev => { onChange={ev => {
const newFile = ev.currentTarget.files?.[0] const newFile = ev.currentTarget.files?.[0]
if (newFile) { if (newFile) {
// TODO: check mask size
console.info('Send custom mask')
emitter.emit(EVENT_CUSTOM_MASK, { mask: newFile })
setMask(newFile) setMask(newFile)
console.info('Send custom mask')
if (!runManually) {
emitter.emit(EVENT_CUSTOM_MASK, { mask: newFile })
}
} }
}} }}
accept="image/png, image/jpeg" accept="image/png, image/jpeg"
@@ -99,17 +105,42 @@ const Header = () => {
Mask Mask
</Button> </Button>
</label> </label>
<Button
style={{ <PopoverPrimitive.Root open={openMaskPopover}>
visibility: mask ? 'visible' : 'hidden', <PopoverPrimitive.Trigger
}} className="btn-primary side-panel-trigger"
icon={<PlayIcon />} onMouseEnter={() => setOpenMaskPopover(true)}
onClick={() => { onMouseLeave={() => setOpenMaskPopover(false)}
if (mask) { style={{
emitter.emit(EVENT_CUSTOM_MASK, { mask }) visibility: mask ? 'visible' : 'hidden',
} outline: 'none',
}} }}
/> onClick={() => {
if (mask) {
emitter.emit(EVENT_CUSTOM_MASK, { mask })
}
}}
>
<PlayIcon />
</PopoverPrimitive.Trigger>
<PopoverPrimitive.Portal>
<PopoverPrimitive.Content
style={{
outline: 'none',
}}
>
{maskImageLoaded ? (
<img
src={maskImage.src}
alt="mask"
className="mask-preview"
/>
) : (
<></>
)}
</PopoverPrimitive.Content>
</PopoverPrimitive.Portal>
</PopoverPrimitive.Root>
</div> </div>
</div> </div>