From 15b9aa674d73f44fac181706d5bbcb65e98fa1a7 Mon Sep 17 00:00:00 2001 From: Hichem Fantar Date: Fri, 4 Oct 2024 01:48:40 +0100 Subject: [PATCH] Update index.ts to add toggleAutoStartup function and menu item --- macapp/src/index.ts | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/macapp/src/index.ts b/macapp/src/index.ts index a5d04d5f..f05faeea 100644 --- a/macapp/src/index.ts +++ b/macapp/src/index.ts @@ -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')