convert the desktop app to system tray

This commit is contained in:
Eva Ho
2023-07-05 16:10:30 -04:00
committed by Jeffrey Morgan
parent 9b8a456c7d
commit 703e10ca68
18 changed files with 724 additions and 42 deletions

View File

@@ -1,43 +1,35 @@
import { app, BrowserWindow, autoUpdater, dialog } from 'electron'
import { spawn, exec } from 'child_process'
import { app, autoUpdater, dialog, Tray, Menu, nativeTheme } from 'electron'
import * as path from 'path'
import * as fs from 'fs'
require('@electron/remote/main').initialize()
// This allows TypeScript to pick up the magic constants that's auto-generated by Forge's Webpack
// plugin that tells the Electron app where to look for the Webpack-bundled app code (depending on
// whether you're running in development or production).
declare const MAIN_WINDOW_WEBPACK_ENTRY: string
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string
let tray: Tray | null = null
const createSystemtray = () => {
const brightModeIconPath = path.join(__dirname, '..', '..', 'src', 'ollama_icon_dark_16x16.png')
const darkModeIconPath = path.join(__dirname, '..', '..', 'src', 'ollama_icon_bright_16x16.png')
tray = new Tray(brightModeIconPath)
if (process.platform === 'darwin') {
tray.setImage(nativeTheme.shouldUseDarkColors ? darkModeIconPath : brightModeIconPath)
nativeTheme.on('updated', () => {
tray.setImage(nativeTheme.shouldUseDarkColors ? darkModeIconPath : brightModeIconPath)
})
}
const contextMenu = Menu.buildFromTemplate([{ label: 'Quit', type: 'normal', click: () => app.quit() }])
tray.setContextMenu(contextMenu)
tray.setToolTip('Ollama')
}
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
app.quit()
}
const createWindow = (): void => {
// Create the browser window.
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
minWidth: 400,
minHeight: 300,
titleBarStyle: 'hiddenInset',
webPreferences: {
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
nodeIntegration: true,
contextIsolation: false,
},
})
require('@electron/remote/main').enable(mainWindow.webContents)
// and load the index.html of the app.
mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY)
}
const ollama = path.join(process.resourcesPath, 'ollama')
// if the app is packaged then run the server
@@ -111,7 +103,11 @@ function installCLI() {
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => {
createWindow()
if (process.platform === 'darwin') {
app.dock.hide()
}
createSystemtray()
if (app.isPackaged) {
installCLI()
@@ -127,14 +123,6 @@ app.on('window-all-closed', () => {
}
})
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
autoUpdater.setFeedURL({