The keymap was scattered over ShortcutsService if-chains, the file panel's key switch, macOS-only menu accelerators and hand-typed hints in 16 tooltips x 7 locales, and large parts of the app had no keyboard path at all. src/shared/keymap.ts is now the one table: scopes, per-platform defaults, exact matching, formatting with localized key names, conflicts, reserved combos and native-menu accelerators, proven by keymap.spec.ts. - ShortcutsService resolves against the effective keymap: claim() takes the key (preventDefault, held keys repeat only movement, never mid-IME), and from a text field only chords and F-keys fire. The file panel, App, editor, manager and Settings windows all dispatch through it. - FileActionsService.run is the one file-command dispatcher for the list keys, the row menu, the macOS menu bar and the toolbar. - New keys: the row menu (Shift+F10 / Menu / Ctrl+Return), the source picker (Mod+K), the path bar (Mod+L), transfers (Mod+J), sorting (Alt+1-7), Shift+movement selection, close other tabs, move and disconnect tabs, refresh and edit on Windows/Linux, permissions, copy path, reveal, trash, open in default app, editor find next/previous. - Right-click menus: soft shortcut hints, icons on the everyday verbs (the toolbar's glyphs), section headers, scrolling, item keys while open, a highlighted first item when opened from the keyboard. The panel source picker now renders through the same menu. - Dialogs: role=dialog, a Tab trap via cycleFocus (WKWebView skips buttons on Tab without Keyboard navigation), initial focus, Esc that stops propagating; the chmod dialog takes focus and applies on Enter. - Fixed: Ctrl+Tab also flipped the active panel; Esc in a manager dialog closed the manager; Ctrl-F/T fired Cmd shortcuts on macOS; shortcuts fired under dialogs; keys died after any button click on macOS; the editor's save prompt and find box never got focus; WebView2 reload keys; a window could save default settings before its first load. - Settings closes on its own close-window key (new settingsClose request). Co-Authored-By: Claude <noreply@anthropic.com>
497 lines
14 KiB
TypeScript
497 lines
14 KiB
TypeScript
import { BrowserView, Updater, Utils } from "electrobun/main";
|
|
import type { AppRPC, EditorOpenContext } from "../shared/rpc";
|
|
import type { AppSettings, Result } from "../shared/types";
|
|
import { readFileText, writeFileText, type EditManager } from "./edits";
|
|
import { listLocalDrives } from "./fs/local";
|
|
import type { SessionRegistry } from "./fs/registry";
|
|
import type { LocalDirWatcher } from "./fs/watcher";
|
|
import { SftpProvider } from "./fs/sftp";
|
|
import type { ConnectionStore } from "./store/connections";
|
|
import type { SettingsStore } from "./store/settings";
|
|
import { loadSshConfigHosts } from "./store/ssh-config";
|
|
import { importWinScpIni } from "./store/winscp-import";
|
|
import type { TransferEngine } from "./transfers/engine";
|
|
import { removeRecursive } from "./transfers/walk";
|
|
|
|
// SFTP ops can be slow (big listings, recursive deletes) — the 1s electrobun
|
|
// default would cut them off.
|
|
const MAX_REQUEST_TIME_MS = 10 * 60_000;
|
|
|
|
export interface AppDeps {
|
|
registry: SessionRegistry;
|
|
engine: TransferEngine;
|
|
store: ConnectionStore;
|
|
settings: SettingsStore;
|
|
edits: EditManager;
|
|
watcher: LocalDirWatcher;
|
|
/** Assigned after the rpc object exists (it needs rpc.send). */
|
|
events: {
|
|
sessionClosed: (info: { sessionId: string; reason: string }) => void;
|
|
connectionsChanged: () => void;
|
|
openEditor: (ctx: EditorOpenContext) => void;
|
|
settingsChanged: (settings: AppSettings) => void;
|
|
openConnectionManager: () => void;
|
|
openSettings: () => void;
|
|
showAbout: () => void;
|
|
/** "Restart to apply" — lives next to the before-quit guard it must get past. */
|
|
applyUpdate: () => Promise<void>;
|
|
};
|
|
}
|
|
|
|
async function wrap<T extends object>(fn: () => Promise<T> | T): Promise<Result<T>> {
|
|
try {
|
|
return { ok: true, ...(await fn()) };
|
|
} catch (error) {
|
|
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
}
|
|
}
|
|
|
|
/** Connection-store requests, shared by the main and manager window rpcs. */
|
|
function connectionStoreHandlers(store: ConnectionStore, onChanged: () => void) {
|
|
return {
|
|
getConnections: () => wrap(async () => ({ store: await store.load() })),
|
|
|
|
upsertConnection: ({ connection }: { connection: Parameters<ConnectionStore["upsertConnection"]>[0] }) =>
|
|
wrap(async () => {
|
|
const id = await store.upsertConnection(connection);
|
|
onChanged();
|
|
return { id };
|
|
}),
|
|
|
|
deleteConnection: ({ id }: { id: string }) =>
|
|
wrap(async () => {
|
|
await store.deleteConnection(id);
|
|
onChanged();
|
|
return {};
|
|
}),
|
|
|
|
upsertStack: ({ stack }: { stack: Parameters<ConnectionStore["upsertStack"]>[0] }) =>
|
|
wrap(async () => {
|
|
const id = await store.upsertStack(stack);
|
|
onChanged();
|
|
return { id };
|
|
}),
|
|
|
|
deleteStack: ({ id }: { id: string }) =>
|
|
wrap(async () => {
|
|
await store.deleteStack(id);
|
|
onChanged();
|
|
return {};
|
|
}),
|
|
|
|
importWinScp: () =>
|
|
wrap(async () => {
|
|
const { homedir } = await import("node:os");
|
|
const picked = await Utils.openFileDialog({
|
|
startingFolder: homedir(),
|
|
allowedFileTypes: "*",
|
|
canChooseFiles: true,
|
|
canChooseDirectory: false,
|
|
allowsMultipleSelection: false,
|
|
});
|
|
const file = picked.find((p) => p.trim().length > 0);
|
|
// dialog cancelled — all-zero result, UI shows nothing
|
|
if (!file) return { connections: 0, stacks: 0, skipped: 0 };
|
|
const text = await (await import("node:fs/promises")).readFile(file, "utf8");
|
|
const result = await importWinScpIni(text, store);
|
|
onChanged();
|
|
return result;
|
|
}),
|
|
|
|
getSshConfigHosts: () => wrap(async () => ({ hosts: loadSshConfigHosts() })),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* saveSettings for every window kind: persist, then hand the new object to the
|
|
* composition root (index.ts), which applies the side effects once and
|
|
* rebroadcasts to every OTHER window. Known ceiling: whole-object writes — two
|
|
* windows saving in the same instant, the later one wins. Upgrade path: send a
|
|
* patch and merge it bun-side.
|
|
*/
|
|
const saveSettingsHandler =
|
|
(store: SettingsStore, onSaved: (next: AppSettings) => void) =>
|
|
({ settings: next }: { settings: AppSettings }) =>
|
|
wrap(async () => {
|
|
await store.save(next);
|
|
onSaved(next);
|
|
return {};
|
|
});
|
|
|
|
const pickKeyFileHandler = () =>
|
|
wrap(async () => {
|
|
const { homedir } = await import("node:os");
|
|
const { join } = await import("node:path");
|
|
const picked = await Utils.openFileDialog({
|
|
startingFolder: join(homedir(), ".ssh"),
|
|
allowedFileTypes: "*",
|
|
canChooseFiles: true,
|
|
canChooseDirectory: false,
|
|
allowsMultipleSelection: false,
|
|
});
|
|
const path = picked.find((p) => p.trim().length > 0) ?? null;
|
|
return { path };
|
|
});
|
|
|
|
/** External-editor picker, shared by the main and settings window rpcs. */
|
|
const pickEditorAppHandler = () =>
|
|
wrap(async () => {
|
|
// PORTABILITY: apps live in different places per OS
|
|
const { homedir } = await import("node:os");
|
|
const [startingFolder, allowedFileTypes] =
|
|
process.platform === "darwin"
|
|
? ["/Applications", "app"]
|
|
: process.platform === "win32"
|
|
? [process.env.ProgramFiles ?? "C:\\Program Files", "exe"]
|
|
: [homedir(), "*"];
|
|
const picked = await Utils.openFileDialog({
|
|
startingFolder,
|
|
allowedFileTypes,
|
|
canChooseFiles: true,
|
|
canChooseDirectory: false,
|
|
allowsMultipleSelection: false,
|
|
});
|
|
const path = picked.find((p) => p.trim().length > 0) ?? null;
|
|
return { path };
|
|
});
|
|
|
|
export function createAppRPC({
|
|
registry,
|
|
engine,
|
|
store,
|
|
settings,
|
|
edits,
|
|
watcher,
|
|
events,
|
|
}: AppDeps) {
|
|
return BrowserView.defineRPC<AppRPC>({
|
|
maxRequestTime: MAX_REQUEST_TIME_MS,
|
|
handlers: {
|
|
requests: {
|
|
// ---- sessions ----
|
|
connect: ({ config }) =>
|
|
wrap(async () => {
|
|
let sessionId = "";
|
|
const provider = await SftpProvider.connect(config, (reason) => {
|
|
if (!sessionId) return;
|
|
registry.drop(sessionId);
|
|
engine.failSession(sessionId, reason);
|
|
edits.stopForSession(sessionId);
|
|
events.sessionClosed({ sessionId, reason });
|
|
});
|
|
sessionId = registry.add(provider);
|
|
const homeDir = await provider.homeDir();
|
|
console.log(`[rpc] connected ${sessionId} -> ${config.username}@${config.host}`);
|
|
return { sessionId, homeDir, fsKey: provider.fsKey };
|
|
}),
|
|
|
|
disconnect: ({ sessionId }) =>
|
|
wrap(async () => {
|
|
edits.stopForSession(sessionId);
|
|
await registry.remove(sessionId);
|
|
console.log(`[rpc] disconnected ${sessionId}`);
|
|
return {};
|
|
}),
|
|
|
|
// ---- filesystem ----
|
|
listDir: ({ sessionId, path }) =>
|
|
wrap(async () => {
|
|
const fs = registry.get(sessionId);
|
|
const dir = fs.normalize(path ?? (await fs.homeDir()));
|
|
return { path: dir, entries: await fs.list(dir) };
|
|
}),
|
|
|
|
setWatchedDirs: ({ dirs }) =>
|
|
wrap(async () => {
|
|
watcher.setWatched(dirs);
|
|
return {};
|
|
}),
|
|
|
|
mkdir: ({ sessionId, path }) =>
|
|
wrap(async () => {
|
|
await registry.get(sessionId).mkdir(path);
|
|
return {};
|
|
}),
|
|
|
|
createFile: ({ sessionId, path }) =>
|
|
wrap(async () => {
|
|
await registry.get(sessionId).createEmptyFile(path);
|
|
return {};
|
|
}),
|
|
|
|
rename: ({ sessionId, oldPath, newPath }) =>
|
|
wrap(async () => {
|
|
await registry.get(sessionId).rename(oldPath, newPath);
|
|
return {};
|
|
}),
|
|
|
|
deleteEntries: ({ sessionId, entries }) =>
|
|
wrap(async () => {
|
|
const fs = registry.get(sessionId);
|
|
for (const { path, type } of entries) {
|
|
await removeRecursive(fs, path, type);
|
|
}
|
|
return {};
|
|
}),
|
|
|
|
chmod: ({ sessionId, paths, mode }) =>
|
|
wrap(async () => {
|
|
const fs = registry.get(sessionId);
|
|
for (const path of paths) {
|
|
await fs.chmod(path, mode);
|
|
}
|
|
return {};
|
|
}),
|
|
|
|
// ---- transfers ----
|
|
enqueueTransfers: (params) =>
|
|
wrap(async () => ({ jobIds: engine.enqueue(params) })),
|
|
|
|
cancelTransfer: ({ jobId }) =>
|
|
wrap(async () => {
|
|
engine.cancel(jobId);
|
|
return {};
|
|
}),
|
|
|
|
dismissTransfer: ({ jobId }) =>
|
|
wrap(async () => {
|
|
engine.dismiss(jobId);
|
|
return {};
|
|
}),
|
|
|
|
// ---- connection store ----
|
|
...connectionStoreHandlers(store, () => events.connectionsChanged()),
|
|
|
|
// ---- settings ----
|
|
getSettings: () => wrap(async () => ({ settings: await settings.load() })),
|
|
|
|
saveSettings: saveSettingsHandler(settings, (next) => events.settingsChanged(next)),
|
|
|
|
// ---- file editing ----
|
|
readFile: ({ sessionId, path }) =>
|
|
wrap(async () => ({
|
|
content: await readFileText(registry.get(sessionId), path),
|
|
})),
|
|
|
|
writeFile: ({ sessionId, path, content }) =>
|
|
wrap(async () => {
|
|
await writeFileText(registry.get(sessionId), path, content);
|
|
return {};
|
|
}),
|
|
|
|
editExternal: ({ sessionId, path }) =>
|
|
wrap(() => edits.editExternal(sessionId, path)),
|
|
|
|
pickEditorApp: pickEditorAppHandler,
|
|
|
|
pickKeyFile: pickKeyFileHandler,
|
|
|
|
getEditLeftovers: () =>
|
|
wrap(async () => ({ names: await edits.listLeftovers() })),
|
|
|
|
clearEditLeftovers: () =>
|
|
wrap(async () => {
|
|
await edits.clearLeftovers();
|
|
return {};
|
|
}),
|
|
|
|
// ---- local shell integration (menu itself is HTML, see ui/) ----
|
|
shellItemAction: ({ action, path }) =>
|
|
wrap(async () => {
|
|
if (action === "reveal") Utils.showItemInFolder(path);
|
|
else if (action === "open") Utils.openPath(path);
|
|
else Utils.clipboardWriteText(path);
|
|
return {};
|
|
}),
|
|
|
|
moveToTrash: ({ path }) =>
|
|
wrap(async () => {
|
|
Utils.moveToTrash(path);
|
|
return {};
|
|
}),
|
|
|
|
// ---- editor windows ----
|
|
openEditor: (ctx) =>
|
|
wrap(async () => {
|
|
events.openEditor(ctx);
|
|
return {};
|
|
}),
|
|
|
|
// ---- connection-manager window ----
|
|
openConnectionManager: () =>
|
|
wrap(async () => {
|
|
events.openConnectionManager();
|
|
return {};
|
|
}),
|
|
|
|
// ---- settings window ----
|
|
openSettings: () =>
|
|
wrap(async () => {
|
|
events.openSettings();
|
|
return {};
|
|
}),
|
|
|
|
showAbout: () =>
|
|
wrap(async () => {
|
|
events.showAbout();
|
|
return {};
|
|
}),
|
|
|
|
listDrives: () => wrap(async () => ({ drives: listLocalDrives() })),
|
|
|
|
// ---- auto-updater ----
|
|
checkForUpdate: () =>
|
|
wrap(async () => {
|
|
const r = await Updater.checkForUpdate();
|
|
return { updateAvailable: r.updateAvailable, version: r.version };
|
|
}),
|
|
|
|
downloadUpdate: () =>
|
|
wrap(async () => {
|
|
await Updater.downloadUpdate();
|
|
return {};
|
|
}),
|
|
|
|
applyUpdate: () =>
|
|
wrap(async () => {
|
|
await events.applyUpdate(); // relaunches + quits; may never resolve
|
|
return {};
|
|
}),
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export interface EditorRpcHooks {
|
|
ctx: EditorOpenContext;
|
|
/** Draft mirror tick: store dirty flag + content on the window record. */
|
|
onDraft: (state: { dirty: boolean; content: string }) => void;
|
|
/** editorSave succeeded: clean the record, retitle, refresh main panels. */
|
|
onSaved: () => void;
|
|
/** Webview resolved its dirty prompt: close without the native fallback. */
|
|
onCloseConfirmed: () => void;
|
|
/** This window saved settings (e.g. the scale shortcuts) — rebroadcast them. */
|
|
onSettingsSaved: (next: AppSettings) => void;
|
|
}
|
|
|
|
/**
|
|
* RPC for one editor window. Each window MUST get its own instance (binding a
|
|
* BrowserView steals the rpc's transport), and the hooks close over that
|
|
* window's record — no ids on the wire.
|
|
*/
|
|
export function createEditorRPC(
|
|
deps: { registry: SessionRegistry; settings: SettingsStore },
|
|
hooks: EditorRpcHooks,
|
|
) {
|
|
return BrowserView.defineRPC<AppRPC>({
|
|
maxRequestTime: MAX_REQUEST_TIME_MS,
|
|
handlers: {
|
|
requests: {
|
|
readFile: ({ sessionId, path }) =>
|
|
wrap(async () => ({
|
|
content: await readFileText(deps.registry.get(sessionId), path),
|
|
})),
|
|
|
|
getSettings: () => wrap(async () => ({ settings: await deps.settings.load() })),
|
|
saveSettings: saveSettingsHandler(deps.settings, hooks.onSettingsSaved),
|
|
|
|
editorSave: ({ content }) =>
|
|
wrap(async () => {
|
|
const { sessionId, path } = hooks.ctx;
|
|
await writeFileText(deps.registry.get(sessionId), path, content);
|
|
hooks.onSaved();
|
|
return {};
|
|
}),
|
|
|
|
editorClose: () =>
|
|
wrap(async () => {
|
|
hooks.onCloseConfirmed();
|
|
return {};
|
|
}),
|
|
},
|
|
messages: {
|
|
editorState: (state) => hooks.onDraft(state),
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export interface ManagerRpcHooks {
|
|
/** Store changed: rebuild the app menu + tell the main window to reload. */
|
|
onChanged: () => void;
|
|
/** Connect / open-stack execute in the MAIN window's workspace. */
|
|
onConnect: (id: string) => void;
|
|
onOpenStack: (id: string) => void;
|
|
onClose: () => void;
|
|
/** This window saved settings (e.g. the scale shortcuts) — rebroadcast them. */
|
|
onSettingsSaved: (next: AppSettings) => void;
|
|
}
|
|
|
|
/** RPC for the single connection-manager window (per-window instance). */
|
|
export function createManagerRPC(
|
|
deps: { store: ConnectionStore; settings: SettingsStore },
|
|
hooks: ManagerRpcHooks,
|
|
) {
|
|
return BrowserView.defineRPC<AppRPC>({
|
|
maxRequestTime: MAX_REQUEST_TIME_MS,
|
|
handlers: {
|
|
requests: {
|
|
...connectionStoreHandlers(deps.store, hooks.onChanged),
|
|
pickKeyFile: pickKeyFileHandler,
|
|
getSettings: () => wrap(async () => ({ settings: await deps.settings.load() })),
|
|
saveSettings: saveSettingsHandler(deps.settings, hooks.onSettingsSaved),
|
|
|
|
managerConnect: ({ id }) =>
|
|
wrap(async () => {
|
|
hooks.onConnect(id);
|
|
return {};
|
|
}),
|
|
|
|
managerOpenStack: ({ id }) =>
|
|
wrap(async () => {
|
|
hooks.onOpenStack(id);
|
|
return {};
|
|
}),
|
|
|
|
managerClose: () =>
|
|
wrap(async () => {
|
|
hooks.onClose();
|
|
return {};
|
|
}),
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export interface SettingsRpcHooks {
|
|
/** Every edit here is a settings save — rebroadcast it to the other windows. */
|
|
onSettingsSaved: (next: AppSettings) => void;
|
|
showAbout: () => void;
|
|
/** The window's own close-window key. */
|
|
close: () => void;
|
|
}
|
|
|
|
/** RPC for the single Settings window (per-window instance). */
|
|
export function createSettingsRPC(deps: { settings: SettingsStore }, hooks: SettingsRpcHooks) {
|
|
return BrowserView.defineRPC<AppRPC>({
|
|
maxRequestTime: MAX_REQUEST_TIME_MS,
|
|
handlers: {
|
|
requests: {
|
|
getSettings: () => wrap(async () => ({ settings: await deps.settings.load() })),
|
|
saveSettings: saveSettingsHandler(deps.settings, hooks.onSettingsSaved),
|
|
pickEditorApp: pickEditorAppHandler,
|
|
showAbout: () =>
|
|
wrap(async () => {
|
|
hooks.showAbout();
|
|
return {};
|
|
}),
|
|
settingsClose: () =>
|
|
wrap(async () => {
|
|
hooks.close();
|
|
return {};
|
|
}),
|
|
},
|
|
},
|
|
});
|
|
}
|