@koinzhang/paseo-plugin-commands 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/client/agent-state.ts +79 -0
- package/client/commands.ts +209 -0
- package/client/menu-list.tsx +194 -0
- package/client/picker.tsx +318 -0
- package/client/settings-screen.tsx +81 -0
- package/client/settings-store.ts +22 -0
- package/index.client.tsx +35 -0
- package/index.server.ts +12 -0
- package/package.json +42 -0
- package/paseo-plugin.json +7 -0
- package/server/control.ts +53 -0
- package/server/daemon.ts +85 -0
- package/shared/commands.ts +376 -0
- package/shared/parse.ts +23 -0
- package/shared/popover-geometry.ts +66 -0
- package/shared/resend.ts +26 -0
- package/shared/resolve-option.ts +80 -0
- package/shared/rpc.ts +42 -0
- package/shared/settings.ts +13 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@koinzhang/paseo-plugin-commands` are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0] - 2026-09-28
|
|
11
|
+
|
|
12
|
+
Requires Paseo **>= 0.9.0**.
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
|
|
16
|
+
- `/model`, `/effort`, `/profile`, `/mode`, `/feature`, `/rename`, and `/cancel` agent slash commands. Options come from provider capabilities; without an argument, a command opens a picker marking the current value.
|
|
17
|
+
- `/rename` renames the current agent tab by default. `-t` / `--tab` do the same; `-w` / `--workspace` rename the workspace.
|
|
18
|
+
- `/resend [text]` agent slash command to resend the latest non-empty user prompt, appending optional text on a new line.
|
|
19
|
+
- Settings → Plugins → commands → Settings: turn each slash command on or off, grouped into Runtime, Session, and Prompt sections, with a short description of what it does.
|
|
20
|
+
|
|
21
|
+
[0.1.0]: https://github.com/koinzhang/paseo-plugins/releases/tag/commands-v0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 koinzhang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Commands
|
|
2
|
+
|
|
3
|
+
Essential client-side slash commands for controlling the current Paseo agent.
|
|
4
|
+
|
|
5
|
+
Commands:
|
|
6
|
+
|
|
7
|
+
- `/model [model]` — show or switch the model
|
|
8
|
+
- `/effort [level]` — show or set thinking effort
|
|
9
|
+
- `/profile [profile]` — apply an agent profile's runtime settings (model, mode, effort, features)
|
|
10
|
+
- `/mode [mode]` — show or switch the mode
|
|
11
|
+
- `/feature [name] [value]` — show or set a provider feature
|
|
12
|
+
- `/rename [-t|-w] <title>` — rename the current tab, or the workspace with `-w`
|
|
13
|
+
- `/cancel` — cancel the running turn
|
|
14
|
+
- `/resend [text]` — resend the latest non-empty user prompt, appending any text on a new line
|
|
15
|
+
|
|
16
|
+
Options come from what the agent's provider reports; nothing is hardcoded. Without an argument,
|
|
17
|
+
a command opens a menu right away, anchored above the composer on desktop and as a sheet on
|
|
18
|
+
mobile, with the current choice checked. Input is matched case-insensitively by ID or label. An exact match wins, then a unique
|
|
19
|
+
prefix. Ambiguous input lists the candidates. `/rename` without a flag renames the current
|
|
20
|
+
tab; `-t` and `--tab` do the same, and `-w` or `--workspace` renames the workspace. `/resend`
|
|
21
|
+
resends the latest prompt as text, appending extra text after one newline.
|
|
22
|
+
|
|
23
|
+
Turn individual commands on or off in **Settings → Plugins → commands → Settings**. A disabled command disappears
|
|
24
|
+
from slash autocomplete, so the provider's own command of the same name (if any) takes over.
|
|
25
|
+
|
|
26
|
+
## Examples
|
|
27
|
+
|
|
28
|
+
```text
|
|
29
|
+
/model gpt-5.6-sol
|
|
30
|
+
/effort high
|
|
31
|
+
/feature fast_mode on
|
|
32
|
+
/profile reviewer
|
|
33
|
+
/rename Payment Review
|
|
34
|
+
/rename -w Payments work
|
|
35
|
+
/resend Also cover the empty input
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
paseo plugin add npm:@koinzhang/paseo-plugin-commands
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
To install from GitHub, use `paseo plugin add koinzhang/paseo-plugins --path commands`.
|
|
45
|
+
|
|
46
|
+
Requires Paseo >= 0.9.0. The plugin SDK cannot change agent settings yet, so the daemon side of
|
|
47
|
+
the plugin connects to its local daemon (`PASEO_LISTEN` or `$PASEO_HOME/paseo.pid`) and sends the
|
|
48
|
+
same requests the app uses. If the daemon requires a password, set `PASEO_PASSWORD` for it.
|
|
49
|
+
|
|
50
|
+
## Development
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
cd commands
|
|
54
|
+
npm install
|
|
55
|
+
npm run typecheck
|
|
56
|
+
npm test
|
|
57
|
+
paseo plugin install "$PWD"
|
|
58
|
+
paseo plugin reload commands
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Command definitions and option matching live in `shared/` (`commands.ts`, `parse.ts`,
|
|
62
|
+
`resolve-option.ts`), daemon-side setting writes in `server/daemon.ts`, and the menus plus
|
|
63
|
+
settings UI in `client/`. Specs are in [`specs/`](./specs/).
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { PluginAgentCommandContext } from "@getpaseo/plugin/client";
|
|
2
|
+
import type { AgentState, Feature, ModelOption, Profile, SelectOption } from "../shared/commands.ts";
|
|
3
|
+
|
|
4
|
+
type Paseo = PluginAgentCommandContext["paseo"];
|
|
5
|
+
|
|
6
|
+
export interface LoadNeeds {
|
|
7
|
+
models?: boolean;
|
|
8
|
+
modes?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function toSelectOption(option: { id: string; label: string; isDefault?: boolean }): SelectOption {
|
|
12
|
+
return { id: option.id, label: option.label, isDefault: option.isDefault };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Reads the live agent snapshot and, when asked, the provider catalogs it depends on. */
|
|
16
|
+
export async function loadAgentState(
|
|
17
|
+
paseo: Paseo,
|
|
18
|
+
agentId: string,
|
|
19
|
+
needs: LoadNeeds = {},
|
|
20
|
+
): Promise<AgentState> {
|
|
21
|
+
const result = await paseo.agents.ref(agentId).refresh();
|
|
22
|
+
const agent = result?.agent;
|
|
23
|
+
if (!agent) throw new Error("Agent not found.");
|
|
24
|
+
|
|
25
|
+
let models: ModelOption[] = [];
|
|
26
|
+
if (needs.models) {
|
|
27
|
+
const catalog = await paseo.providers.listModels(agent.provider, { cwd: agent.cwd });
|
|
28
|
+
if (!catalog.models?.length && catalog.error) throw new Error(catalog.error);
|
|
29
|
+
models = (catalog.models ?? [])
|
|
30
|
+
.filter((model) => model.isSelectable !== false)
|
|
31
|
+
.map((model) => ({
|
|
32
|
+
id: model.id,
|
|
33
|
+
label: model.label,
|
|
34
|
+
aliases: model.aliases,
|
|
35
|
+
isDefault: model.isDefault,
|
|
36
|
+
thinkingOptions: model.thinkingOptions?.map(toSelectOption),
|
|
37
|
+
defaultThinkingOptionId: model.defaultThinkingOptionId,
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let modes: SelectOption[] = agent.availableModes.map(toSelectOption);
|
|
42
|
+
if (needs.modes && modes.length === 0) {
|
|
43
|
+
const catalog = await paseo.providers.listModes(agent.provider, { cwd: agent.cwd });
|
|
44
|
+
modes = (catalog.modes ?? []).map(toSelectOption);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const features: Feature[] = (agent.features ?? []).map((feature) =>
|
|
48
|
+
feature.type === "toggle"
|
|
49
|
+
? { type: "toggle", id: feature.id, label: feature.label, value: feature.value }
|
|
50
|
+
: {
|
|
51
|
+
type: "select",
|
|
52
|
+
id: feature.id,
|
|
53
|
+
label: feature.label,
|
|
54
|
+
value: feature.value,
|
|
55
|
+
options: feature.options.map(toSelectOption),
|
|
56
|
+
},
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
agentId: agent.id,
|
|
61
|
+
provider: agent.provider,
|
|
62
|
+
status: agent.status,
|
|
63
|
+
modelId: agent.model ?? agent.runtimeInfo?.model ?? null,
|
|
64
|
+
thinkingOptionId:
|
|
65
|
+
agent.effectiveThinkingOptionId ??
|
|
66
|
+
agent.thinkingOptionId ??
|
|
67
|
+
agent.runtimeInfo?.thinkingOptionId ??
|
|
68
|
+
null,
|
|
69
|
+
modeId: agent.currentModeId ?? agent.runtimeInfo?.modeId ?? null,
|
|
70
|
+
models,
|
|
71
|
+
modes,
|
|
72
|
+
features,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function loadProfiles(paseo: Paseo): Promise<Profile[]> {
|
|
77
|
+
const { config } = await paseo.config.get();
|
|
78
|
+
return (config.agentProfiles ?? []) as Profile[];
|
|
79
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import type { PluginAgentCommandContext, PluginClientContext } from "@getpaseo/plugin/client";
|
|
2
|
+
import {
|
|
3
|
+
planCancel,
|
|
4
|
+
planEffort,
|
|
5
|
+
planFeature,
|
|
6
|
+
planMode,
|
|
7
|
+
planModel,
|
|
8
|
+
planProfile,
|
|
9
|
+
planRename,
|
|
10
|
+
type Plan,
|
|
11
|
+
} from "../shared/commands.ts";
|
|
12
|
+
import { agentControl } from "../shared/rpc.ts";
|
|
13
|
+
import { composeResendPrompt, latestUserPrompt } from "../shared/resend.ts";
|
|
14
|
+
import { loadAgentState, loadProfiles } from "./agent-state.ts";
|
|
15
|
+
import type { PickerHost, RunControl } from "./picker.tsx";
|
|
16
|
+
import { getDisabledCommands, subscribeDisabledCommands } from "./settings-store.ts";
|
|
17
|
+
|
|
18
|
+
type Context = PluginAgentCommandContext & { args: string };
|
|
19
|
+
|
|
20
|
+
/** Settings-page grouping. Order is the order of titled sections. */
|
|
21
|
+
export const COMMAND_SECTIONS = ["runtime", "session", "prompt"] as const;
|
|
22
|
+
|
|
23
|
+
export type CommandSection = (typeof COMMAND_SECTIONS)[number];
|
|
24
|
+
|
|
25
|
+
interface CommandMetadata {
|
|
26
|
+
name: string;
|
|
27
|
+
description: string;
|
|
28
|
+
/** Longer help shown on the settings screen. */
|
|
29
|
+
details: string;
|
|
30
|
+
argumentHint: string;
|
|
31
|
+
/** Which settings section this command belongs to. */
|
|
32
|
+
section: CommandSection;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type CommandSpec = CommandMetadata &
|
|
36
|
+
(
|
|
37
|
+
| { plan(context: Context): Promise<Plan>; execute?: never }
|
|
38
|
+
| { execute(context: Context): Promise<void>; plan?: never }
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
async function renameCurrent(ctx: Context): Promise<void> {
|
|
42
|
+
const request = planRename(ctx.args);
|
|
43
|
+
if (request.target === "workspace") {
|
|
44
|
+
await ctx.paseo.workspaces.ref(ctx.workspace.id).setTitle(request.title);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
await ctx.rpc(agentControl, { op: "rename", agentId: ctx.agent.id, title: request.title });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function resendLastPrompt(ctx: Context): Promise<void> {
|
|
51
|
+
const agent = ctx.paseo.agents.ref(ctx.agent.id);
|
|
52
|
+
let page = await agent.timeline.refetch({
|
|
53
|
+
direction: "tail",
|
|
54
|
+
projection: "projected",
|
|
55
|
+
limit: 100,
|
|
56
|
+
});
|
|
57
|
+
while (true) {
|
|
58
|
+
const prompt = latestUserPrompt(page.entries);
|
|
59
|
+
if (prompt) {
|
|
60
|
+
await agent.send(composeResendPrompt(prompt, ctx.args));
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (!page.hasOlder || !page.startCursor) {
|
|
64
|
+
throw new Error("No previous user prompt to resend.");
|
|
65
|
+
}
|
|
66
|
+
page = await agent.timeline.refetch({
|
|
67
|
+
direction: "before",
|
|
68
|
+
cursor: page.startCursor,
|
|
69
|
+
projection: "projected",
|
|
70
|
+
limit: 100,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const COMMANDS: readonly CommandSpec[] = [
|
|
76
|
+
{
|
|
77
|
+
name: "model",
|
|
78
|
+
description: "Show or switch the agent's model",
|
|
79
|
+
details:
|
|
80
|
+
"Pick from the provider's selectable models, or pass a model id or name. Matching is exact or by unique prefix.",
|
|
81
|
+
argumentHint: "[model]",
|
|
82
|
+
section: "runtime",
|
|
83
|
+
plan: async (ctx) =>
|
|
84
|
+
planModel(ctx.args, await loadAgentState(ctx.paseo, ctx.agent.id, { models: true })),
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
name: "effort",
|
|
88
|
+
description: "Show or set thinking effort",
|
|
89
|
+
details:
|
|
90
|
+
"Pick a thinking effort level of the current model, or pass one such as high.",
|
|
91
|
+
argumentHint: "[level]",
|
|
92
|
+
section: "runtime",
|
|
93
|
+
plan: async (ctx) =>
|
|
94
|
+
planEffort(ctx.args, await loadAgentState(ctx.paseo, ctx.agent.id, { models: true })),
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: "profile",
|
|
98
|
+
description: "Apply an agent profile's runtime settings",
|
|
99
|
+
details:
|
|
100
|
+
"Apply the model, mode, thinking and feature values of a saved agent profile. Fields that only apply at launch are skipped.",
|
|
101
|
+
argumentHint: "[profile]",
|
|
102
|
+
section: "runtime",
|
|
103
|
+
plan: async (ctx) => {
|
|
104
|
+
const [state, profiles] = await Promise.all([
|
|
105
|
+
loadAgentState(ctx.paseo, ctx.agent.id, { models: true, modes: true }),
|
|
106
|
+
loadProfiles(ctx.paseo),
|
|
107
|
+
]);
|
|
108
|
+
return planProfile(ctx.args, state, profiles);
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: "mode",
|
|
113
|
+
description: "Show or switch the agent's mode",
|
|
114
|
+
details:
|
|
115
|
+
"Pick a mode the agent supports, or pass one such as plan.",
|
|
116
|
+
argumentHint: "[mode]",
|
|
117
|
+
section: "runtime",
|
|
118
|
+
plan: async (ctx) =>
|
|
119
|
+
planMode(ctx.args, await loadAgentState(ctx.paseo, ctx.agent.id, { modes: true })),
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
name: "feature",
|
|
123
|
+
description: "Show or set a provider feature",
|
|
124
|
+
details:
|
|
125
|
+
"Browse provider features and their values, or pass a feature and value such as fast_mode on.",
|
|
126
|
+
argumentHint: "[name] [value]",
|
|
127
|
+
section: "runtime",
|
|
128
|
+
plan: async (ctx) => planFeature(ctx.args, await loadAgentState(ctx.paseo, ctx.agent.id)),
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
name: "rename",
|
|
132
|
+
description: "Rename the tab or workspace",
|
|
133
|
+
details:
|
|
134
|
+
"Rename the current agent tab. Pass -w or --workspace to rename the workspace instead. -t and --tab are the same as the default.",
|
|
135
|
+
argumentHint: "[-t|-w] <title>",
|
|
136
|
+
section: "session",
|
|
137
|
+
execute: renameCurrent,
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
name: "cancel",
|
|
141
|
+
description: "Cancel the agent's running turn",
|
|
142
|
+
details:
|
|
143
|
+
"Stop the current turn. Does nothing while the agent is idle.",
|
|
144
|
+
argumentHint: "",
|
|
145
|
+
section: "session",
|
|
146
|
+
plan: async (ctx) => planCancel(ctx.agent.id, ctx.agent.status),
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
name: "resend",
|
|
150
|
+
description: "Resend the last user prompt",
|
|
151
|
+
details:
|
|
152
|
+
"Send the latest non-empty user prompt again. Text after the command is appended on a new line.",
|
|
153
|
+
argumentHint: "[text]",
|
|
154
|
+
section: "prompt",
|
|
155
|
+
execute: resendLastPrompt,
|
|
156
|
+
},
|
|
157
|
+
];
|
|
158
|
+
|
|
159
|
+
function controlRunner(ctx: Context): RunControl {
|
|
160
|
+
return async (control, note) => {
|
|
161
|
+
const { notice } = await ctx.rpc(agentControl, control);
|
|
162
|
+
const warning = notice && notice.type !== "info" ? notice.message : null;
|
|
163
|
+
return [note, warning].filter(Boolean).join("\n") || null;
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function addCommand(client: PluginClientContext, pickers: PickerHost, command: CommandSpec) {
|
|
168
|
+
return client.addSlashCommand({
|
|
169
|
+
name: command.name,
|
|
170
|
+
description: command.description,
|
|
171
|
+
argumentHint: command.argumentHint,
|
|
172
|
+
context: "agent",
|
|
173
|
+
async onSubmit(ctx) {
|
|
174
|
+
if (command.execute) {
|
|
175
|
+
await command.execute(ctx);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const plan = await command.plan(ctx);
|
|
179
|
+
const run = controlRunner(ctx);
|
|
180
|
+
if (plan.kind === "picker") {
|
|
181
|
+
pickers.show({ workspaceId: ctx.agent.workspaceId, agentId: ctx.agent.id }, plan, run);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const message = await run(plan.control, plan.note);
|
|
185
|
+
if (message) throw new Error(message);
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Registers the enabled commands and re-registers them in order whenever the settings change. */
|
|
191
|
+
export function registerCommands(client: PluginClientContext, pickers: PickerHost) {
|
|
192
|
+
let removers: (() => void)[] = [];
|
|
193
|
+
const clear = () => {
|
|
194
|
+
for (const remove of removers) remove();
|
|
195
|
+
removers = [];
|
|
196
|
+
};
|
|
197
|
+
const sync = (disabled: ReadonlySet<string>) => {
|
|
198
|
+
clear();
|
|
199
|
+
removers = COMMANDS.filter((command) => !disabled.has(command.name)).map((command) =>
|
|
200
|
+
addCommand(client, pickers, command),
|
|
201
|
+
);
|
|
202
|
+
};
|
|
203
|
+
sync(getDisabledCommands());
|
|
204
|
+
const unsubscribe = subscribeDisabledCommands(sync);
|
|
205
|
+
return () => {
|
|
206
|
+
unsubscribe();
|
|
207
|
+
clear();
|
|
208
|
+
};
|
|
209
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import type { PluginTheme } from "@getpaseo/plugin";
|
|
2
|
+
import { Icon } from "@getpaseo/plugin/client/react-native";
|
|
3
|
+
import { useEffect, useMemo, useRef, type ReactElement } from "react";
|
|
4
|
+
import { Pressable, Text, View } from "react-native";
|
|
5
|
+
import type { PickerEntry } from "../shared/commands.ts";
|
|
6
|
+
|
|
7
|
+
export type PickerLeaf = Exclude<PickerEntry, { items: PickerEntry[] }>;
|
|
8
|
+
|
|
9
|
+
/** Paseo menu row heights: a thumb on compact layouts, a pointer on wide ones. */
|
|
10
|
+
const ROW_HEIGHT = { compact: 40, wide: 28 } as const;
|
|
11
|
+
const LINE_HEIGHT = 18;
|
|
12
|
+
|
|
13
|
+
interface KeyEvent {
|
|
14
|
+
key?: string;
|
|
15
|
+
nativeEvent?: { key?: string };
|
|
16
|
+
preventDefault?(): void;
|
|
17
|
+
stopPropagation?(): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type PressState = { pressed: boolean; hovered?: boolean; focused?: boolean };
|
|
21
|
+
|
|
22
|
+
function createStyles(theme: PluginTheme, compact: boolean) {
|
|
23
|
+
const { colors } = theme;
|
|
24
|
+
return {
|
|
25
|
+
page: { paddingVertical: 4 },
|
|
26
|
+
label: { paddingHorizontal: 12, paddingTop: 8, paddingBottom: 4 },
|
|
27
|
+
labelText: { fontSize: 12, color: colors.foregroundMuted },
|
|
28
|
+
item: {
|
|
29
|
+
flexDirection: "row" as const,
|
|
30
|
+
alignItems: "center" as const,
|
|
31
|
+
minHeight: compact ? ROW_HEIGHT.compact : ROW_HEIGHT.wide,
|
|
32
|
+
gap: 8,
|
|
33
|
+
marginHorizontal: 4,
|
|
34
|
+
paddingHorizontal: 8,
|
|
35
|
+
paddingVertical: 4,
|
|
36
|
+
borderWidth: 1,
|
|
37
|
+
borderColor: "transparent",
|
|
38
|
+
borderRadius: 6,
|
|
39
|
+
outlineWidth: 0,
|
|
40
|
+
outlineColor: "transparent",
|
|
41
|
+
},
|
|
42
|
+
itemFilled: { backgroundColor: colors.surface2 },
|
|
43
|
+
itemDisabled: { opacity: 0.5 },
|
|
44
|
+
content: { flexShrink: 1, minWidth: 0 },
|
|
45
|
+
text: {
|
|
46
|
+
fontSize: 14,
|
|
47
|
+
lineHeight: LINE_HEIGHT,
|
|
48
|
+
color: colors.foreground,
|
|
49
|
+
fontWeight: "400" as const,
|
|
50
|
+
},
|
|
51
|
+
trailing: {
|
|
52
|
+
marginLeft: "auto" as const,
|
|
53
|
+
alignItems: "center" as const,
|
|
54
|
+
justifyContent: "center" as const,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* One menu page, drawn with Paseo's menu row geometry. Groups render as a muted label above
|
|
61
|
+
* their rows. With `autoFocus` the first enabled row takes focus and arrow keys move it.
|
|
62
|
+
*/
|
|
63
|
+
export function MenuList({
|
|
64
|
+
items,
|
|
65
|
+
theme,
|
|
66
|
+
compact,
|
|
67
|
+
autoFocus,
|
|
68
|
+
onChoose,
|
|
69
|
+
onClose,
|
|
70
|
+
}: {
|
|
71
|
+
items: readonly PickerEntry[];
|
|
72
|
+
theme: PluginTheme;
|
|
73
|
+
compact: boolean;
|
|
74
|
+
autoFocus: boolean;
|
|
75
|
+
onChoose(entry: PickerLeaf): void;
|
|
76
|
+
onClose(): void;
|
|
77
|
+
}): ReactElement {
|
|
78
|
+
const styles = useMemo(() => createStyles(theme, compact), [theme, compact]);
|
|
79
|
+
const rowRefs = useRef(new Map<string, View | null>());
|
|
80
|
+
const focusedKey = useRef<string | null>(null);
|
|
81
|
+
|
|
82
|
+
const leaves = useMemo(() => {
|
|
83
|
+
const rows: { key: string; entry: PickerLeaf }[] = [];
|
|
84
|
+
for (const item of items) {
|
|
85
|
+
if (!("items" in item)) rows.push({ key: item.id, entry: item });
|
|
86
|
+
else for (const child of item.items) {
|
|
87
|
+
if (!("items" in child)) rows.push({ key: `${item.id}/${child.id}`, entry: child });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return rows;
|
|
91
|
+
}, [items]);
|
|
92
|
+
const enabled = useMemo(
|
|
93
|
+
() => leaves.filter(({ entry }) => entry.disabled !== true && entry.control),
|
|
94
|
+
[leaves],
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
if (!autoFocus) return undefined;
|
|
99
|
+
const first = enabled[0];
|
|
100
|
+
if (!first) return undefined;
|
|
101
|
+
const frame = requestAnimationFrame(() => rowRefs.current.get(first.key)?.focus());
|
|
102
|
+
return () => cancelAnimationFrame(frame);
|
|
103
|
+
}, [autoFocus, enabled]);
|
|
104
|
+
|
|
105
|
+
function onKeyDown(event: KeyEvent) {
|
|
106
|
+
const key = event.key ?? event.nativeEvent?.key;
|
|
107
|
+
if (key === "Escape") {
|
|
108
|
+
event.preventDefault?.();
|
|
109
|
+
event.stopPropagation?.();
|
|
110
|
+
onClose();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (enabled.length === 0) return;
|
|
114
|
+
const current = enabled.findIndex((row) => row.key === focusedKey.current);
|
|
115
|
+
let next: number | null = null;
|
|
116
|
+
if (key === "ArrowDown") next = current < 0 ? 0 : (current + 1) % enabled.length;
|
|
117
|
+
if (key === "ArrowUp") {
|
|
118
|
+
next = current < 0 ? enabled.length - 1 : (current - 1 + enabled.length) % enabled.length;
|
|
119
|
+
}
|
|
120
|
+
if (key === "Home") next = 0;
|
|
121
|
+
if (key === "End") next = enabled.length - 1;
|
|
122
|
+
if (next !== null) {
|
|
123
|
+
event.preventDefault?.();
|
|
124
|
+
event.stopPropagation?.();
|
|
125
|
+
const row = enabled[next];
|
|
126
|
+
if (row) rowRefs.current.get(row.key)?.focus();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if ((key === "Enter" || key === " ") && current >= 0) {
|
|
130
|
+
event.preventDefault?.();
|
|
131
|
+
event.stopPropagation?.();
|
|
132
|
+
const row = enabled[current];
|
|
133
|
+
if (row) onChoose(row.entry);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function renderRow(key: string, entry: PickerLeaf) {
|
|
138
|
+
const disabled = entry.disabled === true || !entry.control;
|
|
139
|
+
return (
|
|
140
|
+
<Pressable
|
|
141
|
+
key={key}
|
|
142
|
+
ref={(node) => {
|
|
143
|
+
rowRefs.current.set(key, node);
|
|
144
|
+
}}
|
|
145
|
+
accessibilityRole="menuitem"
|
|
146
|
+
accessibilityState={{ disabled, checked: entry.current === true }}
|
|
147
|
+
tabIndex={-1}
|
|
148
|
+
disabled={disabled}
|
|
149
|
+
onFocus={() => {
|
|
150
|
+
focusedKey.current = key;
|
|
151
|
+
}}
|
|
152
|
+
onPress={() => onChoose(entry)}
|
|
153
|
+
style={(state) => {
|
|
154
|
+
const { pressed, hovered = false, focused = false } = state as PressState;
|
|
155
|
+
return [
|
|
156
|
+
styles.item,
|
|
157
|
+
!disabled && (hovered || focused || pressed) ? styles.itemFilled : null,
|
|
158
|
+
disabled ? styles.itemDisabled : null,
|
|
159
|
+
];
|
|
160
|
+
}}
|
|
161
|
+
>
|
|
162
|
+
<View style={styles.content}>
|
|
163
|
+
<Text numberOfLines={1} style={styles.text}>
|
|
164
|
+
{entry.title}
|
|
165
|
+
</Text>
|
|
166
|
+
</View>
|
|
167
|
+
{entry.current ? (
|
|
168
|
+
<View style={styles.trailing}>
|
|
169
|
+
<Icon name="Check" size={16} color={theme.colors.foregroundMuted} />
|
|
170
|
+
</View>
|
|
171
|
+
) : null}
|
|
172
|
+
</Pressable>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return (
|
|
177
|
+
<View style={styles.page} {...{ onKeyDown }}>
|
|
178
|
+
{items.map((item) =>
|
|
179
|
+
"items" in item ? (
|
|
180
|
+
<View key={item.id}>
|
|
181
|
+
<View style={styles.label}>
|
|
182
|
+
<Text style={styles.labelText}>{item.title}</Text>
|
|
183
|
+
</View>
|
|
184
|
+
{item.items.map((child) =>
|
|
185
|
+
"items" in child ? null : renderRow(`${item.id}/${child.id}`, child),
|
|
186
|
+
)}
|
|
187
|
+
</View>
|
|
188
|
+
) : (
|
|
189
|
+
renderRow(item.id, item)
|
|
190
|
+
),
|
|
191
|
+
)}
|
|
192
|
+
</View>
|
|
193
|
+
);
|
|
194
|
+
}
|