Skip to content

Commit

Permalink
Alleviate excessive layout jittering when resizing window
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Jun 7, 2020
1 parent 45d1df9 commit 1fa9cd3
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions React/Base/macOS/RCTUIKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#import <objc/runtime.h>

#define RCT_LAYOUT_THROTTLE 0.25

static char RCTGraphicsContextSizeKey;

//
Expand Down Expand Up @@ -209,6 +211,9 @@ @implementation RCTUIView // TODO(macOS ISS#3536887)
BOOL _clipsToBounds;
BOOL _opaque;
BOOL _userInteractionEnabled;

NSDate *_lastLayout;
BOOL _throttleLayout;
}

+ (NSSet<NSString *> *)keyPathsForValuesAffectingValueForKey:(NSString *)key
Expand Down Expand Up @@ -236,7 +241,7 @@ @implementation RCTUIView // TODO(macOS ISS#3536887)
if (self != nil) {
self.wantsLayer = YES;
self->_userInteractionEnabled = YES;

self->_lastLayout = [NSDate new];
}
return self;
}
Expand Down Expand Up @@ -329,8 +334,22 @@ - (void)drawRect:(CGRect)rect

- (void)layout
{
if (self.window != nil) {
[self layoutSubviews];
if (self.window != nil && !_throttleLayout) {
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:_lastLayout];
if (interval >= RCT_LAYOUT_THROTTLE) {
_lastLayout = [NSDate new];
[self layoutSubviews];
} else {
_throttleLayout = YES;
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, RCT_LAYOUT_THROTTLE * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
typeof(self) strongSelf = weakSelf;
if (strongSelf != nil) {
strongSelf->_throttleLayout = NO;
[strongSelf setNeedsLayout];
}
});
}
}
}

Expand Down

0 comments on commit 1fa9cd3

Please sign in to comment.