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