Skip to content

Commit

Permalink
feat(ios): optimize scale factor and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ozonelmy committed Nov 18, 2021
1 parent 6470d12 commit 75bcdbd
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const styles = StyleSheet.create({
separatorLine: {
marginLeft: 12,
marginRight: 12,
height: 0.5,
height: 1,
backgroundColor: '#e5e5e5',
},
loading: {
Expand Down
4 changes: 2 additions & 2 deletions examples/ios-demo/res/index.ios.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ios/sdk/component/view/HippyShadowView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ - (instancetype)init {

_hippySubviews = [NSMutableArray array];

_nodeRef = MTTNodeNew();
_nodeRef = MTTNodeNewWithScaleFactor([UIScreen mainScreen].scale);
}
return self;
}
Expand Down
4 changes: 4 additions & 0 deletions ios/sdk/layout/MTTLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ MTTNodeRef MTTNodeNew() {
return new MTTNode();
}

MTTNodeRef MTTNodeNewWithScaleFactor(float scaleFactor) {
return new MTTNode(scaleFactor);
}

void MTTNodeFree(MTTNodeRef node) {
if (node == nullptr)
return;
Expand Down
1 change: 1 addition & 0 deletions ios/sdk/layout/MTTLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "MTTNode.h"

MTTNodeRef MTTNodeNew();
MTTNodeRef MTTNodeNewWithScaleFactor(float scaleFactor);
void MTTNodeFree(MTTNodeRef node);
void MTTNodeFreeRecursive(MTTNodeRef node);

Expand Down
28 changes: 19 additions & 9 deletions ios/sdk/layout/MTTNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ MTTNode::MTTNode() {

initLayoutResult();
inInitailState = true;
scaleFactor = 1.f;
}

MTTNode::MTTNode(float sf):scaleFactor(sf) {
context = nullptr;
parent = nullptr;
measure = nullptr;
dirtiedFunc = nullptr;
initLayoutResult();
inInitailState = true;
}

MTTNode::~MTTNode() {
Expand Down Expand Up @@ -597,7 +607,7 @@ void MTTNode::layout(float parentWidth,
// node 's layout is complete
// convert its and its descendants position and size to a integer value.
#ifndef ANDROID
convertLayoutResult(0.0f, 0.0f); // layout result convert has been taken in
convertLayoutResult(0.0f, 0.0f, scaleFactor); // layout result convert has been taken in
// java . 3.8.2018. ianwang..
#endif

Expand Down Expand Up @@ -1512,7 +1522,7 @@ void MTTNode::calculateFixedItemPosition(MTTNodeRef item, FlexDirection axis) {
// offset for example: if parent's Fraction offset is 0.3 and current child
// offset is 0.4 then the child's absolute offset is 0.7. if use roundf ,
// roundf(0.7) == 1 so we need absLeft, absTop parameter
void MTTNode::convertLayoutResult(float absLeft, float absTop) {
void MTTNode::convertLayoutResult(float absLeft, float absTop, float scaleFactor) {
if (!hasNewLayout()) {
return;
}
Expand All @@ -1524,8 +1534,8 @@ void MTTNode::convertLayoutResult(float absLeft, float absTop) {
absLeft += left;
absTop += top;
bool isTextNode = style.nodeType == NodeTypeText;
result.position[CSSLeft] = MTTRoundValueToPixelGrid(left, false, isTextNode);
result.position[CSSTop] = MTTRoundValueToPixelGrid(top, false, isTextNode);
result.position[CSSLeft] = MTTRoundValueToPixelGrid(left, scaleFactor, false, isTextNode);
result.position[CSSTop] = MTTRoundValueToPixelGrid(top, scaleFactor, false, isTextNode);

const bool hasFractionalWidth =
!FloatIsEqual(fmodf(width, 1.0), 0) && !FloatIsEqual(fmodf(width, 1.0), 1.0);
Expand All @@ -1534,16 +1544,16 @@ void MTTNode::convertLayoutResult(float absLeft, float absTop) {

const float absRight = absLeft + width;
const float absBottom = absTop + height;
result.dim[DimWidth] = MTTRoundValueToPixelGrid(absRight, (isTextNode && hasFractionalWidth),
result.dim[DimWidth] = MTTRoundValueToPixelGrid(absRight, scaleFactor, (isTextNode && hasFractionalWidth),
(isTextNode && !hasFractionalWidth)) -
MTTRoundValueToPixelGrid(absLeft, false, isTextNode);
MTTRoundValueToPixelGrid(absLeft, scaleFactor, false, isTextNode);

result.dim[DimHeight] = MTTRoundValueToPixelGrid(absBottom, (isTextNode && hasFractionalHeight),
result.dim[DimHeight] = MTTRoundValueToPixelGrid(absBottom, scaleFactor, (isTextNode && hasFractionalHeight),
(isTextNode && !hasFractionalHeight)) -
MTTRoundValueToPixelGrid(absTop, false, isTextNode);
MTTRoundValueToPixelGrid(absTop, scaleFactor, false, isTextNode);
std::vector<MTTNodeRef>& items = children;
for (size_t i = 0; i < items.size(); i++) {
MTTNodeRef item = items[i];
item->convertLayoutResult(absLeft, absTop);
item->convertLayoutResult(absLeft, absTop, scaleFactor);
}
}
10 changes: 9 additions & 1 deletion ios/sdk/layout/MTTNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef void (*MTTDirtiedFunc)(MTTNodeRef node);
class MTTNode {
public:
MTTNode();
MTTNode(float sf);
virtual ~MTTNode();
void initLayoutResult();
bool reset();
Expand Down Expand Up @@ -102,6 +103,12 @@ class MTTNode {
void setLayoutDirection(MTTDirection direction);
MTTDirection getLayoutDirection();
FlexAlign getNodeAlign(MTTNodeRef item);
void setScaleFactor(float sf) {
scaleFactor = sf;
};
float getScaleFactor() {
return scaleFactor;
}

protected:
MTTDirection resolveDirection(MTTDirection parentDirection);
Expand Down Expand Up @@ -135,7 +142,7 @@ class MTTNode {
void layoutFixedItems(MTTSizeMode measureMode, void *layoutContext);
void calculateFixedItemPosition(MTTNodeRef item, FlexDirection axis);

void convertLayoutResult(float absLeft, float absTop);
void convertLayoutResult(float absLeft, float absTop, float scaleFactor);

public:
MTTStyle style;
Expand All @@ -155,6 +162,7 @@ class MTTNode {
MTTLayoutCache layoutCache;
// layout result is in initial state or not
bool inInitailState;
float scaleFactor;
#ifdef LAYOUT_TIME_ANALYZE
int fetchCount;
#endif
Expand Down
24 changes: 14 additions & 10 deletions ios/sdk/layout/MTTUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,27 @@ bool MTTSizeIsEqualInScale(MTTSize a, MTTSize b, float scale) {
FloatIsEqualInScale(a.height, b.height, scale);
}

float MTTRoundValueToPixelGrid(float value, bool forceCeil, bool forceFloor) {
float fractial = fmodf(value, 1.0);
float MTTRoundValueToPixelGrid(float value, float scaleFactor, bool forceCeil, bool forceFloor) {
float scaleValue = value * scaleFactor;
float fractial = fmodf(scaleValue, 1.0);
if (fractial < 0) {
++fractial;
}

if (FloatIsEqual(fractial, 0)) {
// First we check if the value is already rounded
value = value - fractial;
scaleValue = scaleValue - fractial;
} else if (FloatIsEqual(fractial, 1.0)) {
value = value - fractial + 1.0;
scaleValue = scaleValue - fractial + 1.0;
} else if (forceCeil) {
// Next we check if we need to use forced rounding
value = value - fractial + 1.0f;
scaleValue = scaleValue - fractial + 1.0f;
} else if (forceFloor) {
value = value - fractial;
scaleValue = scaleValue - fractial;
} else {
// Finally we just round the value
value = roundf(value);
// value = value - fractial +
// (fractial > 0.5f || FloatIsEqual(fractial, 0.5f) ? 1.0f : 0.0f);
scaleValue = scaleValue - fractial +
(fractial > 0.5f || FloatIsEqual(fractial, 0.5f) ? 1.0f : 0.0f);
}
return value;
return scaleValue / scaleFactor;
}
2 changes: 1 addition & 1 deletion ios/sdk/layout/MTTUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ bool FloatIsEqual(const float a, const float b);
bool FloatIsEqualInScale(float a, float b, float scale);
bool MTTSizeIsEqual(MTTSize a, MTTSize b);
bool MTTSizeIsEqualInScale(MTTSize a, MTTSize b, float scale);
float MTTRoundValueToPixelGrid(float value, bool forceCeil, bool forceFloor);
float MTTRoundValueToPixelGrid(float value, float scaleValue, bool forceCeil, bool forceFloor);

0 comments on commit 75bcdbd

Please sign in to comment.