#import #import #import #import #import using namespace facebook::react; extern const char EffectUIStickyHeaderName[] = "EffectUIStickyHeader"; namespace { class StickyHeaderProps final : public ViewProps { public: using ViewProps::ViewProps; }; } // namespace using HeaderNode = ConcreteViewShadowNode; using HeaderDescriptor = ConcreteComponentDescriptor; @interface EffectUIStickyHeader : RCTViewComponentView @end @implementation EffectUIStickyHeader { __weak NSClipView *_clip; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _props = HeaderNode::defaultSharedProps(); } return self; } + (void)load { [RCTComponentViewFactory.currentComponentViewFactory registerComponentViewClass:self]; } + (ComponentDescriptorProvider)componentDescriptorProvider { return concreteComponentDescriptorProvider(); } - (void)viewDidMoveToWindow { [super viewDidMoveToWindow]; NSClipView *clip = self.window ? self.enclosingScrollView.contentView : nil; if (_clip == clip) return; NSNotificationCenter *center = NSNotificationCenter.defaultCenter; if (_clip) [center removeObserver:self name:NSViewBoundsDidChangeNotification object:_clip]; _clip = clip; if (clip) { [center addObserver:self selector:@selector(pin:) name:NSViewBoundsDidChangeNotification object:clip]; [self pin:nil]; } } // A neighboring header can move or mount while this one is stationary. - (void)mountingTransactionDidMount:(const MountingTransaction &)transaction withSurfaceTelemetry:(const SurfaceTelemetry &)telemetry { [self pin:nil]; } // AppKit draws and hit-tests descendants at the NSView frame. Fabric keeps // its unscrolled layout metrics, including the next section's natural top. - (void)pin:(NSNotification *)notification { NSClipView *clip = _clip; NSView *parent = self.superview; if (!clip || !parent) return; CGFloat top = _layoutMetrics.frame.origin.y; CGFloat y = MAX(top, [parent convertPoint:clip.bounds.origin fromView:clip].y); for (NSView *view in parent.subviews) { if (view == self || ![view isKindOfClass:EffectUIStickyHeader.class]) continue; CGFloat next = ((EffectUIStickyHeader *)view)->_layoutMetrics.frame.origin.y; if (next > top) y = MIN(y, MAX(top, next - self.frame.size.height)); } if (self.frame.origin.y != y) [self setFrameOrigin:NSMakePoint(self.frame.origin.x, y)]; } @end