feat: toggle Selection Assistant on tray menu (#7286)

feat: toggle SelectionService on tray
This commit is contained in:
fullex 2025-06-18 00:02:28 +08:00 committed by GitHub
parent 1408b822ca
commit 26b82f709f

View File

@ -1,4 +1,4 @@
import { isMac } from '@main/constant'
import { isLinux, isMac, isWin } from '@main/constant'
import { locales } from '@main/utils/locales'
import { app, Menu, MenuItemConstructorOptions, nativeImage, nativeTheme, Tray } from 'electron'
@ -6,6 +6,7 @@ import icon from '../../../build/tray_icon.png?asset'
import iconDark from '../../../build/tray_icon_dark.png?asset'
import iconLight from '../../../build/tray_icon_light.png?asset'
import { ConfigKeys, configManager } from './ConfigManager'
import selectionService from './SelectionService'
import { windowService } from './WindowService'
export class TrayService {
@ -29,14 +30,14 @@ export class TrayService {
const iconPath = isMac ? (nativeTheme.shouldUseDarkColors ? iconLight : iconDark) : icon
const tray = new Tray(iconPath)
if (process.platform === 'win32') {
if (isWin) {
tray.setImage(iconPath)
} else if (process.platform === 'darwin') {
} else if (isMac) {
const image = nativeImage.createFromPath(iconPath)
const resizedImage = image.resize({ width: 16, height: 16 })
resizedImage.setTemplateImage(true)
tray.setImage(resizedImage)
} else if (process.platform === 'linux') {
} else if (isLinux) {
const image = nativeImage.createFromPath(iconPath)
const resizedImage = image.resize({ width: 16, height: 16 })
tray.setImage(resizedImage)
@ -46,7 +47,7 @@ export class TrayService {
this.updateContextMenu()
if (process.platform === 'linux') {
if (isLinux) {
this.tray.setContextMenu(this.contextMenu)
}
@ -69,19 +70,31 @@ export class TrayService {
private updateContextMenu() {
const locale = locales[configManager.getLanguage()]
const { tray: trayLocale } = locale.translation
const { tray: trayLocale, selection: selectionLocale } = locale.translation
const enableQuickAssistant = configManager.getEnableQuickAssistant()
const quickAssistantEnabled = configManager.getEnableQuickAssistant()
const selectionAssistantEnabled = configManager.getSelectionAssistantEnabled()
const template = [
{
label: trayLocale.show_window,
click: () => windowService.showMainWindow()
},
enableQuickAssistant && {
quickAssistantEnabled && {
label: trayLocale.show_mini_window,
click: () => windowService.showMiniWindow()
},
isWin && {
label: selectionLocale.name + (selectionAssistantEnabled ? ' - On' : ' - Off'),
// type: 'checkbox',
// checked: selectionAssistantEnabled,
click: () => {
if (selectionService) {
selectionService.toggleEnabled()
this.updateContextMenu()
}
}
},
{ type: 'separator' },
{
label: trayLocale.quit,
@ -118,6 +131,10 @@ export class TrayService {
configManager.subscribe(ConfigKeys.EnableQuickAssistant, () => {
this.updateContextMenu()
})
configManager.subscribe(ConfigKeys.SelectionAssistantEnabled, () => {
this.updateContextMenu()
})
}
private quit() {