12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250 |
- //
- // FLEXExplorerViewController.m
- // Flipboard
- //
- // Created by Ryan Olson on 4/4/14.
- // Copyright (c) 2020 FLEX Team. All rights reserved.
- //
- #import "FLEXExplorerViewController.h"
- #import "FLEXExplorerToolbarItem.h"
- #import "FLEXUtility.h"
- #import "FLEXWindow.h"
- #import "FLEXTabList.h"
- #import "FLEXNavigationController.h"
- #import "FLEXHierarchyViewController.h"
- #import "FLEXGlobalsViewController.h"
- #import "FLEXObjectExplorerViewController.h"
- #import "FLEXObjectExplorerFactory.h"
- #import "FLEXNetworkMITMViewController.h"
- #import "FLEXTabsViewController.h"
- #import "FLEXWindowManagerController.h"
- #import "FLEXViewControllersViewController.h"
- #import "NSUserDefaults+FLEX.h"
- #import "FLEXManager.h"
- #import "FLEXResources.h"
- typedef NS_ENUM(NSUInteger, FLEXExplorerMode) {
- FLEXExplorerModeDefault,
- FLEXExplorerModeSelect,
- FLEXExplorerModeMove
- };
- @interface FLEXExplorerViewController () <FLEXHierarchyDelegate, UIAdaptivePresentationControllerDelegate>{
- UIImageView *cursorView;
- }
- /// Tracks the currently active tool/mode
- @property (nonatomic) FLEXExplorerMode currentMode;
- /// Gesture recognizer for dragging a view in move mode
- @property (nonatomic) UIPanGestureRecognizer *movePanGR;
- /// Gesture recognizer for showing additional details on the selected view
- @property (nonatomic) UITapGestureRecognizer *detailsTapGR;
- /// Only valid while a move pan gesture is in progress.
- @property (nonatomic) CGRect selectedViewFrameBeforeDragging;
- /// Only valid while a toolbar drag pan gesture is in progress.
- @property (nonatomic) CGRect toolbarFrameBeforeDragging;
- /// Only valid while a selected view pan gesture is in progress.
- @property (nonatomic) CGFloat selectedViewLastPanX;
- /// Borders of all the visible views in the hierarchy at the selection point.
- /// The keys are NSValues with the corresponding view (nonretained).
- @property (nonatomic) NSDictionary<NSValue *, UIView *> *outlineViewsForVisibleViews;
- /// The actual views at the selection point with the deepest view last.
- @property (nonatomic) NSArray<UIView *> *viewsAtTapPoint;
- /// The view that we're currently highlighting with an overlay and displaying details for.
- @property (nonatomic) UIView *selectedView;
- /// A colored transparent overlay to indicate that the view is selected.
- @property (nonatomic) UIView *selectedViewOverlay;
- /// self.view.window as a \c FLEXWindow
- @property (nonatomic, readonly) FLEXWindow *window;
- /// All views that we're KVOing. Used to help us clean up properly.
- @property (nonatomic) NSMutableSet<UIView *> *observedViews;
- #if !TARGET_OS_TV
- /// Used to actuate changes in view selection on iOS 10+
- @property (nonatomic, readonly) UISelectionFeedbackGenerator *selectionFBG API_AVAILABLE(ios(10.0));
- /// Used to preserve the target app's UIMenuController items.
- @property (nonatomic) NSArray<UIMenuItem *> *appMenuItems;
- #endif
- @property CGPoint lastTouchLocation;
- @end
- @implementation FLEXExplorerViewController
- #pragma mark - Cursor Input
- #if TARGET_OS_TV
- - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
- {
- for (UIPress *press in presses) {
- if (press.type == UIPressTypeMenu) {
-
- } else {
- [super pressesBegan:presses withEvent:event];
- }
- }
- }
- -(void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {
-
- FXLog(@"presses.anyObject.type: %lu", presses.anyObject.type);
- if (self.currentMode != FLEXExplorerModeSelect && self.currentMode != FLEXExplorerModeMove){
- [super pressesEnded:presses withEvent:event];
- return;
- }
- CGPoint point = [self.view convertPoint:cursorView.frame.origin toView:nil];
- FXLog(@"clicked point: %@", NSStringFromCGPoint(point));
- if (self.currentMode == FLEXExplorerModeSelect){
- [self updateOutlineViewsForSelectionPoint:point];
- }
- if (presses.anyObject.type == UIPressTypeMenu) {
- if (self.currentMode == FLEXExplorerModeMove){
- self.currentMode = FLEXExplorerModeSelect;
- cursorView.hidden = false;
- } else if (self.currentMode == FLEXExplorerModeSelect){
- self.currentMode = FLEXExplorerModeDefault;
- cursorView.hidden = true;
- [self enableToolbar];
- }
- } else if (presses.anyObject.type == UIPressTypeUpArrow) {
- } else if (presses.anyObject.type == UIPressTypeDownArrow) {
- } else if (presses.anyObject.type == UIPressTypeSelect) {
- } else if (presses.anyObject.type == UIPressTypePlayPause){
- }
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- {
- self.lastTouchLocation = CGPointMake(-1, -1);
- }
- - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- {
- for (UITouch *touch in touches)
- {
- CGPoint location = [touch locationInView:self.view];
-
- if(self.lastTouchLocation.x == -1 && self.lastTouchLocation.y == -1)
- {
- // Prevent cursor from recentering
- self.lastTouchLocation = location;
- }
- else
- {
- CGFloat xDiff = location.x - self.lastTouchLocation.x;
- CGFloat yDiff = location.y - self.lastTouchLocation.y;
- CGRect rect = cursorView.frame;
-
- if(rect.origin.x + xDiff >= 0 && rect.origin.x + xDiff <= 1920)
- rect.origin.x += xDiff;//location.x - self.startPos.x;//+= xDiff; //location.x;
-
- if(rect.origin.y + yDiff >= 0 && rect.origin.y + yDiff <= 1080)
- rect.origin.y += yDiff;//location.y - self.startPos.y;//+= yDiff; //location.y;
-
- cursorView.frame = rect;
- self.lastTouchLocation = location;
- }
-
- // We only use one touch, break the loop
- break;
- }
-
- }
- #endif
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- self.observedViews = [NSMutableSet new];
- }
- return self;
- }
- - (void)dealloc {
- for (UIView *view in _observedViews) {
- [self stopObservingView:view];
- }
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- // Toolbar
- _explorerToolbar = [FLEXExplorerToolbar new];
-
- // Start the toolbar off below any bars that may be at the top of the view.
- CGFloat toolbarOriginY = NSUserDefaults.standardUserDefaults.flex_toolbarTopMargin;
-
- CGRect safeArea = [self viewSafeArea];
- CGSize toolbarSize = [self.explorerToolbar sizeThatFits:CGSizeMake(
- CGRectGetWidth(self.view.bounds), CGRectGetHeight(safeArea)
- )];
- [self updateToolbarPositionWithUnconstrainedFrame:CGRectMake(
- CGRectGetMinX(safeArea), toolbarOriginY, toolbarSize.width, toolbarSize.height
- )];
- self.explorerToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth |
- UIViewAutoresizingFlexibleBottomMargin |
- UIViewAutoresizingFlexibleTopMargin;
- [self.view addSubview:self.explorerToolbar];
- [self setupToolbarActions];
- [self setupToolbarGestures];
-
- // View selection
- UITapGestureRecognizer *selectionTapGR = [[UITapGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleSelectionTap:)
- ];
- [self.view addGestureRecognizer:selectionTapGR];
-
- // View moving
- self.movePanGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleMovePan:)];
- self.movePanGR.enabled = self.currentMode == FLEXExplorerModeMove;
- [self.view addGestureRecognizer:self.movePanGR];
- #if !TARGET_OS_TV
- // Feedback
- if (@available(iOS 10.0, *)) {
- _selectionFBG = [UISelectionFeedbackGenerator new];
- }
- #else
- cursorView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)];
- cursorView.center = CGPointMake(CGRectGetMidX([UIScreen mainScreen].bounds), CGRectGetMidY([UIScreen mainScreen].bounds));
- cursorView.image = [FLEXResources cursorImage];
- cursorView.backgroundColor = [UIColor clearColor];
- cursorView.hidden = YES;
-
- UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
- longPress.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause], [NSNumber numberWithInteger:UIPressTypeSelect]];
- [self.view addGestureRecognizer:longPress];
- [self.view addSubview:cursorView];
-
- UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
- doubleTap.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause], [NSNumber numberWithInteger:UIPressTypeSelect]];
- doubleTap.numberOfTapsRequired = 2;
- [self.view addGestureRecognizer:doubleTap];
-
- UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
- rightTap.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeRightArrow]];
- [self.view addGestureRecognizer:rightTap];
-
- #endif
- }
- - (void)doubleTap:(UITapGestureRecognizer *)gesture {
- if (gesture.state == UIGestureRecognizerStateEnded) {
- if (self.currentMode == FLEXExplorerModeSelect || self.currentMode == FLEXExplorerModeMove){
- [self showTVOSOptionsAlert];
-
- }
- }
-
- }
- - (void)showObjectControllerForSelectedView {
- FLEXObjectExplorerViewController *viewExplorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:self.selectedView];
- if (!viewExplorer) return;
- if ([self presentedViewController]){
- FLEXHierarchyViewController *vc = (FLEXHierarchyViewController*)[self presentedViewController];
- if ([vc respondsToSelector:@selector(pushViewController:animated:)]){
- [vc pushViewController:viewExplorer animated:true];
- }
- } else {
- [self toggleViewsToolWithCompletion:^{
- FLEXHierarchyViewController *vc = (FLEXHierarchyViewController*)[self presentedViewController];
- if ([vc respondsToSelector:@selector(pushViewController:animated:)]){
- [vc pushViewController:viewExplorer animated:true];
- }
- }];
- }
- }
- - (void)showTVOSOptionsAlert {
- [FLEXAlert makeAlert:^(FLEXAlert *make) {
- make.title(@"What would you like to do?");
- make.button(@"Show Details").handler(^(NSArray<NSString *> *strings) {
- [self showObjectControllerForSelectedView];
- [[FLEXManager sharedManager] showExplorer];
- });
- if (self.currentMode == FLEXExplorerModeMove){
- make.button(@"Select View").handler(^(NSArray<NSString *> *strings) {
- self.currentMode = FLEXExplorerModeSelect;
- cursorView.hidden = false;
- [[FLEXManager sharedManager] showExplorer];
- });
- } else if (self.currentMode == FLEXExplorerModeSelect){
- make.button(@"Move View").handler(^(NSArray<NSString *> *strings) {
- self.currentMode = FLEXExplorerModeMove;
- cursorView.hidden = true;
- [[FLEXManager sharedManager] showExplorer];
- });
- }
- make.button(@"Show Views").handler(^(NSArray<NSString *> *strings) {
- [self toggleViewsTool];
- [[FLEXManager sharedManager] showExplorer];
- });
- make.button(@"Show Usage Hints").handler(^(NSArray<NSString *> *strings) {
- [[FLEXManager sharedManager] showHintsAlert];
- });
- make.button(@"Cancel").cancelStyle();
- } showFrom:self];
- }
- - (void)longPress:(UILongPressGestureRecognizer*)gesture {
- if ( gesture.state == UIGestureRecognizerStateEnded) {
- [self toggleSelectTool];
- }
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- [self updateButtonStates];
- }
- #pragma mark - Rotation
- - (UIViewController *)viewControllerForRotationAndOrientation {
- UIViewController *viewController = FLEXUtility.appKeyWindow.rootViewController;
- // Obfuscating selector _viewControllerForSupportedInterfaceOrientations
- NSString *viewControllerSelectorString = [@[
- @"_vie", @"wContro", @"llerFor", @"Supported", @"Interface", @"Orientations"
- ] componentsJoinedByString:@""];
- SEL viewControllerSelector = NSSelectorFromString(viewControllerSelectorString);
- if ([viewController respondsToSelector:viewControllerSelector]) {
- viewController = [viewController valueForKey:viewControllerSelectorString];
- }
-
- return viewController;
- }
- - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
- // Commenting this out until I can figure out a better way to solve this
- // if (self.window.isKeyWindow) {
- // [self.window resignKeyWindow];
- // }
-
- UIViewController *viewControllerToAsk = [self viewControllerForRotationAndOrientation];
- UIInterfaceOrientationMask supportedOrientations = FLEXUtility.infoPlistSupportedInterfaceOrientationsMask;
- if (viewControllerToAsk && ![viewControllerToAsk isKindOfClass:[self class]]) {
- supportedOrientations = [viewControllerToAsk supportedInterfaceOrientations];
- }
-
- // The UIViewController docs state that this method must not return zero.
- // If we weren't able to get a valid value for the supported interface
- // orientations, default to all supported.
- if (supportedOrientations == 0) {
- supportedOrientations = UIInterfaceOrientationMaskAll;
- }
-
- return supportedOrientations;
- }
- - (BOOL)shouldAutorotate {
- UIViewController *viewControllerToAsk = [self viewControllerForRotationAndOrientation];
- BOOL shouldAutorotate = YES;
- if (viewControllerToAsk && viewControllerToAsk != self) {
- shouldAutorotate = [viewControllerToAsk shouldAutorotate];
- }
- return shouldAutorotate;
- }
- - (void)viewWillTransitionToSize:(CGSize)size
- withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
- [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
- [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
- for (UIView *outlineView in self.outlineViewsForVisibleViews.allValues) {
- outlineView.hidden = YES;
- }
- self.selectedViewOverlay.hidden = YES;
- } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
- for (UIView *view in self.viewsAtTapPoint) {
- NSValue *key = [NSValue valueWithNonretainedObject:view];
- UIView *outlineView = self.outlineViewsForVisibleViews[key];
- outlineView.frame = [self frameInLocalCoordinatesForView:view];
- if (self.currentMode == FLEXExplorerModeSelect) {
- outlineView.hidden = NO;
- }
- }
- if (self.selectedView) {
- self.selectedViewOverlay.frame = [self frameInLocalCoordinatesForView:self.selectedView];
- self.selectedViewOverlay.hidden = NO;
- }
- }];
- }
- #pragma mark - Setter Overrides
- - (void)setSelectedView:(UIView *)selectedView {
- if (![_selectedView isEqual:selectedView]) {
- if (![self.viewsAtTapPoint containsObject:_selectedView]) {
- [self stopObservingView:_selectedView];
- }
-
- _selectedView = selectedView;
-
- [self beginObservingView:selectedView];
- // Update the toolbar and selected overlay
- self.explorerToolbar.selectedViewDescription = [FLEXUtility
- descriptionForView:selectedView includingFrame:YES
- ];
- self.explorerToolbar.selectedViewOverlayColor = [FLEXUtility
- consistentRandomColorForObject:selectedView
- ];
- if (selectedView) {
- if (!self.selectedViewOverlay) {
- self.selectedViewOverlay = [UIView new];
- [self.view addSubview:self.selectedViewOverlay];
- self.selectedViewOverlay.layer.borderWidth = 1.0;
- }
- UIColor *outlineColor = [FLEXUtility consistentRandomColorForObject:selectedView];
- self.selectedViewOverlay.backgroundColor = [outlineColor colorWithAlphaComponent:0.2];
- self.selectedViewOverlay.layer.borderColor = outlineColor.CGColor;
- self.selectedViewOverlay.frame = [self.view convertRect:selectedView.bounds fromView:selectedView];
-
- // Make sure the selected overlay is in front of all the other subviews
- // except the toolbar, which should always stay on top.
- [self.view bringSubviewToFront:self.selectedViewOverlay];
- [self.view bringSubviewToFront:self.explorerToolbar];
- } else {
- [self.selectedViewOverlay removeFromSuperview];
- self.selectedViewOverlay = nil;
- }
-
- // Some of the button states depend on whether we have a selected view.
- [self updateButtonStates];
- }
- }
- - (void)setViewsAtTapPoint:(NSArray<UIView *> *)viewsAtTapPoint {
- if (![_viewsAtTapPoint isEqual:viewsAtTapPoint]) {
- for (UIView *view in _viewsAtTapPoint) {
- if (view != self.selectedView) {
- [self stopObservingView:view];
- }
- }
-
- _viewsAtTapPoint = viewsAtTapPoint;
-
- for (UIView *view in viewsAtTapPoint) {
- [self beginObservingView:view];
- }
- }
- }
- - (void)setCurrentMode:(FLEXExplorerMode)currentMode {
- if (_currentMode != currentMode) {
- _currentMode = currentMode;
- switch (currentMode) {
- case FLEXExplorerModeDefault:
- [self removeAndClearOutlineViews];
- self.viewsAtTapPoint = nil;
- self.selectedView = nil;
- break;
-
- case FLEXExplorerModeSelect:
- // Make sure the outline views are unhidden in case we came from the move mode.
- for (NSValue *key in self.outlineViewsForVisibleViews) {
- UIView *outlineView = self.outlineViewsForVisibleViews[key];
- outlineView.hidden = NO;
- }
- break;
-
- case FLEXExplorerModeMove:
- // Hide all the outline views to focus on the selected view,
- // which is the only one that will move.
- for (NSValue *key in self.outlineViewsForVisibleViews) {
- UIView *outlineView = self.outlineViewsForVisibleViews[key];
- outlineView.hidden = YES;
- }
- break;
- }
- self.movePanGR.enabled = currentMode == FLEXExplorerModeMove;
- [self updateButtonStates];
- }
- }
- #pragma mark - View Tracking
- - (void)beginObservingView:(UIView *)view {
- // Bail if we're already observing this view or if there's nothing to observe.
- if (!view || [self.observedViews containsObject:view]) {
- return;
- }
-
- for (NSString *keyPath in self.viewKeyPathsToTrack) {
- [view addObserver:self forKeyPath:keyPath options:0 context:NULL];
- }
-
- [self.observedViews addObject:view];
- }
- - (void)stopObservingView:(UIView *)view {
- if (!view) {
- return;
- }
-
- for (NSString *keyPath in self.viewKeyPathsToTrack) {
- [view removeObserver:self forKeyPath:keyPath];
- }
-
- [self.observedViews removeObject:view];
- }
- - (NSArray<NSString *> *)viewKeyPathsToTrack {
- static NSArray<NSString *> *trackedViewKeyPaths = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- NSString *frameKeyPath = NSStringFromSelector(@selector(frame));
- trackedViewKeyPaths = @[frameKeyPath];
- });
- return trackedViewKeyPaths;
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
- change:(NSDictionary<NSString *, id> *)change
- context:(void *)context {
- [self updateOverlayAndDescriptionForObjectIfNeeded:object];
- }
- - (void)updateOverlayAndDescriptionForObjectIfNeeded:(id)object {
- NSUInteger indexOfView = [self.viewsAtTapPoint indexOfObject:object];
- if (indexOfView != NSNotFound) {
- UIView *view = self.viewsAtTapPoint[indexOfView];
- NSValue *key = [NSValue valueWithNonretainedObject:view];
- UIView *outline = self.outlineViewsForVisibleViews[key];
- if (outline) {
- outline.frame = [self frameInLocalCoordinatesForView:view];
- }
- }
- if (object == self.selectedView) {
- // Update the selected view description since we show the frame value there.
- self.explorerToolbar.selectedViewDescription = [FLEXUtility
- descriptionForView:self.selectedView includingFrame:YES
- ];
- CGRect selectedViewOutlineFrame = [self frameInLocalCoordinatesForView:self.selectedView];
- self.selectedViewOverlay.frame = selectedViewOutlineFrame;
- }
- }
- - (CGRect)frameInLocalCoordinatesForView:(UIView *)view {
- // Convert to window coordinates since the view may be in a different window than our view
- CGRect frameInWindow = [view convertRect:view.bounds toView:nil];
- // Convert from the window to our view's coordinate space
- return [self.view convertRect:frameInWindow fromView:nil];
- }
- #pragma mark - Toolbar Buttons
- - (void)setupToolbarActions {
- FLEXExplorerToolbar *toolbar = self.explorerToolbar;
- NSDictionary<NSString *, FLEXExplorerToolbarItem *> *actionsToItems = @{
- NSStringFromSelector(@selector(selectButtonTapped:)): toolbar.selectItem,
- NSStringFromSelector(@selector(hierarchyButtonTapped:)): toolbar.hierarchyItem,
- NSStringFromSelector(@selector(recentButtonTapped:)): toolbar.recentItem,
- NSStringFromSelector(@selector(moveButtonTapped:)): toolbar.moveItem,
- NSStringFromSelector(@selector(globalsButtonTapped:)): toolbar.globalsItem,
- NSStringFromSelector(@selector(closeButtonTapped:)): toolbar.closeItem,
- };
-
- [actionsToItems enumerateKeysAndObjectsUsingBlock:^(NSString *sel, FLEXExplorerToolbarItem *item, BOOL *stop) {
- #if !TARGET_OS_TV
- [item addTarget:self action:NSSelectorFromString(sel) forControlEvents:UIControlEventTouchUpInside];
- #else
- [item addTarget:self action:NSSelectorFromString(sel) forControlEvents:UIControlEventPrimaryActionTriggered];
- #endif
- }];
- }
- - (void)selectButtonTapped:(FLEXExplorerToolbarItem *)sender {
- [self toggleSelectTool];
- }
- - (void)hierarchyButtonTapped:(FLEXExplorerToolbarItem *)sender {
- [self toggleViewsTool];
- }
- - (UIWindow *)statusWindow {
- NSString *statusBarString = [NSString stringWithFormat:@"%@arWindow", @"_statusB"];
- return [UIApplication.sharedApplication valueForKey:statusBarString];
- }
- - (void)recentButtonTapped:(FLEXExplorerToolbarItem *)sender {
- NSAssert(FLEXTabList.sharedList.activeTab, @"Must have active tab");
- [self presentViewController:FLEXTabList.sharedList.activeTab animated:YES completion:nil];
- }
- - (void)moveButtonTapped:(FLEXExplorerToolbarItem *)sender {
- [self toggleMoveTool];
- }
- - (void)globalsButtonTapped:(FLEXExplorerToolbarItem *)sender {
- [self toggleMenuTool];
- }
- - (void)closeButtonTapped:(FLEXExplorerToolbarItem *)sender {
- self.currentMode = FLEXExplorerModeDefault;
- [self.delegate explorerViewControllerDidFinish:self];
- }
- - (void)updateButtonStates {
- FLEXExplorerToolbar *toolbar = self.explorerToolbar;
-
- toolbar.selectItem.selected = self.currentMode == FLEXExplorerModeSelect;
-
- // Move only enabled when an object is selected.
- BOOL hasSelectedObject = self.selectedView != nil;
- toolbar.moveItem.enabled = hasSelectedObject;
- toolbar.moveItem.selected = self.currentMode == FLEXExplorerModeMove;
-
- // Recent only enabled when we have a last active tab
- toolbar.recentItem.enabled = FLEXTabList.sharedList.activeTab != nil;
- }
- #pragma mark - Toolbar Dragging
- - (void)setupToolbarGestures {
- FLEXExplorerToolbar *toolbar = self.explorerToolbar;
-
- // Pan gesture for dragging.
- [toolbar.dragHandle addGestureRecognizer:[[UIPanGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleToolbarPanGesture:)
- ]];
-
- // Tap gesture for hinting.
- [toolbar.dragHandle addGestureRecognizer:[[UITapGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleToolbarHintTapGesture:)
- ]];
-
- // Tap gesture for showing additional details
- self.detailsTapGR = [[UITapGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleToolbarDetailsTapGesture:)
- ];
- [toolbar.selectedViewDescriptionContainer addGestureRecognizer:self.detailsTapGR];
- // Swipe gestures for selecting deeper / higher views at a point
- UIPanGestureRecognizer *leftSwipe = [[UIPanGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleChangeViewAtPointGesture:)
- ];
- // UIPanGestureRecognizer *rightSwipe = [[UIPanGestureRecognizer alloc]
- // initWithTarget:self action:@selector(handleChangeViewAtPointGesture:)
- // ];
- // leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
- // rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
- [toolbar.selectedViewDescriptionContainer addGestureRecognizer:leftSwipe];
- // [toolbar.selectedViewDescriptionContainer addGestureRecognizer:rightSwipe];
-
- // Long press gesture to present tabs manager
- [toolbar.globalsItem addGestureRecognizer:[[UILongPressGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleToolbarShowTabsGesture:)
- ]];
-
- // Long press gesture to present window manager
- [toolbar.selectItem addGestureRecognizer:[[UILongPressGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleToolbarWindowManagerGesture:)
- ]];
-
- // Long press gesture to present view controllers at tap
- [toolbar.hierarchyItem addGestureRecognizer:[[UILongPressGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleToolbarShowViewControllersGesture:)
- ]];
- }
- - (void)handleToolbarPanGesture:(UIPanGestureRecognizer *)panGR {
- switch (panGR.state) {
- case UIGestureRecognizerStateBegan:
- self.toolbarFrameBeforeDragging = self.explorerToolbar.frame;
- [self updateToolbarPositionWithDragGesture:panGR];
- break;
-
- case UIGestureRecognizerStateChanged:
- case UIGestureRecognizerStateEnded:
- [self updateToolbarPositionWithDragGesture:panGR];
- break;
-
- default:
- break;
- }
- }
- - (void)updateToolbarPositionWithDragGesture:(UIPanGestureRecognizer *)panGR {
- CGPoint translation = [panGR translationInView:self.view];
- CGRect newToolbarFrame = self.toolbarFrameBeforeDragging;
- newToolbarFrame.origin.y += translation.y;
-
- [self updateToolbarPositionWithUnconstrainedFrame:newToolbarFrame];
- }
- - (void)updateToolbarPositionWithUnconstrainedFrame:(CGRect)unconstrainedFrame {
- CGRect safeArea = [self viewSafeArea];
- // We only constrain the Y-axis because we want the toolbar
- // to handle the X-axis safeArea layout by itself
- CGFloat minY = CGRectGetMinY(safeArea);
- CGFloat maxY = CGRectGetMaxY(safeArea) - unconstrainedFrame.size.height;
- if (unconstrainedFrame.origin.y < minY) {
- unconstrainedFrame.origin.y = minY;
- } else if (unconstrainedFrame.origin.y > maxY) {
- unconstrainedFrame.origin.y = maxY;
- }
- self.explorerToolbar.frame = unconstrainedFrame;
- NSUserDefaults.standardUserDefaults.flex_toolbarTopMargin = unconstrainedFrame.origin.y;
- }
- - (void)handleToolbarHintTapGesture:(UITapGestureRecognizer *)tapGR {
- // Bounce the toolbar to indicate that it is draggable.
- // TODO: make it bouncier.
- if (tapGR.state == UIGestureRecognizerStateRecognized) {
- CGRect originalToolbarFrame = self.explorerToolbar.frame;
- const NSTimeInterval kHalfwayDuration = 0.2;
- const CGFloat kVerticalOffset = 30.0;
- [UIView animateWithDuration:kHalfwayDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
- CGRect newToolbarFrame = self.explorerToolbar.frame;
- newToolbarFrame.origin.y += kVerticalOffset;
- self.explorerToolbar.frame = newToolbarFrame;
- } completion:^(BOOL finished) {
- [UIView animateWithDuration:kHalfwayDuration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
- self.explorerToolbar.frame = originalToolbarFrame;
- } completion:nil];
- }];
- }
- }
- - (void)handleToolbarDetailsTapGesture:(UITapGestureRecognizer *)tapGR {
- if (tapGR.state == UIGestureRecognizerStateRecognized && self.selectedView) {
- UIViewController *topStackVC = [FLEXObjectExplorerFactory explorerViewControllerForObject:self.selectedView];
- [self presentViewController:
- [FLEXNavigationController withRootViewController:topStackVC]
- animated:YES completion:nil];
- }
- }
- - (void)handleToolbarShowTabsGesture:(UILongPressGestureRecognizer *)sender {
- if (sender.state == UIGestureRecognizerStateBegan) {
- // Back up the UIMenuController items since dismissViewController: will attempt to replace them
- #if !TARGET_OS_TV
- self.appMenuItems = UIMenuController.sharedMenuController.menuItems;
- #endif
- // Don't use FLEXNavigationController because the tab viewer itself is not a tab
- [super presentViewController:[[UINavigationController alloc]
- initWithRootViewController:[FLEXTabsViewController new]
- ] animated:YES completion:nil];
- }
- }
- - (void)handleToolbarWindowManagerGesture:(UILongPressGestureRecognizer *)sender {
- if (sender.state == UIGestureRecognizerStateBegan) {
- // Back up the UIMenuController items since dismissViewController: will attempt to replace them
- #if !TARGET_OS_TV
- self.appMenuItems = UIMenuController.sharedMenuController.menuItems;
- #endif
- [super presentViewController:[FLEXNavigationController
- withRootViewController:[FLEXWindowManagerController new]
- ] animated:YES completion:nil];
- }
- }
- - (void)handleToolbarShowViewControllersGesture:(UILongPressGestureRecognizer *)sender {
- if (sender.state == UIGestureRecognizerStateBegan && self.viewsAtTapPoint.count) {
- // Back up the UIMenuController items since dismissViewController: will attempt to replace them
- #if !TARGET_OS_TV
- self.appMenuItems = UIMenuController.sharedMenuController.menuItems;
- #endif
- UIViewController *list = [FLEXViewControllersViewController
- controllersForViews:self.viewsAtTapPoint
- ];
- [self presentViewController:
- [FLEXNavigationController withRootViewController:list
- ] animated:YES completion:nil];
- }
- }
- #pragma mark - View Selection
- - (void)handleSelectionTap:(UITapGestureRecognizer *)tapGR {
- // Only if we're in selection mode
- if (self.currentMode == FLEXExplorerModeSelect && tapGR.state == UIGestureRecognizerStateRecognized) {
- // Note that [tapGR locationInView:nil] is broken in iOS 8,
- // so we have to do a two step conversion to window coordinates.
- // Thanks to @lascorbe for finding this: https://github.com/Flipboard/FLEX/pull/31
- CGPoint tapPointInView = [tapGR locationInView:self.view];
- CGPoint tapPointInWindow = [self.view convertPoint:tapPointInView toView:nil];
- [self updateOutlineViewsForSelectionPoint:tapPointInWindow];
- }
- }
- - (void)handleChangeViewAtPointGesture:(UIPanGestureRecognizer *)sender {
- NSInteger max = self.viewsAtTapPoint.count - 1;
- NSInteger currentIdx = [self.viewsAtTapPoint indexOfObject:self.selectedView];
- CGFloat locationX = [sender locationInView:self.view].x;
-
- // Track the pan gesture: every N points we move along the X axis,
- // actuate some haptic feedback and move up or down the hierarchy.
- // We only store the "last" location when we've met the threshold.
- // We only change the view and actuate feedback if the view selection
- // changes; that is, as long as we don't go outside or under the array.
- switch (sender.state) {
- case UIGestureRecognizerStateBegan: {
- self.selectedViewLastPanX = locationX;
- break;
- }
- case UIGestureRecognizerStateChanged: {
- static CGFloat kNextLevelThreshold = 20.f;
- CGFloat lastX = self.selectedViewLastPanX;
- NSInteger newSelection = currentIdx;
-
- // Left, go down the hierarchy
- if (locationX < lastX && (lastX - locationX) >= kNextLevelThreshold) {
- // Choose a new view index up to the max index
- newSelection = MIN(max, currentIdx + 1);
- self.selectedViewLastPanX = locationX;
- }
- // Right, go up the hierarchy
- else if (lastX < locationX && (locationX - lastX) >= kNextLevelThreshold) {
- // Choose a new view index down to the min index
- newSelection = MAX(0, currentIdx - 1);
- self.selectedViewLastPanX = locationX;
- }
-
- if (currentIdx != newSelection) {
- self.selectedView = self.viewsAtTapPoint[newSelection];
- [self actuateSelectionChangedFeedback];
- }
-
- break;
- }
-
- default: break;
- }
- }
- - (void)actuateSelectionChangedFeedback {
- #if !TARGET_OS_TV
- if (@available(iOS 10.0, *)) {
- [self.selectionFBG selectionChanged];
- }
- #endif
- }
- - (void)updateOutlineViewsForSelectionPoint:(CGPoint)selectionPointInWindow {
- [self removeAndClearOutlineViews];
-
- // Include hidden views in the "viewsAtTapPoint" array so we can show them in the hierarchy list.
- self.viewsAtTapPoint = [self viewsAtPoint:selectionPointInWindow skipHiddenViews:NO];
-
- // For outlined views and the selected view, only use visible views.
- // Outlining hidden views adds clutter and makes the selection behavior confusing.
- NSArray<UIView *> *visibleViewsAtTapPoint = [self viewsAtPoint:selectionPointInWindow skipHiddenViews:YES];
- NSMutableDictionary<NSValue *, UIView *> *newOutlineViewsForVisibleViews = [NSMutableDictionary new];
- for (UIView *view in visibleViewsAtTapPoint) {
- UIView *outlineView = [self outlineViewForView:view];
- [self.view addSubview:outlineView];
- NSValue *key = [NSValue valueWithNonretainedObject:view];
- [newOutlineViewsForVisibleViews setObject:outlineView forKey:key];
- }
- self.outlineViewsForVisibleViews = newOutlineViewsForVisibleViews;
- self.selectedView = [self viewForSelectionAtPoint:selectionPointInWindow];
-
- // Make sure the explorer toolbar doesn't end up behind the newly added outline views.
- [self.view bringSubviewToFront:self.explorerToolbar];
-
- [self updateButtonStates];
- }
- - (UIView *)outlineViewForView:(UIView *)view {
- CGRect outlineFrame = [self frameInLocalCoordinatesForView:view];
- UIView *outlineView = [[UIView alloc] initWithFrame:outlineFrame];
- outlineView.backgroundColor = UIColor.clearColor;
- outlineView.layer.borderColor = [FLEXUtility consistentRandomColorForObject:view].CGColor;
- outlineView.layer.borderWidth = 1.0;
- return outlineView;
- }
- - (void)removeAndClearOutlineViews {
- for (NSValue *key in self.outlineViewsForVisibleViews) {
- UIView *outlineView = self.outlineViewsForVisibleViews[key];
- [outlineView removeFromSuperview];
- }
- self.outlineViewsForVisibleViews = nil;
- }
- - (NSArray<UIView *> *)viewsAtPoint:(CGPoint)tapPointInWindow skipHiddenViews:(BOOL)skipHidden {
- NSMutableArray<UIView *> *views = [NSMutableArray new];
- for (UIWindow *window in FLEXUtility.allWindows) {
- // Don't include the explorer's own window or subviews.
- if (window != self.view.window && [window pointInside:tapPointInWindow withEvent:nil]) {
- [views addObject:window];
- [views addObjectsFromArray:[self
- recursiveSubviewsAtPoint:tapPointInWindow inView:window skipHiddenViews:skipHidden
- ]];
- }
- }
- return views;
- }
- - (UIView *)viewForSelectionAtPoint:(CGPoint)tapPointInWindow {
- // Select in the window that would handle the touch, but don't just use the result of
- // hitTest:withEvent: so we can still select views with interaction disabled.
- // Default to the the application's key window if none of the windows want the touch.
- UIWindow *windowForSelection = UIApplication.sharedApplication.keyWindow;
- for (UIWindow *window in FLEXUtility.allWindows.reverseObjectEnumerator) {
- // Ignore the explorer's own window.
- if (window != self.view.window) {
- if ([window hitTest:tapPointInWindow withEvent:nil]) {
- windowForSelection = window;
- break;
- }
- }
- }
-
- // Select the deepest visible view at the tap point. This generally corresponds to what the user wants to select.
- return [self recursiveSubviewsAtPoint:tapPointInWindow inView:windowForSelection skipHiddenViews:YES].lastObject;
- }
- - (NSArray<UIView *> *)recursiveSubviewsAtPoint:(CGPoint)pointInView
- inView:(UIView *)view
- skipHiddenViews:(BOOL)skipHidden {
- NSMutableArray<UIView *> *subviewsAtPoint = [NSMutableArray new];
- for (UIView *subview in view.subviews) {
- BOOL isHidden = subview.hidden || subview.alpha < 0.01;
- if (skipHidden && isHidden) {
- continue;
- }
-
- BOOL subviewContainsPoint = CGRectContainsPoint(subview.frame, pointInView);
- if (subviewContainsPoint) {
- [subviewsAtPoint addObject:subview];
- }
-
- // If this view doesn't clip to its bounds, we need to check its subviews even if it
- // doesn't contain the selection point. They may be visible and contain the selection point.
- if (subviewContainsPoint || !subview.clipsToBounds) {
- CGPoint pointInSubview = [view convertPoint:pointInView toView:subview];
- [subviewsAtPoint addObjectsFromArray:[self
- recursiveSubviewsAtPoint:pointInSubview inView:subview skipHiddenViews:skipHidden
- ]];
- }
- }
- return subviewsAtPoint;
- }
- #pragma mark - Selected View Moving
- - (void)handleMovePan:(UIPanGestureRecognizer *)movePanGR {
- switch (movePanGR.state) {
- case UIGestureRecognizerStateBegan:
- self.selectedViewFrameBeforeDragging = self.selectedView.frame;
- [self updateSelectedViewPositionWithDragGesture:movePanGR];
- break;
-
- case UIGestureRecognizerStateChanged:
- case UIGestureRecognizerStateEnded:
- [self updateSelectedViewPositionWithDragGesture:movePanGR];
- break;
-
- default:
- break;
- }
- }
- - (void)updateSelectedViewPositionWithDragGesture:(UIPanGestureRecognizer *)movePanGR {
- CGPoint translation = [movePanGR translationInView:self.selectedView.superview];
- CGRect newSelectedViewFrame = self.selectedViewFrameBeforeDragging;
- newSelectedViewFrame.origin.x = FLEXFloor(newSelectedViewFrame.origin.x + translation.x);
- newSelectedViewFrame.origin.y = FLEXFloor(newSelectedViewFrame.origin.y + translation.y);
- self.selectedView.frame = newSelectedViewFrame;
- }
- #pragma mark - Safe Area Handling
- - (CGRect)viewSafeArea {
- CGRect safeArea = self.view.bounds;
- if (@available(iOS 11.0, *)) {
- safeArea = UIEdgeInsetsInsetRect(self.view.bounds, self.view.safeAreaInsets);
- }
- return safeArea;
- }
- - (void)viewSafeAreaInsetsDidChange {
- if (@available(iOS 11.0, *)) {
- [super viewSafeAreaInsetsDidChange];
- CGRect safeArea = [self viewSafeArea];
- CGSize toolbarSize = [self.explorerToolbar sizeThatFits:CGSizeMake(
- CGRectGetWidth(self.view.bounds), CGRectGetHeight(safeArea)
- )];
- [self updateToolbarPositionWithUnconstrainedFrame:CGRectMake(
- CGRectGetMinX(self.explorerToolbar.frame),
- CGRectGetMinY(self.explorerToolbar.frame),
- toolbarSize.width,
- toolbarSize.height)
- ];
- }
- }
- #pragma mark - Touch Handling
- - (BOOL)shouldReceiveTouchAtWindowPoint:(CGPoint)pointInWindowCoordinates {
- BOOL shouldReceiveTouch = NO;
-
- CGPoint pointInLocalCoordinates = [self.view convertPoint:pointInWindowCoordinates fromView:nil];
-
- // Always if it's on the toolbar
- if (CGRectContainsPoint(self.explorerToolbar.frame, pointInLocalCoordinates)) {
- shouldReceiveTouch = YES;
- }
-
- // Always if we're in selection mode
- if (!shouldReceiveTouch && self.currentMode == FLEXExplorerModeSelect) {
- shouldReceiveTouch = YES;
- }
-
- // Always in move mode too
- if (!shouldReceiveTouch && self.currentMode == FLEXExplorerModeMove) {
- shouldReceiveTouch = YES;
- }
-
- // Always if we have a modal presented
- if (!shouldReceiveTouch && self.presentedViewController) {
- shouldReceiveTouch = YES;
- }
-
- return shouldReceiveTouch;
- }
- #pragma mark - FLEXHierarchyDelegate
- - (void)viewHierarchyDidDismiss:(UIView *)selectedView {
- // Note that we need to wait until the view controller is dismissed to calculate the frame
- // of the outline view, otherwise the coordinate conversion doesn't give the correct result.
- [self toggleViewsToolWithCompletion:^{
- // If the selected view is outside of the tap point array (selected from "Full Hierarchy"),
- // then clear out the tap point array and remove all the outline views.
- if (![self.viewsAtTapPoint containsObject:selectedView]) {
- self.viewsAtTapPoint = nil;
- [self removeAndClearOutlineViews];
- }
-
- // If we now have a selected view and we didn't have one previously, go to "select" mode.
- if (self.currentMode == FLEXExplorerModeDefault && selectedView) {
- //self.currentMode = FLEXExplorerModeSelect;
- [self toggleSelectTool];
- }
-
- // The selected view setter will also update the selected view overlay appropriately.
- self.selectedView = selectedView;
- }];
- }
- #pragma mark - Modal Presentation and Window Management
- - (void)presentViewController:(UIViewController *)toPresent
- animated:(BOOL)animated
- completion:(void (^)(void))completion {
- // Make our window key to correctly handle input.
- [self.view.window makeKeyWindow];
- // Move the status bar on top of FLEX so we can get scroll to top behavior for taps.
- if (!@available(iOS 13, *)) {
- [self statusWindow].windowLevel = self.view.window.windowLevel + 1.0;
- }
- #if !TARGET_OS_TV
- // Back up and replace the UIMenuController items
- // Edit: no longer replacing the items, but still backing them
- // up in case we start replacing them again in the future
- self.appMenuItems = UIMenuController.sharedMenuController.menuItems;
- #endif
- // Show the view controller
- [super presentViewController:toPresent animated:animated completion:completion];
- }
- - (void)dismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion {
- UIWindow *appWindow = self.window.previousKeyWindow;
- [appWindow makeKeyWindow];
- #if !TARGET_OS_TV
- [appWindow.rootViewController setNeedsStatusBarAppearanceUpdate];
- // Restore previous UIMenuController items
- // Back up and replace the UIMenuController items
- UIMenuController.sharedMenuController.menuItems = self.appMenuItems;
- [UIMenuController.sharedMenuController update];
- self.appMenuItems = nil;
- // Restore the status bar window's normal window level.
- // We want it above FLEX while a modal is presented for
- // scroll to top, but below FLEX otherwise for exploration.
- [self statusWindow].windowLevel = UIWindowLevelStatusBar;
- #endif
-
- [self updateButtonStates];
-
- [super dismissViewControllerAnimated:animated completion:completion];
- }
- - (BOOL)wantsWindowToBecomeKey
- {
- return self.window.previousKeyWindow != nil;
- }
- - (void)toggleToolWithViewControllerProvider:(UINavigationController *(^)(void))future
- completion:(void(^)(void))completion {
- if (self.presentedViewController) {
- [self dismissViewControllerAnimated:YES completion:completion];
- } else if (future) {
- [self presentViewController:future() animated:YES completion:completion];
- }
- }
- - (FLEXWindow *)window {
- return (id)self.view.window;
- }
- - (void)disableToolbar {
- [self.explorerToolbar setUserInteractionEnabled:false];
- [self.explorerToolbar setAlpha:0.5];
- [self setNeedsFocusUpdate];
- [self updateFocusIfNeeded];
- }
- - (void)enableToolbar {
- [self.explorerToolbar setUserInteractionEnabled:true];
- [self.explorerToolbar setAlpha:1.0];
- [self setNeedsFocusUpdate];
- [self updateFocusIfNeeded];
- }
- #pragma mark - Keyboard Shortcut Helpers
- - (void)toggleSelectTool {
- if (self.currentMode == FLEXExplorerModeSelect) {
- self.currentMode = FLEXExplorerModeDefault;
- cursorView.hidden = true;
- [self enableToolbar];
- } else {
- self.currentMode = FLEXExplorerModeSelect;
- cursorView.hidden = false;
- [self disableToolbar];
- }
- }
- - (void)toggleMoveTool {
- if (self.currentMode == FLEXExplorerModeMove) {
- self.currentMode = FLEXExplorerModeSelect;
- } else if (self.currentMode == FLEXExplorerModeSelect && self.selectedView) {
- self.currentMode = FLEXExplorerModeMove;
- }
- }
- - (void)toggleViewsTool {
- [self toggleViewsToolWithCompletion:nil];
- }
- - (void)toggleViewsToolWithCompletion:(void(^)(void))completion {
- [self toggleToolWithViewControllerProvider:^UINavigationController *{
- if (self.selectedView) {
- FXLog(@"we have a selected view still: %@", self.selectedView);
- return [FLEXHierarchyViewController
- delegate:self
- viewsAtTap:self.viewsAtTapPoint
- selectedView:self.selectedView
- ];
- } else {
- return [FLEXHierarchyViewController delegate:self];
- }
- } completion:^{
- if (completion) {
- completion();
- }
- }];
- }
- - (void)toggleMenuTool {
- [self toggleToolWithViewControllerProvider:^UINavigationController *{
- return [FLEXNavigationController withRootViewController:[FLEXGlobalsViewController new]];
- } completion:nil];
- }
- - (BOOL)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];
- }
- } else {
- return NO;
- }
-
- return YES;
- }
- - (BOOL)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];
- }
- } else {
- return NO;
- }
-
- return YES;
- }
- - (BOOL)handleRightArrowKeyPressed {
- if (self.currentMode == FLEXExplorerModeMove) {
- CGRect frame = self.selectedView.frame;
- frame.origin.x += 1.0 / UIScreen.mainScreen.scale;
- self.selectedView.frame = frame;
- return YES;
- }
-
- return NO;
- }
- - (BOOL)handleLeftArrowKeyPressed {
- if (self.currentMode == FLEXExplorerModeMove) {
- CGRect frame = self.selectedView.frame;
- frame.origin.x -= 1.0 / UIScreen.mainScreen.scale;
- self.selectedView.frame = frame;
- return YES;
- }
-
- return NO;
- }
- @end
|