|
| 1 | +# Context Menu Quick Reference |
| 2 | + |
| 3 | +This guide provides a quick overview of how to create and display a context menu using our system. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## ContextMenuItem Type |
| 8 | + |
| 9 | +Define each menu item using the `ContextMenuItem` type: |
| 10 | + |
| 11 | +```ts |
| 12 | +type ContextMenuItem = { |
| 13 | + label?: string; |
| 14 | + type?: "separator" | "normal" | "submenu" | "checkbox" | "radio"; |
| 15 | + role?: string; // Electron role (optional) |
| 16 | + click?: () => void; // Callback for item selection (not needed if role is set) |
| 17 | + submenu?: ContextMenuItem[]; // For nested menus |
| 18 | + checked?: boolean; // For checkbox or radio items |
| 19 | + visible?: boolean; |
| 20 | + enabled?: boolean; |
| 21 | + sublabel?: string; |
| 22 | +}; |
| 23 | +``` |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Import and Show the Menu |
| 28 | + |
| 29 | +Import the context menu module: |
| 30 | + |
| 31 | +```ts |
| 32 | +import { ContextMenuModel } from "@/app/store/contextmenu"; |
| 33 | +``` |
| 34 | + |
| 35 | +To display the context menu, call: |
| 36 | + |
| 37 | +```ts |
| 38 | +ContextMenuModel.showContextMenu(menu, event); |
| 39 | +``` |
| 40 | + |
| 41 | +- **menu**: An array of `ContextMenuItem`. |
| 42 | +- **event**: The mouse event that triggered the context menu (typically from an onContextMenu handler). |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +## Basic Example |
| 47 | + |
| 48 | +A simple context menu with a separator: |
| 49 | + |
| 50 | +```ts |
| 51 | +const menu: ContextMenuItem[] = [ |
| 52 | + { |
| 53 | + label: "New File", |
| 54 | + click: () => { |
| 55 | + /* create a new file */ |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + label: "New Folder", |
| 60 | + click: () => { |
| 61 | + /* create a new folder */ |
| 62 | + }, |
| 63 | + }, |
| 64 | + { type: "separator" }, |
| 65 | + { |
| 66 | + label: "Rename", |
| 67 | + click: () => { |
| 68 | + /* rename item */ |
| 69 | + }, |
| 70 | + }, |
| 71 | +]; |
| 72 | + |
| 73 | +ContextMenuModel.showContextMenu(menu, e); |
| 74 | +``` |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## Example with Submenu and Checkboxes |
| 79 | + |
| 80 | +Toggle settings using a submenu with checkbox items: |
| 81 | + |
| 82 | +```ts |
| 83 | +const isClearOnStart = true; // Example setting |
| 84 | + |
| 85 | +const menu: ContextMenuItem[] = [ |
| 86 | + { |
| 87 | + label: "Clear Output On Restart", |
| 88 | + submenu: [ |
| 89 | + { |
| 90 | + label: "On", |
| 91 | + type: "checkbox", |
| 92 | + checked: isClearOnStart, |
| 93 | + click: () => { |
| 94 | + // Set the config to enable clear on restart |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + label: "Off", |
| 99 | + type: "checkbox", |
| 100 | + checked: !isClearOnStart, |
| 101 | + click: () => { |
| 102 | + // Set the config to disable clear on restart |
| 103 | + }, |
| 104 | + }, |
| 105 | + ], |
| 106 | + }, |
| 107 | +]; |
| 108 | + |
| 109 | +ContextMenuModel.showContextMenu(menu, e); |
| 110 | +``` |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## Editing a Config File Example |
| 115 | + |
| 116 | +Open a configuration file (e.g., `widgets.json`) in preview mode: |
| 117 | + |
| 118 | +```ts |
| 119 | +{ |
| 120 | + label: "Edit widgets.json", |
| 121 | + click: () => { |
| 122 | + fireAndForget(async () => { |
| 123 | + const path = `${getApi().getConfigDir()}/widgets.json`; |
| 124 | + const blockDef: BlockDef = { |
| 125 | + meta: { view: "preview", file: path }, |
| 126 | + }; |
| 127 | + await createBlock(blockDef, false, true); |
| 128 | + }); |
| 129 | + }, |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## Summary |
| 136 | + |
| 137 | +- **Menu Definition**: Use the `ContextMenuItem` type. |
| 138 | +- **Actions**: Use `click` for actions; use `submenu` for nested options. |
| 139 | +- **Separators**: Use `type: "separator"` to group items. |
| 140 | +- **Toggles**: Use `type: "checkbox"` or `"radio"` with the `checked` property. |
| 141 | +- **Displaying**: Use `ContextMenuModel.showContextMenu(menu, event)` to render the menu. |
0 commit comments