5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 02:51:56 +08:00

Support more accelerator keys

This commit is contained in:
Lea Anthony 2020-11-29 14:18:53 +11:00
parent fb1d4c6325
commit 5c39467879
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
3 changed files with 81 additions and 7 deletions

View File

@ -14,6 +14,7 @@
#define s(str) sel_registerName(str) #define s(str) sel_registerName(str)
#define u(str) sel_getUid(str) #define u(str) sel_getUid(str)
#define str(input) msg(c("NSString"), s("stringWithUTF8String:"), input) #define str(input) msg(c("NSString"), s("stringWithUTF8String:"), input)
#define strunicode(input) msg(c("NSString"), s("stringWithFormat:"), str("%C"), input)
#define cstr(input) (const char *)msg(input, s("UTF8String")) #define cstr(input) (const char *)msg(input, s("UTF8String"))
#define url(input) msg(c("NSURL"), s("fileURLWithPath:"), str(input)) #define url(input) msg(c("NSURL"), s("fileURLWithPath:"), str(input))
@ -1265,11 +1266,60 @@ unsigned long parseModifiers(const char **modifiers) {
return result; return result;
} }
id processAcceleratorKey(const char *key) {
if( STREQ(key, "Backspace") ) {
return strunicode(0x0008);
}
if( STREQ(key, "Tab") ) {
return strunicode(0x0009);
}
if( STREQ(key, "Return") ) {
return strunicode(0x000d);
}
if( STREQ(key, "Escape") ) {
return strunicode(0x001b);
}
if( STREQ(key, "Left") ) {
return strunicode(0x001c);
}
if( STREQ(key, "Right") ) {
return strunicode(0x001d);
}
if( STREQ(key, "Up") ) {
return strunicode(0x001e);
}
if( STREQ(key, "Down") ) {
return strunicode(0x001f);
}
if( STREQ(key, "Space") ) {
return strunicode(0x0020);
}
if( STREQ(key, "Delete") ) {
return strunicode(0x007f);
}
if( STREQ(key, "Home") ) {
return strunicode(0x2196);
}
if( STREQ(key, "End") ) {
return strunicode(0x2198);
}
if( STREQ(key, "Page Up") ) {
return strunicode(0x21de);
}
if( STREQ(key, "Page Down") ) {
return strunicode(0x21df);
}
return str(key);
}
id parseTextMenuItem(struct Application *app, id parentMenu, const char *title, const char *menuid, bool disabled, const char *acceleratorkey, const char **modifiers) { id parseTextMenuItem(struct Application *app, id parentMenu, const char *title, const char *menuid, bool disabled, const char *acceleratorkey, const char **modifiers) {
id item = ALLOC("NSMenuItem"); id item = ALLOC("NSMenuItem");
id wrappedId = msg(c("NSValue"), s("valueWithPointer:"), menuid); id wrappedId = msg(c("NSValue"), s("valueWithPointer:"), menuid);
msg(item, s("setRepresentedObject:"), wrappedId); msg(item, s("setRepresentedObject:"), wrappedId);
msg(item, s("initWithTitle:action:keyEquivalent:"), str(title), s("menuCallback:"), str(acceleratorkey));
id key = processAcceleratorKey(acceleratorkey);
msg(item, s("initWithTitle:action:keyEquivalent:"), str(title), s("menuCallback:"), key);
msg(item, s("setEnabled:"), !disabled); msg(item, s("setEnabled:"), !disabled);
msg(item, s("autorelease")); msg(item, s("autorelease"));
@ -1301,7 +1351,7 @@ id parseCheckboxMenuItem(struct Application *app, id parentmenu, const char *tit
} }
id parseRadioMenuItem(struct Application *app, id parentmenu, const char *title, const char *menuid, bool disabled, bool checked, const char *key) { id parseRadioMenuItem(struct Application *app, id parentmenu, const char *title, const char *menuid, bool disabled, bool checked, const char *acceleratorkey) {
id item = ALLOC("NSMenuItem"); id item = ALLOC("NSMenuItem");
// Store the item in the menu item map // Store the item in the menu item map
@ -1309,7 +1359,8 @@ id parseRadioMenuItem(struct Application *app, id parentmenu, const char *title,
id wrappedId = msg(c("NSValue"), s("valueWithPointer:"), menuid); id wrappedId = msg(c("NSValue"), s("valueWithPointer:"), menuid);
msg(item, s("setRepresentedObject:"), wrappedId); msg(item, s("setRepresentedObject:"), wrappedId);
msg(item, s("initWithTitle:action:keyEquivalent:"), str(title), s("radioMenuCallback:"), str(key)); id key = processAcceleratorKey(acceleratorkey);
msg(item, s("initWithTitle:action:keyEquivalent:"), str(title), s("radioMenuCallback:"), key);
msg(item, s("setEnabled:"), !disabled); msg(item, s("setEnabled:"), !disabled);
msg(item, s("autorelease")); msg(item, s("autorelease"));
msg(item, s("setState:"), (checked ? NSControlStateValueOn : NSControlStateValueOff)); msg(item, s("setState:"), (checked ? NSControlStateValueOn : NSControlStateValueOff));

View File

@ -22,6 +22,13 @@ type Accelerator struct {
Modifiers []Modifier Modifiers []Modifier
} }
// Accel creates a standard key Accelerator
func Accel(key string) *Accelerator {
return &Accelerator{
Key: key,
}
}
// CmdOrCtrlAccel creates a 'CmdOrCtrl' Accelerator // CmdOrCtrlAccel creates a 'CmdOrCtrl' Accelerator
func CmdOrCtrlAccel(key string) *Accelerator { func CmdOrCtrlAccel(key string) *Accelerator {
return &Accelerator{ return &Accelerator{

View File

@ -29,10 +29,26 @@ func main() {
menu.Front(), menu.Front(),
menu.SubMenu("Test Submenu", []*menu.MenuItem{ menu.SubMenu("Test Submenu", []*menu.MenuItem{
menu.TextWithAccelerator("Shift accelerator", "Shift", menu.ShiftAccel("o")), // Label = "Hi!", ID= "hello" menu.SubMenu("Accelerators", []*menu.MenuItem{
menu.TextWithAccelerator("Control accelerator", "Control", menu.ControlAccel("o")), // Label = "Hi!", ID= "hello" menu.TextWithAccelerator("Shift accelerator", "Shift", menu.ShiftAccel("o")),
menu.TextWithAccelerator("Command accelerator", "Command", menu.CmdOrCtrlAccel("o")), // Label = "Hi!", ID= "hello" menu.TextWithAccelerator("Control accelerator", "Control", menu.ControlAccel("o")),
menu.TextWithAccelerator("Option accelerator", "Option", menu.OptionOrAltAccel("o")), // Label = "Hi!", ID= "hello" menu.TextWithAccelerator("Command accelerator", "Command", menu.CmdOrCtrlAccel("o")),
menu.TextWithAccelerator("Option accelerator", "Option", menu.OptionOrAltAccel("o")),
menu.TextWithAccelerator("Backspace", "Backspace", menu.Accel("Backspace")),
menu.TextWithAccelerator("Tab", "Tab", menu.Accel("Tab")),
menu.TextWithAccelerator("Return", "Return", menu.Accel("Return")),
menu.TextWithAccelerator("Escape", "Escape", menu.Accel("Escape")),
menu.TextWithAccelerator("Left", "Left", menu.Accel("Left")),
menu.TextWithAccelerator("Right", "Right", menu.Accel("Right")),
menu.TextWithAccelerator("Up", "Up", menu.Accel("Up")),
menu.TextWithAccelerator("Down", "Down", menu.Accel("Down")),
menu.TextWithAccelerator("Space", "Space", menu.Accel("Space")),
menu.TextWithAccelerator("Delete", "Delete", menu.Accel("Delete")),
menu.TextWithAccelerator("Home", "Home", menu.Accel("Home")),
menu.TextWithAccelerator("End", "End", menu.Accel("End")),
menu.TextWithAccelerator("Page Up", "Page Up", menu.Accel("Page Up")),
menu.TextWithAccelerator("Page Down", "Page Down", menu.Accel("Page Down")),
}),
&menu.MenuItem{ &menu.MenuItem{
Label: "Disabled Menu", Label: "Disabled Menu",
Type: menu.TextType, Type: menu.TextType,