Right-click context menus on file panels

Native menus (electrobun ContextMenu) on rows and empty panel space. Rows:
Open (dirs navigate; local files open with the default app via Utils.openPath),
Edit, Copy to Other Panel, Rename, Permissions, Copy Path (works for remote
paths too), Reveal in Finder (local, Utils.showItemInFolder), Move to Trash
(local, Utils.moveToTrash — tooltip notes Finder Restore doesn't apply) and
Delete. Empty space / '..': New Folder, New File, Refresh, Reveal in Finder.

Right-click parks the cursor on the row (keeping the selection when clicking
inside it). Local shell actions run bun-side directly; everything else routes
through the existing menuAction channel back to the stored click target.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jokin
2026-07-11 23:48:23 +02:00
co-authored by Claude Fable 5
parent 9737cad6de
commit fbbde16f35
8 changed files with 226 additions and 3 deletions
+91
View File
@@ -0,0 +1,91 @@
import { ContextMenu, Utils, type ApplicationMenuItemConfig } from "electrobun/bun";
export interface RowMenuContext {
path: string;
name: string;
type: "file" | "dir" | "symlink";
isLocal: boolean;
hasEntry: boolean; // false = right-click on empty panel space
}
/**
* Native right-click menus for the file panels. Local-only items run directly
* here via Utils (reveal, open with default app, copy path); everything else
* forwards to the webview through the menuAction channel — the UI kept its own
* context (which panel/row was clicked) when it requested the menu.
*/
export class PanelContextMenu {
private context: RowMenuContext | null = null;
/** Forwards actions the UI must handle (ctx-open, ctx-delete, ...). */
onAction: (action: string) => void = () => {};
constructor() {
ContextMenu.on("context-menu-clicked", (event) => {
const action = (event as { data?: { action?: string } }).data?.action;
const ctx = this.context;
if (!action || !ctx) return;
switch (action) {
case "ctx-reveal":
Utils.showItemInFolder(ctx.path);
break;
case "ctx-open-default":
Utils.openPath(ctx.path);
break;
case "ctx-copy-path":
Utils.clipboardWriteText(ctx.path);
break;
default:
if (action.startsWith("ctx-")) this.onAction(action);
}
});
}
show(context: RowMenuContext): void {
this.context = context;
ContextMenu.showContextMenu(this.build(context));
}
private build(ctx: RowMenuContext): ApplicationMenuItemConfig[] {
const items: ApplicationMenuItemConfig[] = [];
if (!ctx.hasEntry || ctx.name === "..") {
// empty space (or the parent row): folder-level actions
items.push({ label: "New Folder", action: "ctx-new-folder" });
items.push({ label: "New File", action: "ctx-new-file" });
items.push({ type: "separator" });
items.push({ label: "Refresh", action: "ctx-refresh" });
if (ctx.isLocal) {
items.push({ type: "separator" });
items.push({ label: "Reveal in Finder", action: "ctx-reveal" });
}
return items;
}
if (ctx.type === "dir") {
items.push({ label: "Open", action: "ctx-open" });
} else {
if (ctx.isLocal) {
items.push({ label: "Open", action: "ctx-open-default", tooltip: "Open with the default application" });
}
items.push({ label: "Edit", action: "ctx-edit", accelerator: "e" });
}
items.push({ label: "Copy to Other Panel", action: "ctx-copy" });
items.push({ label: "Rename", action: "ctx-rename" });
items.push({ label: "Permissions", action: "ctx-chmod" });
items.push({ type: "separator" });
items.push({ label: "Copy Path", action: "ctx-copy-path" });
if (ctx.isLocal) {
items.push({ label: "Reveal in Finder", action: "ctx-reveal" });
}
items.push({ type: "separator" });
if (ctx.isLocal) {
items.push({
label: "Move to Trash",
action: "ctx-trash",
tooltip: "Finder's Restore is not available for items trashed from here",
});
}
items.push({ label: "Delete", action: "ctx-delete" });
return items;
}
}
+4 -1
View File
@@ -1,6 +1,7 @@
import { ApplicationMenu, BrowserWindow, Updater } from "electrobun/bun";
import { BRANDING } from "../shared/branding";
import { effectiveConcurrency } from "../shared/types";
import { PanelContextMenu } from "./context-menu";
import { EditManager } from "./edits";
import { SessionRegistry } from "./fs/registry";
import { applyApplicationMenu } from "./menu";
@@ -35,11 +36,12 @@ const engine = new TransferEngine(registry);
const store = new ConnectionStore();
const settings = new SettingsStore();
const edits = new EditManager(registry, engine, settings);
const contextMenu = new PanelContextMenu();
const events = {
sessionClosed: (_info: { sessionId: string; reason: string }) => {},
connectionsChanged: () => {},
};
const rpc = createAppRPC({ registry, engine, store, settings, edits, events });
const rpc = createAppRPC({ registry, engine, store, settings, edits, contextMenu, events });
const url = await getMainViewUrl();
const isMac = process.platform === "darwin";
@@ -65,6 +67,7 @@ const win = new BrowserWindow({
});
new AppTray(win);
contextMenu.onAction = (action) => rpc.send.menuAction({ action });
engine.onUpdate = (snapshot) => rpc.send.transferUpdate(snapshot);
events.sessionClosed = (info) => rpc.send.sessionClosed(info);
+24 -1
View File
@@ -1,6 +1,7 @@
import { BrowserView, Utils } from "electrobun/bun";
import type { AppRPC } from "../shared/rpc";
import { effectiveConcurrency, type Result } from "../shared/types";
import type { PanelContextMenu } from "./context-menu";
import { readFileText, writeFileText, type EditManager } from "./edits";
import type { SessionRegistry } from "./fs/registry";
import { SftpProvider } from "./fs/sftp";
@@ -19,6 +20,7 @@ export interface AppDeps {
store: ConnectionStore;
settings: SettingsStore;
edits: EditManager;
contextMenu: PanelContextMenu;
/** Assigned after the rpc object exists (it needs rpc.send). */
events: {
sessionClosed: (info: { sessionId: string; reason: string }) => void;
@@ -34,7 +36,15 @@ async function wrap<T extends object>(fn: () => Promise<T> | T): Promise<Result<
}
}
export function createAppRPC({ registry, engine, store, settings, edits, events }: AppDeps) {
export function createAppRPC({
registry,
engine,
store,
settings,
edits,
contextMenu,
events,
}: AppDeps) {
return BrowserView.defineRPC<AppRPC>({
maxRequestTime: MAX_REQUEST_TIME_MS,
handlers: {
@@ -201,6 +211,19 @@ export function createAppRPC({ registry, engine, store, settings, edits, events
await edits.clearLeftovers();
return {};
}),
// ---- context menu + local shell integration ----
showRowMenu: (params) =>
wrap(async () => {
contextMenu.show(params);
return {};
}),
moveToTrash: ({ path }) =>
wrap(async () => {
Utils.moveToTrash(path);
return {};
}),
},
},
});
+12
View File
@@ -96,6 +96,18 @@ export type AppRPC = {
response: Result<{ names: string[] }>;
};
clearEditLeftovers: { params: Record<string, never>; response: Result };
// context menu + local shell integration
showRowMenu: {
params: {
path: string;
name: string;
type: EntryType;
isLocal: boolean;
hasEntry: boolean;
};
response: Result;
};
moveToTrash: { params: { path: string }; response: Result };
};
messages: Record<never, never>;
};