mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-30 09:56:36 +01:00 
			
		
		
		
	create separate window for setup and then main window
This commit is contained in:
		
							
								
								
									
										97
									
								
								electron.js
									
									
									
									
									
								
							
							
						
						
									
										97
									
								
								electron.js
									
									
									
									
									
								
							| @@ -1,92 +1,20 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
|  |  | ||||||
| const {app, globalShortcut, BrowserWindow} = require('electron'); | const {app, globalShortcut} = require('electron'); | ||||||
| const path = require('path'); |  | ||||||
| const log = require('./src/services/log'); | const log = require('./src/services/log'); | ||||||
| const sqlInit = require('./src/services/sql_init'); | const sqlInit = require('./src/services/sql_init'); | ||||||
| const cls = require('./src/services/cls'); | const cls = require('./src/services/cls'); | ||||||
| const url = require("url"); |  | ||||||
| const port = require('./src/services/port'); |  | ||||||
| const optionService = require('./src/services/options'); |  | ||||||
| const env = require('./src/services/env'); |  | ||||||
| const keyboardActionsService = require('./src/services/keyboard_actions'); | const keyboardActionsService = require('./src/services/keyboard_actions'); | ||||||
| const appIconService = require('./src/services/app_icon'); | const appIconService = require('./src/services/app_icon'); | ||||||
| const windowStateKeeper = require('electron-window-state'); | const windowService = require('./src/services/window'); | ||||||
|  |  | ||||||
| // Adds debug features like hotkeys for triggering dev tools and reload | // Adds debug features like hotkeys for triggering dev tools and reload | ||||||
| require('electron-debug')(); | require('electron-debug')(); | ||||||
|  |  | ||||||
| appIconService.installLocalAppIcon(); | appIconService.installLocalAppIcon(); | ||||||
|  |  | ||||||
| // Prevent window being garbage collected |  | ||||||
| let mainWindow; |  | ||||||
|  |  | ||||||
| require('electron-dl')({ saveAs: true }); | require('electron-dl')({ saveAs: true }); | ||||||
|  |  | ||||||
| function onClosed() { |  | ||||||
|     // Dereference the window |  | ||||||
|     // For multiple windows store them in an array |  | ||||||
|     mainWindow = null; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function createMainWindow() { |  | ||||||
|     await sqlInit.dbConnection; |  | ||||||
|  |  | ||||||
|     let frame = true; |  | ||||||
|  |  | ||||||
|     // if schema doesn't exist -> setup process |  | ||||||
|     // if schema exists, then we need to wait until the migration process is finished |  | ||||||
|     if (await sqlInit.schemaExists()) { |  | ||||||
|         await sqlInit.dbReady; |  | ||||||
|  |  | ||||||
|         frame = await optionService.getOptionBool('nativeTitleBarVisible') |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     const mainWindowState = windowStateKeeper({ |  | ||||||
|         // default window width & height so it's usable on 1600 * 900 display (including some extra panels etc.) |  | ||||||
|         defaultWidth: 1200, |  | ||||||
|         defaultHeight: 800 |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     const win = new BrowserWindow({ |  | ||||||
|         x: mainWindowState.x, |  | ||||||
|         y: mainWindowState.y, |  | ||||||
|         width: mainWindowState.width, |  | ||||||
|         height: mainWindowState.height, |  | ||||||
|         title: 'Trilium Notes', |  | ||||||
|         webPreferences: { |  | ||||||
|             nodeIntegration: true |  | ||||||
|         }, |  | ||||||
|         frame: frame, |  | ||||||
|         icon: path.join(__dirname, 'images/app-icons/png/256x256' + (env.isDev() ? '-dev' : '') + '.png') |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     mainWindowState.manage(win); |  | ||||||
|  |  | ||||||
|     win.setMenuBarVisibility(false); |  | ||||||
|     win.loadURL('http://127.0.0.1:' + await port); |  | ||||||
|     win.on('closed', onClosed); |  | ||||||
|  |  | ||||||
|     win.webContents.on('new-window', (e, url) => { |  | ||||||
|         if (url !== win.webContents.getURL()) { |  | ||||||
|             e.preventDefault(); |  | ||||||
|             require('electron').shell.openExternal(url); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     // prevent drag & drop to navigate away from trilium |  | ||||||
|     win.webContents.on('will-navigate', (ev, targetUrl) => { |  | ||||||
|         const parsedUrl = url.parse(targetUrl); |  | ||||||
|  |  | ||||||
|         // we still need to allow internal redirects from setup and migration pages |  | ||||||
|         if (!['localhost', '127.0.0.1'].includes(parsedUrl.hostname) || (parsedUrl.path && parsedUrl.path !== '/')) { |  | ||||||
|             ev.preventDefault(); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     return win; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| app.on('window-all-closed', () => { | app.on('window-all-closed', () => { | ||||||
|     if (process.platform !== 'darwin') { |     if (process.platform !== 'darwin') { | ||||||
|         app.quit(); |         app.quit(); | ||||||
| @@ -96,12 +24,6 @@ app.on('window-all-closed', () => { | |||||||
|     } |     } | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.on('activate', () => { |  | ||||||
|     if (!mainWindow) { |  | ||||||
|         mainWindow = createMainWindow(); |  | ||||||
|     } |  | ||||||
| }); |  | ||||||
|  |  | ||||||
| async function registerGlobalShortcuts() { | async function registerGlobalShortcuts() { | ||||||
|     await sqlInit.dbReady; |     await sqlInit.dbReady; | ||||||
|  |  | ||||||
| @@ -137,9 +59,20 @@ async function registerGlobalShortcuts() { | |||||||
| app.on('ready', async () => { | app.on('ready', async () => { | ||||||
|     app.setAppUserModelId('com.github.zadam.trilium'); |     app.setAppUserModelId('com.github.zadam.trilium'); | ||||||
|  |  | ||||||
|     mainWindow = await createMainWindow(); |     await sqlInit.dbConnection; | ||||||
|  |  | ||||||
|     registerGlobalShortcuts(); |     // if schema doesn't exist -> setup process | ||||||
|  |     // if schema exists, then we need to wait until the migration process is finished | ||||||
|  |     if (await sqlInit.schemaExists()) { | ||||||
|  |         await sqlInit.dbReady; | ||||||
|  |  | ||||||
|  |         await windowService.createMainWindow(); | ||||||
|  |     } | ||||||
|  |     else { | ||||||
|  |         await windowService.createSetupWindow(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     await registerGlobalShortcuts(); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.on('will-quit', () => { | app.on('will-quit', () => { | ||||||
|   | |||||||
| @@ -81,7 +81,7 @@ function SetupModel() { | |||||||
|                 password: password1, |                 password: password1, | ||||||
|                 theme: theme |                 theme: theme | ||||||
|             }).then(() => { |             }).then(() => { | ||||||
|                 window.location.replace("./"); |                 window.location.replace("./setup"); | ||||||
|             }); |             }); | ||||||
|         } |         } | ||||||
|         else if (this.setupType() === 'sync-from-server') { |         else if (this.setupType() === 'sync-from-server') { | ||||||
|   | |||||||
| @@ -114,7 +114,7 @@ function register(app) { | |||||||
|     route(GET, '/login', [auth.checkAppInitialized], loginRoute.loginPage); |     route(GET, '/login', [auth.checkAppInitialized], loginRoute.loginPage); | ||||||
|     route(POST, '/login', [], loginRoute.login); |     route(POST, '/login', [], loginRoute.login); | ||||||
|     route(POST, '/logout', [csrfMiddleware, auth.checkAuth], loginRoute.logout); |     route(POST, '/logout', [csrfMiddleware, auth.checkAuth], loginRoute.logout); | ||||||
|     route(GET, '/setup', [auth.checkAppNotInitialized], setupRoute.setupPage); |     route(GET, '/setup', [], setupRoute.setupPage); | ||||||
|  |  | ||||||
|     apiRoute(GET, '/api/tree', treeApiRoute.getTree); |     apiRoute(GET, '/api/tree', treeApiRoute.getTree); | ||||||
|     apiRoute(POST, '/api/tree/load', treeApiRoute.load); |     apiRoute(POST, '/api/tree/load', treeApiRoute.load); | ||||||
|   | |||||||
| @@ -2,11 +2,19 @@ | |||||||
|  |  | ||||||
| const sqlInit = require('../services/sql_init'); | const sqlInit = require('../services/sql_init'); | ||||||
| const setupService = require('../services/setup'); | const setupService = require('../services/setup'); | ||||||
|  | const utils = require('../services/utils'); | ||||||
|  | const windowService = require('../services/window'); | ||||||
|  |  | ||||||
| async function setupPage(req, res) { | async function setupPage(req, res) { | ||||||
|     if (await sqlInit.isDbInitialized()) { |     if (await sqlInit.isDbInitialized()) { | ||||||
|  |         if (utils.isElectron()) { | ||||||
|  |             await windowService.createMainWindow(); | ||||||
|  |             windowService.closeSetupWindow(); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|             res.redirect('/'); |             res.redirect('/'); | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     // we got here because DB is not completely initialized so if schema exists |     // we got here because DB is not completely initialized so if schema exists | ||||||
|     // it means we're in sync in progress state. |     // it means we're in sync in progress state. | ||||||
|   | |||||||
							
								
								
									
										86
									
								
								src/services/window.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								src/services/window.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,86 @@ | |||||||
|  | const {BrowserWindow} = require('electron'); | ||||||
|  | const path = require('path'); | ||||||
|  | const url = require("url"); | ||||||
|  | const port = require('./port'); | ||||||
|  | const optionService = require('./options'); | ||||||
|  | const env = require('./env'); | ||||||
|  | const windowStateKeeper = require('electron-window-state'); | ||||||
|  |  | ||||||
|  | // Prevent window being garbage collected | ||||||
|  | /** @type {Electron.BrowserWindow} */ | ||||||
|  | let mainWindow; | ||||||
|  | /** @type {Electron.BrowserWindow} */ | ||||||
|  | let setupWindow; | ||||||
|  |  | ||||||
|  | async function createMainWindow() { | ||||||
|  |     const mainWindowState = windowStateKeeper({ | ||||||
|  |         // default window width & height so it's usable on 1600 * 900 display (including some extra panels etc.) | ||||||
|  |         defaultWidth: 1200, | ||||||
|  |         defaultHeight: 800 | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     mainWindow = new BrowserWindow({ | ||||||
|  |         x: mainWindowState.x, | ||||||
|  |         y: mainWindowState.y, | ||||||
|  |         width: mainWindowState.width, | ||||||
|  |         height: mainWindowState.height, | ||||||
|  |         title: 'Trilium Notes', | ||||||
|  |         webPreferences: { | ||||||
|  |             nodeIntegration: true | ||||||
|  |         }, | ||||||
|  |         frame: await optionService.getOptionBool('nativeTitleBarVisible'), | ||||||
|  |         icon: getIcon() | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     mainWindowState.manage(mainWindow); | ||||||
|  |  | ||||||
|  |     mainWindow.setMenuBarVisibility(false); | ||||||
|  |     mainWindow.loadURL('http://127.0.0.1:' + await port); | ||||||
|  |     mainWindow.on('closed', () => mainWindow = null); | ||||||
|  |  | ||||||
|  |     mainWindow.webContents.on('new-window', (e, url) => { | ||||||
|  |         if (url !== mainWindow.webContents.getURL()) { | ||||||
|  |             e.preventDefault(); | ||||||
|  |             require('electron').shell.openExternal(url); | ||||||
|  |         } | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     // prevent drag & drop to navigate away from trilium | ||||||
|  |     mainWindow.webContents.on('will-navigate', (ev, targetUrl) => { | ||||||
|  |         const parsedUrl = url.parse(targetUrl); | ||||||
|  |  | ||||||
|  |         // we still need to allow internal redirects from setup and migration pages | ||||||
|  |         if (!['localhost', '127.0.0.1'].includes(parsedUrl.hostname) || (parsedUrl.path && parsedUrl.path !== '/')) { | ||||||
|  |             ev.preventDefault(); | ||||||
|  |         } | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function getIcon() { | ||||||
|  |     return path.join(__dirname, 'images/app-icons/png/256x256' + (env.isDev() ? '-dev' : '') + '.png'); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | async function createSetupWindow() { | ||||||
|  |     setupWindow = new BrowserWindow({ | ||||||
|  |         width: 800, | ||||||
|  |         height: 800, | ||||||
|  |         title: 'Trilium Notes Setup', | ||||||
|  |         icon: getIcon() | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     setupWindow.setMenuBarVisibility(false); | ||||||
|  |     setupWindow.loadURL('http://127.0.0.1:' + await port); | ||||||
|  |     setupWindow.on('closed', () => setupWindow = null); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function closeSetupWindow() { | ||||||
|  |     if (setupWindow) { | ||||||
|  |         setupWindow.close(); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | module.exports = { | ||||||
|  |     createMainWindow, | ||||||
|  |     createSetupWindow, | ||||||
|  |     closeSetupWindow | ||||||
|  | }; | ||||||
		Reference in New Issue
	
	Block a user