From c75722e8ae64cf125ecee14cc52905d97e866dd4 Mon Sep 17 00:00:00 2001 From: ethanarbuckle Date: Sat, 23 Jan 2021 19:58:13 -0800 Subject: [PATCH] implement "auto" dock alignment mode --- src/CRCarplayWindow.h | 1 + src/CRCarplayWindow.mm | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/CRCarplayWindow.h b/src/CRCarplayWindow.h index 986c988..752d683 100644 --- a/src/CRCarplayWindow.h +++ b/src/CRCarplayWindow.h @@ -12,6 +12,7 @@ id getCarplayCADisplay(void); @property (nonatomic, retain) id appViewController; @property (nonatomic, retain) id sceneMonitor; @property (nonatomic, retain) id application; +@property (nonatomic, retain) id sessionStatus; @property (nonatomic, retain) NSMutableArray *observers; @property (nonatomic) int orientation; diff --git a/src/CRCarplayWindow.mm b/src/CRCarplayWindow.mm index 475fe7c..16f5444 100644 --- a/src/CRCarplayWindow.mm +++ b/src/CRCarplayWindow.mm @@ -37,6 +37,8 @@ - (id)initWithBundleIdentifier:(id)identifier // Start in landscape self.orientation = 3; + self.sessionStatus = objcInvoke([objc_getClass("CARSessionStatus") alloc], @"initForCarPlayShell"); + self.application = objcInvoke_1(objcInvoke(objc_getClass("SBApplicationController"), @"sharedInstance"), @"applicationWithBundleIdentifier:", identifier); assertGotExpectedObject(self.application, @"SBApplication"); @@ -145,7 +147,7 @@ - (void)setupDock } CGRect rootWindowFrame = [[self rootWindow] frame]; - BOOL rightHandDock = [[CRPreferences sharedInstance] dockAlignment] == CRDockAlignmentRight; + BOOL rightHandDock = [self shouldUseRightHandDock]; CGFloat dockXOrigin = (rightHandDock) ? rootWindowFrame.size.width - CARPLAY_DOCK_WIDTH : 0; self.dockView = [[UIView alloc] initWithFrame:CGRectMake(dockXOrigin, rootWindowFrame.origin.y, CARPLAY_DOCK_WIDTH, rootWindowFrame.size.height)]; @@ -554,17 +556,40 @@ - (void)resizeAppViewForOrientation:(int)desiredOrientation fullscreen:(BOOL)ful [hostingContentView setTransform:CGAffineTransformMakeScale(widthScale, heightScale)]; [[self.appViewController view] setFrame:CGRectMake(xOrigin, [[self.appViewController view] frame].origin.y, carplayDisplaySize.width, carplayDisplaySize.height)]; - BOOL rightHandDock = [[CRPreferences sharedInstance] dockAlignment] == CRDockAlignmentRight; + BOOL rightHandDock = [self shouldUseRightHandDock]; UIView *containingView = [self appContainerView]; CGRect containingViewFrame = [containingView frame]; containingViewFrame.origin.x = (rightHandDock) ? 0 : dockWidth; [containingView setFrame:containingViewFrame]; - [self.dockView setAlpha: (fullscreen) ? 0: 1]; + [self.dockView setAlpha: (fullscreen) ? 0 : 1]; // Update last known orientation and fullscreen status self.orientation = desiredOrientation; self.isFullscreen = fullscreen; } +- (BOOL)shouldUseRightHandDock +{ + switch ([[CRPreferences sharedInstance] dockAlignment]) + { + case CRDockAlignmentLeft: + return NO; + case CRDockAlignmentRight: + return YES; + case CRDockAlignmentAuto: + { + id carplaySession = objcInvoke(self.sessionStatus, @"session"); + id usesRightHand = objcInvoke_1(carplaySession, @"_endpointValueForKey:", @"RightHandDrive"); + return [usesRightHand boolValue]; + } + default: + { + break; + } + } + + return NO; +} + @end \ No newline at end of file