Skip to content

Instantly share code, notes, and snippets.

@berkaycatak
Last active October 21, 2026 18:46
Show Gist options
  • Select an option

  • Save berkaycatak/c05f82676ae849feea4d3ceb20db5868 to your computer and use it in GitHub Desktop.

Select an option

Save berkaycatak/c05f82676ae849feea4d3ceb20db5868 to your computer and use it in GitHub Desktop.
iPhone Duo: does UIKit re-run layout when a reserved region changes? UIKit-only probe mirroring flutter/flutter#193025
// Pure UIKit probe with the exact shape of the engine PR: the view controller's
// root view hosts a UIHingeInteraction, and viewDidLayoutSubviews reads the
// reserved regions with the same two queries. A display link reads the regions
// through a separate sibling view, only to report the true moment of change.
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
static NSString* Describe(UIView* v) {
NSMutableString* s = [NSMutableString string];
for (UIViewReservedRegion* r in [v reservedRegionsOfKind:[UIViewReservedRegionKind divisionRegionKind]
options:UIViewReservedRegionQueryOptionsIncludeInactive]) {
[s appendFormat:@"div(active=%d w=%.0f) ", r.isActive, r.frame.size.width];
}
for (UIViewReservedRegion* r in [v reservedRegionsOfKind:[UIViewReservedRegionKind occlusionRegionKind]
options:UIViewReservedRegionQueryOptionsNone]) {
[s appendFormat:@"occ(active=%d w=%.0f) ", r.isActive, r.frame.size.width];
}
return s.length ? s : @"none";
}
@interface ProbeVC : UIViewController
@property(nonatomic, strong) UIHingeInteraction* interaction;
@property(nonatomic, strong) UIView* sibling;
@property(nonatomic, strong) CADisplayLink* link;
@property(nonatomic, copy) NSString* last;
@end
@implementation ProbeVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.systemBackgroundColor;
UILabel* l = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 400, 40)];
l.text = @"DuoProbe: fold and unfold the device"; [self.view addSubview:l];
__weak ProbeVC* weakSelf = self;
self.interaction = [[UIHingeInteraction alloc] initWithUpdateHandler:^(UIHingeInteraction* i, UIHingeInteractionUpdate* u) {
ProbeVC* s = weakSelf; if (!s) return;
NSLog(@"DUOPROBE %.3f HINGE status=%ld inHandler: %@", CACurrentMediaTime(), (long)(u.hinge ? u.hinge.status : -1), Describe(s.view));
}];
[self.view addInteraction:self.interaction];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// The engine's refresh: read during layout on the controller's root view.
NSLog(@"DUOPROBE %.3f LAYOUT viewDidLayoutSubviews bounds=%@ %@", CACurrentMediaTime(),
NSStringFromCGSize(self.view.bounds.size), Describe(self.view));
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (self.sibling) return;
self.sibling = [[UIView alloc] initWithFrame:self.view.window.bounds];
self.sibling.userInteractionEnabled = NO;
[self.view.window addSubview:self.sibling];
self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick)];
[self.link addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
NSLog(@"DUOPROBE %.3f START", CACurrentMediaTime());
}
- (void)tick {
NSString* now = Describe(self.sibling);
if (![now isEqualToString:self.last]) { self.last = now; NSLog(@"DUOPROBE %.3f REGIONCHANGE(sibling) %@", CACurrentMediaTime(), now); }
}
@end
@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>
@property(nonatomic, strong) UIWindow* window;
@end
@implementation SceneDelegate
- (void)scene:(UIScene*)scene willConnectToSession:(UISceneSession*)session options:(UISceneConnectionOptions*)options {
UIWindowScene* ws = (UIWindowScene*)scene;
self.window = [[UIWindow alloc] initWithWindowScene:ws];
self.window.rootViewController = [ProbeVC new];
[self.window makeKeyAndVisible];
}
@end
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end
@implementation AppDelegate
- (UISceneConfiguration*)application:(UIApplication*)application configurationForConnectingSceneSession:(UISceneSession*)session options:(UISceneConnectionOptions*)options {
UISceneConfiguration* c = [[UISceneConfiguration alloc] initWithName:@"Default" sessionRole:session.role];
c.delegateClass = [SceneDelegate class];
return c;
}
@end
int main(int argc, char* argv[]) {
@autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }
}
// DuoProbe.m with an on-screen copy of its log. The measured view, the hinge
// interaction and the read in viewDidLayoutSubviews are the same as in
// DuoProbe.m; the log panel, the posture header and the orange bar drawn over
// the division region live in a second window so that nothing in the measured
// view changes while it is being observed. Nothing calls setNeedsLayout.
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
// On-screen copy of the log, kept in a separate window so that nothing in the
// measured view changes while it is being observed.
static UITextView* gLogView;
static UILabel* gHeader;
static UIView* gDivisionOverlay;
static NSString* StatusName(long s) {
switch (s) { case 1: return @"closed"; case 2: return @"partiallyOpen"; case 3: return @"fullyOpen"; default: return @"unknown"; }
}
static NSMutableArray<NSString*>* gLines;
static void Log(NSString* fmt, ...) {
va_list a; va_start(a, fmt);
NSString* line = [[NSString alloc] initWithFormat:fmt arguments:a];
va_end(a);
NSLog(@"DUOPROBE %@", line);
if (!gLines) gLines = [NSMutableArray array];
[gLines addObject:line];
while (gLines.count > 26) [gLines removeObjectAtIndex:0];
gLogView.text = [gLines componentsJoinedByString:@"\n"];
}
static NSString* Describe(UIView* v) {
NSMutableString* s = [NSMutableString string];
for (UIViewReservedRegion* r in [v reservedRegionsOfKind:[UIViewReservedRegionKind divisionRegionKind]
options:UIViewReservedRegionQueryOptionsIncludeInactive]) {
[s appendFormat:@"div(active=%d w=%.0f) ", r.isActive, r.frame.size.width];
}
for (UIViewReservedRegion* r in [v reservedRegionsOfKind:[UIViewReservedRegionKind occlusionRegionKind]
options:UIViewReservedRegionQueryOptionsNone]) {
[s appendFormat:@"occ(active=%d w=%.0f) ", r.isActive, r.frame.size.width];
}
return s.length ? s : @"none";
}
@interface ProbeVC : UIViewController
@property(nonatomic, strong) UIHingeInteraction* interaction;
@property(nonatomic, strong) UIView* sibling;
@property(nonatomic, strong) CADisplayLink* link;
@property(nonatomic, copy) NSString* last;
@end
@implementation ProbeVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.systemBackgroundColor;
UILabel* l = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 400, 40)];
l.text = @"DuoProbe: fold and unfold the device"; [self.view addSubview:l];
__weak ProbeVC* weakSelf = self;
self.interaction = [[UIHingeInteraction alloc] initWithUpdateHandler:^(UIHingeInteraction* i, UIHingeInteractionUpdate* u) {
ProbeVC* s = weakSelf; if (!s) return;
if (u.hinge) {
gHeader.text = [NSString stringWithFormat:@"hinge: %@ (%ld) angle: %.0f°",
StatusName(u.hinge.status), (long)u.hinge.status, u.hinge.angle * 180.0 / M_PI];
}
Log(@"%.3f HINGE status=%ld inHandler: %@", CACurrentMediaTime(), (long)(u.hinge ? u.hinge.status : -1), Describe(s.view));
}];
[self.view addInteraction:self.interaction];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// The engine's refresh: read during layout on the controller's root view.
Log(@"%.3f LAYOUT viewDidLayoutSubviews bounds=%@ %@", CACurrentMediaTime(),
NSStringFromCGSize(self.view.bounds.size), Describe(self.view));
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (self.sibling) return;
self.sibling = [[UIView alloc] initWithFrame:self.view.window.bounds];
self.sibling.userInteractionEnabled = NO;
[self.view.window addSubview:self.sibling];
self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick)];
[self.link addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
Log(@"%.3f START", CACurrentMediaTime());
}
- (void)tick {
for (UIViewReservedRegion* r in [self.sibling reservedRegionsOfKind:[UIViewReservedRegionKind divisionRegionKind]
options:UIViewReservedRegionQueryOptionsIncludeInactive]) {
gDivisionOverlay.frame = r.frame;
gDivisionOverlay.hidden = !r.isActive;
}
NSString* now = Describe(self.sibling);
if (![now isEqualToString:self.last]) { self.last = now; Log(@"%.3f REGIONCHANGE(sibling) %@", CACurrentMediaTime(), now); }
}
@end
@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>
@property(nonatomic, strong) UIWindow* window;
@property(nonatomic, strong) UIWindow* logWindow;
@end
@implementation SceneDelegate
- (void)scene:(UIScene*)scene willConnectToSession:(UISceneSession*)session options:(UISceneConnectionOptions*)options {
UIWindowScene* ws = (UIWindowScene*)scene;
self.window = [[UIWindow alloc] initWithWindowScene:ws];
self.window.rootViewController = [ProbeVC new];
[self.window makeKeyAndVisible];
self.logWindow = [[UIWindow alloc] initWithWindowScene:ws];
self.logWindow.windowLevel = UIWindowLevelNormal + 1;
self.logWindow.userInteractionEnabled = NO;
UIViewController* vc = [UIViewController new];
vc.view.backgroundColor = UIColor.clearColor;
gLogView = [[UITextView alloc] initWithFrame:CGRectZero];
gLogView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
gLogView.textColor = UIColor.greenColor;
gLogView.font = [UIFont monospacedSystemFontOfSize:11 weight:UIFontWeightRegular];
gLogView.editable = NO; gLogView.scrollEnabled = NO;
gLogView.translatesAutoresizingMaskIntoConstraints = NO;
[vc.view addSubview:gLogView];
gDivisionOverlay = [[UIView alloc] initWithFrame:CGRectZero];
gDivisionOverlay.backgroundColor = [UIColor.orangeColor colorWithAlphaComponent:0.6];
gDivisionOverlay.hidden = YES;
[vc.view addSubview:gDivisionOverlay];
gHeader = [[UILabel alloc] initWithFrame:CGRectZero];
gHeader.textColor = UIColor.whiteColor;
gHeader.font = [UIFont monospacedSystemFontOfSize:26 weight:UIFontWeightBold];
gHeader.text = @"hinge: waiting for first update";
gHeader.translatesAutoresizingMaskIntoConstraints = NO;
[vc.view addSubview:gHeader];
[NSLayoutConstraint activateConstraints:@[
[gHeader.leadingAnchor constraintEqualToAnchor:vc.view.safeAreaLayoutGuide.leadingAnchor constant:20],
[gHeader.bottomAnchor constraintEqualToAnchor:vc.view.safeAreaLayoutGuide.bottomAnchor constant:-16],
]];
[NSLayoutConstraint activateConstraints:@[
[gLogView.leadingAnchor constraintEqualToAnchor:vc.view.safeAreaLayoutGuide.leadingAnchor constant:8],
[gLogView.trailingAnchor constraintEqualToAnchor:vc.view.safeAreaLayoutGuide.trailingAnchor constant:-8],
[gLogView.topAnchor constraintEqualToAnchor:vc.view.safeAreaLayoutGuide.topAnchor constant:140],
[gLogView.bottomAnchor constraintEqualToAnchor:vc.view.safeAreaLayoutGuide.bottomAnchor constant:-64],
]];
self.logWindow.rootViewController = vc;
self.logWindow.hidden = NO;
}
@end
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end
@implementation AppDelegate
- (UISceneConfiguration*)application:(UIApplication*)application configurationForConnectingSceneSession:(UISceneSession*)session options:(UISceneConnectionOptions*)options {
UISceneConfiguration* c = [[UISceneConfiguration alloc] initWithName:@"Default" sessionRole:session.role];
c.delegateClass = [SceneDelegate class];
return c;
}
@end
int main(int argc, char* argv[]) {
@autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }
}
// Minimal UIKit app: one view controller, no interaction, no display link, no
// second window. viewDidLayoutSubviews logs every call. Built twice: with
// READ_IN_LAYOUT=0 it only logs; with READ_IN_LAYOUT=1 it also reads the
// division region once, which is the one line the engine's refresh adds.
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.systemBackgroundColor;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
#if READ_IN_LAYOUT
UIViewReservedRegion* r =
[self.view reservedRegionsOfKind:[UIViewReservedRegionKind divisionRegionKind]
options:UIViewReservedRegionQueryOptionsIncludeInactive].firstObject;
NSLog(@"VANILLA %.3f viewDidLayoutSubviews bounds=%@ division active=%d", CACurrentMediaTime(),
NSStringFromCGSize(self.view.bounds.size), r.isActive);
#else
NSLog(@"VANILLA %.3f viewDidLayoutSubviews bounds=%@", CACurrentMediaTime(),
NSStringFromCGSize(self.view.bounds.size));
#endif
}
@end
@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>
@property(nonatomic, strong) UIWindow* window;
@end
@implementation SceneDelegate
- (void)scene:(UIScene*)scene willConnectToSession:(UISceneSession*)session options:(UISceneConnectionOptions*)options {
self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene*)scene];
self.window.rootViewController = [ViewController new];
[self.window makeKeyAndVisible];
}
@end
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end
@implementation AppDelegate
- (UISceneConfiguration*)application:(UIApplication*)application configurationForConnectingSceneSession:(UISceneSession*)session options:(UISceneConnectionOptions*)options {
UISceneConfiguration* c = [[UISceneConfiguration alloc] initWithName:@"Default" sessionRole:session.role];
c.delegateClass = [SceneDelegate class];
return c;
}
@end
int main(int argc, char* argv[]) {
@autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment