Skip to content

Commit

Permalink
Implement window manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jalenng committed Jun 9, 2021
1 parent 68c7df3 commit 812f65a
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 58 deletions.
20 changes: 0 additions & 20 deletions public/app/InstanceEnforcer.js

This file was deleted.

3 changes: 2 additions & 1 deletion public/app/createWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const path = require('path')

const { isDev, isWindows, appName } = require('../constants')
const store = require('../store/store')
const { getMainWindow } = require('./windowManager')

const windowStateKeeper = require('electron-window-state')

Expand Down Expand Up @@ -109,7 +110,7 @@ function createWindow (type, destination = '', display = null, isPopup = false)
// Handle the close button action by having window hide
window.on('close', (e) => {
e.preventDefault()
global.mainWindow.hide()
getMainWindow().hide()
})

// If not configured to hide the app on app startup, show window when ready
Expand Down
5 changes: 3 additions & 2 deletions public/app/initializeApp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { BrowserWindow, app } = require('electron')

const { appPath, isMacOS, isDev } = require('../constants')
const { setMainWindow } = require('./windowManager')
const createWindow = require('./createWindow')
const store = require('../store/store')

Expand All @@ -22,12 +23,12 @@ app.whenReady().then(() => {
app.setAppUserModelId(process.execPath)

// Create main window
global.mainWindow = createWindow('main')
setMainWindow(createWindow('main'))

// macOS: Recreate a window if none are open but the dock icon is activated
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) {
global.mainWindow = createWindow('main')
setMainWindow(createWindow('main'))
}
})

Expand Down
9 changes: 5 additions & 4 deletions public/app/initializeMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { Menu } = require('electron')

const { timerSystem } = require('../systems/systems')
const createWindow = require('./createWindow')
const { setPrefsWindow, getPrefsWindow } = require('./windowManager')

const { isDev } = require('../constants')

