Skip to content

Commit

Permalink
Add keyboard shortcuts support when using UIKit for Mac (Project Cata…
Browse files Browse the repository at this point in the history
…lyst)

Fixes FLEXTool#297.
  • Loading branch information
revolter committed Aug 12, 2019
1 parent 85cc51b commit f7659a8
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Classes/FLEXManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,11 @@ typedef UIViewController *(^FLEXCustomContentViewerFuture)(NSData *data);
- (void)setCustomViewerForContentType:(NSString *)contentType
viewControllerFutureBlock:(FLEXCustomContentViewerFuture)viewControllerFutureBlock;

#if TARGET_OS_MACCATALYST

/// Gets the default and custom keyboard shortcuts commands.
- (NSArray<UIKeyCommand *> *)getKeyCommands;

#endif

@end
15 changes: 12 additions & 3 deletions Classes/Manager/FLEXManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,21 @@ - (void)explorerViewControllerDidFinish:(FLEXExplorerViewController *)explorerVi

- (void)registerSimulatorShortcutWithKey:(NSString *)key modifiers:(UIKeyModifierFlags)modifiers action:(dispatch_block_t)action description:(NSString *)description
{
# if TARGET_OS_SIMULATOR
# if TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST
[[FLEXKeyboardShortcutManager sharedManager] registerSimulatorShortcutWithKey:key modifiers:modifiers action:action description:description];
#endif
}

- (void)setSimulatorShortcutsEnabled:(BOOL)simulatorShortcutsEnabled
{
# if TARGET_OS_SIMULATOR
# if TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST
[[FLEXKeyboardShortcutManager sharedManager] setEnabled:simulatorShortcutsEnabled];
#endif
}

- (BOOL)simulatorShortcutsEnabled
{
# if TARGET_OS_SIMULATOR
# if TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST
return [[FLEXKeyboardShortcutManager sharedManager] isEnabled];
#else
return NO;
Expand Down Expand Up @@ -378,4 +378,13 @@ - (void)showExplorerIfNeeded
}
}

#if TARGET_OS_MACCATALYST

- (NSArray<UIKeyCommand *> *)getKeyCommands {

return [[FLEXKeyboardShortcutManager sharedManager] getKeyCommands];
}

#endif

@end
2 changes: 1 addition & 1 deletion Classes/Utility/FLEXKeyboardHelpViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ - (void)viewDidLoad
self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.textView];
#if TARGET_OS_SIMULATOR
#if TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST
self.textView.text = [[FLEXKeyboardShortcutManager sharedManager] keyboardShortcutsDescription];
#endif
self.textView.backgroundColor = [UIColor blackColor];
Expand Down
8 changes: 7 additions & 1 deletion Classes/Utility/FLEXKeyboardShortcutManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import <UIKit/UIKit.h>

#if TARGET_OS_SIMULATOR
#if TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST

@interface FLEXKeyboardShortcutManager : NSObject

Expand All @@ -19,6 +19,12 @@

@property (nonatomic, assign, getter=isEnabled) BOOL enabled;

#if TARGET_OS_MACCATALYST

- (NSArray<UIKeyCommand *> *)getKeyCommands;

#endif

@end

#endif
126 changes: 125 additions & 1 deletion Classes/Utility/FLEXKeyboardShortcutManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#import <objc/runtime.h>
#import <objc/message.h>

#if TARGET_OS_SIMULATOR
#if TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST

@interface UIEvent (UIPhysicalKeyboardEvent)

Expand All @@ -23,21 +23,109 @@ @interface UIEvent (UIPhysicalKeyboardEvent)

@end

#if TARGET_OS_MACCATALYST

@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, assign, readonly) UIKeyModifierFlags flags;
@property (nonatomic, copy, readonly) NSString *helpDescription;

@end

#if TARGET_OS_MACCATALYST

@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
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 @@ -98,6 +186,23 @@ + (instancetype)keyInputForKey:(NSString *)key flags:(UIKeyModifierFlags)flags
return [self keyInputForKey:key flags:flags helpDescription:nil];
}

#if TARGET_OS_MACCATALYST

+ (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
{
FLEXKeyInput *keyInput = [[self alloc] init];
Expand All @@ -109,12 +214,22 @@ + (instancetype)keyInputForKey:(NSString *)key flags:(UIKeyModifierFlags)flags h
return keyInput;
}

#endif

@end

@interface FLEXKeyboardShortcutManager ()

#if TARGET_OS_MACCATALYST

@property (nonatomic, strong) NSMutableDictionary<UIKeyCommand *, dispatch_block_t> *actionsForKeyInputs;

#else

@property (nonatomic, strong) NSMutableDictionary<FLEXKeyInput *, dispatch_block_t> *actionsForKeyInputs;

#endif

@property (nonatomic, assign, getter=isPressingShift) BOOL pressingShift;
@property (nonatomic, assign, getter=isPressingCommand) BOOL pressingCommand;
@property (nonatomic, assign, getter=isPressingControl) BOOL pressingControl;
Expand Down Expand Up @@ -305,6 +420,15 @@ - (NSString *)keyboardShortcutsDescription
return [description copy];
}

#if TARGET_OS_MACCATALYST

- (NSArray<UIKeyCommand *> *)getKeyCommands {

return self.actionsForKeyInputs.allKeys;
}

#endif

@end

#endif

0 comments on commit f7659a8

Please sign in to comment.