|
const { cleanLayers } = require('../psapi') |
|
const psapi = require('../psapi') |
|
const io = require('./io') |
|
const Enum = require('../enum') |
|
const { ViewerManager } = require('../viewer') |
|
const { base64ToBase64Url } = require('./general') |
|
const html_manip = require('./html_manip') |
|
const layer_util = require('./layer') |
|
const SessionState = { |
|
Active: 'active', |
|
Inactive: 'inactive', |
|
} |
|
const GarbageCollectionState = { |
|
Accept: 'accept', |
|
Discard: 'discard', |
|
DiscardSelected: 'discard_selected', |
|
AcceptSelected: 'accept_selected', |
|
} |
|
|
|
class GenerationSession { |
|
constructor() { |
|
|
|
this.id = 0 |
|
this.state = SessionState['Inactive'] |
|
this.mode = 'txt2img' |
|
this.selectionInfo = null |
|
this.isFirstGeneration = true |
|
this.outputGroup |
|
this.prevOutputGroup |
|
this.isLoadingActive = false |
|
this.base64OutputImages = {} |
|
this.base64initImages = {} |
|
this.base64maskImage = [] |
|
this.base64maskExpansionImage |
|
this.activeBase64InitImage |
|
this.activeBase64MaskImage |
|
this.image_paths_to_layers = {} |
|
this.progress_layer |
|
this.last_settings |
|
this.controlNetImage = [] |
|
this.controlNetMask = [] |
|
this.request_status = Enum.RequestStateEnum['Finished'] |
|
this.is_control_net = false |
|
this.control_net_selection_info |
|
this.control_net_preview_selection_info |
|
} |
|
isActive() { |
|
return this.state === SessionState['Active'] |
|
} |
|
isInactive() { |
|
return this.state === SessionState['Inactive'] |
|
} |
|
activate() { |
|
this.state = SessionState['Active'] |
|
} |
|
deactivate() { |
|
this.state = SessionState['Inactive'] |
|
} |
|
name() { |
|
return `session - ${this.id}` |
|
} |
|
async startSession() { |
|
this.id += 1 |
|
this.activate() |
|
this.isFirstGeneration = true |
|
|
|
console.log('current session id: ', this.id) |
|
try { |
|
const session_name = this.name() |
|
const activeLayers = await app.activeDocument.activeLayers |
|
await psapi.unselectActiveLayersExe() |
|
this.prevOutputGroup = this.outputGroup |
|
const outputGroup = await psapi.createEmptyGroup(session_name) |
|
await executeAsModal(async () => { |
|
outputGroup.allLocked = true |
|
}) |
|
this.outputGroup = outputGroup |
|
await psapi.selectLayersExe(activeLayers) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async endSession(garbage_collection_state) { |
|
try { |
|
if (!this.isActive()) { |
|
|
|
return null |
|
} |
|
this.state = SessionState['Inactive'] |
|
|
|
this.deactivate() |
|
|
|
if (garbage_collection_state === GarbageCollectionState['Accept']) { |
|
await acceptAll() |
|
} else if ( |
|
garbage_collection_state === GarbageCollectionState['Discard'] |
|
) { |
|
|
|
|
|
await discardAll() |
|
} else if ( |
|
garbage_collection_state === |
|
GarbageCollectionState['DiscardSelected'] |
|
) { |
|
|
|
await discardSelected() |
|
} else if ( |
|
garbage_collection_state === |
|
GarbageCollectionState['AcceptSelected'] |
|
) { |
|
|
|
await discard() |
|
} |
|
|
|
|
|
|
|
|
|
this.isFirstGeneration = true |
|
|
|
g_viewer_manager.last_selected_viewer_obj = null |
|
g_viewer_manager.onSessionEnd() |
|
await layer_util.collapseFolderExe([this.outputGroup], false) |
|
await executeAsModal(async () => { |
|
this.outputGroup.allLocked = false |
|
}) |
|
|
|
|
|
if ( |
|
this.mode === generationMode['Inpaint'] && |
|
g_sd_mode === generationMode['Inpaint'] |
|
) { |
|
|
|
|
|
g_b_mask_layer_exist = false |
|
await layer_util.deleteLayers([g_inpaint_mask_layer]) |
|
await createTempInpaintMaskLayer() |
|
} |
|
|
|
|
|
|
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
deleteInitImageLayers() {} |
|
async closePreviousOutputGroup() { |
|
try { |
|
|
|
|
|
if (this.prevOutputGroup) { |
|
|
|
await layer_util.collapseFolderExe( |
|
[this.prevOutputGroup], |
|
false |
|
) |
|
|
|
await psapi.selectLayersExe([this.outputGroup]) |
|
|
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
isSameMode(selected_mode) { |
|
if (this.mode === selected_mode) { |
|
return true |
|
} |
|
return false |
|
} |
|
loadLastSession() { |
|
|
|
} |
|
saveCurrentSession() { |
|
|
|
} |
|
async moveToTopOfOutputGroup(layer) { |
|
const output_group_id = await this.outputGroup.id |
|
let group_index = await psapi.getLayerIndex(output_group_id) |
|
const indexOffset = 1 |
|
await executeAsModal(async () => { |
|
await psapi.selectLayersExe([layer]) |
|
await psapi.moveToGroupCommand(group_index - indexOffset, layer.id) |
|
}) |
|
} |
|
|
|
async deleteProgressLayer() { |
|
try { |
|
await layer_util.deleteLayers([this.progress_layer]) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
deleteProgressImageHtml() { |
|
try { |
|
|
|
|
|
|
|
|
|
document.getElementById( |
|
'divProgressImageViewerContainer' |
|
).style.height = '0px' |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
async deleteProgressImage() { |
|
this.deleteProgressImageHtml() |
|
await this.deleteProgressLayer() |
|
} |
|
async setControlNetImageHelper() { |
|
const width = html_manip.getWidth() |
|
const height = html_manip.getHeight() |
|
|
|
|
|
const selectionInfo = await psapi.getSelectionInfoExe() |
|
this.control_net_selection_info = selectionInfo |
|
this.control_net_preview_selection_info = selectionInfo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const use_silent_mode = html_manip.getUseSilentMode() |
|
let layer = null |
|
if (!use_silent_mode) { |
|
await psapi.snapshot_layerExe() |
|
const snapshotLayer = await app.activeDocument.activeLayers[0] |
|
layer = snapshotLayer |
|
} |
|
const base64_image = |
|
await io.IO.getSelectionFromCanvasAsBase64Interface( |
|
width, |
|
height, |
|
layer, |
|
selectionInfo, |
|
true, |
|
use_silent_mode |
|
) |
|
|
|
await layer_util.deleteLayers([layer]) |
|
return base64_image |
|
} |
|
async setControlNetImage(control_net_index = 0, base64_image) { |
|
|
|
|
|
|
|
|
|
this.controlNetImage[control_net_index] = base64_image |
|
html_manip.setControlImageSrc( |
|
base64ToBase64Url(base64_image), |
|
control_net_index |
|
) |
|
|
|
|
|
} |
|
} |
|
|
|
module.exports = { |
|
GenerationSession, |
|
GarbageCollectionState, |
|
SessionState, |
|
} |
|
|