From de7e9d77a9ae1920f39c2cc7cb1b8bc5f23ab793 Mon Sep 17 00:00:00 2001 From: Dominic Go Date: Mon, 7 Oct 2024 08:32:02 +0800 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=EF=B8=8F=20Impl:=20`RNIContentSizingM?= =?UTF-8?q?ode.updateReactSizeIfNeeded`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ios/Temp/RNIContentSizingMode.swift | 128 ++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/ios/Temp/RNIContentSizingMode.swift b/ios/Temp/RNIContentSizingMode.swift index f76c167..b4eccd6 100644 --- a/ios/Temp/RNIContentSizingMode.swift +++ b/ios/Temp/RNIContentSizingMode.swift @@ -7,6 +7,7 @@ import Foundation import DGSwiftUtilities +import react_native_ios_utilities /// Should this really be an enum? @@ -62,6 +63,11 @@ public enum RNIContentSizingMode: String { }; }; + public var isSizingWidthOrHeightFromReact: Bool { + self.isSizingHeightFromReact + || self.isSizingWidthFromReact; + }; + // MARK: Functions // --------------- @@ -128,4 +134,126 @@ public enum RNIContentSizingMode: String { isSizingHeightFromNative: verticalAlignmentPosition.isStretching ); }; + + public func updateReactSizeIfNeeded( + forReactParent reactParentView: RNIContentViewParentDelegate, + withNewSize newSize: CGSize + ){ + + switch self { + case .sizingFromReact: + return; + + case .sizingFromNative: + let didSizeChange = + reactParentView.intrinsicContentSize.width != newSize.width + || reactParentView.intrinsicContentSize.height != newSize.height; + + guard didSizeChange else { + return; + }; + + reactParentView.setSize(newSize); + reactParentView.intrinsicContentSizeOverride = newSize; + reactParentView.invalidateIntrinsicContentSize(); + + case .sizingWidthFromReactAndHeightFromNative: + let oldReactHeight = reactParentView.intrinsicContentSize.height; + let newReactHeight = newSize.height; + + guard newReactHeight != oldReactHeight else { + return; + }; + + reactParentView.intrinsicContentSizeOverride = .init( + width: 0, + height: newReactHeight + ); + + reactParentView.invalidateIntrinsicContentSize(); + + #if RCT_NEW_ARCH_ENABLED + reactParentView.requestToUpdateState( + .init( + minSize: .init(width: 0, height: newReactHeight), + shouldSetMinWidth: true, + maxSize: .init(width: 0, height: newReactHeight), + shouldSetMaxHeight: true + ) + ); + #else + guard let shadowView = reactParentView.cachedShadowView else { + return; + }; + + let didChange = + CGFloat(shadowView.minHeight.value) != newReactHeight + || CGFloat(shadowView.maxHeight.value) != newReactHeight; + + guard didChange else { + return; + }; + + shadowView.minHeight = .init( + value: Float(newReactHeight), + unit: .point + ); + + shadowView.maxHeight = .init( + value: Float(newReactHeight), + unit: .point + ); + + shadowView.dirtyLayout(); + #endif + + case .sizingHeightFromReactAndWidthFromNative: + let oldReactWidth = reactParentView.intrinsicContentSize.width; + let newReactWidth = newSize.width; + + guard newReactWidth != oldReactWidth else { + return; + }; + + reactParentView.intrinsicContentSizeOverride = .init( + width: newReactWidth, + height: 0 + ); + + #if RCT_NEW_ARCH_ENABLED + reactParentView.requestToUpdateState( + .init( + minSize: .init(width: newReactWidth, height: 0), + shouldSetMinWidth: true, + maxSize: .init(width: newReactWidth, height: 0), + shouldSetMaxHeight: true + ) + ); + #else + guard let shadowView = reactParentView.cachedShadowView else { + return; + }; + + let didChange = + CGFloat(shadowView.minWidth.value) != newReactWidth + || CGFloat(shadowView.maxWidth.value) != newReactWidth; + + guard didChange else { + return; + }; + + shadowView.minWidth = .init( + value: Float(newWidth), + unit: .point + ); + + shadowView.maxWidth = .init( + value: Float(newWidth), + unit: .point + ); + + shadowView.dirtyLayout(); + #endif + }; + }; };