Add an action to a context menu
Add Reset panel preferences to the project/session context menu. This example extends the settings panel project with a second action. It resets the stored accent and control-size values to known defaults. It does not modify workspace files or delete user data.
The menu calls an installed action with an empty input object and the host's project/session context. It does not automatically provide selected file contents or message text. Choose an action that can work with that contract.
Start from the panel project
Create the Accessible Theme project or download its TypeScript source. Keep its current Save action, panel and storage declarations.
Declare the menu action
Append this entry to contributions.actions in glixo.extension.json:
{
"id": "reset-preferences",
"title": "Reset panel preferences",
"component": "panel-actions",
"placements": ["context-menu.project"],
"context": "project",
"parameters": { "type": "object", "additionalProperties": false },
"result": {
"type": "object",
"additionalProperties": false,
"required": ["accepted"],
"properties": { "accepted": { "type": "boolean" } }
},
"timeoutMs": 5000,
"cancellable": true
}
Extend the existing host.action permission's scope to include both actions:
{
"id": "host.action",
"scope": { "actions": ["save-preferences", "reset-preferences"] },
"reason": "Save or reset this extension's appearance preferences when the user asks."
}
Keep storage.extension scoped to accessible-theme/preferences/. Adding the new action expands the requested capability and requires consent again. A menu placement declares where a feature appears; it is not permission to read files or messages.
Supported context menu placements are context-menu.project, context-menu.file and context-menu.message. The project placement is currently mounted on sidebar session rows and receives the session/project context. File and message menus also have their own host context; they do not send arbitrary selection data to an extension.
Implement the action
Create src/reset.ts with the complete handler:
import type { HostEnvelope, ScopedState } from './handler.js';
export function handleResetPreferences(request: HostEnvelope, state: ScopedState): { accepted: true } {
if (request.kind !== 'actions' || request.contributionId !== 'reset-preferences') {
throw new Error('contribution_mismatch');
}
if (typeof request.context?.sessionId !== 'string' || !request.context.sessionId.trim()) {
throw new Error('session_context_missing');
}
if (!request.input || typeof request.input !== 'object' ||
Array.isArray(request.input) || Object.keys(request.input).length !== 0) {
throw new Error('reset_input_invalid');
}
state.set('accessible-theme/preferences/current',
JSON.stringify({ accent: '#10b981', largeControls: false }));
return { accepted: true };
}
Replace src/guest.ts with this complete entry point. It retains the existing Save handler and dispatches the new Reset action explicitly:
import { stateSet } from 'glixo:contribution/broker@1.0.0';
import { handleSavePreferences, type HostEnvelope, type ScopedState } from './handler.js';
import { handleResetPreferences } from './reset.js';
const state: ScopedState = { set(key, value) { stateSet(key, value); } };
export const guest = {
invoke(requestJson: string): string {
try {
const request = JSON.parse(requestJson) as HostEnvelope;
const result = request.contributionId === 'reset-preferences'
? handleResetPreferences(request, state)
: handleSavePreferences(request, state);
return JSON.stringify(result);
} catch (error) {
const code = error instanceof Error ? error.message :
typeof error === 'string' ? error : 'preferences_action_failed';
throw code.slice(0, 128) || 'preferences_action_failed';
}
},
};
The host resolves the action only from the installed package's declaration and checks its permission. The guest also rejects the wrong contribution kind, missing session context and unexpected parameters. Use the broker's scoped storage; no filesystem path or credential is passed through the menu.
Build the changed project
glxdev check
npm ci --install-links
npm run build
npm test
glxdev pack --output ./dist/package-menu
glxdev verify --output ./dist/package-menu
The downloadable project includes tests for Save and Reset, including empty input, scoped storage and rejection of invalid context/parameters. To add the same Reset tests yourself, save this as test/reset.test.mjs:
import assert from 'node:assert/strict';
import test from 'node:test';
import { handleResetPreferences } from '../dist/js/reset.js';
const request = { kind: 'actions', contributionId: 'reset-preferences', input: {}, context: { sessionId: 'session-example' } };
test('reset writes only the declared preference record', () => {
const writes = [];
assert.deepEqual(handleResetPreferences(request, { set: (...args) => writes.push(args) }), { accepted: true });
assert.deepEqual(writes, [['accessible-theme/preferences/current', '{"accent":"#10b981","largeControls":false}']]);
});
test('invalid action, context and inputs never write', () => {
const state = { set() { assert.fail('invalid request wrote state'); } };
for (const invalid of [
{ ...request, kind: 'tools' },
{ ...request, contributionId: 'save-preferences' },
{ ...request, context: {} },
{ ...request, input: { path: '/outside' } },
{ ...request, input: [] },
{ ...request, input: null },
]) assert.throws(() => handleResetPreferences(invalid, state));
});
The published provider fixture driver does not exercise menu actions. Use the signing and installation instructions, running installation from this source project after building.
Test the menu action
- Install the changed package and approve its updated action permission.
- Start it, open a project with a session, and open that session row's context menu.
- Select Reset panel preferences. Confirm the host reports no action error.
- Confirm through your handler test or a separately declared read action that the preference record contains the defaults.
- Revoke
reset-preferencespermission; invoking the menu action must fail without writing.
This is an authoring recipe based on the public panel sample and Glixo's declared menu/action interface. The complete installed menu sequence remains a Preview check. The panel's current UI does not read persisted preferences back into its controls, so a visual color change alone cannot verify the Reset operation.