Expand All @@ -29,11 +30,11 @@ const menu = Menu.buildFromTemplate([
label: 'Preferences',
accelerator: 'CmdOrCtrl+,',
click: () => {
if (!global.prefsWindow || global.prefsWindow.isDestroyed()) {
global.prefsWindow = createWindow('preferences', 'preferences')
if (!getPrefsWindow() || getPrefsWindow().isDestroyed()) {
setPrefsWindow(createWindow('preferences', 'preferences'))
} else {
global.prefsWindow.restore()
global.prefsWindow.focus()
getPrefsWindow().restore()
getPrefsWindow().focus()
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions public/app/initializeTray.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { app, nativeTheme, nativeImage, Menu, Tray } = require('electron')

const { isMacOS } = require('../constants')
const { timerSystem } = require('../systems/systems')
const { getMainWindow } = require('./windowManager')

let appTray = null

Expand Down Expand Up @@ -39,8 +40,8 @@ appTray = new Tray(getTrayImage())
appTray.setToolTip(app.getName())
appTray.setContextMenu(contextMenu)
appTray.on('click', () => {
global.mainWindow.show()
global.mainWindow.focus()
getMainWindow().show()
getMainWindow().focus()
})

// Update system tray icon on an interval
Expand Down
9 changes: 5 additions & 4 deletions public/app/ipcHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { BrowserWindow, ipcMain, app, nativeTheme } = require('electron')

const { isDev } = require('../constants')
const createWindow = require('./createWindow')
const { getPrefsWindow, setPrefsWindow } = require('./windowManager')

/* ------------------------------------------------------------------------- */
/* Main */
Expand All @@ -23,11 +24,11 @@ ipcMain.handle('log-to-main', (event, content) => {

// Open preferences
ipcMain.handle('open-preferences', () => {
if (!global.prefsWindow || global.prefsWindow.isDestroyed()) {
global.prefsWindow = createWindow('preferences', 'preferences')
if (!getPrefsWindow() || getPrefsWindow().isDestroyed()) {
setPrefsWindow(createWindow('preferences', 'preferences'))
} else {
global.prefsWindow.restore()
global.prefsWindow.focus()
getPrefsWindow().restore()
getPrefsWindow().focus()
}
})

Expand Down
9 changes: 5 additions & 4 deletions public/app/theming.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { nativeTheme } = require('electron')

const store = require('../store/store')
const { getMainWindow, getPrefsWindow } = require('./windowManager')

/** Theming */

Expand All @@ -11,11 +12,11 @@ nativeTheme.themeSource = store.get('preferences.appearance.theme')
nativeTheme.on('updated', () => {
const themeName = nativeTheme.shouldUseDarkColors ? 'dark' : 'light'

if (global.mainWindow && !global.mainWindow.isDestroyed()) {
global.mainWindow.webContents.send('theme-updated', themeName)
if (getMainWindow() && !getMainWindow().isDestroyed()) {
getMainWindow().webContents.send('theme-updated', themeName)
}

if (global.prefsWindow && !global.prefsWindow.isDestroyed()) {
global.prefsWindow.webContents.send('theme-updated', themeName)
if (getPrefsWindow() && !getPrefsWindow().isDestroyed()) {
getPrefsWindow().webContents.send('theme-updated', themeName)
}
})
23 changes: 23 additions & 0 deletions public/app/windowManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
let mainWindow
let prefsWindow

function sendToAllWindows (channel, message) {
for (const window of [mainWindow, prefsWindow]) {
if (window && !window.isDestroyed()) {
window.webContents.send(channel, message)
}
}
}

/** Exports */
module.exports = {

getMainWindow: () => mainWindow,
getPrefsWindow: () => prefsWindow,

setMainWindow: (window) => { mainWindow = window },
setPrefsWindow: (window) => { prefsWindow = window },

sendToAllWindows: sendToAllWindows

}
6 changes: 4 additions & 2 deletions public/enforceInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

const { app } = require('electron')

const { getMainWindow } = require('./app/windowManager')

// Lock acquisition
const gotSingleInstanceLock = app.requestSingleInstanceLock()
if (!gotSingleInstanceLock) app.exit() // Quit the app if another app has the lock

// Show first instance if a second instance is requested
app.on('second-instance', () => {
if (global.mainWindow && !global.mainWindow.isDestroyed()) {
global.mainWindow.show()
if (getMainWindow() && !getMainWindow().isDestroyed()) {
getMainWindow().show()
}
})
3 changes: 2 additions & 1 deletion public/ipc/storeIPC.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const path = require('path')
const { ipcMain, app, dialog } = require('electron')

const store = require('../store/store')
const { getPrefsWindow } = require('../app/windowManager')

// Show a dialog to import a custom sound
ipcMain.handle('add-custom-sound', (event) => {
dialog.showOpenDialog(global.mainWindow, {
dialog.showOpenDialog(getPrefsWindow(), {
title: 'Choose custom sound',
filters: [{
name: 'Audio files',
Expand Down
4 changes: 2 additions & 2 deletions public/ipc/systemsIPC.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { ipcMain } = require('electron')

const { timerSystem, breakSystem, blockerSystem } = require('../systems/systems')
const { timerSystem, breakSystem, blockerSystem, appSnapshotSystem } = require('../systems/systems')

/* ------------------------------------------------------------------------- */
/* System-related */
Expand Down Expand Up @@ -37,7 +37,7 @@ ipcMain.handle('play-sound', () => {

// Get list of open windows
ipcMain.on('get-open-windows', async (event) => {
event.returnValue = global.systems.appSnapshot.getLastSnapshot()
event.returnValue = appSnapshotSystem.getLastSnapshot()
})

// Get timer status
Expand Down
18 changes: 4 additions & 14 deletions public/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { nativeTheme, app } = require('electron')
const Store = require('electron-store')

const storeSchema = require('./storeSchema')
const { getMainWindow, sendToAllWindows } = require('../app/windowManager')

/** Initialize the store and reset if necessary */

Expand All @@ -30,29 +31,18 @@ store.onDidChange('preferences.startup.startAppOnLogin', (newVal, oldVal) => {

// Configure the main BrowserWindow's alwaysOnTop property when its corresponding preference option changes
store.onDidChange('preferences.appearance.alwaysOnTop', (newVal, oldVal) => {
global.mainWindow.setAlwaysOnTop(newVal)
getMainWindow().setAlwaysOnTop(newVal)
})

// Notify the preferences window and main window when any preference changes
store.onDidChange('preferences', () => {
if (global.mainWindow && !global.mainWindow.isDestroyed()) {
global.mainWindow.webContents.send('store-changed', 'preferences')
}
sendToAllWindows('store-changed', 'preferences')

if (global.prefsWindow && !global.prefsWindow.isDestroyed()) {
global.prefsWindow.webContents.send('store-changed', 'preferences')
}
})

// Notify the preferences and main window when there is an update to the list of sounds
store.onDidChange('sounds', () => {
if (global.mainWindow && !global.mainWindow.isDestroyed()) {
global.mainWindow.webContents.send('store-changed', 'sounds')
}

if (global.prefsWindow && !global.prefsWindow.isDestroyed()) {
global.prefsWindow.webContents.send('store-changed', 'sounds')
}
sendToAllWindows('store-changed', 'sounds')
})

// Update the Electron nativeTheme's themeSource property when its corresponding preference changes
Expand Down
5 changes: 3 additions & 2 deletions public/systems/modules/TimerSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

const EventEmitter = require('./EventEmitter')
const store = require('../../store/store')
const { sendToAllWindows } = require('../../app/windowManager')

/** Timer states */
const states = {
Expand Down Expand Up @@ -179,10 +180,10 @@ module.exports = class TimerSystem extends EventEmitter {
resetSendingInterval () {
clearInterval(this.sendingInterval)

if (global.mainWindow && !global.mainWindow.isDestroyed()) { global.mainWindow.webContents.send('receive-timer-status', this.getStatus()) }
sendToAllWindows('receive-timer-status', this.getStatus())

this.sendingInterval = setInterval(() => {
if (global.mainWindow && !global.mainWindow.isDestroyed()) { global.mainWindow.webContents.send('receive-timer-status', this.getStatus()) }
sendToAllWindows('receive-timer-status', this.getStatus())
}, 1000)
}

Expand Down

0 comments on commit 812f65a

Please sign in to comment.