Skip to content

Commit

Permalink
fix(ios): relayout dom when rootView frame change
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg authored and hippy-actions[bot] committed Oct 23, 2023
1 parent 3963f68 commit e7820f9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,13 @@ - (instancetype)initWithPageCache:(HippyPageCache *)pageCache {
_debugURL = pageCache.debugURL;
_isDebugMode = pageCache.isDebugMode;
_hippyRootView = pageCache.rootView;
[_hippyRootView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:NULL];
_hippyBridge = pageCache.hippyBridge;
_fromCache = YES;
}
return self;
}

- (void)dealloc {
[_hippyRootView removeObserver:self forKeyPath:@"frame"];
[[HippyPageCacheManager defaultPageCacheManager] addPageCache:[self toPageCache]];
NSLog(@"%@ dealloc", self.class);
}
Expand Down Expand Up @@ -187,30 +185,9 @@ - (void)mountConnector:(HippyBridge *)hippyBridge {
rootView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

[self.contentAreaView addSubview:rootView];
if (_hippyRootView) {
[_hippyRootView removeObserver:self forKeyPath:@"frame" context:NULL];
}
[rootView addObserver:self
forKeyPath:@"frame"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:NULL];
_hippyRootView = rootView;
}

- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context {
if ([keyPath isEqualToString:@"frame"] &&
object == _hippyRootView) {
CGRect frame = [change[NSKeyValueChangeNewKey] CGRectValue];
CGRect oldFrame = [change[NSKeyValueChangeOldKey] CGRectValue];
if (!CGRectEqualToRect(frame, oldFrame)) {
[_hippyBridge resetRootSize:frame.size];
}
}
}

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_hippyRootView.frame = self.contentAreaView.bounds;
Expand Down
6 changes: 4 additions & 2 deletions framework/examples/ios-demo/podfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ install! 'cocoapods',
workspace 'HippyDemo.xcworkspace'

target "HippyDemo" do
project './HippyDemo.xcodeproj'
pod 'hippy', :path => '../../..'
platform :ios, '11.0'

pod 'hippy', :path => '../../..'

end

4 changes: 4 additions & 0 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,10 @@ - (void)resetRootSize:(CGSize)size {
auto strongRootNode = rootNode.lock();
auto strongDomManager = weakDomManager.lock();
if (strongRootNode && strongDomManager) {
if (std::abs(std::get<0>(strongRootNode->GetRootSize()) - size.width) < DBL_EPSILON &&
std::abs(std::get<1>(strongRootNode->GetRootSize()) - size.height) < DBL_EPSILON) {
return;
}
strongRootNode->SetRootSize(size.width, size.height);
strongDomManager->DoLayout(strongRootNode);
strongDomManager->EndBatch(strongRootNode);
Expand Down
2 changes: 1 addition & 1 deletion renderer/native/ios/renderer/HippyRootView.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extern NSString *const HippyContentDidAppearNotification;

/// Business bundle loading completion notification
/// This notification is for compatibility with hippy2 and is not recommended for further use
extern NSString *const HippySecondaryBundleDidLoadNotification DEPRECATED_MSG_ATTRIBUTE("Use HippyRootView's delegate");
extern NSString *const HippySecondaryBundleDidLoadNotification;



Expand Down
29 changes: 18 additions & 11 deletions renderer/native/ios/renderer/HippyRootView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@
#import "NativeRenderDefines.h"
#import "HippyInvalidating.h"
#import "HippyBridge.h"
#import "HippyUIManager.h"
#import "HippyDeviceBaseInfo.h"
#include <objc/runtime.h>


// Sent when the first subviews are added to the root view
NSString *const HippyContentDidAppearNotification = @"HippyContentDidAppearNotification";

// In hippy2 there are two concepts: common package and business package;
// After the success of the business package loading will send a `SecondaryBundleDidLoad` notification;
// For compatibility, hippy3 retains this notice and its actual meaning.
NSString *const HippySecondaryBundleDidLoadNotification = @"HippySecondaryBundleDidLoadNotification";

NSNumber *AllocRootViewTag(void) {
Expand Down Expand Up @@ -302,16 +307,18 @@ - (instancetype)initWithFrame:(CGRect)frame
HIPPY_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (nonnull NSCoder *)aDecoder)


// FIXME: check
//- (void)setFrame:(CGRect)frame {
// CGRect originFrame = self.frame;
// if (!CGRectEqualToRect(originFrame, frame)) {
// super.frame = frame;
// if (self.hippyTag && _bridge.isValid) {
// [_bridge.uiManager setFrame:frame fromOriginFrame:originFrame forView:self];
// }
// }
//}
- (void)setFrame:(CGRect)frame {
CGRect originFrame = self.frame;
if (!CGRectEqualToRect(originFrame, frame)) {
super.frame = frame;
if (self.hippyTag && _bridge.isValid) {
// TODO: check
// hippy2 使用[_bridge.uiManager setFrame:frame fromOriginFrame:originFrame forView:self];
// 进行frame更新时的UI重布局及刷新,有待检查hippy3此处架构合理性
[_bridge resetRootSize:frame.size];
}
}
}

#pragma mark - HippyComponent Method

Expand Down

0 comments on commit e7820f9

Please sign in to comment.