Restrucuterde Autotype commands a bit

This commit is contained in:
michael starke
2014-02-11 20:58:27 +01:00
parent 0d66451e0c
commit 4f55d93451
11 changed files with 66 additions and 32 deletions

View File

@@ -45,4 +45,11 @@ extern NSString *const kMPAutptypeCommandEnter;
*/
- (void)execute;
/**
* Validates the command and returns the result
*
* @return YES if the command is valid and can be executed. NO otherwise
*/
- (BOOL)isValid;
@end

View File

@@ -118,4 +118,8 @@ NSString *const kMPAutotypeCommandF1 = @"{F1}";
- (void)execute {
NSAssert(NO, @"Not Implemented");
}
- (BOOL)isValid {
return NO; // No valid command
}
@end

View File

@@ -25,12 +25,7 @@ NSString *const kMPApplciationNameKey = @"applicationName";
- (id)init {
self = [super init];
if (self) {
/*
Test the system for enabled access for assistive devices. Otherwise we cannot work properly
Use defaults to determine if global hotkey is enabled
[self _registerHotKey];
*/
}
return self;
}
@@ -49,7 +44,9 @@ NSString *const kMPApplciationNameKey = @"applicationName";
break;
}
}
if(currentDocument.encrypted) {
return; // No need to search in closed documents
}
/*
Determine the window title of the current front most application
Start searching the db for the best fit (based on title, then on window associations
@@ -63,7 +60,7 @@ NSString *const kMPApplciationNameKey = @"applicationName";
Query the document to generate a autotype command list for the window title
We do not care where this came form, just get the autotype commands
*/
NSArray *autotypeCandidates = [[currentDocument buildContextsForWindowTitle:windowTitle] lastObject];
NSArray *autotypeCandidates = [[currentDocument autotypContextsForWindowTitle:windowTitle] lastObject];
NSUInteger candiates = [autotypeCandidates count];
if(candiates == 0) {
return; // No Entries found.

View File

@@ -8,7 +8,12 @@
#import "MPAutotypeCommand.h"
/**
* Autotype command to press a single key. Can be used with modifer keys as well
*/
@interface MPAutotypeKeyPress : MPAutotypeCommand
@property (assign) CGEventFlags modifierMask;
@property (assign) CGKeyCode keyCode;
@end

View File

@@ -7,11 +7,25 @@
//
#import "MPAutotypeKeyPress.h"
#import "MPKeyMapper.h"
@implementation MPAutotypeKeyPress
- (void)execute {
if(![self isValid]) {
return; // no valid command. Stop.
}
CGKeyCode mappedKey = [self _transformKeyCode];
[self sendPressKey:mappedKey modifierFlags:self.modifierMask];
}
- (BOOL)isValid {
return ([self _transformKeyCode] != kMPUnknownKeyCode);
}
- (CGKeyCode)_transformKeyCode {
NSString *key = [MPKeyMapper stringForKey:self.keyCode];
return [MPKeyMapper keyCodeForCharacter:key];
}
@end

View File

@@ -8,6 +8,11 @@
#import "MPAutotypeCommand.h"
/**
* Simple Paste action. Uses the Clipboard to copy and then paste contents in place
*/
@interface MPAutotypePaste : MPAutotypeCommand
- (instancetype)initWithString:(NSString *)aString;
@end

View File

@@ -13,29 +13,31 @@
@interface MPAutotypePaste ()
@property (retain) NSString *commandString;
@property (strong) NSString *pasteData;
@end
@implementation MPAutotypePaste
/**
* Simple copy paste action
*/
- (void)executeWithEntry:(KPKEntry *)entry {
if([self.commandString length] > 0) {
- (instancetype)initWithString:(NSString *)aString {
self = [super init];
if(self) {
self.pasteData = aString;
}
return self;
}
- (void)execute {
if([self.pasteData length] > 0) {
MPPasteBoardController *controller = [MPPasteBoardController defaultController];
if([self.commandString isPlaceholder]) {
BOOL didReplace;
NSString *evaluatedPlaceholder = [self.commandString evaluatePlaceholderWithEntry:entry didReplace:&didReplace];
[controller copyObjects:@[evaluatedPlaceholder]];
}
else {
[controller copyObjects:@[self.commandString]];
}
/* Find the correct key code! */
[controller copyObjects:@[self.pasteData]];
[self sendPasteKeyCode];
}
}
- (BOOL)isValid {
/* Pasting shoudl always be valid */
return YES;
}
@end

View File

@@ -24,6 +24,6 @@
@interface MPDocument (Autotype)
- (NSArray *)buildContextsForWindowTitle:(NSString *)windowTitle;
- (NSArray *)autotypContextsForWindowTitle:(NSString *)windowTitle;
@end

View File

@@ -30,21 +30,22 @@
@implementation MPDocument (Autotype)
- (NSArray *)buildContextsForWindowTitle:(NSString *)windowTitle {
- (NSArray *)autotypContextsForWindowTitle:(NSString *)windowTitle {
NSArray *autotypeEntries = [self.root autotypeableChildEntries];
NSMutableArray *contexts = [[NSMutableArray alloc] initWithCapacity:ceil([autotypeEntries count] / 4.0)];
for(KPKEntry *entry in autotypeEntries) {
/* Test for title */
NSRange titleRange = [entry.title rangeOfString:windowTitle options:NSCaseInsensitiveSearch];
MPAutotypeContext *context;
if(titleRange.location != NSNotFound && titleRange.length != 0) {
MPAutotypeContext *context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:entry.autotype.defaultSequence];
[contexts addObject:context];
context = [[MPAutotypeContext alloc] initWithEntry:entry andSequence:entry.autotype.defaultSequence];
}
/* search in Autotype entries for match */
else {
KPKWindowAssociation *association = [entry.autotype windowAssociationMatchingWindowTitle:windowTitle];
MPAutotypeContext *context = [[MPAutotypeContext alloc] initWithWindowAssociation:association];
context = [[MPAutotypeContext alloc] initWithWindowAssociation:association];
}
if(context) {
[contexts addObject:context];
}
}

View File

@@ -15,7 +15,7 @@ extern uint16_t const kMPUnknownKeyCode;
/**
* Retrieves the string representation with the current keyboard mapping for the keycode
*
* @param keyCode The virutal keycode to be pressed
* @param keyCode The virtual keycode to be pressed
* @return NSString containing the current mapping for the keyCode
*/
+ (NSString *)stringForKey:(CGKeyCode)keyCode;

View File

@@ -31,5 +31,4 @@
return NSLocalizedString(@"UPDATE_SETTINGS", @"Update Settings Label");
}
@end