Skip to content

Commit 18f83e9

Browse files
committed
feat: add onTrayUpdate trigger to plugin system
1 parent e97da7f commit 18f83e9

File tree

8 files changed

+65
-8
lines changed

8 files changed

+65
-8
lines changed

frontend/src/constant/app.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ export const DraggableOptions = {
8484
}
8585

8686
export const PluginsTriggerOptions = [
87-
{ label: 'plugin.on::manual', value: PluginTrigger.OnManual },
8887
{ label: 'plugin.on::startup', value: PluginTrigger.OnStartup },
8988
{ label: 'plugin.on::ready', value: PluginTrigger.OnReady },
9089
{ label: 'plugin.on::shutdown', value: PluginTrigger.OnShutdown },
90+
{ label: 'plugin.on::manual', value: PluginTrigger.OnManual },
9191
{ label: 'plugin.on::generate', value: PluginTrigger.OnGenerate },
9292
{ label: 'plugin.on::subscribe', value: PluginTrigger.OnSubscribe },
93+
{ label: 'plugin.on::tray::update', value: PluginTrigger.OnTrayUpdate },
9394
{ label: 'plugin.on::before::core::start', value: PluginTrigger.OnBeforeCoreStart },
9495
{ label: 'plugin.on::core::started', value: PluginTrigger.OnCoreStarted },
9596
{ label: 'plugin.on::before::core::stop', value: PluginTrigger.OnBeforeCoreStop },

frontend/src/enums/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ export enum PluginTrigger {
6464
OnCoreStopped = 'on::core::stopped',
6565
OnBeforeCoreStart = 'on::before::core::start',
6666
OnBeforeCoreStop = 'on::before::core::stop',
67+
OnTrayUpdate = 'on::tray::update',
6768
}
6869

6970
export enum PluginTriggerEvent {
7071
OnInstall = 'onInstall',
7172
OnUninstall = 'onUninstall',
7273
OnManual = 'onRun',
74+
OnTrayUpdate = 'onTrayUpdate',
7375
OnSubscribe = 'onSubscribe',
7476
OnGenerate = 'onGenerate',
7577
OnStartup = 'onStartup',

frontend/src/lang/locale/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ export default {
482482
'on::core::stopped': 'on::core::stopped',
483483
'on::before::core::start': 'on::before::core::start',
484484
'on::before::core::stop': 'on::before::core::stop',
485+
'on::tray::update': 'on::tray::update',
485486
name: 'Name',
486487
version: 'Version',
487488
description: 'Description',

frontend/src/lang/locale/zh.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ export default {
481481
'on::core::stopped': '核心停止后',
482482
'on::before::core::start': '核心启动前',
483483
'on::before::core::stop': '核心停止前',
484+
'on::tray::update': '托盘更新时',
484485
name: '名称',
485486
version: '版本号',
486487
description: '描述',

frontend/src/stores/plugins.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
asyncPool,
1919
} from '@/utils'
2020

21-
import type { Plugin, Subscription } from '@/types/app'
21+
import type { Plugin, Subscription, TrayContent, MenuItem } from '@/types/app'
2222

2323
const PluginsCache: Recordable<{ plugin: Plugin; code: string }> = {}
2424

@@ -32,6 +32,10 @@ const PluginsTriggerMap: {
3232
fnName: PluginTriggerEvent.OnManual,
3333
observers: [],
3434
},
35+
[PluginTrigger.OnTrayUpdate]: {
36+
fnName: PluginTriggerEvent.OnTrayUpdate,
37+
observers: [],
38+
},
3539
[PluginTrigger.OnSubscribe]: {
3640
fnName: PluginTriggerEvent.OnSubscribe,
3741
observers: [],
@@ -125,7 +129,7 @@ export const usePluginsStore = defineStore('plugins', () => {
125129
configuration[key] = value
126130
}
127131
Object.assign(configuration, appSettingsStore.app.pluginSettings[plugin.id] ?? {})
128-
return { ...plugin, ...configuration }
132+
return deepClone({ ...plugin, ...configuration })
129133
}
130134

131135
const isPluginUnavailable = (
@@ -465,6 +469,35 @@ export const usePluginsStore = defineStore('plugins', () => {
465469
}
466470
}
467471

472+
const onTrayUpdateTrigger = async (tray: TrayContent, menus: MenuItem[]) => {
473+
const { fnName, observers } = PluginsTriggerMap[PluginTrigger.OnTrayUpdate]
474+
475+
let finalTray = tray
476+
let finalMenus = menus
477+
for (const observer of observers) {
478+
const cache = PluginsCache[observer]
479+
480+
if (isPluginUnavailable(cache)) continue
481+
482+
const metadata = getPluginMetadata(cache.plugin)
483+
try {
484+
const fn = new window.AsyncFunction(
485+
'Plugin',
486+
'tray',
487+
'menus',
488+
`${cache.code}; return await ${fnName}(tray, menus)`,
489+
)
490+
const { tray, menus } = await fn(metadata, finalTray, finalMenus)
491+
finalTray = tray
492+
finalMenus = menus
493+
} catch (error: any) {
494+
throw `${cache.plugin.name} : ` + (error.message || error)
495+
}
496+
}
497+
498+
return [finalTray, finalMenus] as const
499+
}
500+
468501
const _watchDisabled = computed(() =>
469502
plugins.value
470503
.map((v) => v.disabled)
@@ -496,6 +529,7 @@ export const usePluginsStore = defineStore('plugins', () => {
496529
updatePlugins,
497530
getPluginById,
498531
reloadPlugin,
532+
onTrayUpdateTrigger,
499533
onSubscribeTrigger,
500534
onGenerateTrigger,
501535
onStartupTrigger: () => noParamsTrigger(PluginTrigger.OnStartup),

frontend/src/utils/completion.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ export const getCompletions = (pluginScope: any = undefined) => {
4444
detail: t('plugin.trigger') + ' ' + t('plugin.on::manual'),
4545
},
4646
),
47+
snippetCompletion(
48+
`/* ${t('plugin.trigger') + ' ' + t('plugin.on::tray::update')} */\n` +
49+
`const ${PluginTriggerEvent.OnTrayUpdate} = async (tray, menus) => {\n\t\${}\n\treturn { tray, menus }\n}`,
50+
{
51+
label: PluginTriggerEvent.OnTrayUpdate,
52+
type: 'keyword',
53+
detail: t('plugin.trigger') + ' ' + t('plugin.on::tray::update'),
54+
},
55+
),
4756
snippetCompletion(
4857
`/* ${t('plugin.trigger') + ' ' + t('plugin.on::subscribe')} */\n` +
4958
`const ${PluginTriggerEvent.OnSubscribe} = async (proxies, subscription) => {\n\t\${}\n\treturn proxies\n}`,

frontend/src/utils/tray.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,21 @@ const getTrayMenus = () => {
388388
},
389389
]
390390

391-
return generateUniqueEventsForMenu(trayMenus)
391+
return trayMenus
392392
}
393393

394394
export const updateTrayMenus = debounce(async () => {
395395
const trayMenus = getTrayMenus()
396396
const trayIcons = getTrayIcons()
397+
const pluginsStore = usePluginsStore()
397398

398399
const isDarwin = useEnvStore().env.os === 'darwin'
399400
const title = isDarwin ? '' : APP_TITLE
400401

401-
await UpdateTray({ icon: trayIcons, title, tooltip: APP_TITLE + ' ' + APP_VERSION })
402-
await UpdateTrayMenus(trayMenus as any)
402+
const tray = { icon: trayIcons, title, tooltip: APP_TITLE + ' ' + APP_VERSION }
403+
404+
const [finalTray, finalMenus] = await pluginsStore.onTrayUpdateTrigger(tray, trayMenus)
405+
406+
await UpdateTray(finalTray)
407+
await UpdateTrayMenus(generateUniqueEventsForMenu(finalMenus) as any)
403408
}, 500)

frontend/src/views/PluginsView/components/PluginForm.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,15 @@ defineExpose({ modalSlots })
208208
</div>
209209
<div class="form-item">
210210
<div class="mr-8">{{ t('plugin.trigger') }}</div>
211-
<CheckBox v-model="plugin.triggers" :options="PluginsTriggerOptions.slice(0, 6)" />
211+
<CheckBox v-model="plugin.triggers" :options="PluginsTriggerOptions.slice(0, 3)" />
212212
</div>
213213
<div class="form-item">
214214
<div class="name"></div>
215-
<CheckBox v-model="plugin.triggers" :options="PluginsTriggerOptions.slice(6)" />
215+
<CheckBox v-model="plugin.triggers" :options="PluginsTriggerOptions.slice(3, 7)" />
216+
</div>
217+
<div class="form-item">
218+
<div class="name"></div>
219+
<CheckBox v-model="plugin.triggers" :options="PluginsTriggerOptions.slice(7)" />
216220
</div>
217221
<div class="form-item">
218222
{{ t('plugin.name') }} *

0 commit comments

Comments
 (0)