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; - (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 @end

View File

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

View File

@@ -25,12 +25,7 @@ NSString *const kMPApplciationNameKey = @"applicationName";
- (id)init { - (id)init {
self = [super init]; self = [super init];
if (self) { 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]; [self _registerHotKey];
*/
} }
return self; return self;
} }
@@ -49,7 +44,9 @@ NSString *const kMPApplciationNameKey = @"applicationName";
break; break;
} }
} }
if(currentDocument.encrypted) {
return; // No need to search in closed documents
}
/* /*
Determine the window title of the current front most application 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 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 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 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]; NSUInteger candiates = [autotypeCandidates count];
if(candiates == 0) { if(candiates == 0) {
return; // No Entries found. return; // No Entries found.

View File

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

View File

@@ -7,11 +7,25 @@
// //
#import "MPAutotypeKeyPress.h" #import "MPAutotypeKeyPress.h"
#import "MPKeyMapper.h"
@implementation MPAutotypeKeyPress @implementation MPAutotypeKeyPress
- (void)execute { - (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 @end

View File

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

View File

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

View File

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

View File

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

View File

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