Skip to content

Commit

Permalink
Simplify Mac shortcuts implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
revolter committed Apr 29, 2022
1 parent 72edbdc commit 5e33058
Showing 1 changed file with 3 additions and 109 deletions.
112 changes: 3 additions & 109 deletions Classes/Utility/Keyboard/FLEXKeyboardShortcutManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,104 +23,20 @@ @interface UIEvent (UIPhysicalKeyboardEvent)

@end

#if TARGET_OS_MACCATALYST || TARGET_CPU_ARM64

@interface FLEXKeyInput : UIKeyCommand @end

@interface UIKeyCommand (FLEX)

@property (nonatomic, assign, readonly) BOOL isCreatedByFLEX;

#else

@interface FLEXKeyInput : NSObject <NSCopying>

#endif

@property (nonatomic, copy, readonly) NSString *key;
@property (nonatomic, readonly) UIKeyModifierFlags flags;
@property (nonatomic, copy, readonly) NSString *helpDescription;

@end

#if TARGET_OS_MACCATALYST || TARGET_CPU_ARM64

@implementation FLEXKeyInput @end

@implementation UIKeyCommand (FLEX)

- (NSString *)key {

return objc_getAssociatedObject(self, @selector(key));
}

- (void)setKey:(NSString *)key {

objc_setAssociatedObject(self, @selector(key), key, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIKeyModifierFlags)flags {

UIKeyModifierFlags (^block)() = objc_getAssociatedObject(self, @selector(flags));

return (block ? block() : kNilOptions);
}

- (void)setFlags:(UIKeyModifierFlags)flags {

UIKeyModifierFlags (^block)() = ^{
return flags;
};

objc_setAssociatedObject(self, @selector(flags), block, OBJC_ASSOCIATION_COPY);
}

- (NSString *)helpDescription {

return objc_getAssociatedObject(self, @selector(helpDescription));
}

- (void)setHelpDescription:(NSString *)helpDescription {

objc_setAssociatedObject(self, @selector(helpDescription), helpDescription, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (BOOL)isCreatedByFLEX {

BOOL (^block)() = objc_getAssociatedObject(self, @selector(isCreatedByFLEX));

return (block ? block() : NO);
}

- (void)setIsCreatedByFLEX:(BOOL)isCreatedByFLEX {

BOOL (^block)() = ^{
return isCreatedByFLEX;
};

objc_setAssociatedObject(self, @selector(isCreatedByFLEX), block, OBJC_ASSOCIATION_COPY);
}

#else

@implementation FLEXKeyInput

#endif

- (BOOL)isEqual:(id)object {
BOOL isEqual = NO;
#if TARGET_OS_MACCATALYST || TARGET_CPU_ARM64
if ([object isKindOfClass:[UIKeyCommand class]]) {
UIKeyCommand *keyCommand = (UIKeyCommand *)object;
if (!keyCommand.isCreatedByFLEX) {
// Not FLEX's business anymore.

return [super isEqual:object];
}
#else
if ([object isKindOfClass:[FLEXKeyInput class]]) {
FLEXKeyInput *keyCommand = (FLEXKeyInput *)object;
#endif
BOOL equalKeys = self.key == keyCommand.key || [self.key isEqual:keyCommand.key];
BOOL equalFlags = self.flags == keyCommand.flags;
isEqual = equalKeys && equalFlags;
Expand Down Expand Up @@ -179,24 +95,6 @@ + (instancetype)keyInputForKey:(NSString *)key flags:(UIKeyModifierFlags)flags {
return [self keyInputForKey:key flags:flags helpDescription:nil];
}

#if TARGET_OS_MACCATALYST || TARGET_CPU_ARM64

+ (instancetype)keyInputForKey:(NSString *)key
flags:(UIKeyModifierFlags)flags
helpDescription:(NSString *)helpDescription {
FLEXKeyInput *keyInput = [UIKeyCommand keyCommandWithInput:key modifierFlags:flags action:nil];
if (keyInput) {
[keyInput setKey:key];
[keyInput setFlags:flags];
[keyInput setHelpDescription:helpDescription];

[keyInput setIsCreatedByFLEX:YES];
}
return keyInput;
}

#else

+ (instancetype)keyInputForKey:(NSString *)key
flags:(UIKeyModifierFlags)flags
helpDescription:(NSString *)helpDescription {
Expand All @@ -209,17 +107,11 @@ + (instancetype)keyInputForKey:(NSString *)key
return keyInput;
}

#endif

@end

@interface FLEXKeyboardShortcutManager ()

#if TARGET_OS_MACCATALYST || TARGET_CPU_ARM64
@property (nonatomic, strong) NSMutableDictionary<UIKeyCommand *, dispatch_block_t> *actionsForKeyInputs;
#else
@property (nonatomic) NSMutableDictionary<FLEXKeyInput *, dispatch_block_t> *actionsForKeyInputs;
#endif

@property (nonatomic, getter=isPressingShift) BOOL pressingShift;
@property (nonatomic, getter=isPressingCommand) BOOL pressingCommand;
Expand Down Expand Up @@ -432,7 +324,9 @@ - (NSString *)keyboardShortcutsDescription {
#if TARGET_OS_MACCATALYST || TARGET_CPU_ARM64

- (NSArray<UIKeyCommand *> *)getKeyCommands {
return self.actionsForKeyInputs.allKeys;
return [self.actionsForKeyInputs.allKeys flex_mapped:^UIKeyCommand *(FLEXKeyInput *keyInput, NSUInteger index) {
return [UIKeyCommand keyCommandWithInput:keyInput.key modifierFlags:keyInput.flags action:nil];
}];
}

#endif
Expand Down

0 comments on commit 5e33058

Please sign in to comment.