File size: 3,811 Bytes
34097e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const io = require('../io')

function getUseSharpMask() {
    const isChecked = document.getElementById('chUseSharpMask').checked
    return isChecked
}
function setUseSharpMask() {
    console.warn('setUseSharpMask is not setup')
}

function getUseLiveProgressImage() {
    const b_live_update = document.getElementById('chLiveProgressImage').checked
    return b_live_update
}
function setUseLiveProgressImage(b_live_update) {
    document.getElementById('chLiveProgressImage').checked = b_live_update
}

function getExtensionType() {
    return [...document.getElementsByClassName('rbExtensionType')].filter(
        (e) => e.checked == true
    )[0].value
}

document.getElementById('btnGetDocPath').addEventListener('click', async () => {
    const docPath = await io.IOFolder.getDocumentFolderNativePath()
    document.getElementById('tiDocPath').value = docPath

    const uuid = await getUniqueDocumentId()
    doc_entry = await io.IOFolder.getDocFolder(uuid)
    await shell.openPath(doc_entry.nativePath)
})

document.getElementById('btnSdUrl').addEventListener('click', async () => {
    //change the sdUrl in server in proxy server
    // console.log('you clicked btnSdUrl')
    let new_sd_url = document.getElementById('tiSdUrl').value
    changeSdUrl(new_sd_url)
})

function getSdUrlHtml() {
    let sd_url = document.getElementById('tiSdUrl').value
    return sd_url
}
function setSdUrlHtml(sd_url) {
    document.getElementById('tiSdUrl').value = sd_url
}
async function changeSdUrl(sd_url) {
    sd_url = sd_url.trim()
    console.log('sd_url.trim(): ', sd_url)

    if (sd_url.length > 0) {
        //check if the last character of the url has "/" or '\' and remove it

        last_index = sd_url.length - 1

        if (sd_url[last_index] === '/' || sd_url[last_index] === '\\') {
            sd_url = sd_url.slice(0, -1)
        }

        //submit the change
        await sdapi.changeSdUrl(sd_url)
    }
}

async function saveSettings() {
    const settings_tab_settings = {
        use_sharp_mask: getUseSharpMask(),
        extension_type: getExtensionType(),
        sd_url: getSdUrlHtml(),
    }

    const folder = await io.IOFolder.getSettingsFolder()
    await io.IOJson.saveJsonToFile(
        settings_tab_settings,
        folder,
        'settings_tab.json'
    )
}
async function loadSettings() {
    try {
        const folder = await io.IOFolder.getSettingsFolder()
        let settings_tab_settings = await io.IOJson.loadJsonFromFile(
            folder,
            'settings_tab.json'
        )
        setSdUrlHtml(settings_tab_settings['sd_url'])
        await changeSdUrl(settings_tab_settings['sd_url'])
    } catch (e) {
        console.warn(e)
    }
}

document.getElementById('chUseSharpMask').addEventListener('change', (ev) => {
    const isChecked = ev.target.checked
    if (isChecked) {
        document.getElementById('slMaskBlur').setAttribute('disabled')
    } else {
        document.getElementById('slMaskBlur').removeAttribute('disabled')
    }
})

document.getElementById('chUseSmartObject').addEventListener('change', (ev) => {
    const isChecked = ev.target.checked
    if (isChecked) {
        g_b_use_smart_object = true
    } else {
        g_b_use_smart_object = false
    }
})

function getUseOriginalPrompt() {
    const b_use_original_prompt = document.getElementById(
        'chUseOriginalPrompt'
    ).checked
    return b_use_original_prompt
}

document
    .getElementById('btnSaveSettingsTabs')
    .addEventListener('click', async () => {
        await saveSettings()
    })

module.exports = {
    getUseSharpMask,
    setUseSharpMask,
    getExtensionType,
    getSdUrlHtml,
    setSdUrlHtml,
    changeSdUrl,
    loadSettings,
    saveSettings,
    getUseLiveProgressImage,
    setUseLiveProgressImage,
    getUseOriginalPrompt,
}