From a9ea86a8ca37fe5aa4cdf699345888accf5c2103 Mon Sep 17 00:00:00 2001 From: Alex Freska Date: Thu, 25 Jan 2024 09:37:49 -0500 Subject: [PATCH] refactor renterd, do not show window on startup if configured --- hostd/electron-src/download.ts | 2 +- hostd/electron-src/index.ts | 18 +-- hostd/electron-src/startup.ts | 12 ++ hostd/electron-src/utils.ts | 0 renterd/electron-src/daemon.ts | 37 ++++-- renterd/electron-src/download.ts | 49 +++----- renterd/electron-src/index.ts | 197 +++--------------------------- renterd/electron-src/ipc.ts | 57 +++++++++ renterd/electron-src/shortcuts.ts | 15 +++ renterd/electron-src/startup.ts | 14 +++ renterd/electron-src/state.ts | 21 +++- renterd/electron-src/tray.ts | 32 +++++ renterd/electron-src/window.ts | 62 ++++++++++ 13 files changed, 275 insertions(+), 241 deletions(-) create mode 100644 hostd/electron-src/startup.ts delete mode 100644 hostd/electron-src/utils.ts create mode 100644 renterd/electron-src/ipc.ts create mode 100644 renterd/electron-src/shortcuts.ts create mode 100644 renterd/electron-src/startup.ts create mode 100644 renterd/electron-src/tray.ts create mode 100644 renterd/electron-src/window.ts diff --git a/hostd/electron-src/download.ts b/hostd/electron-src/download.ts index ebbebc0..49b5753 100644 --- a/hostd/electron-src/download.ts +++ b/hostd/electron-src/download.ts @@ -86,7 +86,7 @@ function getBinaryZipStagingPath(): string { return path.join(getTempDownloadsPath(), binaryName + '.zip') } -function releaseAsset() { +function releaseAsset(): string { let goos switch (process.platform) { case 'win32': diff --git a/hostd/electron-src/index.ts b/hostd/electron-src/index.ts index 2efa8d4..c878bc4 100644 --- a/hostd/electron-src/index.ts +++ b/hostd/electron-src/index.ts @@ -1,13 +1,12 @@ import { app, globalShortcut } from 'electron' import prepareNext from 'electron-next' -import { startDaemon, stopDaemon } from './daemon' -import { downloadRelease } from './download' -import { doesBinaryExist, getIsConfigured } from './config' +import { stopDaemon } from './daemon' import { initTray } from './tray' import { state } from './state' import { initWindow } from './window' import { initShortcuts } from './shortcuts' import { initIpc } from './ipc' +import { startup } from './startup' app.on('ready', async () => { await prepareNext('./renderer') @@ -15,18 +14,7 @@ app.on('ready', async () => { initTray() initShortcuts() initIpc() - - const needsInitialDownload = !doesBinaryExist() - if (needsInitialDownload) { - await downloadRelease() - } - - // If the app is already configured, start the daemon and open browser - // and do not show the configuration window. - if (getIsConfigured()) { - startDaemon() - state.mainWindow?.close() - } + startup() }) app.on('before-quit', async () => { diff --git a/hostd/electron-src/startup.ts b/hostd/electron-src/startup.ts new file mode 100644 index 0000000..e8c30b5 --- /dev/null +++ b/hostd/electron-src/startup.ts @@ -0,0 +1,12 @@ +import { startDaemon } from './daemon' +import { getIsConfigured } from './config' +import { state } from './state' + +export function startup() { + // If the app is already configured, start the daemon and open browser + // and do not show the configuration window. + if (getIsConfigured()) { + startDaemon() + state.mainWindow?.close() + } +} diff --git a/hostd/electron-src/utils.ts b/hostd/electron-src/utils.ts deleted file mode 100644 index e69de29..0000000 diff --git a/renterd/electron-src/daemon.ts b/renterd/electron-src/daemon.ts index 1c9947d..1c4e13c 100644 --- a/renterd/electron-src/daemon.ts +++ b/renterd/electron-src/daemon.ts @@ -2,6 +2,7 @@ import { spawn } from 'child_process' import { state } from './state' import { getBinaryFilePath, getConfig, getConfigFilePath } from './config' import axios from 'axios' +import { Octokit } from '@octokit/rest' export function startDaemon(): Promise { return new Promise(async (resolve, reject) => { @@ -10,29 +11,29 @@ export function startDaemon(): Promise { try { const config = getConfig() const binaryFilePath = getBinaryFilePath() - state.process = spawn(binaryFilePath, [], { + state.daemon = spawn(binaryFilePath, [], { env: { ...process.env, RENTERD_CONFIG_FILE: getConfigFilePath() }, cwd: config.directory, }) - state.process.stdout?.on('data', (data) => { + state.daemon.stdout?.on('data', (data) => { console.log(`stdout: ${data}`) // Emit events or log data as needed }) - state.process.stderr?.on('data', (data) => { + state.daemon.stderr?.on('data', (data) => { console.error(`stderr: ${data}`) // Emit events or log data as needed }) - state.process.on('close', (code) => { + state.daemon.on('close', (code) => { console.log(`child process exited with code ${code}`) - state.process = null + state.daemon = null }) resolve() } catch (err) { - state.process = null + state.daemon = null reject(err) } }) @@ -40,22 +41,22 @@ export function startDaemon(): Promise { export function stopDaemon(): Promise { return new Promise((resolve) => { - if (!state.process) { + if (!state.daemon) { resolve() return } - state.process.on('close', () => { - state.process = null + state.daemon.on('close', () => { + state.daemon = null resolve() }) - state.process.kill('SIGINT') + state.daemon.kill('SIGINT') }) } export function getIsDaemonRunning(): boolean { - return !!state.process && !state.process.killed + return !!state.daemon && !state.daemon.killed } export async function getInstalledVersion(): Promise { @@ -85,3 +86,17 @@ export async function getInstalledVersion(): Promise { return '' } } + +export async function getLatestVersion(): Promise { + try { + const octokit = new Octokit() + const response = await octokit.repos.getLatestRelease({ + owner: 'SiaFoundation', + repo: 'renterd', + }) + return response.data.tag_name + } catch (err) { + console.error(err) + return '' + } +} diff --git a/renterd/electron-src/download.ts b/renterd/electron-src/download.ts index 602ba52..bdfd94f 100644 --- a/renterd/electron-src/download.ts +++ b/renterd/electron-src/download.ts @@ -6,20 +6,7 @@ import { getBinaryFilePath, getConfigAndBinaryDirectoryPath } from './config' import { promisify } from 'util' import stream from 'stream' import axios from 'axios' - -export async function getLatestVersion(): Promise { - try { - const octokit = new Octokit() - const response = await octokit.repos.getLatestRelease({ - owner: 'SiaFoundation', - repo: 'renterd', - }) - return response.data.tag_name - } catch (err) { - console.error(err) - return '' - } -} +import { system } from './state' export async function downloadRelease(): Promise { try { @@ -69,7 +56,7 @@ async function extractBinary(): Promise { const zip = new admZip(zipFilePath) zip.extractAllTo(extractDir, true) - const binaryName = process.platform === 'win32' ? 'renterd.exe' : 'renterd' + const binaryName = system.isWindows ? 'renterd.exe' : 'renterd' const extractedBinaryPath = path.join(extractDir, binaryName) const finalBinaryPath = getBinaryFilePath() @@ -111,28 +98,26 @@ function releaseAsset(): string { case 'linux': goos = 'linux' break - // Add additional mappings as needed default: throw new Error(`Unsupported platform: ${process.platform}`) } let goarch - switch (process.arch) { - case 'x64': - goarch = 'amd64' - break - case 'ia32': - goarch = '386' - break - case 'arm': - goarch = 'arm' - break - case 'arm64': - goarch = 'arm64' - break - // Add additional mappings as needed - default: - throw new Error(`Unsupported architecture: ${process.arch}`) + if (process.platform === 'win32') { + // Windows only supports amd64 + goarch = 'amd64' + } else { + // For Darwin and Linux, consider both amd64 and arm64 + switch (process.arch) { + case 'x64': + goarch = 'amd64' + break + case 'arm64': + goarch = 'arm64' + break + default: + throw new Error(`Unsupported architecture: ${process.arch}`) + } } return `renterd_${goos}_${goarch}.zip` diff --git a/renterd/electron-src/index.ts b/renterd/electron-src/index.ts index dac54f0..c878bc4 100644 --- a/renterd/electron-src/index.ts +++ b/renterd/electron-src/index.ts @@ -1,155 +1,24 @@ -import path, { join } from 'path' -import { - BrowserWindow, - app, - ipcMain, - shell, - Tray, - Menu, - globalShortcut, -} from 'electron' -import isDev from 'electron-is-dev' +import { app, globalShortcut } from 'electron' import prepareNext from 'electron-next' -import { - getInstalledVersion, - getIsDaemonRunning, - startDaemon, - stopDaemon, -} from './daemon' -import { downloadRelease, getLatestVersion } from './download' -import { - Config, - doesBinaryExist, - getConfig, - getDefaultDataPath, - getIsConfigured, - saveConfig, -} from './config' +import { stopDaemon } from './daemon' +import { initTray } from './tray' import { state } from './state' -import { format } from 'url' - -let mainWindow: BrowserWindow | null = null -let appIcon = null -const isDarwin = process.platform === 'darwin' -const isLinux = process.platform === 'linux' -let isQuitting = false -// const isWindows = process.platform === 'win32' +import { initWindow } from './window' +import { initShortcuts } from './shortcuts' +import { initIpc } from './ipc' +import { startup } from './startup' app.on('ready', async () => { await prepareNext('./renderer') - - mainWindow = new BrowserWindow({ - width: 500, - height: 700, - minWidth: 500, - minHeight: 600, - maxWidth: 500, - maxHeight: 800, - webPreferences: { - nodeIntegration: false, - contextIsolation: true, - preload: join(__dirname, 'preload.js'), - }, - }) - - // Hide the main window instead of closing it, to keep the app running in the tray - mainWindow.on('close', (event) => { - if (!isQuitting) { - event.preventDefault() - if (isDarwin) { - app.dock.hide() - } - mainWindow?.hide() - } - }) - - // Setup close to tray settings for both minimize and close events - mainWindow.on('minimize', () => { - if (isDarwin) { - app.dock.hide() - } - // TODO: Does Linux support tray? - // https://electronjs.org/docs/api/tray - // minimize instead of attempting to go to system tray. - if (isLinux) { - return true - } - mainWindow?.hide() - return false - }) - - const iconName = isDarwin ? 'tray.png' : 'tray-win.png' - const iconPath = isDev - ? path.join(process.cwd(), 'assets', iconName) - : path.join(__dirname, '../assets', iconName) - - appIcon = new Tray(iconPath) - const trayContextMenu = Menu.buildFromTemplate([ - { - label: 'Configure', - click: function () { - if (isDarwin) { - app.dock.show() - } - mainWindow?.show() - }, - }, - { - label: 'Quit', - click: function () { - isQuitting = true - mainWindow = null - state.process?.kill() - app.quit() - }, - }, - ]) - appIcon.setToolTip('renterd') - appIcon.setContextMenu(trayContextMenu) - const url = isDev - ? 'http://localhost:8000/' - : format({ - pathname: path.join(__dirname, '../renderer/out/index.html'), - protocol: 'file:', - slashes: true, - }) - mainWindow.loadURL(url) - - if (isDev) { - mainWindow.setMaximumSize(2000, 2000) - mainWindow.setSize(1000, 800) - mainWindow.webContents.openDevTools() - } - - const needsDownload = !doesBinaryExist() - if (needsDownload) { - await downloadRelease() - } - - if (getIsConfigured()) { - startDaemon() - } - // Register a global shortcut listener for the developer tools - const devToolsShortcut = - process.platform === 'darwin' ? 'Cmd+Alt+I' : 'Ctrl+Shift+I' - globalShortcut.register(devToolsShortcut, () => { - // Open the DevTools - if (mainWindow && mainWindow.webContents) { - mainWindow.webContents.openDevTools() - mainWindow.setSize(1000, 800) - mainWindow.setMaximumSize(2000, 2000) - } - }) + initWindow() + initTray() + initShortcuts() + initIpc() + startup() }) -async function quitDaemonAndApp() { - isQuitting = true - await stopDaemon() - mainWindow = null -} - app.on('before-quit', async () => { - if (!isQuitting) { + if (!state.isQuitting) { await quitDaemonAndApp() } }) @@ -163,40 +32,8 @@ app.on('will-quit', async () => { // even though closing windows is not really possible app.on('window-all-closed', app.quit) -ipcMain.handle('open-browser', (_, url: string) => { - shell.openExternal(url) -}) -ipcMain.handle('daemon-start', async (_) => { - await startDaemon() -}) -ipcMain.handle('daemon-stop', async (_) => { +async function quitDaemonAndApp() { + state.isQuitting = true await stopDaemon() -}) -ipcMain.handle('daemon-is-running', (_) => { - const isDaemonRunning = getIsDaemonRunning() - return isDaemonRunning -}) -ipcMain.handle('config-get', (_) => { - const config = getConfig() - return config -}) -ipcMain.handle('get-is-configured', (_) => { - return getIsConfigured() -}) -ipcMain.handle('open-data-directory', (_) => { - shell.openPath(getDefaultDataPath()) - return true -}) -ipcMain.handle('get-default-data-directory', (_) => { - return getDefaultDataPath() -}) -ipcMain.handle('get-installed-version', (_) => { - return getInstalledVersion() -}) -ipcMain.handle('get-latest-version', (_) => { - return getLatestVersion() -}) -ipcMain.handle('config-save', async (_, config: Config) => { - await saveConfig(config) - return true -}) + state.mainWindow = null +} diff --git a/renterd/electron-src/ipc.ts b/renterd/electron-src/ipc.ts new file mode 100644 index 0000000..f346296 --- /dev/null +++ b/renterd/electron-src/ipc.ts @@ -0,0 +1,57 @@ +import fs from 'fs' +import { ipcMain, shell } from 'electron' +import { + getInstalledVersion, + getIsDaemonRunning, + getLatestVersion, + startDaemon, + stopDaemon, +} from './daemon' +import { + Config, + getConfig, + getDefaultDataPath, + getIsConfigured, + saveConfig, +} from './config' + +export function initIpc() { + ipcMain.handle('open-browser', (_, url: string) => { + shell.openExternal(url) + }) + ipcMain.handle('daemon-start', async (_) => { + await startDaemon() + }) + ipcMain.handle('daemon-stop', async (_) => { + await stopDaemon() + }) + ipcMain.handle('daemon-is-running', (_) => { + const isDaemonRunning = getIsDaemonRunning() + return isDaemonRunning + }) + ipcMain.handle('config-get', (_) => { + const config = getConfig() + return config + }) + ipcMain.handle('get-is-configured', (_) => { + return getIsConfigured() + }) + ipcMain.handle('open-data-directory', (_) => { + shell.openPath(getDefaultDataPath()) + return true + }) + ipcMain.handle('get-default-data-directory', async (_) => { + await fs.promises.mkdir(getDefaultDataPath(), { recursive: true }) + return getDefaultDataPath() + }) + ipcMain.handle('get-installed-version', (_) => { + return getInstalledVersion() + }) + ipcMain.handle('get-latest-version', (_) => { + return getLatestVersion() + }) + ipcMain.handle('config-save', async (_, config: Config) => { + await saveConfig(config) + return true + }) +} diff --git a/renterd/electron-src/shortcuts.ts b/renterd/electron-src/shortcuts.ts new file mode 100644 index 0000000..d84900a --- /dev/null +++ b/renterd/electron-src/shortcuts.ts @@ -0,0 +1,15 @@ +import { globalShortcut } from 'electron' +import { state, system } from './state' + +export function initShortcuts() { + // Register a global shortcut listener for the developer tools + const devToolsShortcut = system.isDarwin ? 'Cmd+Alt+I' : 'Ctrl+Shift+I' + globalShortcut.register(devToolsShortcut, () => { + // Open the DevTools + if (state.mainWindow && state.mainWindow.webContents) { + state.mainWindow.webContents.openDevTools() + state.mainWindow.setSize(1000, 800) + state.mainWindow.setMaximumSize(2000, 2000) + } + }) +} diff --git a/renterd/electron-src/startup.ts b/renterd/electron-src/startup.ts new file mode 100644 index 0000000..9bd4544 --- /dev/null +++ b/renterd/electron-src/startup.ts @@ -0,0 +1,14 @@ +import { shell } from 'electron' +import { startDaemon } from './daemon' +import { getConfig, getIsConfigured } from './config' +import { state } from './state' + +export function startup() { + // If the app is already configured, start the daemon and open browser + // and do not show the configuration window. + if (getIsConfigured()) { + startDaemon() + state.mainWindow?.close() + shell.openExternal(`http://${getConfig().http.address}`) + } +} diff --git a/renterd/electron-src/state.ts b/renterd/electron-src/state.ts index 72cce9e..9f6088a 100644 --- a/renterd/electron-src/state.ts +++ b/renterd/electron-src/state.ts @@ -1,7 +1,24 @@ import { ChildProcess } from 'child_process' +import { BrowserWindow, Tray } from 'electron' export let state: { - process: ChildProcess | null + mainWindow: BrowserWindow | null + tray: Tray | null + daemon: ChildProcess | null + isQuitting: boolean } = { - process: null, + mainWindow: null, + tray: null, + isQuitting: false, + daemon: null, +} + +export const system: { + isDarwin: boolean + isLinux: boolean + isWindows: boolean +} = { + isDarwin: process.platform === 'darwin', + isLinux: process.platform === 'linux', + isWindows: process.platform === 'win32', } diff --git a/renterd/electron-src/tray.ts b/renterd/electron-src/tray.ts new file mode 100644 index 0000000..fbce2de --- /dev/null +++ b/renterd/electron-src/tray.ts @@ -0,0 +1,32 @@ +import path from 'path' +import { app, Tray, Menu } from 'electron' +import isDev from 'electron-is-dev' +import { state, system } from './state' + +export function initTray() { + const iconName = system.isDarwin ? 'tray.png' : 'tray-win.png' + const iconPath = isDev + ? path.join(process.cwd(), 'assets', iconName) + : path.join(__dirname, '../assets', iconName) + + state.tray = new Tray(iconPath) + const trayContextMenu = Menu.buildFromTemplate([ + { + label: 'Configure', + click: function () { + if (system.isDarwin) { + app.dock.show() + } + state.mainWindow?.show() + }, + }, + { + label: 'Quit', + click: function () { + app.quit() + }, + }, + ]) + state.tray.setToolTip('hostd') + state.tray.setContextMenu(trayContextMenu) +} diff --git a/renterd/electron-src/window.ts b/renterd/electron-src/window.ts new file mode 100644 index 0000000..5d222c5 --- /dev/null +++ b/renterd/electron-src/window.ts @@ -0,0 +1,62 @@ +import path, { join } from 'path' +import { BrowserWindow, app } from 'electron' +import isDev from 'electron-is-dev' +import { format } from 'url' +import { state, system } from './state' + +export function initWindow() { + state.mainWindow = new BrowserWindow({ + width: 500, + height: 700, + minWidth: 500, + minHeight: 600, + maxWidth: 500, + maxHeight: 800, + webPreferences: { + nodeIntegration: false, + contextIsolation: true, + preload: join(__dirname, 'preload.js'), + }, + }) + + // Hide the main window instead of closing it, to keep the app running in the tray + state.mainWindow.on('close', (event) => { + if (!state.isQuitting) { + event.preventDefault() + if (system.isDarwin) { + app.dock.hide() + } + state.mainWindow?.hide() + } + }) + + // Setup close to tray settings for both minimize and close events + state.mainWindow.on('minimize', () => { + if (system.isDarwin) { + app.dock.hide() + } + // TODO: Does Linux support tray? + // https://electronjs.org/docs/api/tray + // minimize instead of attempting to go to system tray + if (system.isLinux) { + return true + } + state.mainWindow?.hide() + return false + }) + + const url = isDev + ? 'http://localhost:8000/' + : format({ + pathname: path.join(__dirname, '../renderer/out/index.html'), + protocol: 'file:', + slashes: true, + }) + state.mainWindow.loadURL(url) + + if (isDev) { + state.mainWindow.setMaximumSize(2000, 2000) + state.mainWindow.setSize(1000, 800) + state.mainWindow.webContents.openDevTools() + } +}