|
1 | 1 | // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. |
2 | 2 | // See LICENSE.txt for license information. |
3 | 3 |
|
4 | | -import type {Menu} from 'electron'; |
| 4 | +import type {Menu, MenuItem} from 'electron'; |
| 5 | + |
| 6 | +function normalizeTrayMenuLabel(value: string): string { |
| 7 | + return value.toLowerCase().replace(/&/g, '').replace(/[.\u2026…]+$/u, '').trim(); |
| 8 | +} |
| 9 | + |
| 10 | +function trayMenuLabelMatches(itemLabel: string, label: string, truncated: string): boolean { |
| 11 | + const normalizedItem = normalizeTrayMenuLabel(itemLabel); |
| 12 | + const normalizedTarget = normalizeTrayMenuLabel(label); |
| 13 | + return itemLabel === label || |
| 14 | + itemLabel === truncated || |
| 15 | + normalizedItem === normalizedTarget; |
| 16 | +} |
| 17 | + |
| 18 | +function clickTrayMenuItemsByRole(items: MenuItem[], role: string): boolean { |
| 19 | + for (const item of items) { |
| 20 | + if (item.role === role && typeof item.click === 'function') { |
| 21 | + item.click(); |
| 22 | + return true; |
| 23 | + } |
| 24 | + if (item.submenu?.items && clickTrayMenuItemsByRole(item.submenu.items, role)) { |
| 25 | + return true; |
| 26 | + } |
| 27 | + } |
| 28 | + return false; |
| 29 | +} |
| 30 | + |
| 31 | +function findTrayMenuItemByLabelPredicate( |
| 32 | + items: MenuItem[], |
| 33 | + predicate: (normalizedLabel: string, itemLabel: string) => boolean, |
| 34 | +): MenuItem | null { |
| 35 | + for (const item of items) { |
| 36 | + const itemLabel = typeof item.label === 'string' ? item.label : ''; |
| 37 | + if ( |
| 38 | + predicate(normalizeTrayMenuLabel(itemLabel), itemLabel) && |
| 39 | + item.enabled !== false && |
| 40 | + item.visible !== false && |
| 41 | + typeof item.click === 'function' |
| 42 | + ) { |
| 43 | + return item; |
| 44 | + } |
| 45 | + if (item.submenu?.items) { |
| 46 | + const nested = findTrayMenuItemByLabelPredicate(item.submenu.items, predicate); |
| 47 | + if (nested) { |
| 48 | + return nested; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + return null; |
| 53 | +} |
| 54 | + |
| 55 | +function clickTrayMenuItems(items: MenuItem[], label: string, truncated: string): boolean { |
| 56 | + const item = findTrayMenuItemByLabelPredicate(items, (normalized, raw) => |
| 57 | + trayMenuLabelMatches(raw, label, truncated) || normalized === normalizeTrayMenuLabel(label), |
| 58 | + ); |
| 59 | + if (!item) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + item.click(); |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +function clickTraySettingsMenuItem(items: MenuItem[]): boolean { |
| 67 | + const item = findTrayMenuItemByLabelPredicate(items, (normalized) => |
| 68 | + normalized === 'settings' || |
| 69 | + normalized === 'preferences' || |
| 70 | + normalized.startsWith('settings') || |
| 71 | + normalized.startsWith('preferences'), |
| 72 | + ); |
| 73 | + if (!item) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + item.click(); |
| 77 | + return true; |
| 78 | +} |
5 | 79 |
|
6 | 80 | export function createClickTrayMenuItem(getTrayMenu: () => Menu) { |
7 | 81 | return (label: string) => { |
8 | | - const truncated = label.length > 50 ? `${label.slice(0, 50)}...` : label; |
| 82 | + if (label.startsWith('role:')) { |
| 83 | + const role = label.slice('role:'.length); |
| 84 | + if (!clickTrayMenuItemsByRole(getTrayMenu().items, role)) { |
| 85 | + throw new Error(`Tray menu item with role not found: ${role}`); |
| 86 | + } |
| 87 | + return; |
| 88 | + } |
9 | 89 |
|
10 | | - function clickItem(items: Electron.MenuItem[]): boolean { |
11 | | - for (const item of items) { |
12 | | - const itemLabel = typeof item.label === 'string' ? item.label : ''; |
13 | | - if ( |
14 | | - (itemLabel === label || itemLabel === truncated) && |
15 | | - item.enabled !== false && |
16 | | - item.visible !== false && |
17 | | - typeof item.click === 'function' |
18 | | - ) { |
19 | | - item.click(); |
20 | | - return true; |
21 | | - } |
22 | | - if (item.submenu?.items && clickItem(item.submenu.items)) { |
23 | | - return true; |
24 | | - } |
| 90 | + if (label === 'tray:settings') { |
| 91 | + if (!clickTraySettingsMenuItem(getTrayMenu().items)) { |
| 92 | + throw new Error('Tray settings menu item not found'); |
25 | 93 | } |
26 | | - return false; |
| 94 | + return; |
27 | 95 | } |
28 | 96 |
|
29 | | - if (!clickItem(getTrayMenu().items)) { |
| 97 | + const truncated = label.length > 50 ? `${label.slice(0, 50)}...` : label; |
| 98 | + if (!clickTrayMenuItems(getTrayMenu().items, label, truncated)) { |
30 | 99 | throw new Error(`Tray menu item not found: ${label}`); |
31 | 100 | } |
32 | 101 | }; |
|
0 commit comments