1
- 'use strict'
2
-
3
- import { app , protocol , BrowserWindow , Config , screen , shell } from 'electron'
4
- import {
5
- createProtocol ,
6
- installVueDevtools
7
- } from 'vue-cli-plugin-electron-builder/lib'
8
- const isDevelopment = process . env . NODE_ENV !== 'production'
9
- // Keep a global reference of the window object, if you don't, the window will
1
+ 'use strict' ;
2
+ import { app , protocol , BrowserWindow , Config , screen , shell , globalShortcut , ipcMain , Tray , Menu } from 'electron' ;
3
+ import { createProtocol , installVueDevtools } from 'vue-cli-plugin-electron-builder/lib' ;
4
+ const isDevelopment = process . env . NODE_ENV !== 'production' ;
5
+ // Keep a global reference of the window and tray objects else they will
10
6
// be closed automatically when the JavaScript object is garbage collected.
11
- let win ;
7
+ let win : BrowserWindow ;
8
+ let tray : Tray ;
12
9
13
- // Standard scheme must be registered before the app is ready
14
10
protocol . registerStandardSchemes ( [ 'app' ] , { secure : true } )
11
+
15
12
function createWindow ( ) {
16
- // Create the browser window.
13
+
17
14
win = new BrowserWindow ( {
18
- minWidth : 230 ,
15
+ minWidth : 265 ,
19
16
width : 400 ,
20
17
height : 400 ,
21
18
minHeight : 400 ,
@@ -26,93 +23,104 @@ function createWindow() {
26
23
} ) ;
27
24
28
25
if ( process . env . WEBPACK_DEV_SERVER_URL ) {
29
- // Load the url of the dev server if in development mode
30
- win . loadURL ( process . env . WEBPACK_DEV_SERVER_URL as string )
31
- if ( ! process . env . IS_TEST ) win . webContents . openDevTools ( )
32
- } else {
33
- createProtocol ( 'app' )
34
- // Load the index.html when not in development
35
- win . loadURL ( 'app://./index.html' )
26
+ win . loadURL ( process . env . WEBPACK_DEV_SERVER_URL as string ) ;
27
+ if ( ! process . env . IS_TEST ) win . webContents . openDevTools ( ) ;
28
+ }
29
+ else {
30
+ createProtocol ( 'app' ) ;
31
+ win . loadURL ( 'app://./index.html' ) ;
36
32
}
37
33
38
- win . on ( 'closed' , ( ) => {
39
- win = null
34
+ win . on ( 'close' , ( e ) => {
35
+ e . preventDefault ( ) ;
36
+ console . log ( "test" )
37
+ win . hide ( ) ;
40
38
} )
41
39
42
- win . webContents . on ( 'new-window' , function ( e , url ) {
40
+ win . on ( 'closed' , ( e ) => {
41
+ ( < any > win ) = null ;
42
+ } )
43
+
44
+ win . webContents . on ( 'new-window' , function ( e , url ) {
43
45
e . preventDefault ( ) ;
44
46
shell . openExternal ( url ) ;
45
- } ) ;
46
-
47
+ } ) ;
47
48
}
48
49
49
-
50
- // Quit when all windows are closed.
51
50
app . on ( 'window-all-closed' , ( ) => {
52
- // On macOS it is common for applications and their menu bar
53
- // to stay active until the user quits explicitly with Cmd + Q
54
51
if ( process . platform !== 'darwin' ) {
55
- app . quit ( )
52
+ app . quit ( ) ;
56
53
}
57
54
} )
58
55
59
56
app . on ( 'activate' , ( ) => {
60
- // On macOS it's common to re-create a window in the app when the
61
- // dock icon is clicked and there are no other windows open.
62
57
if ( win === null ) {
63
- createWindow ( )
58
+ createWindow ( ) ;
64
59
}
65
60
} )
66
61
67
- // This method will be called when Electron has finished
68
- // initialization and is ready to create browser windows.
69
- // Some APIs can only be used after this event occurs.
70
62
app . on ( 'ready' , async ( ) => {
71
63
if ( isDevelopment && ! process . env . IS_TEST ) {
72
- // Install Vue Devtools
73
- await installVueDevtools ( )
64
+ await installVueDevtools ( ) ;
74
65
}
66
+ globalShortcut . register ( 'CommandOrControl+Shift+A' , ( ) => {
67
+ if ( win . isFocused ( ) && ! win . isAlwaysOnTop ( ) ) {
68
+ win . minimize ( ) ;
69
+ } else {
70
+ win . restore ( ) ;
71
+ win . focus ( ) ;
72
+ ipcMain . emit ( "FocusEditor" ) ;
73
+ moveWindow ( ) ;
74
+ }
75
+ } )
76
+ createTray ( )
75
77
createWindow ( ) ;
76
78
moveWindow ( ) ;
77
79
} )
78
80
81
+ function createTray ( ) {
82
+ tray = new Tray ( require ( 'path' ) . join ( __dirname , '/assets/icon.ico' ) ) ;
83
+ const contextMenu = Menu . buildFromTemplate ( [
84
+ { label : 'Restore' , click : _ => win . show ( ) } ,
85
+ { label : 'Quit' , click : _ => { win . destroy ( ) ; app . quit ( ) } }
86
+ ] )
87
+ tray . setContextMenu ( contextMenu ) ;
88
+ }
89
+
79
90
var locked = app . requestSingleInstanceLock ( ) ;
80
91
if ( ! locked ) {
81
- app . quit ( )
92
+ app . quit ( ) ;
82
93
} else {
83
- app . on ( 'second-instance' , ( event , commandLine , workingDirectory ) => {
84
- // Someone tried to run a second instance, we should focus our window.
94
+ app . on ( 'second-instance' , _ => {
85
95
if ( win ) {
86
- if ( win . isMinimized ( ) ) win . restore ( )
96
+ if ( win . isMinimized ( ) ) win . restore ( ) ;
87
97
moveWindow ( ) ;
88
- win . focus ( )
98
+ win . focus ( ) ;
89
99
}
90
100
} )
91
101
}
92
102
93
- function moveWindow ( ) {
94
-
103
+ function moveWindow ( ) {
104
+ if ( ! win . isVisible ) win . show ( ) ;
95
105
let mousePos = screen . getCursorScreenPoint ( ) ;
96
106
let bounds = screen . getDisplayNearestPoint ( mousePos ) . bounds ;
97
107
let winBounds = win . getBounds ( ) ;
98
- let horizontalBleed = ( bounds . x + bounds . width ) - winBounds . width
99
- let verticalBleed = ( bounds . y + bounds . height ) - winBounds . height
100
- win . setPosition ( mousePos . x > horizontalBleed ? horizontalBleed : mousePos . x , mousePos . y > verticalBleed ? verticalBleed : mousePos . y )
108
+ let horizontalBleed = ( bounds . x + bounds . width ) - winBounds . width ;
109
+ let verticalBleed = ( bounds . y + bounds . height ) - winBounds . height ;
110
+ win . setPosition ( mousePos . x > horizontalBleed ? horizontalBleed : mousePos . x , mousePos . y > verticalBleed ? verticalBleed : mousePos . y ) ;
101
111
}
102
112
103
-
104
-
105
113
// Exit cleanly on request from parent process in development mode.
106
114
if ( isDevelopment ) {
107
115
if ( process . platform === 'win32' ) {
108
116
process . on ( 'message' , data => {
109
117
if ( data === 'graceful-exit' ) {
110
- app . quit ( )
118
+ app . quit ( ) ;
111
119
}
112
- } )
120
+ } ) ;
113
121
} else {
114
122
process . on ( 'SIGTERM' , ( ) => {
115
- app . quit ( )
116
- } )
123
+ app . quit ( ) ;
124
+ } ) ;
117
125
}
118
126
}
0 commit comments