Skip to content

Commit bf95e7c

Browse files
committed
chore: preset selection dropdown working
1 parent d7c93bf commit bf95e7c

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

src/command/KeyBindingManager.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ define(function (require, exports, module) {
5757
_userKeyMapFilePath = path.normalize(brackets.app.getApplicationSupportDirectory() + "/" + KEYMAP_FILENAME);
5858

5959
const EVENT_KEY_BINDING_ADDED = "keyBindingAdded",
60-
EVENT_KEY_BINDING_REMOVED = "keyBindingRemoved";
60+
EVENT_KEY_BINDING_REMOVED = "keyBindingRemoved",
61+
EVENT_NEW_PRESET = "newPreset",
62+
EVENT_PRESET_CHANGED = "presetChanged";
6163

6264
const KEY = Keys.KEY;
6365
const knownBindableCommands = new Set();
@@ -1629,28 +1631,33 @@ define(function (require, exports, module) {
16291631
if(_customKeymapIDInUse === packID) {
16301632
_loadUserKeyMap();
16311633
}
1634+
exports.trigger(EVENT_NEW_PRESET, packID);
16321635
}
16331636

16341637
function getAllCustomKeymapPacks() {
16351638
const packDetails = [];
16361639
for(let packID of Object.keys(_registeredCustomKeyMaps)){
1637-
packDetails.push([{
1640+
packDetails.push({
16381641
packID,
16391642
packageName: _registeredCustomKeyMaps[packID].packageName,
16401643
keyMap: structuredClone(_registeredCustomKeyMaps[packID].keyMap)
1641-
}]);
1644+
});
16421645
}
16431646
return packDetails;
16441647
}
16451648

1649+
function getCurrentCustomKeymapPack() {
1650+
return _registeredCustomKeyMaps[_customKeymapIDInUse];
1651+
}
1652+
16461653
/**
16471654
* Determines the origin of a custom keyboard shortcut is from user keymap.json or a custom keymap preset.
16481655
* If it is neither (Eg. phoenix default shortcuts, will return null.)
16491656
*
16501657
* @param {string} shortcut - The keyboard shortcut to check.
16511658
* @returns {string|null} - The origin of the custom shortcut, or null if it is not a custom shortcut.
16521659
*/
1653-
function getCustomShortcutOrigin(shortcut) {
1660+
function _getCustomShortcutOrigin(shortcut) {
16541661
shortcut = normalizeKeyDescriptorString(shortcut);
16551662
if(_originalUserKeyMap.hasOwnProperty(shortcut)){
16561663
return Strings.KEYBOARD_SHORTCUT_SRC_USER;
@@ -1667,9 +1674,9 @@ define(function (require, exports, module) {
16671674
* @param packID
16681675
* @private
16691676
*/
1670-
function _useCustomKeymapPack(packID) {
1677+
function _setCurrentCustomKeymapPack(packID) {
16711678
if(!PreferencesManager){
1672-
throw new Error("_useCustomKeymapPack should be called only after appinit event.");
1679+
throw new Error("setCurrentCustomKeymapPack should be called only after appinit event.");
16731680
}
16741681
PreferencesManager.stateManager.set(STATE_CUSTOM_KEY_MAP_ID, packID);
16751682
}
@@ -1678,6 +1685,7 @@ define(function (require, exports, module) {
16781685
if(!_customKeymapIDInUse || !_registeredCustomKeyMaps[_customKeymapIDInUse]){
16791686
return userKeyMap;
16801687
}
1688+
Metrics.countEvent(Metrics.EVENT_TYPE.KEYBOARD, 'preset', _customKeymapIDInUse);
16811689
// the custom keymap is something like {"Ctrl-Shift-&": "navigate.gotoFirstProblem"} .
16821690
// user defined shortcuts take precedence over custom shortcuts.
16831691
const customKeyMap = _registeredCustomKeyMaps[_customKeymapIDInUse].keyMap;
@@ -1837,6 +1845,7 @@ define(function (require, exports, module) {
18371845
.on("change", ()=>{
18381846
_customKeymapIDInUse = PreferencesManager.stateManager.get(STATE_CUSTOM_KEY_MAP_ID);
18391847
_loadUserKeyMap();
1848+
exports.constructor(EVENT_PRESET_CHANGED, _customKeymapIDInUse);
18401849
});
18411850
_customKeymapIDInUse = PreferencesManager.stateManager.get(STATE_CUSTOM_KEY_MAP_ID);
18421851
_loadUserKeyMap();
@@ -1984,8 +1993,9 @@ define(function (require, exports, module) {
19841993
exports._onCtrlUp = _onCtrlUp;
19851994

19861995
// private api
1987-
exports._useCustomKeymapPack = _useCustomKeymapPack;
19881996
exports._getKnownBindableCommands = _getKnownBindableCommands;
1997+
exports._getCustomShortcutOrigin = _getCustomShortcutOrigin;
1998+
exports._setCurrentCustomKeymapPack = _setCurrentCustomKeymapPack;
19891999

19902000
// Define public API
19912001
exports.getKeymap = getKeymap;
@@ -2002,13 +2012,15 @@ define(function (require, exports, module) {
20022012
exports.showShortcutSelectionDialog = showShortcutSelectionDialog;
20032013
exports.registerCustomKeymapPack = registerCustomKeymapPack;
20042014
exports.getAllCustomKeymapPacks = getAllCustomKeymapPacks;
2005-
exports.getCustomShortcutOrigin = getCustomShortcutOrigin;
2015+
exports.getCurrentCustomKeymapPack = getCurrentCustomKeymapPack;
20062016

20072017
// public constants
20082018
exports.KEY = KEY;
20092019
// public events
20102020
exports.EVENT_KEY_BINDING_ADDED = EVENT_KEY_BINDING_ADDED;
20112021
exports.EVENT_KEY_BINDING_REMOVED = EVENT_KEY_BINDING_REMOVED;
2022+
exports.EVENT_NEW_PRESET = EVENT_NEW_PRESET;
2023+
exports.EVENT_PRESET_CHANGED = EVENT_PRESET_CHANGED;
20122024

20132025
/**
20142026
* Use windows-specific bindings if no other are found (e.g. Linux).

src/extensionsIntegrated/DisplayShortcuts/main.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ define(function (require, exports, module) {
3838
Dialogs = require("widgets/Dialogs"),
3939
Metrics = require("utils/Metrics"),
4040
WorkspaceManager = require("view/WorkspaceManager"),
41+
StringUtils = require("utils/StringUtils"),
4142
AppInit = require("utils/AppInit"),
4243
DropdownButton = require("widgets/DropdownButton").DropdownButton,
4344
Strings = require("strings");
@@ -47,6 +48,7 @@ define(function (require, exports, module) {
4748
const panelHtml = require("text!./templates/bottom-panel.html"),
4849
shortcutsHtml = require("text!./templates/shortcut-table.html"),
4950
TOGGLE_SHORTCUTS_ID = Commands.HELP_TOGGLE_SHORTCUTS_PANEL;
51+
const DEFAULT_PACK_PLACEHOLDER = "default";
5052
let keyList = [],
5153
panel,
5254
$shortcutsPanel,
@@ -111,7 +113,7 @@ define(function (require, exports, module) {
111113
// Core commands in Brackets use a simple command title as an id, for example "open.file".
112114
// Extensions should use the following format: "author.myextension.mycommandname".
113115
// For example, "lschmitt.csswizard.format.css".
114-
const customOrigin = KeyBindingManager.getCustomShortcutOrigin(keyBinding);
116+
const customOrigin = KeyBindingManager._getCustomShortcutOrigin(keyBinding);
115117
if(customOrigin){
116118
return customOrigin;
117119
}
@@ -337,6 +339,7 @@ define(function (require, exports, module) {
337339
}
338340

339341
function _showShortcuts() {
342+
_updatePresets();
340343
let $shortcuts = $("#shortcuts-panel");
341344

342345
// Apply any active filter
@@ -391,6 +394,21 @@ define(function (require, exports, module) {
391394
keyList = [];
392395
}
393396

397+
function _updatePresets() {
398+
if (!panel || !panel.isVisible()) {
399+
return;
400+
}
401+
const allPacks = KeyBindingManager.getAllCustomKeymapPacks();
402+
const currentKeymapPack = KeyBindingManager.getCurrentCustomKeymapPack();
403+
if(currentKeymapPack){
404+
presetPicker.$button.text(StringUtils.format(
405+
Strings.KEYBOARD_SHORTCUT_PRESET_USING, currentKeymapPack.packageName));
406+
} else {
407+
presetPicker.$button.text(Strings.KEYBOARD_SHORTCUT_PRESET_SELECT);
408+
}
409+
presetPicker.items = [DEFAULT_PACK_PLACEHOLDER, ...allPacks.map(pack=>pack.packID)];
410+
}
411+
394412
_updateKeyBindings = _.debounce(function () {
395413
// Update keylist
396414
destroyKeyList();
@@ -430,13 +448,17 @@ define(function (require, exports, module) {
430448
}
431449
}
432450

433-
function _presetRenderer(item) {
434-
var html = "<span class='stylesheet-name'>" + _.escape(item.name);
435-
if (item.subDirStr) {
436-
html += "<span class='stylesheet-dir'> — " + _.escape(item.subDirStr) + "</span>";
451+
function _presetRenderer(packID) {
452+
if(packID === DEFAULT_PACK_PLACEHOLDER) {
453+
return Strings.DEFAULT;
437454
}
438-
html += "</span>";
439-
return html;
455+
const allPacks = KeyBindingManager.getAllCustomKeymapPacks();
456+
for(let pack of allPacks) {
457+
if(pack.packID === packID) {
458+
return pack.packageName;
459+
}
460+
}
461+
return packID;
440462
}
441463

442464
AppInit.appReady(function() {
@@ -461,10 +483,17 @@ define(function (require, exports, module) {
461483

462484
$shortcutsPanel = $("#shortcuts-panel");
463485

464-
presetPicker = new DropdownButton("Select Shortcut Presets", [], _presetRenderer, {
465-
cssClasses: "presetPicker"
486+
presetPicker = new DropdownButton(Strings.KEYBOARD_SHORTCUT_PRESET_SELECT, [], _presetRenderer, {
487+
cssClasses: "presetPicker no-focus"
466488
});
467489
$shortcutsPanel.find(".presetPickerContainer").append(presetPicker.$button);
490+
presetPicker.on("select", function (e, selectedPackID) {
491+
if(selectedPackID === DEFAULT_PACK_PLACEHOLDER) {
492+
KeyBindingManager._setCurrentCustomKeymapPack(null);
493+
}
494+
KeyBindingManager._setCurrentCustomKeymapPack(selectedPackID);
495+
_updatePresets();
496+
});
468497

469498
// Events
470499
$shortcutsPanel.on("dblclick", function (e) {
@@ -510,5 +539,7 @@ define(function (require, exports, module) {
510539
});
511540
KeyBindingManager.on(KeyBindingManager.EVENT_KEY_BINDING_ADDED, _updateKeyBindings);
512541
KeyBindingManager.on(KeyBindingManager.EVENT_KEY_BINDING_REMOVED, _updateKeyBindings);
542+
KeyBindingManager.on(KeyBindingManager.EVENT_NEW_PRESET, _updatePresets);
543+
KeyBindingManager.on(KeyBindingManager.EVENT_PRESET_CHANGED, _updatePresets);
513544
});
514545
});

src/nls/root/strings.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ define({
371371
//Bottom panel
372372
"KEYBOARD_SHORTCUT_PANEL_TITLE": "Keyboard Shortcuts",
373373
"KEYBOARD_SHORTCUT_PRESET_TOOLTIP": "Use keyboard shortcuts from editors like VSCode, WebStorm, or Sublime Text",
374+
"KEYBOARD_SHORTCUT_PRESET_SELECT": "Select Shortcut Presets",
375+
"KEYBOARD_SHORTCUT_PRESET_USING": "Using {0}",
376+
"DEFAULT": "Default",
374377
"KEYBOARD_SHORTCUT_PANEL_FILTER": "Filter&hellip;",
375378
"KEYBOARD_SHORTCUT_PANEL_RESET": "Reset\u2026",
376379
"KEYBOARD_SHORTCUT_PANEL_RESET_DEFAULT": "Reset To Default Shortcuts\u2026",

0 commit comments

Comments
 (0)