forked from mirabarukaso/character_select_stand_alone_app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
143 lines (119 loc) · 4.8 KB
/
Copy pathmain.js
File metadata and controls
143 lines (119 loc) · 4.8 KB
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
// main.js
// Modules to control application life and create native browser window
import { app, BrowserWindow, ipcMain } from 'electron';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
// common functions for main and wsService
import { setupIPCs, getAppVersion } from './main-common.js';
// WebSocket server
import { setupHttpServer, closeWebSocketServer } from './webserver/back/wsService.js';
// Import custom modules
import { setupFileHandlers } from './scripts/main/fileHandlers.js';
import { setupGlobalSettings } from './scripts/main/globalSettings.js';
import { setupDownloadFiles } from './scripts/main/downloadFiles.js';
import { setupModelList } from './scripts/main/modelList.js';
import { setupTagAutoCompleteBackend } from './scripts/main/tagAutoComplete_backend.js';
import { setupModelApi } from './scripts/main/remoteAI_backend.js';
import { setupGenerateBackendComfyUI, sendToRenderer } from './scripts/main/generate_backend_comfyui.js';
import { setupGenerateBackendWebUI } from './scripts/main/generate_backend_webui.js';
import { setupCachedFiles } from './scripts/main/cachedFiles.js';
import { setupWildcardsHandlers } from './scripts/main/wildCards.js';
import { setupTagger } from './scripts/main/imageTagger.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let mainWindow; // Main browser window instance
function replaceMisspelling(word) {
mainWindow.webContents.replaceMisspelling(word);
return true;
}
function addToDictionary(word) {
mainWindow.webContents.session.addWordToSpellCheckerDictionary(word);
return true;
}
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
autoHideMenuBar: true, // Hide menu
width: 1300,
height: 1200,
icon: path.join(__dirname, 'html/icon.png'),
webPreferences: {
preload: path.join(__dirname, 'scripts/preload.js'),
contextIsolation: true, // Enable context isolation
nodeIntegration: false, // Disable Node.js integration
nodeIntegrationInWorker: true, // Enable multithread
spellcheck: true, // Enable spellcheck
sandbox: false, // Disable sandbox for ES modules
webSecurity: true, //Enable web security
}
});
// Set the spellchecker to check English US
mainWindow.webContents.session.setSpellCheckerLanguages(['en-US']);
// Send the spellcheck suggestions to the renderer process
mainWindow.webContents.on('context-menu', (event, params) => {
event.preventDefault();
const suggestions = params.dictionarySuggestions || [];
const word = params.misspelledWord || '';
sendToRenderer(`none`, `rightClickMenu_spellCheck`, suggestions, word);
});
// and load the index_electron.html of the app.
mainWindow.loadFile('index_electron.html');
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
async function initializeApp() {
const version = getAppVersion();
console.log("Character Select SAA Version:", version);
setupFileHandlers();
const SETTINGS = setupGlobalSettings();
setupModelList(SETTINGS);
const downloadSuccess = await setupDownloadFiles();
const cacheSuccess = setupCachedFiles();
// Ensure wildcards list are set up before tag auto-complete
setupWildcardsHandlers();
const tacSuccess = await setupTagAutoCompleteBackend();
setupModelApi();
setupGenerateBackendComfyUI();
setupGenerateBackendWebUI();
setupTagger();
if (downloadSuccess && cacheSuccess && tacSuccess) {
createWindow();
mainWindow.setTitle(`Wai Character Select SAA ${version}`);
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
} else {
console.error('[Main] Failed to download required files. Exiting...');
app.quit();
}
// IPC handlers for spellcheck
ipcMain.handle('replace-misspelling', async (event, word) => {
return replaceMisspelling(word);
});
ipcMain.handle('add-to-dictionary', async (event, word) => {
return addToDictionary(word);
});
setupIPCs();
// Start the HTTP server
if(SETTINGS.ws_service) {
setupHttpServer(path.join(__dirname), SETTINGS.ws_addr, SETTINGS.ws_port);
}
}
// Initialize the app
// eslint-disable-next-line unicorn/prefer-top-level-await
(async () => {
await app.whenReady();
await initializeApp();
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
});
})();
// Quit when all windows are closed
app.on('window-all-closed', function () {
// close the WebSocket server
closeWebSocketServer();
app.quit()
})