Selaa lähdekoodia

Expand default FLEX keyboard shortcuts

Ryan Olson 10 vuotta sitten
vanhempi
commit
6d3afe36d1

+ 11 - 0
Classes/ExplorerToolbar/FLEXExplorerViewController.h

@@ -17,6 +17,17 @@
 - (BOOL)shouldReceiveTouchAtWindowPoint:(CGPoint)pointInWindowCoordinates;
 - (BOOL)wantsWindowToBecomeKey;
 
+// Keyboard shortcut helpers
+
+- (void)toggleSelectTool;
+- (void)toggleMoveTool;
+- (void)toggleViewsTool;
+- (void)toggleMenuTool;
+- (void)handleDownArrowKeyPressed;
+- (void)handleUpArrowKeyPressed;
+- (void)handleRightArrowKeyPressed;
+- (void)handleLeftArrowKeyPressed;
+
 @end
 
 @protocol FLEXExplorerViewControllerDelegate <NSObject>

+ 104 - 21
Classes/ExplorerToolbar/FLEXExplorerViewController.m

@@ -14,6 +14,7 @@
 #import "FLEXGlobalsTableViewController.h"
 #import "FLEXObjectExplorerViewController.h"
 #import "FLEXObjectExplorerFactory.h"
+#import "FLEXNetworkHistoryTableViewController.h"
 
 typedef NS_ENUM(NSUInteger, FLEXExplorerMode) {
     FLEXExplorerModeDefault,
@@ -367,21 +368,12 @@ typedef NS_ENUM(NSUInteger, FLEXExplorerMode) {
 
 - (void)selectButtonTapped:(FLEXToolbarItem *)sender
 {
-    if (self.currentMode == FLEXExplorerModeSelect) {
-        self.currentMode = FLEXExplorerModeDefault;
-    } else {
-        self.currentMode = FLEXExplorerModeSelect;
-    }
+    [self toggleSelectTool];
 }
 
 - (void)hierarchyButtonTapped:(FLEXToolbarItem *)sender
 {
-    NSArray *allViews = [self allViewsInHierarchy];
-    NSDictionary *depthsForViews = [self hierarchyDepthsForViews:allViews];
-    FLEXHierarchyTableViewController *hierarchyTVC = [[FLEXHierarchyTableViewController alloc] initWithViews:allViews viewsAtTap:self.viewsAtTapPoint selectedView:self.selectedView depths:depthsForViews];
-    hierarchyTVC.delegate = self;
-    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:hierarchyTVC];
-    [self makeKeyAndPresentViewController:navigationController animated:YES completion:nil];
+    [self toggleViewsTool];
 }
 
 - (NSArray *)allViewsInHierarchy
@@ -427,20 +419,12 @@ typedef NS_ENUM(NSUInteger, FLEXExplorerMode) {
 
 - (void)moveButtonTapped:(FLEXToolbarItem *)sender
 {
-    if (self.currentMode == FLEXExplorerModeMove) {
-        self.currentMode = FLEXExplorerModeDefault;
-    } else {
-        self.currentMode = FLEXExplorerModeMove;
-    }
+    [self toggleMoveTool];
 }
 
 - (void)globalsButtonTapped:(FLEXToolbarItem *)sender
 {
-    FLEXGlobalsTableViewController *globalsViewController = [[FLEXGlobalsTableViewController alloc] init];
-    globalsViewController.delegate = self;
-    [FLEXGlobalsTableViewController setApplicationWindow:[[UIApplication sharedApplication] keyWindow]];
-    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:globalsViewController];
-    [self makeKeyAndPresentViewController:navigationController animated:YES completion:nil];
+    [self toggleMenuTool];
 }
 
 - (void)closeButtonTapped:(FLEXToolbarItem *)sender
@@ -830,4 +814,103 @@ typedef NS_ENUM(NSUInteger, FLEXExplorerMode) {
     return self.previousKeyWindow != nil;
 }
 
+#pragma mark - Keyboard Shortcut Helpers
+
+- (void)toggleSelectTool
+{
+    if (self.currentMode == FLEXExplorerModeSelect) {
+        self.currentMode = FLEXExplorerModeDefault;
+    } else {
+        self.currentMode = FLEXExplorerModeSelect;
+    }
+}
+
+- (void)toggleMoveTool
+{
+    if (self.currentMode == FLEXExplorerModeMove) {
+        self.currentMode = FLEXExplorerModeDefault;
+    } else {
+        self.currentMode = FLEXExplorerModeMove;
+    }
+}
+
+- (void)toggleViewsTool
+{
+    BOOL viewsModalShown = [[self presentedViewController] isKindOfClass:[UINavigationController class]];
+    viewsModalShown = viewsModalShown && [[[(UINavigationController *)[self presentedViewController] viewControllers] firstObject] isKindOfClass:[FLEXHierarchyTableViewController class]];
+    if (viewsModalShown) {
+        [self resignKeyAndDismissViewControllerAnimated:YES completion:nil];
+    } else {
+        [self resignKeyAndDismissViewControllerAnimated:NO completion:nil];
+        NSArray *allViews = [self allViewsInHierarchy];
+        NSDictionary *depthsForViews = [self hierarchyDepthsForViews:allViews];
+        FLEXHierarchyTableViewController *hierarchyTVC = [[FLEXHierarchyTableViewController alloc] initWithViews:allViews viewsAtTap:self.viewsAtTapPoint selectedView:self.selectedView depths:depthsForViews];
+        hierarchyTVC.delegate = self;
+        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:hierarchyTVC];
+        [self makeKeyAndPresentViewController:navigationController animated:YES completion:nil];
+    }
+}
+
+- (void)toggleMenuTool
+{
+    BOOL menuModalShown = [[self presentedViewController] isKindOfClass:[UINavigationController class]];
+    menuModalShown = menuModalShown && [[[(UINavigationController *)[self presentedViewController] viewControllers] firstObject] isKindOfClass:[FLEXGlobalsTableViewController class]];
+    if (menuModalShown) {
+        [self resignKeyAndDismissViewControllerAnimated:YES completion:nil];
+    } else {
+        [self resignKeyAndDismissViewControllerAnimated:NO completion:nil];
+        FLEXGlobalsTableViewController *globalsViewController = [[FLEXGlobalsTableViewController alloc] init];
+        globalsViewController.delegate = self;
+        [FLEXGlobalsTableViewController setApplicationWindow:[[UIApplication sharedApplication] keyWindow]];
+        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:globalsViewController];
+        [self makeKeyAndPresentViewController:navigationController animated:YES completion:nil];
+    }
+}
+
+- (void)handleDownArrowKeyPressed
+{
+    if (self.currentMode == FLEXExplorerModeMove) {
+        CGRect frame = self.selectedView.frame;
+        frame.origin.y += 1.0 / [[UIScreen mainScreen] scale];
+        self.selectedView.frame = frame;
+    } else if (self.currentMode == FLEXExplorerModeSelect && [self.viewsAtTapPoint count] > 0) {
+        NSInteger selectedViewIndex = [self.viewsAtTapPoint indexOfObject:self.selectedView];
+        if (selectedViewIndex > 0) {
+            self.selectedView = [self.viewsAtTapPoint objectAtIndex:selectedViewIndex - 1];
+        }
+    }
+}
+
+- (void)handleUpArrowKeyPressed
+{
+    if (self.currentMode == FLEXExplorerModeMove) {
+        CGRect frame = self.selectedView.frame;
+        frame.origin.y -= 1.0 / [[UIScreen mainScreen] scale];
+        self.selectedView.frame = frame;
+    } else if (self.currentMode == FLEXExplorerModeSelect && [self.viewsAtTapPoint count] > 0) {
+        NSInteger selectedViewIndex = [self.viewsAtTapPoint indexOfObject:self.selectedView];
+        if (selectedViewIndex < [self.viewsAtTapPoint count] - 1) {
+            self.selectedView = [self.viewsAtTapPoint objectAtIndex:selectedViewIndex + 1];
+        }
+    }
+}
+
+- (void)handleRightArrowKeyPressed
+{
+    if (self.currentMode == FLEXExplorerModeMove) {
+        CGRect frame = self.selectedView.frame;
+        frame.origin.x += 1.0 / [[UIScreen mainScreen] scale];
+        self.selectedView.frame = frame;
+    }
+}
+
+- (void)handleLeftArrowKeyPressed
+{
+    if (self.currentMode == FLEXExplorerModeMove) {
+        CGRect frame = self.selectedView.frame;
+        frame.origin.x -= 1.0 / [[UIScreen mainScreen] scale];
+        self.selectedView.frame = frame;
+    }
+}
+
 @end

