-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tray & app menu item "Check for Updates..."
When the check is running, replace "Check for Updates" with a disabled item "Checking for Updates". Signed-off-by: Miroslav Bajtoš <saturn@bajtos.net>
- Loading branch information
Showing
9 changed files
with
325 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const { Menu, MenuItem, ipcMain } = require('electron') | ||
const { ipcMainEvents } = require('./ipc') | ||
|
||
function setupAppMenu (/** @type {import('./typings').Context} */ ctx) { | ||
// Add "Check for updates..." item to the Application menu on MacOS | ||
// GitHub issues tracking the work to add this menu item on other platforms: | ||
// - Windows: https://github.com/filecoin-project/filecoin-station/issues/63 | ||
// - Linux: https://github.com/filecoin-project/filecoin-station/issues/64 | ||
if (process.platform !== 'darwin') return | ||
|
||
const menu = Menu.getApplicationMenu() | ||
if (!menu) return | ||
|
||
menu.items[0].submenu?.insert(1, new MenuItem({ | ||
id: 'checkForUpdates', | ||
label: 'Check For Updates...', | ||
click: () => { ctx.manualCheckForUpdates() } | ||
})) | ||
menu.items[0].submenu?.insert(2, new MenuItem({ | ||
id: 'checkingForUpdates', | ||
label: 'Checking For Updates', | ||
enabled: false, | ||
visible: false | ||
})) | ||
|
||
Menu.setApplicationMenu(menu) | ||
setupIpcEventListeners(menu) | ||
} | ||
|
||
/** | ||
* @param {Electron.Menu} appMenu | ||
*/ | ||
function setupIpcEventListeners (appMenu) { | ||
ipcMain.on(ipcMainEvents.UPDATE_CHECK_STARTED, () => { | ||
getItemById('checkForUpdates').visible = false | ||
getItemById('checkingForUpdates').visible = true | ||
}) | ||
|
||
ipcMain.on(ipcMainEvents.UPDATE_CHECK_FINISHED, () => { | ||
getItemById('checkForUpdates').visible = true | ||
getItemById('checkingForUpdates').visible = false | ||
}) | ||
|
||
/** | ||
* Get an item from the main app menu or fail with a useful error message. | ||
* @param {string} id | ||
* @returns {Electron.MenuItem} | ||
*/ | ||
function getItemById (id) { | ||
const item = appMenu.getMenuItemById(id) | ||
if (!item) throw new Error(`Unknown app menu item id: ${id}`) | ||
return item | ||
} | ||
} | ||
|
||
module.exports = { | ||
setupAppMenu | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { dialog } = require('electron') | ||
const { IS_MAC } = require('./consts') | ||
|
||
module.exports = { | ||
showDialogSync | ||
} | ||
|
||
/** | ||
* NOTE: always send the buttons in the order [OK, Cancel, ...Actions]. | ||
* See this post for more interesting information about the topic: | ||
* https://medium.muz.li/ok-key-and-cancel-key-which-one-should-be-set-up-on-the-left-4780e86c16eb | ||
* | ||
* @param {import('electron').MessageBoxSyncOptions & {title: string}} args | ||
* @returns | ||
*/ | ||
function showDialogSync ({ | ||
title, | ||
message, | ||
type = 'info', | ||
buttons = ['OK', 'Cancel'], | ||
...opts | ||
}) { | ||
const options = { | ||
type, | ||
buttons, | ||
title, | ||
message, | ||
...opts | ||
} | ||
|
||
if (IS_MAC) { | ||
options.message = title | ||
options.detail = message | ||
} | ||
|
||
const isInverse = !IS_MAC | ||
|
||
if (isInverse) { | ||
options.buttons.reverse() | ||
} | ||
|
||
if (buttons.length > 1) { | ||
options.defaultId = isInverse ? buttons.length - 1 : 0 | ||
options.cancelId = isInverse ? buttons.length - 2 : 1 | ||
} | ||
|
||
const selected = dialog.showMessageBoxSync(options) | ||
|
||
return isInverse | ||
? buttons.length - selected - 1 | ||
: selected | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export interface Context { | ||
showUI: () => void | ||
loadWebUIFromDist: import('electron-serve').loadURL | ||
manualCheckForUpdates: () => void | ||
} |
Oops, something went wrong.