Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
refactor: combile local and remote orientation
Browse files Browse the repository at this point in the history
Signed-off-by: Lessica <[email protected]>
  • Loading branch information
Lessica committed Jan 26, 2024
1 parent 1e87627 commit 30ebbeb
Showing 1 changed file with 17 additions and 37 deletions.
54 changes: 17 additions & 37 deletions sources/HUDRootViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,9 @@ @implementation HUDRootViewController {
NSLayoutConstraint *_centerXConstraint;
NSLayoutConstraint *_leadingConstraint;
NSLayoutConstraint *_trailingConstraint;
UIInterfaceOrientation _orientation;
#if !NO_TROLL
FBSOrientationObserver *_remoteOrientationObserver;
UIInterfaceOrientation _remoteOrientation;
#else
UIInterfaceOrientation _localOrientation;
FBSOrientationObserver *_orientationObserver;
#endif
}

Expand Down Expand Up @@ -441,17 +439,11 @@ + (BOOL)passthroughMode

- (BOOL)isLandscapeOrientation
{
UIInterfaceOrientation orientation;
#if !NO_TROLL
orientation = _remoteOrientation;
#else
orientation = _localOrientation;
#endif
BOOL isLandscape;
if (orientation == UIInterfaceOrientationUnknown) {
if (_orientation == UIInterfaceOrientationUnknown) {
isLandscape = CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds);
} else {
isLandscape = UIInterfaceOrientationIsLandscape(orientation);
isLandscape = UIInterfaceOrientationIsLandscape(_orientation);
}
return isLandscape;
}
Expand Down Expand Up @@ -559,9 +551,9 @@ - (instancetype)init
_constraints = [NSMutableArray array];
[self registerNotifications];
#if !NO_TROLL
_remoteOrientationObserver = [[objc_getClass("FBSOrientationObserver") alloc] init];
_orientationObserver = [[objc_getClass("FBSOrientationObserver") alloc] init];
__weak HUDRootViewController *weakSelf = self;
[_remoteOrientationObserver setHandler:^(FBSOrientationUpdate *orientationUpdate) {
[_orientationObserver setHandler:^(FBSOrientationUpdate *orientationUpdate) {
HUDRootViewController *strongSelf = weakSelf;
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf updateOrientation:(UIInterfaceOrientation)orientationUpdate.orientation animateWithDuration:orientationUpdate.duration];
Expand All @@ -575,16 +567,17 @@ - (instancetype)init
- (void)dealloc
{
#if !NO_TROLL
[_remoteOrientationObserver invalidate];
[_orientationObserver invalidate];
#endif
}

- (void)updateSpeedLabel
{
log_debug(OS_LOG_DEFAULT, "updateSpeedLabel");
NSAttributedString *attributedText = formattedAttributedString(_isFocused);
if (attributedText)
if (attributedText) {
[_speedLabel setAttributedText:attributedText];
}
[_speedLabel sizeToFit];
}

Expand Down Expand Up @@ -672,21 +665,14 @@ - (void)updateViewConstraints
[_constraints removeAllObjects];

#if NO_TROLL
_localOrientation = [self.view.window.windowScene interfaceOrientation];
#endif

UIInterfaceOrientation orientation;
#if !NO_TROLL
orientation = _remoteOrientation;
#else
orientation = _localOrientation;
_orientation = [self.view.window.windowScene interfaceOrientation];
#endif

BOOL isLandscape;
if (orientation == UIInterfaceOrientationUnknown) {
if (_orientation == UIInterfaceOrientationUnknown) {
isLandscape = CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds);
} else {
isLandscape = UIInterfaceOrientationIsLandscape(orientation);
isLandscape = UIInterfaceOrientationIsLandscape(_orientation);
}

HUDPresetPosition selectedMode = [self selectedModeForCurrentOrientation];
Expand Down Expand Up @@ -718,8 +704,8 @@ - (void)updateViewConstraints
#endif

[_constraints addObjectsFromArray:@[
[_contentView.leadingAnchor constraintEqualToAnchor:layoutGuide.leadingAnchor constant:(orientation == UIInterfaceOrientationLandscapeLeft ? -paddingFarFromNotch : paddingNearNotch)],
[_contentView.trailingAnchor constraintEqualToAnchor:layoutGuide.trailingAnchor constant:(orientation == UIInterfaceOrientationLandscapeLeft ? -paddingNearNotch : paddingFarFromNotch)],
[_contentView.leadingAnchor constraintEqualToAnchor:layoutGuide.leadingAnchor constant:(_orientation == UIInterfaceOrientationLandscapeLeft ? -paddingFarFromNotch : paddingNearNotch)],
[_contentView.trailingAnchor constraintEqualToAnchor:layoutGuide.trailingAnchor constant:(_orientation == UIInterfaceOrientationLandscapeLeft ? -paddingNearNotch : paddingFarFromNotch)],
]];

CGFloat minimumLandscapeTopConstant = 0;
Expand Down Expand Up @@ -1000,13 +986,7 @@ - (void)panGestureRecognized:(UIPanGestureRecognizer *)sender

if (sender.state == UIGestureRecognizerStateEnded)
{
UIInterfaceOrientation orientation;
#if !NO_TROLL
orientation = _remoteOrientation;
#else
orientation = _localOrientation;
#endif
if (UIInterfaceOrientationIsLandscape(orientation))
if (UIInterfaceOrientationIsLandscape(_orientation))
[self setCurrentLandscapePositionY:_topConstraint.constant];
else
[self setCurrentPositionY:_topConstraint.constant];
Expand Down Expand Up @@ -1089,11 +1069,11 @@ - (void)updateOrientation:(UIInterfaceOrientation)orientation animateWithDuratio
return;
}

if (orientation == _remoteOrientation) {
if (orientation == _orientation) {
return;
}

_remoteOrientation = orientation;
_orientation = orientation;
[self cancelPreviousPerformRequestsWithTarget:_contentView];

CGRect bounds = orientationBounds(orientation, [UIScreen mainScreen].bounds);
Expand Down

0 comments on commit 30ebbeb

Please sign in to comment.