-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bee lifecycle management to tray
- Loading branch information
Cafe137
committed
Mar 7, 2022
1 parent
7f0a27b
commit 45a4648
Showing
4 changed files
with
83 additions
and
12 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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
const { runLauncher } = require('./src/launcher') | ||
const { runElectronTray } = require('./src/electron') | ||
const { runStaticServer } = require('./src/static-server') | ||
|
||
runLauncher() | ||
runElectronTray() | ||
runStaticServer() |
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,17 +1,44 @@ | ||
const { app, Tray, Menu, shell } = require('electron') | ||
const { runLauncher } = require('./launcher') | ||
const { BeeManager } = require('./lifecycle') | ||
|
||
let tray | ||
|
||
function rebuildElectronTray() { | ||
const contextMenu = Menu.buildFromTemplate([ | ||
{ | ||
label: BeeManager.isRunning() ? 'Stop Bee' : 'Start Bee', | ||
click: () => { | ||
if (BeeManager.isRunning()) { | ||
BeeManager.stop() | ||
} else { | ||
runLauncher() | ||
} | ||
} | ||
}, | ||
{ type: 'separator' }, | ||
{ label: 'Open Web UI', click: () => shell.openExternal('http://localhost:5000') }, | ||
{ type: 'separator' }, | ||
{ | ||
label: 'Exit', | ||
click: async () => { | ||
BeeManager.stop() | ||
await BeeManager.waitForSigtermToFinish() | ||
app.quit() | ||
} | ||
} | ||
]) | ||
tray.setContextMenu(contextMenu) | ||
} | ||
|
||
function main() { | ||
app.whenReady().then(() => { | ||
tray = new Tray('tray.png') | ||
const contextMenu = Menu.buildFromTemplate([ | ||
{ label: 'Open Web UI', click: () => shell.openExternal('http://localhost:5000') } | ||
]) | ||
tray.setContextMenu(contextMenu) | ||
rebuildElectronTray() | ||
}) | ||
} | ||
|
||
module.exports = { | ||
runElectronTray: main | ||
runElectronTray: main, | ||
rebuildElectronTray | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const state = { | ||
process: null, | ||
running: false, | ||
abortController: null | ||
} | ||
|
||
const BeeManager = { | ||
signalRunning: (abortController, process) => { | ||
state.abortController = abortController | ||
state.process = process | ||
state.running = true | ||
}, | ||
signalStopped: () => { | ||
state.running = false | ||
}, | ||
isRunning: () => state.running || (state.abortController && !state.abortController.signal.aborted), | ||
stop: () => { | ||
if (state.abortController) { | ||
state.abortController.abort() | ||
} | ||
}, | ||
waitForSigtermToFinish: async () => { | ||
if (state.process) { | ||
await state.process | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
BeeManager | ||
} |