Introduce menu entry support
Summary:
[interesting] since it shows how Flipper APIs are exposed through sandy. However, the next diff is a much simpler example of that
This diff adds support for adding menu entries for sandy plugin (renamed keyboard actions to menus, as it always creates a menu entry, but not necessarily a keyboard shortcut)
```
client.addMenuEntry(
// custom entry
{
label: 'Reset Selection',
topLevelMenu: 'Edit',
handler: () => {
selectedID.set(null);
},
},
// based on built-in action (sets standard label, shortcut)
{
action: 'createPaste',
handler: () => {
console.log('creating paste');
},
},
);
```
Most of this diff is introducing the concept of FlipperUtils, a set of static Flipper methods (not related to a device or client) that can be used from Sandy. This will for example be used to implement things as `createPaste` as well
Reviewed By: nikoant
Differential Revision: D22766990
fbshipit-source-id: ce90af3b700e6c3d9a779a3bab4673ba356f3933
This commit is contained in:
committed by
Facebook GitHub Bot
parent
94eaaf5dca
commit
9c202a4a10
@@ -26,8 +26,9 @@ import electron, {MenuItemConstructorOptions} from 'electron';
|
||||
import {notNull} from './utils/typeUtils';
|
||||
import constants from './fb-stubs/constants';
|
||||
import {Logger} from './fb-interfaces/Logger';
|
||||
import {NormalizedMenuEntry, buildInMenuEntries} from 'flipper-plugin';
|
||||
|
||||
export type DefaultKeyboardAction = 'clear' | 'goToBottom' | 'createPaste';
|
||||
export type DefaultKeyboardAction = keyof typeof buildInMenuEntries;
|
||||
export type TopLevelMenu = 'Edit' | 'View' | 'Window' | 'Help';
|
||||
|
||||
export type KeyboardAction = {
|
||||
@@ -37,26 +38,6 @@ export type KeyboardAction = {
|
||||
topLevelMenu: TopLevelMenu;
|
||||
};
|
||||
|
||||
const defaultKeyboardActions: Array<KeyboardAction> = [
|
||||
{
|
||||
label: 'Clear',
|
||||
accelerator: 'CmdOrCtrl+K',
|
||||
topLevelMenu: 'View',
|
||||
action: 'clear',
|
||||
},
|
||||
{
|
||||
label: 'Go To Bottom',
|
||||
accelerator: 'CmdOrCtrl+B',
|
||||
topLevelMenu: 'View',
|
||||
action: 'goToBottom',
|
||||
},
|
||||
{
|
||||
label: 'Create Paste',
|
||||
topLevelMenu: 'Edit',
|
||||
action: 'createPaste',
|
||||
},
|
||||
];
|
||||
|
||||
export type KeyboardActions = Array<DefaultKeyboardAction | KeyboardAction>;
|
||||
|
||||
const menuItems: Map<string, electron.MenuItem> = new Map();
|
||||
@@ -82,14 +63,12 @@ export function setupMenuBar(
|
||||
logger,
|
||||
);
|
||||
// collect all keyboard actions from all plugins
|
||||
const registeredActions: Set<KeyboardAction> = new Set(
|
||||
const registeredActions = new Set(
|
||||
plugins
|
||||
.map((plugin) => plugin.keyboardActions || [])
|
||||
.reduce((acc: KeyboardActions, cv) => acc.concat(cv), [])
|
||||
.flat()
|
||||
.map((action: DefaultKeyboardAction | KeyboardAction) =>
|
||||
typeof action === 'string'
|
||||
? defaultKeyboardActions.find((a) => a.action === action)
|
||||
: action,
|
||||
typeof action === 'string' ? buildInMenuEntries[action] : action,
|
||||
)
|
||||
.filter(notNull),
|
||||
);
|
||||
@@ -97,7 +76,7 @@ export function setupMenuBar(
|
||||
// add keyboard actions to
|
||||
registeredActions.forEach((keyboardAction) => {
|
||||
if (keyboardAction != null) {
|
||||
appendMenuItem(template, actionHandler, keyboardAction);
|
||||
appendMenuItem(template, keyboardAction);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -126,7 +105,6 @@ export function setupMenuBar(
|
||||
|
||||
function appendMenuItem(
|
||||
template: Array<MenuItemConstructorOptions>,
|
||||
actionHandler: (action: string) => void,
|
||||
item: KeyboardAction,
|
||||
) {
|
||||
const keyboardAction = item;
|
||||
@@ -183,6 +161,49 @@ export function activateMenuItems(
|
||||
);
|
||||
}
|
||||
|
||||
export function addSandyPluginEntries(entries: NormalizedMenuEntry[]) {
|
||||
if (!electron.remote.Menu) {
|
||||
return;
|
||||
}
|
||||
|
||||
// disable all keyboard actions
|
||||
for (const item of menuItems) {
|
||||
item[1].enabled = false;
|
||||
}
|
||||
|
||||
pluginActionHandler = (action: string) => {
|
||||
entries.find((e) => e.action === action)?.handler();
|
||||
};
|
||||
|
||||
let changedItems = false;
|
||||
const currentMenu = electron.remote.Menu.getApplicationMenu();
|
||||
for (const entry of entries) {
|
||||
const item = menuItems.get(entry.action!);
|
||||
if (item) {
|
||||
item.enabled = true;
|
||||
item.accelerator = entry.accelerator;
|
||||
} else {
|
||||
const parent = currentMenu?.items.find(
|
||||
(i) => i.label === entry.topLevelMenu,
|
||||
);
|
||||
if (parent) {
|
||||
const item = new electron.remote.MenuItem({
|
||||
enabled: true,
|
||||
click: () => pluginActionHandler?.(entry.action!),
|
||||
label: entry.label,
|
||||
accelerator: entry.accelerator,
|
||||
});
|
||||
parent.submenu!.append(item);
|
||||
menuItems.set(entry.action!, item);
|
||||
changedItems = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changedItems) {
|
||||
electron.remote.Menu.setApplicationMenu(currentMenu);
|
||||
}
|
||||
}
|
||||
|
||||
function getTemplate(
|
||||
app: electron.App,
|
||||
shell: electron.Shell,
|
||||
|
||||
Reference in New Issue
Block a user