Skip to content

Commit 8378d3b

Browse files
fix(ios): improve safe area layout lifecycle (tidev#14269)
* fix(ios): improve safe area layout lifecycle * chore: use BOOL instead of bool * Update iphone/TitaniumKit/TitaniumKit/Sources/API/TiViewProxy.m --------- Co-authored-by: Hans Knöchel <[email protected]>
1 parent f7f98c6 commit 8378d3b

File tree

6 files changed

+60
-19
lines changed

6 files changed

+60
-19
lines changed

iphone/TitaniumKit/TitaniumKit/Sources/API/TiLayoutQueue.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ + (void)layoutProxy:(TiViewProxy *)thisProxy
7474
} else {
7575
[thisProxy refreshView:nil];
7676
}
77+
78+
[thisProxy didFinishLayout];
7779
}
7880

7981
+ (void)addViewProxy:(TiViewProxy *)newViewProxy

iphone/TitaniumKit/TitaniumKit/Sources/API/TiViewProxy.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,11 @@ enum {
535535
*/
536536
- (void)parentWillHide;
537537

538+
/**
539+
Tells the view proxy that rendering via the layout queue finished.
540+
*/
541+
- (void)didFinishLayout;
542+
538543
#pragma mark Layout actions
539544

540545
- (void)refreshView:(TiUIView *)transferView;

iphone/TitaniumKit/TitaniumKit/Sources/API/TiViewProxy.m

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,13 @@ - (void)parentWillHide
20832083
}
20842084
}
20852085

2086+
- (void)didFinishLayout
2087+
{
2088+
VerboseLog(@"[INFO] Did finish layout for %@", self);
2089+
2090+
// No-op, can be overridden by subclasses.
2091+
}
2092+
20862093
#pragma mark Layout actions
20872094

20882095
// Need this so we can overload the sandbox bounds on split view detail/master
@@ -2286,9 +2293,12 @@ - (void)relayout
22862293
if ([self respondsToSelector:@selector(processForSafeArea)]) {
22872294
TiUIWindowProxy *windowProxy = (TiUIWindowProxy *)self;
22882295

2289-
[windowProxy processForSafeArea];
2290-
layoutChanged = layoutChanged || windowProxy.safeAreaInsetsUpdated;
2291-
windowProxy.safeAreaInsetsUpdated = NO;
2296+
BOOL safeAreaInsetsChanged = [windowProxy processForSafeArea];
2297+
layoutChanged = layoutChanged || safeAreaInsetsChanged;
2298+
if (safeAreaInsetsChanged && windowProxy.pendingSafeAreaUpdate) {
2299+
// Reset the pending safe area update here because it was already updated in the current layout cycle
2300+
windowProxy.pendingSafeAreaUpdate = NO;
2301+
}
22922302
}
22932303

22942304
if (layoutChanged && [self _hasListeners:@"postlayout" checkParent:NO]) {

iphone/TitaniumKit/TitaniumKit/Sources/API/TiWindowProxy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
@property (nonatomic, readonly) TiProxy *tabGroup;
3939
@property (nonatomic) BOOL isMasterWindow;
4040
@property (nonatomic) BOOL isDetailWindow;
41-
@property (nonatomic) BOOL safeAreaInsetsUpdated;
41+
@property (nonatomic) BOOL pendingSafeAreaUpdate;
4242

43-
- (void)processForSafeArea;
43+
- (BOOL)processForSafeArea;
4444
- (UIViewController *)windowHoldingController;
4545
- (TiUIiOSTransitionAnimationProxy *)transitionAnimation;
4646

iphone/TitaniumKit/TitaniumKit/Sources/API/TiWindowProxy.m

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,28 @@ - (BOOL)suppressesRelayout
9494
return [super suppressesRelayout];
9595
}
9696

