OSX native shortcuts support, part 1 - issue #121

This commit is contained in:
Vadim Lopatin 2016-01-26 20:43:07 +03:00
parent 29fb51bcd1
commit f3f89282f8
3 changed files with 88 additions and 11 deletions

View File

@ -33,13 +33,36 @@ struct Accelerator {
/// Returns accelerator text description
@property dstring label() {
dstring buf;
if (keyFlags & KeyFlag.Control)
buf ~= "Ctrl+";
if (keyFlags & KeyFlag.Alt)
buf ~= "Alt+";
if (keyFlags & KeyFlag.Shift)
buf ~= "Shift+";
buf ~= toUTF32(keyName(keyCode));
version (OSX) {
static if (true) {
if (keyFlags & KeyFlag.Control)
buf ~= "Ctrl+";
if (keyFlags & KeyFlag.Shift)
buf ~= "Shift+";
if (keyFlags & KeyFlag.Option)
buf ~= "Opt+";
if (keyFlags & KeyFlag.Command)
buf ~= "Cmd+";
} else {
if (keyFlags & KeyFlag.Control)
buf ~= "⌃";
if (keyFlags & KeyFlag.Shift)
buf ~= "⇧";
if (keyFlags & KeyFlag.Option)
buf ~= "⌥";
if (keyFlags & KeyFlag.Command)
buf ~= "⌘";
}
buf ~= toUTF32(keyName(keyCode));
} else {
if (keyFlags & KeyFlag.Control)
buf ~= "Ctrl+";
if (keyFlags & KeyFlag.Alt)
buf ~= "Alt+";
if (keyFlags & KeyFlag.Shift)
buf ~= "Shift+";
buf ~= toUTF32(keyName(keyCode));
}
return cast(dstring)buf;
}
/// Serializes accelerator text description
@ -51,6 +74,8 @@ struct Accelerator {
buf ~= "Alt+";
if (keyFlags & KeyFlag.Shift)
buf ~= "Shift+";
if (keyFlags & KeyFlag.Menu)
buf ~= "Menu+";
buf ~= keyName(keyCode);
return cast(string)buf;
}
@ -76,6 +101,11 @@ struct Accelerator {
s = s[6 .. $];
flagFound = true;
}
if (s.startsWith("Menu+")) {
keyFlags |= KeyFlag.Menu;
s = s[5 .. $];
flagFound = true;
}
if (!flagFound)
break;
s = s.strip;
@ -226,12 +256,23 @@ class Action {
_id = id;
_label = labelResourceId;
_iconId = iconResourceId;
if (keyCode)
if (keyCode) {
version (OSX) {
if (keyFlags & KeyFlag.Control) {
_accelerators ~= Accelerator(keyCode, (keyFlags & ~KeyFlag.Control) | KeyFlag.Command);
}
}
_accelerators ~= Accelerator(keyCode, keyFlags);
}
}
/// action with accelerator, w/o label
this(int id, uint keyCode, uint keyFlags = 0) {
_id = id;
version (OSX) {
if (keyFlags & KeyFlag.Control) {
_accelerators ~= Accelerator(keyCode, (keyFlags & ~KeyFlag.Control) | KeyFlag.Command);
}
}
_accelerators ~= Accelerator(keyCode, keyFlags);
}
/// action with label, icon, and accelerator
@ -239,8 +280,14 @@ class Action {
_id = id;
_label = label;
_iconId = iconResourceId;
if (keyCode)
if (keyCode) {
version (OSX) {
if (keyFlags & KeyFlag.Control) {
_accelerators ~= Accelerator(keyCode, (keyFlags & ~KeyFlag.Control) | KeyFlag.Command);
}
}
_accelerators ~= Accelerator(keyCode, keyFlags);
}
}
/// returs array of accelerators
@property Accelerator[] accelerators() {
@ -290,6 +337,21 @@ class Action {
_accelerators ~= Accelerator(keyCode, keyFlags);
return this;
}
/// adds one more accelerator only if platform is OSX
Action addMacAccelerator(uint keyCode, uint keyFlags = 0) {
version (OSX) {
_accelerators ~= Accelerator(keyCode, keyFlags);
}
return this;
}
/// adds one more accelerator only if platform is not OSX
Action addNonMacAccelerator(uint keyCode, uint keyFlags = 0) {
version (OSX) {
} else {
_accelerators ~= Accelerator(keyCode, keyFlags);
}
return this;
}
/// returns true if accelerator matches provided key code and flags
bool checkAccelerator(uint keyCode, uint keyFlags) {
foreach(a; _accelerators)
@ -692,6 +754,10 @@ enum KeyFlag : uint {
Shift = 0x0004,
/// Alt key is down
Alt = 0x0080,
Option = Alt,
/// Menu key
Menu = 0x0040,
Command = Menu,
/// Right Ctrl key is down
RControl = 0x0108,
/// Right Shift key is down

View File

@ -735,6 +735,10 @@ class SDLWindow : Window {
return KeyCode.RSHIFT;
case SDLK_RALT:
return KeyCode.RALT;
case SDLK_LGUI:
return KeyCode.LWIN;
case SDLK_RGUI:
return KeyCode.RWIN;
case '/':
return KeyCode.KEY_DIVIDE;
default:
@ -750,6 +754,8 @@ class SDLWindow : Window {
res |= KeyFlag.Shift;
if (flags & KMOD_ALT)
res |= KeyFlag.Alt;
if (flags & KMOD_GUI)
res |= KeyFlag.Menu;
if (flags & KMOD_RCTRL)
res |= KeyFlag.RControl | KeyFlag.Control;
if (flags & KMOD_RSHIFT)
@ -796,6 +802,10 @@ class SDLWindow : Window {
case KeyCode.CONTROL:
flags |= KeyFlag.Control;
break;
case KeyCode.LWIN:
case KeyCode.RWIN:
flags |= KeyFlag.Menu;
break;
case KeyCode.RCONTROL:
flags |= KeyFlag.Control | KeyFlag.RControl;
break;
@ -811,6 +821,7 @@ class SDLWindow : Window {
case KeyCode.LSHIFT:
flags |= KeyFlag.Shift | KeyFlag.LShift;
break;
default:
break;
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<theme id="theme_default"
fontSize="9pt"
fontFace="Verdana,Arial,DejaVu Sans,Liberation Sans"
fontSize="10pt"
fontFace="Helvetica Neue,Verdana,Arial,DejaVu Sans,Liberation Sans,Helvetica"
fontFamily="SansSerif"
>
<color id="window_background" value="#FFFFFF"/>