Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to edit keyboard shortcuts #110

Merged
merged 1 commit into from
Nov 9, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
innerHTML.bind="item.text">
</div>
<div class="shortcut" show.bind="item.shortcut">
${item.shortcut.keyComboString}
${item.shortcut.keyCombo.asString}
</div>
</div>
<hr else/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ import {
IRenameProvider,
ISignatureHelpProvider
} from "./providers/interfaces";
import {KeyCodeNum} from "@common";
import {IEventBus, Settings, SettingsUpdatedEvent} from "@domain";
import {ShortcutIds} from "@application/shortcuts/builtin-shortcuts";

export class EditorSetup {
constructor(
private readonly settings: Settings,
@IEventBus private readonly eventBus: IEventBus,
@all(ICommandProvider) private readonly commandProviders: ICommandProvider[],
@all(IActionProvider) private readonly actionProviders: IActionProvider[],
@all(ICompletionItemProvider) private readonly completionItemProviders: ICompletionItemProvider[],
Expand All @@ -50,6 +55,7 @@ export class EditorSetup {
this.registerThemes();
this.registerCommands();
this.registerActions();
this.registerKeyboardShortcuts();
this.registerCompletionProviders();
this.registerSemanticTokensProviders();
this.registerDocumentSymbolProviders();
Expand Down Expand Up @@ -123,6 +129,68 @@ export class EditorSetup {
});
}

private registerKeyboardShortcuts() {
// Currently we are only overriding the Command Palette keybinding.
let commandPaletteKeybinding: number;

const addOrUpdateShortcuts = (settings: Settings) => {
const commandPaletteShortcutConfig = this.settings.keyboardShortcuts.shortcuts
.find(s => s.id === ShortcutIds.openCommandPalette);

// If the config for this shortcut doesn't exist yet, or did but is now removed.
if (!commandPaletteShortcutConfig) {
if (commandPaletteKeybinding) {
// Disable previous rule
monaco.editor.addKeybindingRule({
keybinding: commandPaletteKeybinding,
command: null,
});
}

monaco.editor.addKeybindingRule({
keybinding: monaco.KeyCode.F1,
command: "editor.action.quickCommand",
});

return;
}

if (commandPaletteKeybinding === undefined) {
// If this is first time we are customizing the command palette keybinding,
// disable default show command palette
monaco.editor.addKeybindingRule({
keybinding: monaco.KeyCode.F1,
command: null,
});
} else {
// Disable previous rule
monaco.editor.addKeybindingRule({
keybinding: commandPaletteKeybinding,
command: null,
});
}

// Add a new rule for the new keybinding
const combo: number[] = [];
if (commandPaletteShortcutConfig.meta) combo.push(monaco.KeyMod.WinCtrl);
if (commandPaletteShortcutConfig.alt) combo.push(monaco.KeyMod.Alt);
if (commandPaletteShortcutConfig.ctrl) combo.push(monaco.KeyMod.CtrlCmd);
if (commandPaletteShortcutConfig.shift) combo.push(monaco.KeyMod.Shift);
if (commandPaletteShortcutConfig.key) combo.push(KeyCodeNum[commandPaletteShortcutConfig.key!.toString() as keyof typeof KeyCodeNum]);

commandPaletteKeybinding = combo.reduce((a, b) => a | b, 0);

monaco.editor.addKeybindingRule({
keybinding: commandPaletteKeybinding,
command: "editor.action.quickCommand",
});
};

this.eventBus.subscribeToServer(SettingsUpdatedEvent, event => addOrUpdateShortcuts(event.settings));

addOrUpdateShortcuts(this.settings);
}

private registerCompletionProviders() {
for (const completionItemProvider of this.completionItemProviders) {
monaco.languages.registerCompletionItemProvider(completionItemProvider.language, completionItemProvider);
Expand Down
2 changes: 2 additions & 0 deletions src/Apps/NetPad.Apps.App/App/src/core/@application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ export * from "./panes/ipane-host-view-state-controller";
export * from "./panes/pane-action"
export * from "./panes/pane";

export * from "./shortcuts/key-combo";
export * from "./shortcuts/shortcut";
export * from "./shortcuts/shortcut-action-execution-context";
export * from "./shortcuts/ishortcut-manager";
export * from "./shortcuts/shortcut-manager";
export * from "./shortcuts/builtin-shortcuts";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div repeat.for="pane of panes"
class="pane-tag ${pane === active ? 'active' : ''}"
click.trigger="toggle(pane)"
title="${pane.name} ${pane.shortcut ? ('(' + pane.shortcut.keyComboString + ')') : ''}">
title="${pane.name} ${pane.shortcut ? ('(' + pane.shortcut.keyCombo.asString + ')') : ''}">
<i class="pane-icon ${pane.icon}" show.bind="pane.icon"></i>
<span>${pane.name}</span>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {IContainer} from "aurelia";
import {IBackgroundService, WithDisposables} from "@common";
import {ChannelInfo, Settings} from "@domain";
import {ChannelInfo} from "@domain";
import {IShortcutManager, Shortcut} from "@application";
import {ElectronIpcGateway} from "./electron-ipc-gateway";
import {IShortcutManager} from "@application";
import {IMainMenuService} from "../../../../../windows/main/titlebar/main-menu/main-menu-service";
import {IMenuItem} from "../../../../../windows/main/titlebar/main-menu/imenu-item";
import {Shortcut} from "@application";
import {IContainer} from "aurelia";

/**
* Handles top-level IPC events sent by Electron's main process.
Expand Down Expand Up @@ -71,7 +70,7 @@ export class ElectronEventHandlerBackgroundService extends WithDisposables imple
return {
name: shortcut.name,
isEnabled: shortcut.isEnabled,
keyCombo: shortcut.keyComboString.split('+').map(x => x.trim())
keyCombo: shortcut.keyCombo.asArray
};
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import {CreateScriptDto, IScriptService, ISettingService} from "@domain";
import {KeyCode} from "@common";
import {CreateScriptDto, IScriptService, ISettingsService} from "@domain";
import {Shortcut} from "./shortcut";
import {EditorUtil} from "../editor/editor-util";
import {ITextEditorService} from "../editor/text-editor-service";
import {Explorer, NamespacesPane, OutputPane} from "../../../windows/main/panes";
import {RunScriptEvent, TogglePaneEvent} from "@application"
import * as monaco from "monaco-editor";

export enum ShortcutIds {
openCommandPalette = "shortcut.commandpalette.open",
quickOpenDocument = "shortcut.documents.quickopen",
openLastActiveDocument = "shortcut.documents.switchtolastactive",
newDocument = "shortcut.documents.new",
closeDocument = "shortcut.documents.close",
saveDocument = "shortcut.documents.save",
saveAllDocuments = "shortcut.documents.saveall",
runDocument = "shortcut.documents.run",
openDocumentProperties = "shortcut.documents.properties",
openSettings = "shortcut.settings.open",
openOutput = "shortcut.output.open",
openExplorer = "shortcut.explorer.open",
openNamespaces = "shortcut.namespaces.open",
reloadWindow = "shortcut.window.reload",
}

export const BuiltinShortcuts = [
new Shortcut("Command Palette")
new Shortcut(ShortcutIds.openCommandPalette, "Command Palette")
.withKey(KeyCode.F1)
.hasAction(ctx => {
const editor = ctx.container.get(ITextEditorService).active?.monaco;
Expand All @@ -18,63 +31,61 @@ export const BuiltinShortcuts = [
editor.focus();
editor.trigger("", "editor.action.quickCommand", null);
})
.configurable(false)
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut("Go to Script")
new Shortcut(ShortcutIds.quickOpenDocument, "Go to Script")
.withCtrlKey()
.withKey(KeyCode.KeyT)
.hasAction(ctx => {
const activeScriptId = ctx.session.active?.script.id;
if (!activeScriptId) {
return;
}

const editors = monaco.editor.getEditors();
if (!editors.length) {
return;
}

let editor = editors.find(e => {
const model = e.getModel();
return !model ? false : (EditorUtil.getScriptId(model) === activeScriptId);
})
const editor = ctx.container.get(ITextEditorService).active?.monaco;

if (!editor) {
editor = editors.find(e => e.hasTextFocus() || e.hasWidgetFocus()) || editors[0];
}
if (!editor) return;

editor.focus();
editor.getAction("netpad.action.goToScript")?.run();
editor.trigger("", "netpad.action.goToScript", null);
})
.configurable(false)
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut(ShortcutIds.openLastActiveDocument, "Switch to Last Active Script")
.withCtrlKey()
.withKey(KeyCode.Tab)
.hasAction((ctx) => ctx.session.activateLastActive())
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut("New")
new Shortcut(ShortcutIds.newDocument, "New")
.withCtrlKey()
.withKey(KeyCode.KeyN)
.hasAction((ctx) => ctx.container.get(IScriptService).create(new CreateScriptDto()))
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut("Close")
new Shortcut(ShortcutIds.closeDocument, "Close")
.withCtrlKey()
.withKey(KeyCode.KeyW)
.hasAction((ctx) => {
if (ctx.session.active) ctx.session.close(ctx.session.active.script.id);
})
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut("Save")
new Shortcut(ShortcutIds.saveDocument, "Save")
.withCtrlKey()
.withKey(KeyCode.KeyS)
.hasAction((ctx) => {
if (ctx.session.active) ctx.container.get(IScriptService).save(ctx.session.active.script.id);
})
.captureDefaultKeyCombo()
.enabled(),

new Shortcut("Save All")
new Shortcut(ShortcutIds.saveAllDocuments, "Save All")
.withCtrlKey()
.withShiftKey()
.withKey(KeyCode.KeyS)
Expand All @@ -84,63 +95,79 @@ export const BuiltinShortcuts = [
await scriptService.save(environment.script.id);
}
})
.captureDefaultKeyCombo()
.enabled(),

new Shortcut("Run")
new Shortcut(ShortcutIds.runDocument, "Run")
.withKey(KeyCode.F5)
.firesEvent(RunScriptEvent)
.firesEvent(async () => new (await import("@application/events/action-events")).RunScriptEvent())
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut("Script Properties")
new Shortcut(ShortcutIds.openDocumentProperties, "Script Properties")
.withKey(KeyCode.F4)
.hasAction((ctx) => {
if (ctx.session.active) {
ctx.container.get(IScriptService).openConfigWindow(ctx.session.active.script.id, null);
}
})
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut("Output")
.withCtrlKey()
.withKey(KeyCode.KeyR)
.firesEvent(() => new TogglePaneEvent(OutputPane))
new Shortcut(ShortcutIds.openSettings, "Settings")
.withKey(KeyCode.F12)
.hasAction((ctx) => ctx.container.get(ISettingsService).openSettingsWindow(null))
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut("Switch to Last Active Script")
new Shortcut(ShortcutIds.openOutput, "Output")
.withCtrlKey()
.withKey(KeyCode.Tab)
.hasAction((ctx) => ctx.session.activateLastActive())
.configurable()
.enabled(),
.withKey(KeyCode.KeyR)
.firesEvent(async () => {
const TogglePaneEvent = (await import("@application/events/action-events")).TogglePaneEvent;
const OutputPane = (await import("../../../windows/main/panes")).OutputPane;

new Shortcut("Settings")
.withKey(KeyCode.F12)
.hasAction((ctx) => ctx.container.get(ISettingService).openSettingsWindow(null))
return new TogglePaneEvent(OutputPane);
})
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut("Explorer")
new Shortcut(ShortcutIds.openExplorer, "Explorer")
.withAltKey()
.withKey(KeyCode.KeyE)
.firesEvent(() => new TogglePaneEvent(Explorer))
.firesEvent(async () => {
const TogglePaneEvent = (await import("@application/events/action-events")).TogglePaneEvent;
const Explorer = (await import("../../../windows/main/panes")).Explorer;

return new TogglePaneEvent(Explorer);
})
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut("Namespaces")
new Shortcut(ShortcutIds.openNamespaces, "Namespaces")
.withAltKey()
.withKey(KeyCode.KeyN)
.firesEvent(() => new TogglePaneEvent(NamespacesPane))
.firesEvent(async () => {
const TogglePaneEvent = (await import("@application/events/action-events")).TogglePaneEvent;
const NamespacesPane = (await import("../../../windows/main/panes")).NamespacesPane;

return new TogglePaneEvent(NamespacesPane);
})
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut("Reload")
new Shortcut(ShortcutIds.reloadWindow, "Reload")
.withCtrlKey()
.withShiftKey()
.withKey(KeyCode.KeyR)
.hasAction(() => window.location.reload())
.captureDefaultKeyCombo()
.configurable()
.enabled(),
];
Loading
Loading