Skip to content

Commit

Permalink
Add an option to control display of menu buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenguh committed Nov 25, 2021
1 parent d98b9d0 commit cdee8ca
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add command (`whichkey.undoKey`) to undo entered key.
- Add menu buttons to which key menu.
- Added three additional sorting options
- Add menu buttons to which key menu (Use `whichkey.showButtons` in config to turn on/off).
- Add three additional sorting options
- `custom`:
Menu items are sorted by the key in the following 'categories'
then by a custom order within each 'category'.
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,12 @@
},
"whichkey.showIcons": {
"type": "boolean",
"markdownDescription": "Controls whether to show or hide icons in which-key menu.",
"markdownDescription": "Controls whether to show or hide icons in the which-key menu.",
"default": true
},
"whichkey.showButtons": {
"type": "boolean",
"markdownDescription": "Controls whether to show or hide buttons in the which-key menu.",
"default": true
},
"whichkey.sortOrder": {
Expand Down
1 change: 1 addition & 0 deletions src/config/menuConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface WhichKeyMenuConfig {
title?: string;
delay: number;
showIcons: boolean;
showButtons: boolean;
bindings: BindingItem[];
}

Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const contributePrefix = 'whichkey';
export enum ConfigKey {
Delay = "delay",
ShowIcons = "showIcons",
ShowButtons = "showButtons",
SortOrder = "sortOrder",
Bindings = "bindings",
Overrides = "bindingOverrides",
Expand Down Expand Up @@ -38,6 +39,7 @@ export enum ContextKey {
export const Configs = {
Delay: `${contributePrefix}.${ConfigKey.Delay}`,
ShowIcons: `${contributePrefix}.${ConfigKey.ShowIcons}`,
ShowButtons: `${contributePrefix}.${ConfigKey.ShowButtons}`,
SortOrder: `${contributePrefix}.${ConfigKey.SortOrder}`,
Bindings: `${contributePrefix}.${ConfigKey.Bindings}`,
Overrides: `${contributePrefix}.${ConfigKey.Overrides}`,
Expand Down
4 changes: 3 additions & 1 deletion src/menu/baseWhichKeyMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export abstract class BaseWhichKeyMenu<T extends BaseWhichKeyMenuItem> implement
onDidResolve?: () => any;
onDidReject?: (reason?: any) => any;

showButtons = true;

constructor(cmdRelay: CommandRelay) {
this._acceptQueue = new DispatchQueue(this.handleAcceptanceDispatch.bind(this));
this._valueQueue = new DispatchQueue(this.handleValueDispatch.bind(this));
Expand Down Expand Up @@ -280,7 +282,7 @@ export abstract class BaseWhichKeyMenu<T extends BaseWhichKeyMenuItem> implement
update(state: BaseWhichKeyMenuState<T>): void {
this.clearDelay();
this._qp.title = state.title;
this._qp.buttons = state.buttons ?? [];
this._qp.buttons = this.showButtons ? state.buttons ?? [] : [];
// Need clear the current rendered menu items
// when user click the back button with delay
// so we won't show the old menu items while the menu is
Expand Down
1 change: 1 addition & 0 deletions src/menu/whichKeyMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export function showWhichKeyMenu(statusBar: StatusBar, cmdRelay: CommandRelay, r
const menu = new WhichKeyMenu(statusBar, cmdRelay, repeater);
menu.delay = config.delay;
menu.showIcons = config.showIcons;
menu.showButtons = config.showButtons;
menu.update({
items: config.bindings,
title: config.title,
Expand Down
2 changes: 2 additions & 0 deletions src/test/suite/menu/whichkeyMenu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ suite("WhichKeyMenu", function () {
const config: WhichKeyMenuConfig = {
delay: 0,
showIcons: false,
showButtons: false,
title: "Test",
bindings: [
{
Expand Down Expand Up @@ -60,6 +61,7 @@ suite("WhichKeyMenu", function () {
const config: WhichKeyMenuConfig = {
delay: 0,
showIcons: false,
showButtons: false,
title: "Test",
bindings: [
{
Expand Down
5 changes: 4 additions & 1 deletion src/whichKeyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,12 @@ export default class WhichKeyCommand {
show(): void {
const delay = getConfig<number>(Configs.Delay) ?? 0;
const showIcons = getConfig<boolean>(Configs.ShowIcons) ?? true;
const showButtons = getConfig<boolean>(Configs.ShowButtons) ?? true;
const config = {
bindings: this.bindingItems!,
delay,
showIcons,
showButtons,
title: this.config?.title
};
showWhichKeyMenu(this.statusBar, this.cmdRelay, this.repeater, config);
Expand All @@ -271,7 +273,8 @@ export default class WhichKeyCommand {
static show(bindings: BindingItem[], statusBar: StatusBar, cmdRelay: CommandRelay): void {
const delay = getConfig<number>(Configs.Delay) ?? 0;
const showIcons = getConfig<boolean>(Configs.ShowIcons) ?? true;
const config = { bindings, delay, showIcons };
const showButtons = getConfig<boolean>(Configs.ShowButtons) ?? true;
const config = { bindings, delay, showIcons, showButtons };
showWhichKeyMenu(statusBar, cmdRelay, undefined, config);
}
}

0 comments on commit cdee8ca

Please sign in to comment.