This repository has been archived by the owner on Jan 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
Use nativeEvent.layout in onLayout, instead of buggy measureInWindow #42
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,9 +137,16 @@ class SafeView extends Component { | |
); | ||
} | ||
|
||
_onLayout = (...args) => { | ||
_onLayout = (info, ...args) => { | ||
if (!this.view) return; | ||
if (!info || !info.nativeEvent) { | ||
if (this.props.onLayout) { | ||
this.props.onLayout(...args); | ||
return; | ||
} | ||
} | ||
|
||
const { layout } = info.nativeEvent; | ||
const { isLandscape } = this.props; | ||
const { orientation } = this.state; | ||
const newOrientation = isLandscape ? 'landscape' : 'portrait'; | ||
|
@@ -150,42 +157,37 @@ class SafeView extends Component { | |
const WIDTH = isLandscape ? X_HEIGHT : X_WIDTH; | ||
const HEIGHT = isLandscape ? X_WIDTH : X_HEIGHT; | ||
|
||
this.view._component.measureInWindow((winX, winY, winWidth, winHeight) => { | ||
if (!this.view) { | ||
return; | ||
} | ||
let realY = winY; | ||
let realX = winX; | ||
let realY = layout.y; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will give the x and y relative to the container, not the x and y relative to the entire window. we need to know the x/y relative to the window because that's the only way to know if the container overlaps with a safe area. again run https://snack.expo.io/@notbrent/thrilled-pizza and notice the logs for "nested" show x/y of 0,0 despite it not being in the top left of the screen. |
||
let realX = layout.x; | ||
|
||
if (realY >= HEIGHT) { | ||
realY = realY % HEIGHT; | ||
} else if (realY < 0) { | ||
realY = realY % HEIGHT + HEIGHT; | ||
} | ||
if (realY >= HEIGHT) { | ||
realY = realY % HEIGHT; | ||
} else if (realY < 0) { | ||
realY = (realY % HEIGHT) + HEIGHT; | ||
} | ||
|
||
if (realX >= WIDTH) { | ||
realX = realX % WIDTH; | ||
} else if (realX < 0) { | ||
realX = realX % WIDTH + WIDTH; | ||
} | ||
if (realX >= WIDTH) { | ||
realX = realX % WIDTH; | ||
} else if (realX < 0) { | ||
realX = (realX % WIDTH) + WIDTH; | ||
} | ||
|
||
const touchesTop = realY === 0; | ||
const touchesBottom = realY + winHeight >= HEIGHT; | ||
const touchesLeft = realX === 0; | ||
const touchesRight = realX + winWidth >= WIDTH; | ||
|
||
this.setState({ | ||
touchesTop, | ||
touchesBottom, | ||
touchesLeft, | ||
touchesRight, | ||
orientation: newOrientation, | ||
viewWidth: winWidth, | ||
viewHeight: winHeight, | ||
}); | ||
|
||
if (this.props.onLayout) this.props.onLayout(...args); | ||
const touchesTop = realY === 0; | ||
const touchesBottom = realY + layout.height >= HEIGHT; | ||
const touchesLeft = realX === 0; | ||
const touchesRight = realX + layout.width >= WIDTH; | ||
|
||
this.setState({ | ||
touchesTop, | ||
touchesBottom, | ||
touchesLeft, | ||
touchesRight, | ||
orientation: newOrientation, | ||
viewWidth: layout.width, | ||
viewHeight: layout.height | ||
}); | ||
|
||
if (this.props.onLayout) this.props.onLayout(...args); | ||
}; | ||
|
||
_getSafeAreaStyle = () => { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to pass the
info
var through here. i'd call ite
orevent
instead, also. to see why it's necessary to pass it through have a look at https://snack.expo.io/@notbrent/thrilled-pizza - run it and then look at logs, you'll see this: