1
- import {
2
- app ,
3
- BrowserWindow ,
4
- ipcMain ,
5
- net ,
6
- protocol ,
7
- screen ,
8
- shell ,
9
- } from "electron" ;
1
+ import { app , BrowserWindow , net , protocol } from "electron" ;
10
2
import path from "node:path" ;
11
3
import url , { fileURLToPath } from "node:url" ;
4
+ import window from "./utils/window" ;
12
5
6
+ export const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
13
7
export let win : BrowserWindow | null ;
14
8
15
9
// DEEP LINKING
@@ -40,7 +34,7 @@ if (!gotTheLock) {
40
34
} ) ;
41
35
42
36
app . whenReady ( ) . then ( async ( ) => {
43
- createWindow ( ) ;
37
+ win = window . createWindow ( ) ;
44
38
protocol . handle ( "local" , ( request ) => {
45
39
const filePath = request . url . slice ( "local:" . length ) ;
46
40
return net . fetch ( url . pathToFileURL ( decodeURI ( filePath ) ) . toString ( ) ) ;
@@ -60,11 +54,8 @@ if (!gotTheLock) {
60
54
} ) ;
61
55
}
62
56
63
- const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
64
-
65
57
process . env . APP_ROOT = path . join ( __dirname , ".." ) ;
66
58
67
- // 🚧 Use ['ENV_NAME'] avoid vite:define plugin - [email protected]
68
59
export const VITE_DEV_SERVER_URL = process . env [ "VITE_DEV_SERVER_URL" ] ;
69
60
export const MAIN_DIST = path . join ( process . env . APP_ROOT , "dist-electron" ) ;
70
61
export const RENDERER_DIST = path . join ( process . env . APP_ROOT , "dist" ) ;
@@ -73,52 +64,6 @@ process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL
73
64
? path . join ( process . env . APP_ROOT , "public" )
74
65
: RENDERER_DIST ;
75
66
76
- /**
77
- * Updates an existing key-value pair in an object, or inserts a new pair
78
- * if the key does not already exist.
79
- *
80
- * @param {Record<string, unknown> } obj
81
- * @param {string } keyToChange
82
- * @param {* } value
83
- * @returns {void }
84
- */
85
-
86
- function createWindow ( ) {
87
- const { width : screenWidth , height : screenHeight } =
88
- screen . getPrimaryDisplay ( ) . workAreaSize ;
89
-
90
- win = new BrowserWindow ( {
91
- icon : path . join ( process . env . VITE_PUBLIC , "icon.png" ) ,
92
- webPreferences : {
93
- preload : path . join ( __dirname , "preload.mjs" ) ,
94
- } ,
95
- autoHideMenuBar : true ,
96
- minWidth : 1000 ,
97
- minHeight : 600 ,
98
- frame : false ,
99
-
100
- // Set the initial width and height based on available screen size
101
- width : Math . min ( screenWidth * 0.8 , 1000 ) , // 80% of screen width, max 1000
102
- height : Math . min ( screenHeight * 0.8 , 600 ) , // 80% of screen height, max 600
103
-
104
- resizable : true ,
105
- } ) ;
106
-
107
- // if (!isDev()) {
108
- // win.removeMenu();
109
- // }
110
-
111
- ipcMain . handle ( "openExternal" , async ( _e , url : string ) => {
112
- await shell . openExternal ( url ) ;
113
- } ) ;
114
-
115
- if ( VITE_DEV_SERVER_URL ) {
116
- win . loadURL ( VITE_DEV_SERVER_URL ) ;
117
- } else {
118
- win . loadFile ( path . join ( RENDERER_DIST , "index.html" ) ) ;
119
- }
120
- }
121
-
122
67
// Quit when all windows are closed, except on macOS. There, it's common
123
68
// for applications and their menu bar to stay active until the user quits
124
69
// explicitly with Cmd + Q.
@@ -130,9 +75,6 @@ app.on("window-all-closed", () => {
130
75
} ) ;
131
76
132
77
app . on ( "activate" , ( ) => {
133
- // On OS X it's common to re-create a window in the app when the
134
- // dock icon is clicked and there are no other windows open.
135
- if ( BrowserWindow . getAllWindows ( ) . length === 0 ) {
136
- createWindow ( ) ;
137
- }
78
+ if ( BrowserWindow . getAllWindows ( ) . length <= 0 ) return ;
79
+ win = window . createWindow ( ) ;
138
80
} ) ;
0 commit comments