+ 143 - 0
Classes/ExplorerToolbar/FLEXManager.m

@@ -15,6 +15,8 @@
 #import "FLEXNetworkObserver.h"
 #import "FLEXNetworkRecorder.h"
 #import "FLEXKeyboardShortcutManager.h"
+#import "FLEXFileBrowserTableViewController.h"
+#import "FLEXNetworkHistoryTableViewController.h"
 
 @interface FLEXManager () <FLEXWindowEventDelegate, FLEXExplorerViewControllerDelegate>
 
@@ -160,6 +162,68 @@
             [self hideExplorer];
         }
     } description:@"Toggle FLEX toolbar"];
+    
+    [self registerSimulatorShortcutWithKey:@"g" modifiers:0 action:^{
+        [self showExplorerIfNeeded];
+        [self.explorerViewController toggleMenuTool];
+    } description:@"Toggle FLEX globlas menu"];
+    
+    [self registerSimulatorShortcutWithKey:@"v" modifiers:0 action:^{
+        [self showExplorerIfNeeded];
+        [self.explorerViewController toggleViewsTool];
+    } description:@"Toggle view hierarchy menu"];
+    
+    [self registerSimulatorShortcutWithKey:@"s" modifiers:0 action:^{
+        [self showExplorerIfNeeded];
+        [self.explorerViewController toggleSelectTool];
+    } description:@"Toggle select tool"];
+    
+    [self registerSimulatorShortcutWithKey:@"m" modifiers:0 action:^{
+        [self showExplorerIfNeeded];
+        [self.explorerViewController toggleMoveTool];
+    } description:@"Toggle move tool"];
+    
+    [self registerSimulatorShortcutWithKey:@"n" modifiers:0 action:^{
+        [self toggleTopViewControllerOfClass:[FLEXNetworkHistoryTableViewController class]];
+    } description:@"Toggle network history view"];
+    
+    [self registerSimulatorShortcutWithKey:UIKeyInputDownArrow modifiers:0 action:^{
+        if ([self isHidden]) {
+            [self tryScrollDown];
+        } else {
+            [self.explorerViewController handleDownArrowKeyPressed];
+        }
+    } description:@"Cycle view selection\n\t\tMove view down\n\t\tScroll down"];
+    
+    [self registerSimulatorShortcutWithKey:UIKeyInputUpArrow modifiers:0 action:^{
+        if ([self isHidden]) {
+            [self tryScrollUp];
+        } else {
+            [self.explorerViewController handleUpArrowKeyPressed];
+        }
+    } description:@"Cycle view selection\n\t\tMove view up\n\t\tScroll up"];
+    
+    [self registerSimulatorShortcutWithKey:UIKeyInputRightArrow modifiers:0 action:^{
+        if (![self isHidden]) {
+            [self.explorerViewController handleRightArrowKeyPressed];
+        }
+    } description:@"Move selected view right"];
+    
+    [self registerSimulatorShortcutWithKey:UIKeyInputLeftArrow modifiers:0 action:^{
+        if ([self isHidden]) {
+            [self tryGoBack];
+        } else {
+            [self.explorerViewController handleLeftArrowKeyPressed];
+        }
+    } description:@"Move selected view left"];
+    
+    [self registerSimulatorShortcutWithKey:UIKeyInputEscape modifiers:0 action:^{
+        [[[self topViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];
+    } description:@"End editing text\n\t\tDismiss top view controller"];
+    
+    [self registerSimulatorShortcutWithKey:@"o" modifiers:UIKeyModifierCommand|UIKeyModifierShift action:^{
+        [self toggleTopViewControllerOfClass:[FLEXFileBrowserTableViewController class]];
+    } description:@"Toggle file browser menu"];
 }
 
 + (void)load
@@ -205,4 +269,83 @@
     [self.userGlobalEntries addObject:entry];
 }
 
