-
Notifications
You must be signed in to change notification settings - Fork 2
/
app-manager.js
170 lines (125 loc) · 4.98 KB
/
app-manager.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const fs = require('fs').promises
const homedir = require('os').homedir()
const { name, version } = require( `${__dirname}/../package.json` )
const { block, unBlock, panicUnblockAll, say } = require( './process-management' )
const { app, BrowserWindow, ipcMain, globalShortcut } = require('electron')
const fetch = require( 'electron-fetch' ).default
// WIndow management
const WindowListener = require( './events' )
class App {
constructor() {
// Backend functionality
this.globalBlocklist = []
this.defaultBlocklist = 'Google Chrome.*\nAdobe.*\nSafari.*\nMicrosoft.*\nMail\nFranz.*\nAffinity.*\nSublime Text.*\nBackup and sync.*'
this.blocking = false
this.debug = process.env.debug
this.app = app
this.currentWindow = new WindowListener( 500 )
this.windowSize = { width: 400, height: 700 }
this.rawRepo = 'https://raw.githubusercontent.com/actuallymentor/wintertime-mac-background-freezer/master/package.json'
// Choices
this.shortcut = 'Command+Shift+Space'
// Initialisations
this.configElectron()
this.configIpc()
this.configWindowcheck()
this.checkUpdates( version )
}
toggleBlocking( ) {
this.blocking = !this.blocking
}
configElectron() {
// Application configs
this.app.on( 'ready', f => {
this.registerShortcuts()
this.render()
} )
this.app.on( 'window-all-closed', f => {
return panicUnblockAll()
.then( f => {
globalShortcut.unregisterAll()
this.app.quit()
} )
} )
this.app.on( 'activate', f => this.reload() )
}
registerShortcuts( ) {
const registration = globalShortcut.register( this.shortcut, f => {
// Tell interface to do action
this.window.webContents.send( 'keyboard-shortcut', 'toggle-block' )
// Shortcut sends signal, so the this.blocking = true means after full execution it is off
say( `Wintertime ${ this.blocking ? 'OFF' : 'ON' }` )
} )
if( process.env.debug ) console.log( globalShortcut.isRegistered( this.shortcut ), ' shortcut status ' )
}
configIpc() {
// Event listeners
ipcMain.on( 'restore-config', ( event, boolean ) => {
if( this.debug ) console.log( 'Config restoration request received' )
// Data inits
return fs.readFile( `${ homedir }/Library/Application Support/${ name.toLowerCase() }/blocklist`, 'utf8' )
.then( blocklist => {
event.sender.send( 'restored-config-data', blocklist )
this.globalBlocklist = blocklist.split( ',' )
} )
.catch( err => {
// If the config file doesn't exist, return some defaults
if( err.code == 'ENOENT' ) return event.sender.send( 'restored-config-data', this.defaultBlocklist )
// if not, console
if( this.debug ) console.log( 'File loading error ', err )
} )
} )
// Start blocking
ipcMain.on( 'block', ( event, blocklist ) => {
if( this.debug ) console.log( 'Blocking start request received' )
// Save text field to blocklist
this.globalBlocklist = blocklist
// Toggle on/off
this.toggleBlocking()
// If off restore all
// Changed this.globalBlocklist.map( item => unBlock( item ) ) to panicunblock for simplicity
if( !this.blocking ) panicUnblockAll()
// If blocking, manually trigger window check
if( this.blocking ) this.currentWindow.checkWindow().then( currentApp => this.doBlocking( currentApp ) )
// Save new data to config
fs.writeFile( `${ homedir }/Library/Application Support/${ name.toLowerCase() }/blocklist`, this.globalBlocklist, 'utf8' )
} )
// Panic button ( CONT ev erything )
ipcMain.on( 'panic', ( event, boolean ) => panicUnblockAll() )
}
configWindowcheck() {
this.currentWindow.emitter.on( 'change', current => {
if( this.debug ) console.log( 'Window changed to ', current )
this.doBlocking( current )
} )
}
doBlocking( current ) {
if( this.debug ) console.log( 'Not blocking ', current )
if( this.blocking && this.globalBlocklist ) this.globalBlocklist.map( item => current.match( item ) ? unBlock( item ) : block( item ) )
}
render() {
// Create the window
this.window = new BrowserWindow( this.windowSize )
this.window.loadFile( `${ __dirname }/../src/index.html` )
// Listeners
this.window.on( 'closed', f => this.window = null )
// If devving open inspector
if( process.env.debug ) this.window.webContents.openDevTools( )
}
reload() {
if( this.window == null ) this.init()
}
checkUpdates( version ) {
return fetch( this.rawRepo )
.then(res => res.json() )
.then( json => {
if( json.version > version ) {
this.updateWindow = new BrowserWindow( { width: 400, height: 200 } )
this.updateWindow.loadFile( `${ __dirname }/../src/update.html` )
this.window.on( 'closed', f => this.updateWindow = null )
if( process.env.debug ) this.updateWindow.webContents.openDevTools( )
}
} )
}
}
module.exports = new App()