mirror of
https://github.com/MacPass/MacPass.git
synced 2026-01-30 13:18:18 +00:00
Command creation works with simple paste commands.
This commit is contained in:
@@ -20,6 +20,24 @@
|
|||||||
|
|
||||||
#import <Carbon/Carbon.h>
|
#import <Carbon/Carbon.h>
|
||||||
|
|
||||||
|
@interface NSNumber (AutotypeCommand)
|
||||||
|
|
||||||
|
- (CGEventFlags)eventFlagsValue;
|
||||||
|
- (CGKeyCode)keyCodeValue;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation NSNumber (AutotypeCommand)
|
||||||
|
|
||||||
|
- (CGEventFlags)eventFlagsValue {
|
||||||
|
return (CGEventFlags)[self integerValue];
|
||||||
|
}
|
||||||
|
- (CGKeyCode)keyCodeValue {
|
||||||
|
return (CGKeyCode)[self integerValue];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
@implementation MPAutotypeCommand
|
@implementation MPAutotypeCommand
|
||||||
|
|
||||||
+ (NSDictionary *)keypressCommands {
|
+ (NSDictionary *)keypressCommands {
|
||||||
@@ -57,7 +75,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Mapping for modifier to CGEventFlags.
|
* Mapping for modifier to CGEventFlags.
|
||||||
* @note KeypassControl is mapped to command!
|
* @note KeepassControl is mapped to command!
|
||||||
*
|
*
|
||||||
* @return dictionary with commands as keys and CGEventFlags as wrapped values
|
* @return dictionary with commands as keys and CGEventFlags as wrapped values
|
||||||
*/
|
*/
|
||||||
@@ -83,7 +101,7 @@
|
|||||||
NSMutableArray __block *commandRanges = [[NSMutableArray alloc] initWithCapacity:reserverd];
|
NSMutableArray __block *commandRanges = [[NSMutableArray alloc] initWithCapacity:reserverd];
|
||||||
NSRegularExpression *commandRegExp = [[NSRegularExpression alloc] initWithPattern:@"\\{[^\\}]+\\}" options:NSRegularExpressionCaseInsensitive error:0];
|
NSRegularExpression *commandRegExp = [[NSRegularExpression alloc] initWithPattern:@"\\{[^\\}]+\\}" options:NSRegularExpressionCaseInsensitive error:0];
|
||||||
NSAssert(commandRegExp, @"RegExp is constant. Has to work all the time");
|
NSAssert(commandRegExp, @"RegExp is constant. Has to work all the time");
|
||||||
[commandRegExp enumerateMatchesInString:context.normalizedCommand options:0 range:NSMakeRange(0, [context.normalizedCommand length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
|
[commandRegExp enumerateMatchesInString:context.evaluatedCommand options:0 range:NSMakeRange(0, [context.evaluatedCommand length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
|
||||||
@autoreleasepool {
|
@autoreleasepool {
|
||||||
[commandRanges addObject:[NSValue valueWithRange:result.range]];
|
[commandRanges addObject:[NSValue valueWithRange:result.range]];
|
||||||
}
|
}
|
||||||
@@ -94,16 +112,17 @@
|
|||||||
NSRange commandRange = [rangeValue rangeValue];
|
NSRange commandRange = [rangeValue rangeValue];
|
||||||
/* All non-commands will get translated into paste commands */
|
/* All non-commands will get translated into paste commands */
|
||||||
if(commandRange.location > lastLocation) {
|
if(commandRange.location > lastLocation) {
|
||||||
/* If were modifiers we need to use the next single stroke and make update the modifier command */
|
/* If there were modifiers we need to use the next single stroke and make update the modifier command */
|
||||||
if(keyPress) {
|
if(keyPress) {
|
||||||
|
|
||||||
}
|
}
|
||||||
NSString *pasteValue = [context.normalizedCommand substringWithRange:NSMakeRange(lastLocation, commandRange.location - lastLocation)];
|
NSString *pasteValue = [context.evaluatedCommand substringWithRange:NSMakeRange(lastLocation, commandRange.location - lastLocation)];
|
||||||
// Determin if it's amodifier key, and collect them!
|
// Determin if it's amodifier key, and collect them!
|
||||||
[self appendPasteCommandForContent:pasteValue toCommands:commands];
|
[self appendPasteCommandForContent:pasteValue toCommands:commands];
|
||||||
}
|
}
|
||||||
NSString *commandString = [context.normalizedCommand substringWithRange:commandRange];
|
NSString *commandString = [context.evaluatedCommand substringWithRange:commandRange];
|
||||||
[self appendCommandForString:commandString toCommands:commands];
|
[self appendCommandForString:commandString toCommands:commands keyPressCommand:&keyPress];
|
||||||
lastLocation = commandRange.location;
|
lastLocation = commandRange.location + commandRange.length;
|
||||||
}
|
}
|
||||||
return commands;
|
return commands;
|
||||||
}
|
}
|
||||||
@@ -115,10 +134,41 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (void)appendCommandForString:(NSString *)commandString toCommands:(NSMutableArray *)commands {
|
+ (void)appendCommandForString:(NSString *)commandString toCommands:(NSMutableArray *)commands keyPressCommand:(MPAutotypeKeyPress **)keyPress {
|
||||||
MPAutotypeCommand *command;
|
|
||||||
if(!commandString) {
|
if(!commandString) {
|
||||||
NSDictionary *modifier = [self modifierCommands];
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NSNumber *flagNumber = [self modifierCommands][commandString];
|
||||||
|
NSNumber *keyCodeNumber = [self keypressCommands][commandString];
|
||||||
|
|
||||||
|
/* modifier key */
|
||||||
|
if(flagNumber) {
|
||||||
|
if(keyPress == NULL) {
|
||||||
|
return; // We need a pointer to return the keypress!
|
||||||
|
}
|
||||||
|
CGEventFlags flags = [flagNumber eventFlagsValue];
|
||||||
|
if(*keyPress == nil) {
|
||||||
|
*keyPress = [[MPAutotypeKeyPress alloc] initWithModifierMask:flags keyCode:0];
|
||||||
|
[commands addObject:*keyPress];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
NSAssert([*keyPress isKindOfClass:[MPAutotypeKeyPress class]], @"Invalid command supplied");
|
||||||
|
(*keyPress).modifierMask |= flags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* key press */
|
||||||
|
else if(keyCodeNumber) {
|
||||||
|
CGKeyCode keyCode = [keyCodeNumber keyCodeValue];
|
||||||
|
/* we have no modifers collected */
|
||||||
|
if(keyPress != NULL && keyPress == nil) {
|
||||||
|
*keyPress = [[MPAutotypeKeyPress alloc] initWithModifierMask:0 keyCode:keyCode];
|
||||||
|
[commands addObject:*keyPress];
|
||||||
|
}
|
||||||
|
/* modifers collected, set keycode */
|
||||||
|
else if(keyPress) {
|
||||||
|
(*keyPress).keyCode = keyCode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,10 @@
|
|||||||
*/
|
*/
|
||||||
@property (nonatomic, readonly, copy) NSString *command;
|
@property (nonatomic, readonly, copy) NSString *command;
|
||||||
@property (nonatomic, readonly, copy) NSString *normalizedCommand;
|
@property (nonatomic, readonly, copy) NSString *normalizedCommand;
|
||||||
|
/**
|
||||||
|
* Command with placeholders and references resolved
|
||||||
|
*/
|
||||||
|
@property (nonatomic, readonly, copy) NSString *evaluatedCommand;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Designated initializer
|
* Designated initializer
|
||||||
@@ -45,6 +49,4 @@
|
|||||||
*/
|
*/
|
||||||
- (BOOL)isValid;
|
- (BOOL)isValid;
|
||||||
|
|
||||||
- (NSString *)evaluatedCommand;
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -13,6 +13,12 @@
|
|||||||
#import "KPKWindowAssociation.h"
|
#import "KPKWindowAssociation.h"
|
||||||
#import "NSString+Commands.h"
|
#import "NSString+Commands.h"
|
||||||
|
|
||||||
|
@interface MPAutotypeContext () {
|
||||||
|
NSString *_evaluatedCommand;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
@implementation MPAutotypeContext
|
@implementation MPAutotypeContext
|
||||||
|
|
||||||
- (instancetype)initWithWindowAssociation:(KPKWindowAssociation *)association {
|
- (instancetype)initWithWindowAssociation:(KPKWindowAssociation *)association {
|
||||||
@@ -46,12 +52,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *)evaluatedCommand {
|
- (NSString *)evaluatedCommand {
|
||||||
static NSString *evaluated;
|
if(!_evaluatedCommand) {
|
||||||
if(!evaluated) {
|
|
||||||
NSString *placeholderFilled = [self.normalizedCommand evaluatePlaceholderWithEntry:self.entry];
|
NSString *placeholderFilled = [self.normalizedCommand evaluatePlaceholderWithEntry:self.entry];
|
||||||
evaluated = [placeholderFilled resolveReferencesWithTree:self.entry.tree];
|
_evaluatedCommand = [[placeholderFilled resolveReferencesWithTree:self.entry.tree] copy];
|
||||||
}
|
}
|
||||||
return evaluated;
|
return _evaluatedCommand;
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -65,7 +65,6 @@ NSString *const kMPApplciationNameKey = @"applicationName";
|
|||||||
NSMenuItem *item = [self.matchSelectionButton selectedItem];
|
NSMenuItem *item = [self.matchSelectionButton selectedItem];
|
||||||
MPAutotypeContext *context = [item representedObject];
|
MPAutotypeContext *context = [item representedObject];
|
||||||
[self.matchSelectionWindow orderOut:self];
|
[self.matchSelectionWindow orderOut:self];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)_didPressHotKey {
|
- (void)_didPressHotKey {
|
||||||
|
|||||||
@@ -16,4 +16,6 @@
|
|||||||
@property (assign) CGEventFlags modifierMask;
|
@property (assign) CGEventFlags modifierMask;
|
||||||
@property (assign) CGKeyCode keyCode;
|
@property (assign) CGKeyCode keyCode;
|
||||||
|
|
||||||
|
- (instancetype)initWithModifierMask:(CGEventFlags)modiferMask keyCode:(CGKeyCode)code;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -11,6 +11,15 @@
|
|||||||
|
|
||||||
@implementation MPAutotypeKeyPress
|
@implementation MPAutotypeKeyPress
|
||||||
|
|
||||||
|
- (instancetype)initWithModifierMask:(CGEventFlags)modiferMask keyCode:(CGKeyCode)code {
|
||||||
|
self = [super init];
|
||||||
|
if(self) {
|
||||||
|
_modifierMask = modiferMask;
|
||||||
|
_keyCode = code;
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)execute {
|
- (void)execute {
|
||||||
if(![self isValid]) {
|
if(![self isValid]) {
|
||||||
return; // no valid command. Stop.
|
return; // no valid command. Stop.
|
||||||
|
|||||||
Reference in New Issue
Block a user