Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions apps/desktop/src/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import { Menu, type MenuItemConstructorOptions } from 'electron';

export function installMenu(): void {
const template: MenuItemConstructorOptions[] = [
...(process.platform === 'darwin' ? [{ role: 'appMenu' } as const] : []),
{ role: 'fileMenu' },
...(process.platform === 'darwin'
? [
{ role: 'appMenu' } as const,
// The window closes on Cmd+Shift+W, which leaves Cmd+W to the panel that has the focus.
{ label: 'File', submenu: [{ role: 'close', accelerator: 'Cmd+Shift+W' } as const] },
]
: [{ role: 'fileMenu' } as const]),
{ role: 'editMenu' },
{ role: 'viewMenu' },
{ role: 'windowMenu' },
Expand Down
50 changes: 49 additions & 1 deletion apps/web/src/command/command-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
type FileTreeCommand,
type FileTreeSelection,
type Keys,
type Panel,
type PanelCommand,
type Project,
type ProjectCommand,
type TerminalCommand,
Expand Down Expand Up @@ -50,6 +52,17 @@ const WORKSPACE: Workspace = {
absolutePath: '/p/w',
};

const FILE: Panel = { id: 'file:1', type: 'file', tab: true, closable: true, relativePath: 'a.ts' };

const TERMINAL_PANEL: Panel = {
id: 'terminal:a',
type: 'terminal',
tab: true,
closable: true,
session: SESSION.id,
closeConfirmation: false,
};

// Returns the commands of an application whose editor is `editor` and terminal is `terminals`, on `platform`.
function commands(
editor = new EditorManager(),
Expand Down Expand Up @@ -205,7 +218,7 @@ describe('addGroups', () => {
const manager = commands();
manager.addGroups([{ id: 'io.example', label: 'Example' }]);

expect([...manager.groups.keys()].slice(-2)).toEqual([DEFAULT_COMMAND_GROUPS.workspace.id, 'io.example']);
expect([...manager.groups.keys()].slice(-2)).toEqual([DEFAULT_COMMAND_GROUPS.panel.id, 'io.example']);
expect(manager.groups.get('io.example')?.label).toBe('Example');
});

Expand Down Expand Up @@ -398,6 +411,41 @@ describe('run', () => {
expect(run.mock.lastCall?.[1]).toBe(selection);
});

test('runs panel command against panel, else active panel', () => {
const application = builtInApplication();
const run = vi.fn<PanelCommand['run']>(() => true);
application.commands.add([{ id: 'c', label: 'C', scope: 'panel', group: APPLICATION, run }]);

expect(application.commands.run('c')).toBe(false);
expect(run).not.toHaveBeenCalled();

application.docks.active = { activePanel: FILE } as never;
expect(application.commands.run('c')).toBe(true);
expect(run.mock.lastCall?.[1]).toBe(FILE);

expect(application.commands.run('c', TERMINAL_PANEL)).toBe(true);
expect(run.mock.lastCall?.[1]).toBe(TERMINAL_PANEL);
});

test('refuses panel for command of other surface', () => {
const manager = commands();
const run = vi.fn(() => true);
manager.add([
{ id: 'editor', label: 'Editor', scope: 'editor', group: EDITOR, run },
{ id: 'terminal', label: 'Terminal', scope: 'terminal', group: TERMINAL, run },
{ id: 'fileTree', label: 'File tree', scope: 'fileTree', group: APPLICATION, run },
{ id: 'panel', label: 'Panel', scope: 'panel', group: APPLICATION, run },
]);

expect(manager.run('editor', FILE)).toBe(false);
expect(manager.run('terminal', TERMINAL_PANEL)).toBe(false);
expect(manager.run('fileTree', FILE)).toBe(false);
expect(manager.run('panel', view('a'))).toBe(false);
expect(manager.run('panel', { session: SESSION, terminal: new XTerm({ allowProposedApi: true }) })).toBe(false);
expect(run).not.toHaveBeenCalled();
expect(warn).toHaveBeenCalledTimes(5);
});

test('refuses target of other surface', () => {
const manager = commands();
const run = vi.fn(() => true);
Expand Down
70 changes: 62 additions & 8 deletions apps/web/src/command/command-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
Disposable,
FileTreeSelection,
Keymap,
Panel,
Project,
TerminalView,
Workspace,
Expand All @@ -23,11 +24,26 @@ import {
EDITOR_EDIT,
EDITOR_NAVIGATE,
FILE_TREE,
PANEL,
PROJECT,
TERMINAL,
WORKSPACE,
} from '@/command/groups';
import { ApplicationKeymap } from '@/command/keymap/application-keymap';
import {
PANEL_CLOSE,
PANEL_CLOSE_ALL,
PANEL_CLOSE_LEFT,
PANEL_CLOSE_OTHERS,
PANEL_CLOSE_RIGHT,
PANEL_FILE_COPY_PATH,
PANEL_FILE_COPY_RELATIVE_PATH,
PANEL_FILE_OPEN_FILE_MANAGER,
PANEL_HIDE,
PANEL_SPLIT_MOVE_DOWN,
PANEL_SPLIT_MOVE_RIGHT,
PANEL_TERMINAL_RENAME,
} from '@/dock/commands';
import { EDITOR_APPLY_TOP_FIX, EDITOR_SHOW_ACTIONS } from '@/editor/actions';
import {
EDITOR_ADD_CARET_ABOVE,
Expand Down Expand Up @@ -128,6 +144,7 @@ export const DEFAULT_GROUPS: readonly CommandGroup[] = [
FILE_TREE,
PROJECT,
WORKSPACE,
PANEL,
];

export const DEFAULT_COMMANDS: readonly Command[] = [
Expand Down Expand Up @@ -226,6 +243,19 @@ export const DEFAULT_COMMANDS: readonly Command[] = [
FILE_TREE_DELETE,
FILE_TREE_OPEN_FILE,
FILE_TREE_OPEN_FILE_MANAGER,
// Panel
PANEL_CLOSE,
PANEL_CLOSE_OTHERS,
PANEL_CLOSE_ALL,
PANEL_CLOSE_LEFT,
PANEL_CLOSE_RIGHT,
PANEL_HIDE,
PANEL_SPLIT_MOVE_RIGHT,
PANEL_SPLIT_MOVE_DOWN,
PANEL_FILE_COPY_PATH,
PANEL_FILE_COPY_RELATIVE_PATH,
PANEL_FILE_OPEN_FILE_MANAGER,
PANEL_TERMINAL_RENAME,
];

/**
Expand Down Expand Up @@ -375,7 +405,7 @@ export class CommandManager implements Commands {
this.notify();
}

run(id: string, target?: Project | Workspace | EditorView | TerminalView | FileTreeSelection): boolean {
run(id: string, target?: Project | Workspace | EditorView | TerminalView | FileTreeSelection | Panel): boolean {
const command = this.currentCommands.get(id);
if (command === undefined) {
console.warn(`Unknown command ${id}`);
Expand Down Expand Up @@ -471,6 +501,24 @@ export class CommandManager implements Commands {
return command.run(this.application, target);
}

if (command.scope === 'panel') {
if (target !== undefined && !isPanel(target)) {
console.warn(`Command ${id} runs on a panel`);
return false;
}

const panel = target ?? this.application.docks.active?.activePanel ?? null;
if (panel === null) {
return false;
}

if (command.enabled?.(this.application, panel) === false) {
return false;
}

return command.run(this.application, panel);
}

if (command.enabled?.(this.application) === false) {
return false;
}
Expand Down Expand Up @@ -501,35 +549,41 @@ export class CommandManager implements Commands {
}

export function isProject(
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection,
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection | Panel,
): target is Project {
return 'absolutePath' in target && !('main' in target);
}

export function isWorkspace(
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection,
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection | Panel,
): target is Workspace {
return 'absolutePath' in target && 'main' in target;
}

export function isEditor(
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection,
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection | Panel,
): target is EditorView {
return !('absolutePath' in target) && !('relativePaths' in target) && !('session' in target);
return !('absolutePath' in target) && !('relativePaths' in target) && !('session' in target) && !isPanel(target);
}

export function isTerminalView(
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection,
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection | Panel,
): target is TerminalView {
return 'session' in target;
return 'session' in target && !isPanel(target);
}

export function isFileTreeSelection(
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection,
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection | Panel,
): target is FileTreeSelection {
return 'relativePaths' in target;
}

export function isPanel(
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection | Panel,
): target is Panel {
return 'closable' in target;
}

export function useCommandManager(): CommandManager;
export function useCommandManager<T>(select: (commands: CommandManager) => T): T;
export function useCommandManager<T>(select?: (commands: CommandManager) => T): CommandManager | T {
Expand Down
11 changes: 10 additions & 1 deletion apps/web/src/command/context-menu/context-menu.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type Command,
type ContextMenuGroup,
DEFAULT_COMMAND_GROUPS,
type Panel,
type TerminalView,
} from '@elsewise/frontend-plugin';
import { describe, expect, test, vi } from 'vitest';
Expand Down Expand Up @@ -136,6 +137,7 @@ describe('CommandContextMenu', () => {
},
'File tree',
],
['panel', { id: 'file:1', type: 'file', tab: true, closable: true, relativePath: 'a.ts' } satisfies Panel, 'Panel'],
])('shows commands of %s target only', async (_, target, shown) => {
const application = setUp([
{
Expand All @@ -159,8 +161,15 @@ describe('CommandContextMenu', () => {
label: 'File tree',
run: () => true,
},
{
scope: 'panel',
group: DEFAULT_COMMAND_GROUPS.panel.id,
id: 'Panel',
label: 'Panel',
run: () => true,
},
]);
await mount([group('a', [item('Editor'), item('Terminal'), item('File tree')])], application, {
await mount([group('a', [item('Editor'), item('Terminal'), item('File tree'), item('Panel')])], application, {
target: { current: target },
});

Expand Down
12 changes: 9 additions & 3 deletions apps/web/src/command/context-menu/context-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
Application,
ContextMenuGroup,
FileTreeSelection,
Panel,
Project,
TerminalView,
Workspace,
Expand All @@ -26,6 +27,7 @@ import {
type CommandManager,
isEditor,
isFileTreeSelection,
isPanel,
isProject,
isTerminalView,
isWorkspace,
Expand All @@ -38,9 +40,9 @@ export interface CommandContextMenuProps {
*/
menu: readonly ContextMenuGroup[];
/**
* The project, workspace, editor, terminal or file tree selection that the menu's commands run against.
* The project, workspace, editor, terminal, file tree selection or panel that the menu's commands run against.
*/
target?: RefObject<Project | Workspace | EditorView | TerminalView | FileTreeSelection | null>;
target?: RefObject<Project | Workspace | EditorView | TerminalView | FileTreeSelection | Panel | null>;
/**
* Returns whether the menu opens for `event`, e.g. a right-click or a touch's long-press. Defaults to true.
*/
Expand Down Expand Up @@ -149,7 +151,7 @@ function resolve(
menu: readonly ContextMenuGroup[],
application: Application,
commands: CommandManager,
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection | null,
target: Project | Workspace | EditorView | TerminalView | FileTreeSelection | Panel | null,
path: readonly string[],
): Group[] {
const groups: Group[] = [];
Expand Down Expand Up @@ -221,6 +223,10 @@ function resolve(
shown = command.shown?.(application, target);
enabled = command.enabled?.(application, target);
active = command.active?.(application, target);
} else if (command.scope === 'panel' && target !== null && isPanel(target)) {
shown = command.shown?.(application, target);
enabled = command.enabled?.(application, target);
active = command.active?.(application, target);
} else {
continue;
}
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/command/groups.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type CommandGroup, DEFAULT_COMMAND_GROUPS } from '@elsewise/frontend-plugin';

const { application, editor, terminal, fileTree, project, workspace } = DEFAULT_COMMAND_GROUPS;
const { application, editor, terminal, fileTree, project, workspace, panel } = DEFAULT_COMMAND_GROUPS;

export const APPLICATION: CommandGroup = { id: application.id, label: 'Application' };

Expand All @@ -21,3 +21,5 @@ export const FILE_TREE: CommandGroup = { id: fileTree.id, label: 'File tree' };
export const PROJECT: CommandGroup = { id: project.id, label: 'Project' };

export const WORKSPACE: CommandGroup = { id: workspace.id, label: 'Workspace' };

export const PANEL: CommandGroup = { id: panel.id, label: 'Panel' };
2 changes: 2 additions & 0 deletions apps/web/src/command/keymap/application-keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class ApplicationKeymap implements ActiveKeymap {
editor: new Map(),
terminal: new Map(),
fileTree: new Map(),
panel: new Map(),
};

constructor(private readonly platform: Platform = PLATFORM) {}
Expand Down Expand Up @@ -50,6 +51,7 @@ export class ApplicationKeymap implements ActiveKeymap {
editor: new Map<string, string[]>(),
terminal: new Map<string, string[]>(),
fileTree: new Map<string, string[]>(),
panel: new Map<string, string[]>(),
};
for (const command of commands) {
const entry = Object.hasOwn(keymap.keys, command.id) ? keymap.keys[command.id] : undefined;
Expand Down
Loading
Loading