Skip to content

Commit

Permalink
fix(OSX): Add flag to indicate if menu item is from key press or not (#…
Browse files Browse the repository at this point in the history
…1044)

__Issue:__ In Onivim, when pressing a keyboard shortcut for a menu item, the command gets triggered twice - once via the input handler, and once via the menu callback.

From the menu callback, currently, we don't know whether this came from a keyboard command (which can be ignored, since the keyboard-input pipeline handles it), or if it came from a click / other gesture, in which case we should run it.

__Fix:__ Add a flag to indicate if this callback is due to a keypress or not.
  • Loading branch information
bryphe authored Jan 14, 2021
1 parent 8497f52 commit 0c3be5d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
11 changes: 9 additions & 2 deletions examples/NativeMenuExample.re
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 20,15 @@ module View = {
let menu1 = Menu.create("Test 1");
Menu.addSubmenu(~parent=menuBar, ~child=menu1);

let menuCallback = (str, ()) => {
print_endline(Printf.sprintf("%s clicked: %d", str, currentNonce));
let menuCallback = (str, ~fromKeyPress, ()) => {
print_endline(
Printf.sprintf(
"%s clicked: %d (from key press: %b)",
str,
currentNonce,
fromKeyPress,
),
);
};

let item11 =
Expand Down
4 changes: 2 additions & 2 deletions src/Native/Menu.re
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 54,10 @@ module Item = {

let callbackTbl = CallbackTbl.create(32);

let callbackForMenuItem = item => {
let callbackForMenuItem = (fromKeyPress, item) => {
let callback = CallbackTbl.find_opt(callbackTbl, item);
switch (callback) {
| Some(cb) => cb()
| Some(cb) => cb(~fromKeyPress, ())
| None => ()
};
};
Expand Down
2 changes: 1 addition & 1 deletion src/Native/Menu.rei
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 23,7 @@ module Item: {
let create:
(
~title: string,
~onClick: unit => unit,
~onClick: (~fromKeyPress: bool, unit) => unit,
~keyEquivalent: KeyEquivalent.t=?,
unit
) =>
Expand Down
17 changes: 15 additions & 2 deletions src/Native/cocoa/ReveryMenuItemTarget.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 28,23 @@ static const camlValue *callbackForMenuItem;
CAMLparam0();
CAMLlocal1(vMenuItem);

// Whether the menu item 'click' was due to a shortcut key
int fDueToKeyPress = 0;

switch (NSApp.currentEvent.type) {
case NSEventTypeKeyDown:
case NSEventTypeKeyUp:
fDueToKeyPress = 1;
break;
default:
fDueToKeyPress = 0;
break;
}

if (callbackForMenuItem != NULL) {
vMenuItem = revery_wrapPointer(sender);
camlValue args[] = {vMenuItem};
revery_caml_call_n(*callbackForMenuItem, 1, args);
camlValue args[] = {Val_int(fDueToKeyPress), vMenuItem};
revery_caml_call_n(*callbackForMenuItem, 2, args);
} else {
NSLog(@"Unable to acquire menu item callback!");
}
Expand Down

0 comments on commit 0c3be5d

Please sign in to comment.