Spaces:
Configuration error
Configuration error
File size: 6,835 Bytes
74aacd5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
import { FolderIcon, PhotoIcon } from '@heroicons/react/24/outline'
import { PlayIcon, ReloadIcon } from '@radix-ui/react-icons'
import React, { useCallback, useState } from 'react'
import { useRecoilState, useRecoilValue } from 'recoil'
import * as PopoverPrimitive from '@radix-ui/react-popover'
import {
enableFileManagerState,
fileState,
isInpaintingState,
isPix2PixState,
isSDState,
maskState,
runManuallyState,
showFileManagerState,
} from '../../store/Atoms'
import Button from '../shared/Button'
import Shortcuts from '../Shortcuts/Shortcuts'
import { ThemeChanger } from './ThemeChanger'
import SettingIcon from '../Settings/SettingIcon'
import PromptInput from './PromptInput'
import CoffeeIcon from '../CoffeeIcon/CoffeeIcon'
import emitter, { EVENT_CUSTOM_MASK, RERUN_LAST_MASK } from '../../event'
import { useImage } from '../../utils'
import useHotKey from '../../hooks/useHotkey'
const Header = () => {
const isInpainting = useRecoilValue(isInpaintingState)
const [file, setFile] = useRecoilState(fileState)
const [mask, setMask] = useRecoilState(maskState)
const [maskImage, maskImageLoaded] = useImage(mask)
const [uploadElemId] = useState(`file-upload-${Math.random().toString()}`)
const [maskUploadElemId] = useState(`mask-upload-${Math.random().toString()}`)
const isSD = useRecoilValue(isSDState)
const isPix2Pix = useRecoilValue(isPix2PixState)
const runManually = useRecoilValue(runManuallyState)
const [openMaskPopover, setOpenMaskPopover] = useState(false)
const [showFileManager, setShowFileManager] =
useRecoilState(showFileManagerState)
const enableFileManager = useRecoilValue(enableFileManagerState)
useHotKey(
'f',
() => {
if (enableFileManager && !isInpainting) {
setShowFileManager(!showFileManager)
}
},
{},
[showFileManager, enableFileManager, isInpainting]
)
const handleRerunLastMask = useCallback(() => {
emitter.emit(RERUN_LAST_MASK)
}, [])
useHotKey(
'r',
() => {
if (!isInpainting) {
handleRerunLastMask()
}
},
{},
[isInpainting, handleRerunLastMask]
)
const renderHeader = () => {
return (
<header>
<div
style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
gap: 4,
}}
>
{enableFileManager ? (
<Button
icon={<FolderIcon />}
style={{ border: 0 }}
toolTip="Open File Manager"
disabled={isInpainting}
onClick={() => {
setShowFileManager(true)
}}
/>
) : (
<></>
)}
<label htmlFor={uploadElemId}>
<Button
icon={<PhotoIcon />}
style={{ border: 0, gap: 0 }}
disabled={isInpainting}
toolTip="Upload image"
>
<input
style={{ display: 'none' }}
id={uploadElemId}
name={uploadElemId}
type="file"
onChange={ev => {
const newFile = ev.currentTarget.files?.[0]
if (newFile) {
setFile(newFile)
}
}}
accept="image/png, image/jpeg"
/>
</Button>
</label>
<div
style={{
visibility: file ? 'visible' : 'hidden',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
}}
>
<label htmlFor={maskUploadElemId}>
<Button
style={{ border: 0 }}
disabled={isInpainting}
toolTip="Upload custom mask"
>
<input
style={{ display: 'none' }}
id={maskUploadElemId}
name={maskUploadElemId}
type="file"
onClick={e => {
const element = e.target as HTMLInputElement
element.value = ''
}}
onChange={ev => {
const newFile = ev.currentTarget.files?.[0]
if (newFile) {
setMask(newFile)
console.info('Send custom mask')
if (!runManually) {
emitter.emit(EVENT_CUSTOM_MASK, { mask: newFile })
}
}
}}
accept="image/png, image/jpeg"
/>
Mask
</Button>
</label>
{mask ? (
<PopoverPrimitive.Root open={openMaskPopover}>
<PopoverPrimitive.Trigger
className="btn-primary side-panel-trigger"
onMouseEnter={() => setOpenMaskPopover(true)}
onMouseLeave={() => setOpenMaskPopover(false)}
style={{
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>
) : (
<></>
)}
<Button
icon={<ReloadIcon style={{ height: 16, width: 16 }} />}
style={{ border: 0, gap: 0 }}
disabled={isInpainting}
toolTip="Rerun last mask [r]"
onClick={handleRerunLastMask}
/>
</div>
</div>
{(isSD || isPix2Pix) && file ? <PromptInput /> : <></>}
<div className="header-icons-wrapper">
<CoffeeIcon />
<ThemeChanger />
<div className="header-icons">
<Shortcuts />
<SettingIcon />
</div>
</div>
</header>
)
}
return renderHeader()
}
export default Header
|