File size: 2,607 Bytes
b173115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { defineComponent, ref, toRaw } from 'vue'
import { open } from '@tauri-apps/api/dialog'
import { fs } from '@tauri-apps/api'
import { Button, Modal, message } from 'ant-design-vue'
import { relaunch } from '@tauri-apps/api/process'
import { delay } from 'vue3-ts-util'
import { t } from './i18n'
import { invoke } from '@tauri-apps/api/tauri'
export const appConfFilename = 'app.conf.json'
interface TauriAppLaunchConf {
  sdwebui_dir: string
}
const conf = ref<TauriAppLaunchConf>()
const save = () => {
  return fs.writeFile(appConfFilename, JSON.stringify(toRaw(conf.value), null, 4))
}

const TauriLaunchConfModal = defineComponent({
  setup () {

    const onSelectSdWebuiClick = async () => {
      const dir = await open({ directory: true })
      if (typeof dir === 'string') {
        if (!(await fs.exists(`${dir}/config.json`))) {
          return message.error(t('tauriLaunchConfMessages.configNotFound'))
        }
        if (!(await fs.exists(`${dir}/extensions/sd-webui-infinite-image-browsing`))) {
          return message.error(t('tauriLaunchConfMessages.folderNotFound'))
        }
        conf.value!.sdwebui_dir = dir
        message.info(t('tauriLaunchConfMessages.configCompletedMessage'))
        await save()
        await invoke('shutdown_api_server_command')
        await delay(1500)
        await relaunch()
      }
    }

    return () => (
      <div style={{ padding: '32px 0' }}>
        <div style={{ padding: '16px 0' }}>
          <h2>{t('tauriLaunchConf.readSdWebuiConfigTitle')}</h2>
          <p>
            {t('tauriLaunchConf.readSdWebuiConfigDescription')}
          </p>
          <Button onClick={onSelectSdWebuiClick} type="primary">
            {t('tauriLaunchConf.selectSdWebuiFolder')}
          </Button>
        </div>
        <div style={{ padding: '16px 0' }}>
          <h2>{t('tauriLaunchConf.skipThisConfigTitle')}</h2>
          <p>{t('tauriLaunchConf.skipThisConfigDescription')}</p>
          <Button type="primary" onClick={Modal.destroyAll}>
            {t('tauriLaunchConf.skipButton')}
          </Button>
        </div>
      </div>
    )
  }
})

export const openModal = async () => {
  try {
    conf.value = JSON.parse(await fs.readTextFile(appConfFilename))
  } catch (error) { }
  if (!conf.value) {
    conf.value = {
      sdwebui_dir: ''
    }
    await save()
    Modal.info({
      title: t('tauriLaunchConfMessages.firstTimeUserTitle'),
      content: <TauriLaunchConfModal />,
      width: '80vw',
      okText: t('tauriLaunchConf.skipButton'),
      okButtonProps: {
        onClick: Modal.destroyAll
      }
    })
  }
}