mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-14 11:42:30 +00:00
Restrucuterde Autotype commands a bit
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -118,4 +118,8 @@ NSString *const kMPAutotypeCommandF1 = @"{F1}";
|
||||
- (void)execute {
|
||||
NSAssert(NO, @"Not Implemented");
|
||||
}
|
||||
|
||||
- (BOOL)isValid {
|
||||
return NO; // No valid command
|
||||
}
|
||||
@end
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -24,6 +24,6 @@
|
||||
|
||||
@interface MPDocument (Autotype)
|
||||
|
||||
- (NSArray *)buildContextsForWindowTitle:(NSString *)windowTitle;
|
||||
- (NSArray *)autotypContextsForWindowTitle:(NSString *)windowTitle;
|
||||
|
||||
@end
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -31,5 +31,4 @@
|
||||
return NSLocalizedString(@"UPDATE_SETTINGS", @"Update Settings Label");
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user