Skip to content

Commit 192673a

Browse files
committed
🔧 Support custom menus in ShowMenuRequest
1 parent 7848a77 commit 192673a

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

src/common/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
export * from './settings-schemata';
1212

13-
import { AchievementStats } from './settings-schemata';
13+
import { AchievementStats, Menu } from './settings-schemata';
1414

1515
/** This type is used to pass command line arguments to the app. */
1616
export type CommandlineOptions = {
@@ -179,12 +179,14 @@ export type KeySequence = Array<KeyStroke>;
179179
/**
180180
* There are different reasons why a menu should be shown. This type is used to describe
181181
* the request to show a menu. A menu can be shown because a shortcut was pressed (in this
182-
* case `trigger` will be the shortcut or the shortcut ID) or because a menu was requested
183-
* by name.
182+
* case `trigger` will be the shortcut or the shortcut ID), because a menu was requested
183+
* by name, or a custom menu is requested (in this case `menu` contains the menu
184+
* structure).
184185
*/
185186
export type ShowMenuRequest = {
186-
readonly trigger: string;
187-
readonly name: string;
187+
readonly trigger?: string;
188+
readonly name?: string;
189+
readonly menu?: Menu;
188190
};
189191

190192
/**

src/main/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ export class KandoApp {
356356
*
357357
* @param request Required information to select correct menu.
358358
*/
359-
public async showMenu(request: Partial<ShowMenuRequest>) {
359+
public async showMenu(request: ShowMenuRequest) {
360360
// Create and load the main window if it does not exist yet.
361361
if (!this.menuWindow) {
362362
this.menuWindow = new MenuWindow(this);

src/main/menu-window.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,13 @@ export class MenuWindow extends BrowserWindow {
137137
* @param systemIconsChanged True if the system icon theme has changed since the last
138138
* time the menu was shown.
139139
*/
140-
public showMenu(
141-
request: Partial<ShowMenuRequest>,
142-
info: WMInfo,
143-
systemIconsChanged: boolean
144-
) {
140+
public showMenu(request: ShowMenuRequest, info: WMInfo, systemIconsChanged: boolean) {
145141
const sameShortcutBehavior = this.kando
146142
.getGeneralSettings()
147143
.get('sameShortcutBehavior');
148144

149145
// If a menu is currently shown and the user presses the same shortcut again we will
150-
// either close the menu or show the next one with the same shortcut. There is also
151-
// the option to do nothing in this case, but in this case the menu's shortcut will be
152-
// inhibited and thus this method will not be called in the first place.
146+
// either close the menu or show the next one with the same shortcut.
153147
if (this.isVisible()) {
154148
let isSameMenu = false;
155149
if (request.name != null) {
@@ -206,7 +200,9 @@ export class MenuWindow extends BrowserWindow {
206200
if (!this.kando.allShortcutsInhibited() && sameShortcutBehavior === 'nothing') {
207201
const useID = !this.kando.getBackend().getBackendInfo().supportsShortcuts;
208202
const shortcut = useID ? menu.shortcutID : menu.shortcut;
209-
this.kando.getBackend().inhibitShortcuts([shortcut]);
203+
if (shortcut && shortcut.length > 0) {
204+
this.kando.getBackend().inhibitShortcuts([shortcut]);
205+
}
210206
}
211207

212208
// Store the last menu to be able to execute the selected action later. The WMInfo
@@ -404,21 +400,27 @@ export class MenuWindow extends BrowserWindow {
404400
/**
405401
* This chooses the correct menu depending on the environment.
406402
*
407-
* If the request contains a menu name, this menu is chosen. If no menu with the given
408-
* name is found, an exception is thrown. If there are multiple menus with the same
409-
* name, the first one is chosen. No other conditions are checked in this case.
403+
* If the request contains a custom menu, this menu is returned. No other conditions are
404+
* checked in this case.
410405
*
411-
* If the request contains a trigger (shortcut or shortcutID), a list of menus bound to
412-
* this trigger is assembled and the menu with the best matching conditions is chosen.
413-
* If no menu with the given trigger is found, null is returned.
406+
* If the request contains a trigger (shortcut or shortcutID) or menu name, we first
407+
* check whether there are any menus with the given trigger or name. If none are found,
408+
* an exception is thrown.
414409
*
415-
* If neither a menu name nor a trigger is given, null is returned.
410+
* Then, a list of menu candidates is assembled and the menu with the best matching
411+
* conditions is chosen. If no menu with the given trigger or name is found, null is
412+
* returned.
416413
*
417414
* @param request Required information to select correct menu.
418415
* @param info Information about current desktop environment.
419416
* @returns The selected menu or null if no menu was found.
420417
*/
421-
public chooseMenu(request: Partial<ShowMenuRequest>, info: WMInfo) {
418+
public chooseMenu(request: ShowMenuRequest, info: WMInfo) {
419+
// If a custom menu is given in the request, we return this menu directly.
420+
if (request.menu) {
421+
return request.menu;
422+
}
423+
422424
// Get list of current menus.
423425
const menus = this.kando.getMenuSettings().get('menus');
424426

@@ -427,7 +429,19 @@ export class MenuWindow extends BrowserWindow {
427429
if (request.name != null) {
428430
const menu = menus.find((m) => m.root.name === request.name);
429431
if (!menu) {
430-
throw new Error(`Menu with name "${request.name}" not found.`);
432+
throw new Error(`No menu with name "${request.name}" found.`);
433+
}
434+
}
435+
436+
// We check if the request has a trigger. If no menu with this trigger is found, we
437+
// throw an error.
438+
if (request.trigger != null) {
439+
const useID = !this.kando.getBackend().getBackendInfo().supportsShortcuts;
440+
const menu = menus.find(
441+
(m) => (useID ? m.shortcutID : m.shortcut) === request.trigger
442+
);
443+
if (!menu) {
444+
throw new Error(`No menu with trigger "${request.trigger}" found.`);
431445
}
432446
}
433447

0 commit comments

Comments
 (0)