Update index.ts to add toggleAutoStartup function and menu item
This commit is contained in:
parent
f40bb398f6
commit
15b9aa674d
@ -1,5 +1,15 @@
|
||||
import { spawn, ChildProcess } from 'child_process'
|
||||
import { app, autoUpdater, dialog, Tray, Menu, BrowserWindow, MenuItemConstructorOptions, nativeTheme } from 'electron'
|
||||
import {
|
||||
app,
|
||||
autoUpdater,
|
||||
dialog,
|
||||
Tray,
|
||||
Menu,
|
||||
BrowserWindow,
|
||||
MenuItemConstructorOptions,
|
||||
nativeTheme,
|
||||
Notification,
|
||||
} from 'electron'
|
||||
import Store from 'electron-store'
|
||||
import winston from 'winston'
|
||||
import 'winston-daily-rotate-file'
|
||||
@ -55,6 +65,7 @@ app.on('ready', () => {
|
||||
app.focus({ steal: true })
|
||||
|
||||
init()
|
||||
updateTray()
|
||||
})
|
||||
|
||||
function firstRunWindow() {
|
||||
@ -104,6 +115,21 @@ function updateTrayIcon() {
|
||||
}
|
||||
}
|
||||
|
||||
function toggleAutoStartup() {
|
||||
const currentSettings = app.getLoginItemSettings()
|
||||
const newOpenAtLogin = !currentSettings.openAtLogin
|
||||
|
||||
app.setLoginItemSettings({ openAtLogin: newOpenAtLogin })
|
||||
|
||||
const notification = new Notification({
|
||||
title: 'Auto Startup',
|
||||
body: `Auto startup is now ${newOpenAtLogin ? 'enabled' : 'disabled'}`,
|
||||
})
|
||||
notification.show()
|
||||
|
||||
updateTray()
|
||||
}
|
||||
|
||||
function updateTray() {
|
||||
const updateItems: MenuItemConstructorOptions[] = [
|
||||
{ label: 'An update is available', enabled: false },
|
||||
@ -114,13 +140,28 @@ function updateTray() {
|
||||
{ type: 'separator' },
|
||||
]
|
||||
|
||||
const isAutoStartupEnabled = app.getLoginItemSettings().openAtLogin
|
||||
const toggleAutoStartupItem: MenuItemConstructorOptions = {
|
||||
label: isAutoStartupEnabled ? 'Disable Auto Startup' : 'Enable Auto Startup',
|
||||
click: () => {
|
||||
toggleAutoStartup()
|
||||
},
|
||||
}
|
||||
|
||||
const menu = Menu.buildFromTemplate([
|
||||
...(updateAvailable ? updateItems : []),
|
||||
{ role: 'quit', label: 'Quit Ollama', accelerator: 'Command+Q' },
|
||||
toggleAutoStartupItem,
|
||||
{ role: 'about' },
|
||||
{ role: 'quit', accelerator: 'Command+Q' },
|
||||
])
|
||||
|
||||
if (!tray) {
|
||||
tray = new Tray(trayIconPath())
|
||||
// make sure the tray is updated when clicked to avoid stale info
|
||||
// e.g. user disables auto startup in system preferences but tray menu still shows it as enabled
|
||||
tray.on('click', () => {
|
||||
updateTray()
|
||||
})
|
||||
}
|
||||
|
||||
tray.setToolTip(updateAvailable ? 'An update is available' : 'Ollama')
|
||||
|
Loading…
x
Reference in New Issue
Block a user