Bug 282097 - Part 3: Add key bindings recorder to nsCocoaUtils. r=masayuki, r=smichaud

This commit is contained in:
J. Ryan Stinnett 2013-07-10 09:07:39 -05:00
parent 278ae6155f
commit 422f8b06fc
2 changed files with 81 additions and 0 deletions

View File

@ -12,6 +12,7 @@
#include "imgIContainer.h"
#include "nsEvent.h"
#include "npapi.h"
#include "nsTArray.h"
// This must be the last include:
#include "nsObjCExceptions.h"
@ -78,6 +79,26 @@ private:
@end
struct KeyBindingsCommand
{
SEL selector;
id data;
};
@interface NativeKeyBindingsRecorder : NSResponder
{
@private
nsTArray<KeyBindingsCommand>* mCommands;
}
- (void)startRecording:(nsTArray<KeyBindingsCommand>&)aCommands;
- (void)doCommandBySelector:(SEL)aSelector;
- (void)insertText:(id)aString;
@end // NativeKeyBindingsRecorder
class nsCocoaUtils
{
public:
@ -286,6 +307,14 @@ class nsCocoaUtils
* once we're comfortable with the HiDPI behavior.
*/
static bool HiDPIEnabled();
/**
* Keys can optionally be bound by system or user key bindings to one or more
* commands based on selectors. This collects any such commands in the
* provided array.
*/
static void GetCommandsFromKeyEvent(NSEvent* aEvent,
nsTArray<KeyBindingsCommand>& aCommands);
};
#endif // nsCocoaUtils_h_

View File

@ -574,3 +574,55 @@ nsCocoaUtils::HiDPIEnabled()
return sHiDPIEnabled;
}
void
nsCocoaUtils::GetCommandsFromKeyEvent(NSEvent* aEvent,
nsTArray<KeyBindingsCommand>& aCommands)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
MOZ_ASSERT(aEvent);
static NativeKeyBindingsRecorder* sNativeKeyBindingsRecorder;
if (!sNativeKeyBindingsRecorder) {
sNativeKeyBindingsRecorder = [NativeKeyBindingsRecorder new];
}
[sNativeKeyBindingsRecorder startRecording:aCommands];
// This will trigger 0 - N calls to doCommandBySelector: and insertText:
[sNativeKeyBindingsRecorder
interpretKeyEvents:[NSArray arrayWithObject:aEvent]];
NS_OBJC_END_TRY_ABORT_BLOCK;
}
@implementation NativeKeyBindingsRecorder
- (void)startRecording:(nsTArray<KeyBindingsCommand>&)aCommands
{
mCommands = &aCommands;
mCommands->Clear();
}
- (void)doCommandBySelector:(SEL)aSelector
{
KeyBindingsCommand command = {
aSelector,
nil
};
mCommands->AppendElement(command);
}
- (void)insertText:(id)aString
{
KeyBindingsCommand command = {
@selector(insertText:),
aString
};
mCommands->AppendElement(command);
}
@end // NativeKeyBindingsRecorder