Add open-in-editor feature with Cmd+O shortcut
Add dropdown in header to open workspace in Cursor or system file manager (Finder/Explorer/Files). Include Cmd+O/Ctrl+O global keyboard shortcut to open in last-used editor, persisted to localStorage. Co-Authored-By: Claude Haiku 4.5 <[email protected]>
This commit is contained in:
1 parent
153a130a09
commit
3303d9a7fa
4 files changed
+126
-1
No files matched your search
@@ -3,7 +3,14 @@ fixPath();
|
||||
|
||||
import { spawn } from "node:child_process";
|
||||
import path from "node:path";
|
||||
import { BrowserWindow, app, dialog, ipcMain, session } from "electron";
|
||||
import {
|
||||
BrowserWindow,
|
||||
app,
|
||||
dialog,
|
||||
ipcMain,
|
||||
session,
|
||||
shell,
|
||||
} from "electron";
|
||||
|
||||
import {
|
||||
IPC_CHANNELS,
|
||||
@@ -125,6 +132,27 @@ function registerIpcHandlers(): void {
|
||||
}),
|
||||
);
|
||||
|
||||
// Shell handlers
|
||||
ipcMain.handle(
|
||||
IPC_CHANNELS.shellOpenInEditor,
|
||||
async (_event, cwd: string, editor: string) => {
|
||||
if (editor === "file-manager") {
|
||||
await shell.openPath(cwd);
|
||||
return;
|
||||
}
|
||||
const EDITOR_COMMANDS: Record<string, { command: string; args: (cwd: string) => string[] }> = {
|
||||
cursor: { command: "cursor", args: (p) => [p] },
|
||||
};
|
||||
const entry = EDITOR_COMMANDS[editor];
|
||||
if (!entry) throw new Error(`Unknown editor: ${editor}`);
|
||||
const child = spawn(entry.command, entry.args(cwd), {
|
||||
detached: true,
|
||||
stdio: "ignore",
|
||||
});
|
||||
child.unref();
|
||||
},
|
||||
);
|
||||
|
||||
// Agent handlers
|
||||
ipcMain.handle(
|
||||
IPC_CHANNELS.agentSpawn,
|
||||
|
||||
@@ -52,6 +52,10 @@ const nativeApi: NativeApi = {
|
||||
ipcRenderer.removeListener(IPC_CHANNELS.providerEvent, listener);
|
||||
},
|
||||
},
|
||||
shell: {
|
||||
openInEditor: (cwd: string, editor: string) =>
|
||||
ipcRenderer.invoke(IPC_CHANNELS.shellOpenInEditor, cwd, editor),
|
||||
},
|
||||
};
|
||||
|
||||
contextBridge.exposeInMainWorld("nativeApi", nativeApi);
|
||||
@@ -30,6 +30,18 @@ function formatMessageMeta(createdAt: string, duration: string | null): string {
|
||||
return `${formatTimestamp(createdAt)} • ${duration}`;
|
||||
}
|
||||
|
||||
const FILE_MANAGER_LABEL = navigator.platform.includes("Mac")
|
||||
? "Finder"
|
||||
: navigator.platform.includes("Win")
|
||||
? "Explorer"
|
||||
: "Files";
|
||||
|
||||
const EDITORS = [
|
||||
{ id: "cursor", label: "Cursor" },
|
||||
{ id: "file-manager", label: FILE_MANAGER_LABEL },
|
||||
] as const;
|
||||
const LAST_EDITOR_KEY = "codething:last-editor";
|
||||
|
||||
function statusLabel(phase: string): string {
|
||||
if (phase === "running") return "Thinking / working";
|
||||
if (phase === "connecting") return "Connecting";
|
||||
@@ -51,12 +63,17 @@ export default function ChatView() {
|
||||
const [isSending, setIsSending] = useState(false);
|
||||
const [isConnecting, setIsConnecting] = useState(false);
|
||||
const [isModelMenuOpen, setIsModelMenuOpen] = useState(false);
|
||||
const [isEditorMenuOpen, setIsEditorMenuOpen] = useState(false);
|
||||
const [lastEditor, setLastEditor] = useState(
|
||||
() => localStorage.getItem(LAST_EDITOR_KEY) ?? EDITORS[0].id,
|
||||
);
|
||||
const [selectedEffort, setSelectedEffort] =
|
||||
useState<string>(DEFAULT_REASONING);
|
||||
const [nowTick, setNowTick] = useState(() => Date.now());
|
||||
const messagesEndRef = useRef<HTMLDivElement>(null);
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||
const modelMenuRef = useRef<HTMLDivElement>(null);
|
||||
const editorMenuRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const activeThread = state.threads.find((t) => t.id === state.activeThreadId);
|
||||
const activeProject = state.projects.find(
|
||||
@@ -144,6 +161,47 @@ export default function ChatView() {
|
||||
};
|
||||
}, [isModelMenuOpen]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isEditorMenuOpen) return;
|
||||
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
if (!editorMenuRef.current) return;
|
||||
if (
|
||||
event.target instanceof Node &&
|
||||
!editorMenuRef.current.contains(event.target)
|
||||
) {
|
||||
setIsEditorMenuOpen(false);
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("mousedown", handleClickOutside);
|
||||
return () => {
|
||||
window.removeEventListener("mousedown", handleClickOutside);
|
||||
};
|
||||
}, [isEditorMenuOpen]);
|
||||
|
||||
// Cmd+O / Ctrl+O to open in last-used editor
|
||||
useEffect(() => {
|
||||
const handler = (e: globalThis.KeyboardEvent) => {
|
||||
if (e.key === "o" && (e.metaKey || e.ctrlKey) && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
if (api && activeProject) {
|
||||
void api.shell.openInEditor(activeProject.cwd, lastEditor);
|
||||
}
|
||||
}
|
||||
};
|
||||
window.addEventListener("keydown", handler);
|
||||
return () => window.removeEventListener("keydown", handler);
|
||||
}, [api, activeProject, lastEditor]);
|
||||
|
||||
const openInEditor = (editorId: string) => {
|
||||
if (!api || !activeProject) return;
|
||||
void api.shell.openInEditor(activeProject.cwd, editorId);
|
||||
setLastEditor(editorId);
|
||||
localStorage.setItem(LAST_EDITOR_KEY, editorId);
|
||||
setIsEditorMenuOpen(false);
|
||||
};
|
||||
|
||||
const ensureSession = async (): Promise<string | null> => {
|
||||
if (!api || !activeThread || !activeProject) return null;
|
||||
if (activeThread.session && activeThread.session.status !== "closed") {
|
||||
@@ -317,6 +375,37 @@ export default function ChatView() {
|
||||
</span>
|
||||
<span>{statusLabel(phase)}</span>
|
||||
</div>
|
||||
{/* Open in editor */}
|
||||
{activeProject && (
|
||||
<div className="relative" ref={editorMenuRef}>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-md px-2 py-1 text-[10px] text-[#a0a0a0]/40 transition-colors duration-150 hover:text-[#a0a0a0]/60"
|
||||
onClick={() => setIsEditorMenuOpen((v) => !v)}
|
||||
>
|
||||
Open in…
|
||||
</button>
|
||||
{isEditorMenuOpen && (
|
||||
<div className="absolute right-0 top-full z-50 mt-1 min-w-[120px] rounded-md border border-white/[0.08] bg-[#1b1b1d] py-1 shadow-xl">
|
||||
{EDITORS.map((editor) => (
|
||||
<button
|
||||
key={editor.id}
|
||||
type="button"
|
||||
className="flex w-full items-center gap-2 px-3 py-1.5 text-left text-[11px] text-[#e0e0e0] hover:bg-white/[0.06]"
|
||||
onClick={() => openInEditor(editor.id)}
|
||||
>
|
||||
{editor.label}
|
||||
{editor.id === lastEditor && (
|
||||
<kbd className="ml-auto text-[9px] text-[#a0a0a0]/40">
|
||||
{navigator.platform.includes("Mac") ? "\u2318" : "Ctrl+"}O
|
||||
</kbd>
|
||||
)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{/* Diff toggle */}
|
||||
<button
|
||||
type="button"
|
||||
|
||||
@@ -29,6 +29,7 @@ export const IPC_CHANNELS = {
|
||||
providerSessionStop: "provider:session:stop",
|
||||
providerSessionList: "provider:session:list",
|
||||
providerEvent: "provider:event",
|
||||
shellOpenInEditor: "shell:open-in-editor",
|
||||
} as const;
|
||||
|
||||
export interface NativeApi {
|
||||
@@ -63,4 +64,7 @@ export interface NativeApi {
|
||||
listSessions: () => Promise<ProviderSession[]>;
|
||||
onEvent: (callback: (event: ProviderEvent) => void) => () => void;
|
||||
};
|
||||
shell: {
|
||||
openInEditor: (cwd: string, editor: string) => Promise<void>;
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user