Update index.ts to add toggleAutoStartup function and menu item

This commit is contained in:
Hichem Fantar 2024-10-04 01:48:40 +01:00
parent f40bb398f6
commit 15b9aa674d

View File

@ -1,5 +1,15 @@
import { spawn, ChildProcess } from 'child_process' 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 Store from 'electron-store'
import winston from 'winston' import winston from 'winston'
import 'winston-daily-rotate-file' import 'winston-daily-rotate-file'
@ -55,6 +65,7 @@ app.on('ready', () => {
app.focus({ steal: true }) app.focus({ steal: true })
init() init()
updateTray()
}) })
function firstRunWindow() { 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() { function updateTray() {
const updateItems: MenuItemConstructorOptions[] = [ const updateItems: MenuItemConstructorOptions[] = [
{ label: 'An update is available', enabled: false }, { label: 'An update is available', enabled: false },
@ -114,13 +140,28 @@ function updateTray() {
{ type: 'separator' }, { type: 'separator' },
] ]
const isAutoStartupEnabled = app.getLoginItemSettings().openAtLogin
const toggleAutoStartupItem: MenuItemConstructorOptions = {
label: isAutoStartupEnabled ? 'Disable Auto Startup' : 'Enable Auto Startup',
click: () => {
toggleAutoStartup()
},
}
const menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
...(updateAvailable ? updateItems : []), ...(updateAvailable ? updateItems : []),
{ role: 'quit', label: 'Quit Ollama', accelerator: 'Command+Q' }, toggleAutoStartupItem,
{ role: 'about' },
{ role: 'quit', accelerator: 'Command+Q' },
]) ])
if (!tray) { if (!tray) {
tray = new Tray(trayIconPath()) 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') tray.setToolTip(updateAvailable ? 'An update is available' : 'Ollama')