97+
- (void)didFinishLayout
98+
{
99+
if (self.pendingSafeAreaUpdate) {
100+
self.pendingSafeAreaUpdate = NO;
101+
[self willChangeSizeForSafeArea];
102+
}
103+
104+
[super didFinishLayout];
105+
}
106+
107+
- (void)willChangeSizeForSafeArea
108+
{
109+
if ((*((char *)&dirtyflags) & (1 << (7 - TiRefreshViewSize))) != 0) {
110+
// Layout is in progress, defer the safe area update
111+
self.pendingSafeAreaUpdate = YES;
112+
return;
113+
}
114+
115+
// Proceed with normal size change logic
116+
[self willChangeSize];
117+
}
118+
97119
#pragma mark - Utility Methods
98120
- (void)windowWillOpen
99121
{
@@ -762,7 +784,7 @@ - (void)viewWillAppear:(BOOL)animated
762784

763785
- (void)viewSafeAreaInsetsDidChange
764786
{
765-
[self willChangeSize];
787+
[self willChangeSizeForSafeArea];
766788
}
767789

768790
- (void)viewWillDisappear:(BOOL)animated
@@ -977,9 +999,10 @@ - (void)setTransitionAnimation:(id)args
977999
[self rememberProxy:transitionProxy];
9781000
}
9791001

980-
- (void)processForSafeArea
1002+
- (BOOL)processForSafeArea
9811003
{
9821004
// Overridden in subclass
1005+
return NO;
9831006
}
9841007

9851008
@end

iphone/TitaniumKit/TitaniumKit/Sources/Modules/TiUIWindowProxy.m

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ - (TiViewProxy *)safeAreaView
10741074
return self.safeAreaViewProxy;
10751075
}
10761076

1077-
- (void)processForSafeArea
1077+
- (BOOL)processForSafeArea
10781078
{
10791079
[self setValue:@{ @"top" : NUMFLOAT(0.0),
10801080
@"left" : NUMFLOAT(0.0),
@@ -1096,18 +1096,17 @@ - (void)processForSafeArea
10961096
edgeInsets = [self defaultEdgeInsetsForSafeAreaInset:safeAreaInset];
10971097
}
10981098

1099+
[self setValue:@{ @"top" : NUMFLOAT(edgeInsets.top),
1100+
@"left" : NUMFLOAT(edgeInsets.left),
1101+
@"bottom" : NUMFLOAT(edgeInsets.bottom),
1102+
@"right" : NUMFLOAT(edgeInsets.right) }
1103+
forKey:@"safeAreaPadding"];
1104+
1105+
BOOL safeAreaInsetsChanged = !UIEdgeInsetsEqualToEdgeInsets(edgeInsets, oldSafeAreaInsets);
1106+
oldSafeAreaInsets = edgeInsets;
1107+
10991108
if (self.shouldExtendSafeArea) {
1100-
[self setValue:@{ @"top" : NUMFLOAT(edgeInsets.top),
1101-
@"left" : NUMFLOAT(edgeInsets.left),
1102-
@"bottom" : NUMFLOAT(edgeInsets.bottom),
1103-
@"right" : NUMFLOAT(edgeInsets.right) }
1104-
forKey:@"safeAreaPadding"];
1105-
1106-
if (!UIEdgeInsetsEqualToEdgeInsets(edgeInsets, oldSafeAreaInsets)) {
1107-
self.safeAreaInsetsUpdated = YES;
1108-
}
1109-
oldSafeAreaInsets = edgeInsets;
1110-
return;
1109+
return safeAreaInsetsChanged;
11111110
}
11121111

11131112
TiViewProxy *safeAreaProxy = [self safeAreaViewProxy];
@@ -1128,6 +1127,8 @@ - (void)processForSafeArea
11281127
if (!oldRight || [oldRight floatValue] != edgeInsets.right) {
11291128
[safeAreaProxy setRight:NUMFLOAT(edgeInsets.right)];
11301129
}
1130+
1131+
return safeAreaInsetsChanged;
11311132
}
11321133

11331134
- (UIEdgeInsets)tabGroupEdgeInsetsForSafeAreaInset:(UIEdgeInsets)safeAreaInset

0 commit comments

Comments
 (0)