+- (void)tryScrollDown
+{
+    UIScrollView *firstScrollView = [self firstScrollView];
+    CGPoint contentOffset = [firstScrollView contentOffset];
+    CGFloat distance = floor(firstScrollView.frame.size.height / 2.0);
+    CGFloat maxContentOffsetY = firstScrollView.contentSize.height + firstScrollView.contentInset.bottom - firstScrollView.frame.size.height;
+    distance = MIN(maxContentOffsetY - firstScrollView.contentOffset.y, distance);
+    contentOffset.y += distance;
+    [firstScrollView setContentOffset:contentOffset animated:YES];
+}
+
+- (void)tryScrollUp
+{
+    UIScrollView *firstScrollView = [self firstScrollView];
+    CGPoint contentOffset = [firstScrollView contentOffset];
+    CGFloat distance = floor(firstScrollView.frame.size.height / 2.0);
+    CGFloat minContentOffsetY = -firstScrollView.contentInset.top;
+    distance = MIN(firstScrollView.contentOffset.y - minContentOffsetY, distance);
+    contentOffset.y -= distance;
+    [firstScrollView setContentOffset:contentOffset animated:YES];
+}
+
+- (UIScrollView *)firstScrollView
+{
+    NSMutableArray *views = [[[[UIApplication sharedApplication] keyWindow] subviews] mutableCopy];
+    UIScrollView *scrollView = nil;
+    while ([views count] > 0) {
+        UIView *view = [views firstObject];
+        [views removeObjectAtIndex:0];
+        if ([view isKindOfClass:[UIScrollView class]]) {
+            scrollView = (UIScrollView *)view;
+            break;
+        } else {
+            [views addObjectsFromArray:[view subviews]];
+        }
+    }
+    return scrollView;
+}
+
+- (void)tryGoBack
+{
+    UINavigationController *navigationController = nil;
+    UIViewController *topViewController = [self topViewController];
+    if ([topViewController isKindOfClass:[UINavigationController class]]) {
+        navigationController = (UINavigationController *)topViewController;
+    } else {
+        navigationController = topViewController.navigationController;
+    }
+    [navigationController popViewControllerAnimated:YES];
+}
+
+- (UIViewController *)topViewController
+{
+    UIViewController *topViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
+    while ([topViewController presentedViewController]) {
+        topViewController = [topViewController presentedViewController];
+    }
+    return topViewController;
+}
+
+- (void)toggleTopViewControllerOfClass:(Class)class
+{
+    UIViewController *topViewController = [self topViewController];
+    if ([topViewController isKindOfClass:[UINavigationController class]] && [[[(UINavigationController *)topViewController viewControllers] firstObject] isKindOfClass:[class class]]) {
+        [[topViewController presentingViewController] dismissViewControllerAnimated:YES completion:nil];
+    } else {
+        id viewController = [[class alloc] init];
+        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
+        [topViewController presentViewController:navigationController animated:YES completion:nil];
+    }
+}
+
+- (void)showExplorerIfNeeded
+{
+    if ([self isHidden]) {
+        [self showExplorer];
+    }
+}
+
 @end