Changeset View
Changeset View
Standalone View
Standalone View
desktop/src/main.js
Show All 9 Lines | import { | ||||
autoUpdater, | autoUpdater, | ||||
// eslint-disable-next-line import/extensions | // eslint-disable-next-line import/extensions | ||||
} from 'electron/main'; | } from 'electron/main'; | ||||
import fs from 'fs'; | import fs from 'fs'; | ||||
import path from 'path'; | import path from 'path'; | ||||
import { initAutoUpdate } from './auto-update.js'; | import { initAutoUpdate } from './auto-update.js'; | ||||
import { handleSquirrelEvent } from './handle-squirrel-event.js'; | import { handleSquirrelEvent } from './handle-squirrel-event.js'; | ||||
import { | |||||
listenForNotifications, | |||||
registerForNotifications, | |||||
} from './push-notifications.js'; | |||||
const isDev = process.env.ENV === 'dev'; | const isDev = process.env.ENV === 'dev'; | ||||
const url = isDev ? 'http://localhost:3000/comm/' : 'https://web.comm.app'; | const url = isDev ? 'http://localhost:3000/comm/' : 'https://web.comm.app'; | ||||
const isMac = process.platform === 'darwin'; | const isMac = process.platform === 'darwin'; | ||||
const scrollbarCSS = fs.promises.readFile( | const scrollbarCSS = fs.promises.readFile( | ||||
path.resolve(__dirname, '../scrollbar.css'), | path.resolve(__dirname, '../scrollbar.css'), | ||||
'utf8', | 'utf8', | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | const menu = Menu.buildFromTemplate([ | ||||
{ role: 'fileMenu' }, | { role: 'fileMenu' }, | ||||
{ role: 'editMenu' }, | { role: 'editMenu' }, | ||||
viewMenu, | viewMenu, | ||||
windowMenu, | windowMenu, | ||||
]); | ]); | ||||
Menu.setApplicationMenu(menu); | Menu.setApplicationMenu(menu); | ||||
}; | }; | ||||
const createMainWindow = () => { | let mainWindow = null; | ||||
const createMainWindow = (urlPath?: string) => { | |||||
const win = new BrowserWindow({ | const win = new BrowserWindow({ | ||||
show: false, | show: false, | ||||
width: 1300, | width: 1300, | ||||
height: 800, | height: 800, | ||||
minWidth: 1100, | minWidth: 1100, | ||||
minHeight: 600, | minHeight: 600, | ||||
titleBarStyle: 'hidden', | titleBarStyle: 'hidden', | ||||
trafficLightPosition: { x: 20, y: 24 }, | trafficLightPosition: { x: 20, y: 24 }, | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | const createMainWindow = (urlPath?: string) => { | ||||
ipcMain.on('double-click-top-bar', doubleClickTopBar); | ipcMain.on('double-click-top-bar', doubleClickTopBar); | ||||
const updateDownloaded = (event, releaseNotes, releaseName) => { | const updateDownloaded = (event, releaseNotes, releaseName) => { | ||||
win.webContents.send('on-new-version-available', releaseName); | win.webContents.send('on-new-version-available', releaseName); | ||||
}; | }; | ||||
autoUpdater.on('update-downloaded', updateDownloaded); | autoUpdater.on('update-downloaded', updateDownloaded); | ||||
win.on('closed', () => { | win.on('closed', () => { | ||||
mainWindow = null; | |||||
ipcMain.removeListener('clear-history', clearHistory); | ipcMain.removeListener('clear-history', clearHistory); | ||||
ipcMain.removeListener('double-click-top-bar', doubleClickTopBar); | ipcMain.removeListener('double-click-top-bar', doubleClickTopBar); | ||||
autoUpdater.removeListener('update-downloaded', updateDownloaded); | autoUpdater.removeListener('update-downloaded', updateDownloaded); | ||||
}); | }); | ||||
win.webContents.setWindowOpenHandler(({ url: openURL }) => { | win.webContents.setWindowOpenHandler(({ url: openURL }) => { | ||||
shell.openExternal(openURL); | shell.openExternal(openURL); | ||||
// Returning 'deny' prevents a new electron window from being created | // Returning 'deny' prevents a new electron window from being created | ||||
return { action: 'deny' }; | return { action: 'deny' }; | ||||
}); | }); | ||||
(async () => { | (async () => { | ||||
const css = await scrollbarCSS; | const css = await scrollbarCSS; | ||||
win.webContents.insertCSS(css); | win.webContents.insertCSS(css); | ||||
})(); | })(); | ||||
win.loadURL(url); | win.loadURL(url + (urlPath ?? '')); | ||||
mainWindow = win; | |||||
return win; | return win; | ||||
}; | }; | ||||
const createSplashWindow = () => { | const createSplashWindow = () => { | ||||
const win = new BrowserWindow({ | const win = new BrowserWindow({ | ||||
width: 300, | width: 300, | ||||
height: 300, | height: 300, | ||||
Show All 22 Lines | const createErrorWindow = () => { | ||||
win.on('close', () => { | win.on('close', () => { | ||||
app.quit(); | app.quit(); | ||||
}); | }); | ||||
win.loadFile(path.resolve(__dirname, '../pages/error.html')); | win.loadFile(path.resolve(__dirname, '../pages/error.html')); | ||||
return win; | return win; | ||||
}; | }; | ||||
const show = () => { | const show = (urlPath?: string) => { | ||||
const splash = createSplashWindow(); | const splash = createSplashWindow(); | ||||
const error = createErrorWindow(); | const error = createErrorWindow(); | ||||
const main = createMainWindow(); | const main = createMainWindow(urlPath); | ||||
let loadedSuccessfully = true; | let loadedSuccessfully = true; | ||||
main.webContents.on('did-fail-load', () => { | main.webContents.on('did-fail-load', () => { | ||||
loadedSuccessfully = false; | loadedSuccessfully = false; | ||||
if (!splash.isDestroyed()) { | if (!splash.isDestroyed()) { | ||||
splash.destroy(); | splash.destroy(); | ||||
} | } | ||||
if (!error.isDestroyed()) { | if (!error.isDestroyed()) { | ||||
Show All 11 Lines | if (loadedSuccessfully) { | ||||
if (!splash.isDestroyed()) { | if (!splash.isDestroyed()) { | ||||
splash.destroy(); | splash.destroy(); | ||||
} | } | ||||
if (!error.isDestroyed()) { | if (!error.isDestroyed()) { | ||||
error.destroy(); | error.destroy(); | ||||
} | } | ||||
main.show(); | main.show(); | ||||
if (app.isPackaged) { | |||||
(async () => { | |||||
const token = await registerForNotifications(); | |||||
main.webContents.send('on-device-token-registered', token); | |||||
})(); | |||||
} | |||||
} | } | ||||
}); | }); | ||||
}; | }; | ||||
const run = () => { | const run = () => { | ||||
app.setName('Comm'); | app.setName('Comm'); | ||||
setApplicationMenu(); | setApplicationMenu(); | ||||
(async () => { | (async () => { | ||||
await app.whenReady(); | await app.whenReady(); | ||||
if (app.isPackaged) { | if (app.isPackaged) { | ||||
try { | try { | ||||
initAutoUpdate(); | initAutoUpdate(); | ||||
} catch (error) { | } catch (error) { | ||||
console.error(error); | console.error(error); | ||||
} | } | ||||
listenForNotifications(threadID => { | |||||
if (mainWindow) { | |||||
mainWindow.webContents.send('on-notification-clicked', { | |||||
threadID, | |||||
}); | |||||
} else { | |||||
show(`chat/thread/${threadID}/`); | |||||
} | |||||
}); | |||||
} | } | ||||
ipcMain.on('set-badge', (event, value) => { | ipcMain.on('set-badge', (event, value) => { | ||||
if (isMac) { | if (isMac) { | ||||
app.dock.setBadge(value?.toString() ?? ''); | app.dock.setBadge(value?.toString() ?? ''); | ||||
} | } | ||||
}); | }); | ||||
ipcMain.on('get-version', event => { | ipcMain.on('get-version', event => { | ||||
Show All 26 Lines |