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

fix: reserved keyboard shortcuts in mac used for nav back and forward #2067

Merged
merged 4 commits into from
Jan 17, 2025
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
15 changes: 12 additions & 3 deletions src/command/KeyBindingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,11 +719,19 @@ define(function (require, exports, module) {
normalized,
normalizedDisplay,
explicitPlatform = keyBinding.platform || platform,
explicitBrowserOnly = keyBinding.browserOnly,
explicitNativeOnly = keyBinding.nativeOnly,
targetPlatform,
command,
bindingsToDelete = [],
existing;

if(Phoenix.isNativeApp && explicitBrowserOnly) {
return null;
}
if(!Phoenix.isNativeApp && explicitNativeOnly) {
return null;
}
// For platform: "all", use explicit current platform
if (explicitPlatform && explicitPlatform !== "all") {
targetPlatform = explicitPlatform;
Expand Down Expand Up @@ -983,12 +991,13 @@ define(function (require, exports, module) {
* Returns record(s) for valid key binding(s).
*
* @param {!string | Command} command - A command ID or command object
* @param {{key: string, displayKey:string, platform: string}} keyBindings
* @param {{key: string, displayKey:string, platform: string, browserOnly: boolean, nativeOnly:boolean}} keyBindings
* A single key binding or an array of keybindings.
* In an array of keybinding `platform` property is also available. Example:
* "Shift-Cmd-F". Mac and Win key equivalents are automatically
* mapped to each other. Use displayKey property to display a different
* string (e.g. "CMD+" instead of "CMD=").
* string (e.g. "CMD+" instead of "CMD="). if browserOnly is true, then the shortcut will only apply in browser
* if nativeOnly is set, the shortcut will only apply in native apps
* @param {?string} platform The target OS of the keyBindings either
* "mac", "win" or "linux". If undefined, all platforms not explicitly
* defined will use the key binding.
Expand Down Expand Up @@ -1022,7 +1031,7 @@ define(function (require, exports, module) {
// process platform-specific bindings first
keyBindings.sort(_sortByPlatform);

keyBindings.forEach(function addSingleBinding(keyBindingRequest) {
keyBindings.forEach(function (keyBindingRequest) {
// attempt to add keybinding
keyBinding = _addBinding(commandID, keyBindingRequest, {
platform: keyBindingRequest.platform,
Expand Down
16 changes: 4 additions & 12 deletions src/extensions/default/CodeFolding/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ define(function (require, exports, module) {
GUTTER_NAME = "CodeMirror-foldgutter",
CODE_FOLDING_GUTTER_PRIORITY = Editor.CODE_FOLDING_GUTTER_PRIORITY,
codeFoldingMenuDivider = "codefolding.divider",
collapseKey = "Alt-Shift-Left",
collapseKeyDisplay = "Alt-Shift-←",
expandKey = "Alt-Shift-Right",
expandKeyDisplay = "Alt-Shift-→",
collapseAllKey = "Ctrl-Alt-[",
expandAllKey = "Ctrl-Alt-]";
collapseKey = "Ctrl-Shift-[",
expandKey = "Ctrl-Shift-]";

ExtensionUtils.loadStyleSheet(module, "main.less");

Expand Down Expand Up @@ -360,8 +356,6 @@ define(function (require, exports, module) {

KeyBindingManager.removeBinding(collapseKey);
KeyBindingManager.removeBinding(expandKey);
KeyBindingManager.removeBinding(collapseAllKey);
KeyBindingManager.removeBinding(expandAllKey);

//remove menus
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).removeMenuDivider(codeFoldingMenuDivider.id);
Expand Down Expand Up @@ -419,10 +413,8 @@ define(function (require, exports, module) {
Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).addMenuItem(EXPAND);

//register keybindings
KeyBindingManager.addBinding(COLLAPSE_ALL, [ {key: collapseAllKey}]);
KeyBindingManager.addBinding(EXPAND_ALL, [ {key: expandAllKey}]);
KeyBindingManager.addBinding(COLLAPSE, [{key: collapseKey, displayKey: collapseKeyDisplay}]);
KeyBindingManager.addBinding(EXPAND, [{key:expandKey, displayKey: expandKeyDisplay}]);
KeyBindingManager.addBinding(COLLAPSE, [{key: collapseKey}]);
KeyBindingManager.addBinding(EXPAND, [{key:expandKey}]);


// Add gutters & restore saved expand/collapse state in all currently open editors
Expand Down
38 changes: 28 additions & 10 deletions src/extensionsIntegrated/NavigationAndHistory/keyboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
{
"key": "Ctrl-Tab",
"platform": "mac"
},
{
"key": "Cmd-Shift-]",
"platform": "mac"
}
],
"recent-files.prev": [
Expand All @@ -19,22 +15,44 @@
{
"key": "Ctrl-Shift-Tab",
"platform": "mac"
},
{
"key": "Cmd-Shift-[",
"platform": "mac"
}
],
"navigation.jump.back": [
{
"key": "Alt-Left",
"displayKey": "Alt-←"
"displayKey": "Alt-←",
"platform": "win"
},
{
"key": "Cmd-Alt-Left",
"displayKey": "Cmd-Alt-←",
"platform": "mac",
"nativeOnly": true
},
{
"key": "Ctrl-Alt-Left",
"displayKey": "Ctrl-Alt-←",
"platform": "mac",
"browserOnly": true
}
],
"navigation.jump.fwd": [
{
"key": "Alt-Right",
"displayKey": "Alt-→"
"displayKey": "Alt-→",
"platform": "win"
},
{
"key": "Cmd-Alt-Right",
"displayKey": "Cmd-Alt-→",
"platform": "mac",
"nativeOnly": true
},
{
"key": "Ctrl-Alt-Right",
"displayKey": "Ctrl-Alt-→",
"platform": "mac",
"browserOnly": true
}
]
}
52 changes: 52 additions & 0 deletions test/spec/KeyBindingManager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,58 @@ define(function (require, exports, module) {

expect(KeyBindingManager.getKeymap()).toEqual(expected);
});

it("should handle browserOnly and nativeOnly bindings correctly", function () {
// Test browser-only bindings
let result = KeyBindingManager.addBinding("test.browser", {
key: "F6",
browserOnly: true
});
let keymap = KeyBindingManager.getKeymap();
if(Phoenix.isNativeApp) {
expect(result).toBeNull();
expect(keymap["F6"]).not.toBeDefined();
} else {
expect(result.key).toBe("F6");
expect(keymap["F6"].key).toBe("F6");
}
// Test native-only bindings
result = KeyBindingManager.addBinding("test.native", {
key: "F7",
nativeOnly: true
});
keymap = KeyBindingManager.getKeymap();
if(!Phoenix.isNativeApp) {
expect(result).toBeNull();
expect(keymap["F7"]).not.toBeDefined();
} else {
expect(result.key).toBe("F7");
expect(keymap["F7"].key).toBe("F7");
}
});

it("should handle browserOnly and nativeOnly in multiple bindings", function () {
// Test browser-only bindings
let result = KeyBindingManager.addBinding("test.allPlats", [
{key: "F6", browserOnly: true},
{key: "F7", nativeOnly: true}
]);
let keymap = KeyBindingManager.getKeymap();
expect(result.length).toBe(1);
if (Phoenix.isNativeApp) {
// Native app environment
expect(result[0].key).toBe("F7"); // Only native binding should be added
expect(keymap["F7"]).toBeDefined();
expect(keymap["F7"].key).toBe("F7");
expect(keymap["F6"]).not.toBeDefined(); // Browser-only should not be defined
} else {
// Browser environment
expect(result[0].key).toBe("F6"); // Only browser binding should be added
expect(keymap["F6"]).toBeDefined();
expect(keymap["F6"].key).toBe("F6");
expect(keymap["F7"]).not.toBeDefined(); // Native-only should not be defined
}
});
});

describe("removeBinding", function () {
Expand Down
Loading