Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Android, Fabric): jumping content with native header #2169

Merged
merged 66 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
6b2e6da
Stash current changes with jni setup
kkafar Jun 4, 2024
2a2e1bc
Play around with various ideas
kkafar Jun 5, 2024
5c8fd35
Working PoC
kkafar Jun 7, 2024
581cd7e
Get access to font size!
kkafar Jun 7, 2024
fd86a96
Add missing callsite parameter
kkafar Jun 7, 2024
fd9b32f
Improve PoC by adding font-size param
kkafar Jun 7, 2024
bdfc038
Take font size into consideration
kkafar Jun 7, 2024
1ff7f95
Cleanup custom toolbar
kkafar Jun 7, 2024
29a92f3
Remove native function from ScreenViewManager
kkafar Jun 7, 2024
8b445d8
Partially cleanup package file
kkafar Jun 7, 2024
ed0b6ca
Cleanup C++ code & move implementation back to header file
kkafar Jun 7, 2024
342c27e
Format common/ C++ code
kkafar Jun 7, 2024
7d2c5d8
Add ifdefs for Android implementation
kkafar Jun 7, 2024
5ed1933
Cleanup library entrypoint files
kkafar Jun 7, 2024
aadfe60
Move WEAK_THIS initialization to createViewManagers & ...
kkafar Jun 7, 2024
ecd33f6
Missing code comment & cleaup
kkafar Jun 7, 2024
14b0724
Cleanup shadow nodes
kkafar Jun 7, 2024
9df3744
Remove console log from Screen
kkafar Jun 7, 2024
b74618b
Restore in apps directory
kkafar Jun 7, 2024
1c1f8a0
More cleanup in java files
kkafar Jun 7, 2024
81805f0
Optimize imports
kkafar Jun 7, 2024
f8a251b
Improve signature of getInstance method
kkafar Jun 9, 2024
29f6b16
Improve error message in C++ layer when receiving null instance
kkafar Jun 9, 2024
6997c26
Whitespaces in C++ code
kkafar Jun 9, 2024
f67ba5e
Improve JVM code a bit more, nicer error messages
kkafar Jun 9, 2024
27c6ec9
Remove unnecessary line setting support action bar on Activity
kkafar Jun 9, 2024
35f41b7
Restore compiler warning flags
kkafar Jun 9, 2024
5d9210a
Add static variable for font-unset value & cleanup in loggs
kkafar Jun 9, 2024
5221637
Remove unnecessary library initialization code
kkafar Jun 9, 2024
73e533f
Include fbjni only on Android
kkafar Jun 9, 2024
55418d9
Cleanup imports
kkafar Jun 9, 2024
eb36e66
Setup custom component descriptor for screen stack
kkafar Jun 9, 2024
61c42d1
Add simple LRU cache for headerHeight computation
kkafar Jun 10, 2024
f76a0d9
Use HEADER_HEIGHT_CORRECTION global in custom header config descriptor
kkafar Jun 10, 2024
fcc5415
Handle headerShown: false
kkafar Jun 10, 2024
8ea06ac
Make HeaderHeightCacheEntry private
kkafar Jun 10, 2024
8e3362f
Better naming & rnscreens namespace
kkafar Jun 10, 2024
608a11c
Use initialized empty option instead of uninitialized option as retur…
kkafar Jun 13, 2024
9884e83
Use margin instead of setting header config size
kkafar Jun 13, 2024
699015e
Remove setup wiht screen stack header config component descriptor
kkafar Jun 13, 2024
f465673
Try out setup with setting margin
kkafar Jun 13, 2024
51ebbb7
Go back to padding solution
kkafar Jun 13, 2024
e90f57d
Update frame origin & height manually
kkafar Jun 13, 2024
d3da357
Separate JVM implementation to separate class
kkafar Jun 13, 2024
c3aab0e
Working PoC with padding & keeping header height in node state
kkafar Jun 14, 2024
175f49c
Some comments for JVM implementation part
kkafar Jun 14, 2024
02a58c6
Use std:cref
kkafar Jun 14, 2024
2c42d65
Cleanup example a bit
kkafar Jun 14, 2024
8a638c4
Restore Test42 as default
kkafar Jun 14, 2024
dc73577
Merge branch 'main' into @kkafar/attempt-to-fix-jumping-header
kkafar Jun 14, 2024
fc701ef
Add test header to new App.js
kkafar Jun 14, 2024
cc110c5
Remove leftover showcase code
kkafar Jun 14, 2024
3746958
Rename HeaderCorrectionModes to FrameCorrectionModes & put it behind …
kkafar Jun 16, 2024
e3c2fd8
Add verbose error message
kkafar Jun 16, 2024
8fb2709
Cleanup debug logging
kkafar Jun 16, 2024
5938d56
Make compareFrameSizes inline
kkafar Jun 16, 2024
359bc49
Merge branch 'main' into @kkafar/attempt-to-fix-jumping-header
kkafar Jun 17, 2024
54463b4
Use Float instead of raw float to accomodate for platform differences
kkafar Jun 17, 2024
d31dd00
Merge branch 'main' into @kkafar/attempt-to-fix-jumping-header
kkafar Jun 17, 2024
c5e3c6e
Remove merge artifact: App.js in old localisation
kkafar Jun 17, 2024
184a527
Make comparision equality weak
kkafar Jun 17, 2024
fbda22e
Set padding only if headerConfig child is present
kkafar Jun 17, 2024
cb6fe2b
Handle weird case when title is empty but there is custom font size
kkafar Jun 17, 2024
db138f9
Add comment why 0.01 was chosen as default value for parameter
kkafar Jun 17, 2024
a0d15bb
Revert building example from source
kkafar Jun 18, 2024
4f34895
Revert disabling of headerTopInset
kkafar Jun 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Take font size into consideration
  • Loading branch information
kkafar committed Jun 7, 2024
commit bdfc038d919dc1751ae1d9705808b1fc76f83f08
18 changes: 18 additions & 0 deletions android/src/main/java/com/swmansion/rnscreens/RNScreensPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.swmansion.rnscreens
import android.graphics.Color
import android.view.View
import android.view.View.MeasureSpec
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
Expand Down Expand Up @@ -88,6 +89,11 @@ class RNScreensPackage : TurboReactPackage() {

val widthMeasureSpec = MeasureSpec.makeMeasureSpec(decorViewWidth, MeasureSpec.EXACTLY)
val heightMeasureSpec = MeasureSpec.makeMeasureSpec(decorViewHeight, MeasureSpec.EXACTLY)

val textView = findTextViewInToolbar(toolbar)

textView?.takeIf { fontSize != -1 }?.let { it.textSize = fontSize.toFloat() }

coordinatorLayout.measure(widthMeasureSpec, heightMeasureSpec)
coordinatorLayout.layout(0, 0, decorViewWidth, decorViewHeight)

Expand All @@ -97,6 +103,18 @@ class RNScreensPackage : TurboReactPackage() {
return height
}

private fun findTextViewInToolbar(toolbar: Toolbar): TextView? {
for (i in 0 until toolbar.childCount) {
val view = toolbar.getChildAt(i)
if (view is TextView) {
if (view.text == toolbar.title) {
return view
}
}
}
return null
}

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
WEAK_THIS = WeakReference(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ class ScreenStackFragment : ScreenFragment, ScreenStackFragmentWrapper {

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
super.onLayout(changed, l, t, r, b)
val headerNativelyMeasuredDimension = this.getChildAt(1).height
val tmp = headerNativelyMeasuredDimension
val tmpInDIP = PixelUtil.toDIPFromPixel(tmp.toFloat());
val tmp2 = tmpInDIP
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
Expand Down
6 changes: 3 additions & 3 deletions apps/test-examples/src/TestHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export default function App(): JSX.Element {
options={{
headerShown: true,
statusBarTranslucent: false,
// headerTitleStyle: {
// fontSize: 64,
// }
headerTitleStyle: {
fontSize: 64,
}
}}
/>
<Stack.Screen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <fbjni/fbjni.h>
#include <react/debug/react_native_assert.h>
#include <react/renderer/core/ConcreteComponentDescriptor.h>
#include <react/renderer/components/rnscreens/Props.h>
#include "RNSScreenShadowNode.h"

jint HEADER_HEIGHT = 0;
Expand All @@ -16,7 +17,18 @@ class RNSScreenComponentDescriptor final
public:
using ConcreteComponentDescriptor::ConcreteComponentDescriptor;

void adopt(ShadowNode &shadowNode) const override {

std::optional<std::reference_wrapper<const ShadowNode::Shared>> findHeaderConfigChild(const RNSScreenShadowNode &screenShadowNode) const {
for (const ShadowNode::Shared &child : screenShadowNode.getChildren()) {
if (std::strcmp(child->getComponentName(), "RNSScreenStackHeaderConfig") == 0) {
return {std::ref(child)};
}
}
return std::nullopt;
// return std::optional<std::reference_wrapper<const ShadowNode::Shared>>();
}

void adopt(ShadowNode &shadowNode) const override {
react_native_assert(dynamic_cast<RNSScreenShadowNode *>(&shadowNode));
auto &screenShadowNode = static_cast<RNSScreenShadowNode &>(shadowNode);

Expand Down Expand Up @@ -50,8 +62,16 @@ class RNSScreenComponentDescriptor final
layoutableShadowNode.setPadding({.bottom = 0});
layoutableShadowNode.setSize(
Size{stateData.frameSize.width, stateData.frameSize.height});

} else {
auto headerConfigChildOpt = findHeaderConfigChild(screenShadowNode);
int fontSize = -1;
if (headerConfigChildOpt) {
auto headerConfigChild = headerConfigChildOpt->get();
const auto &headerProps = *std::static_pointer_cast<const RNSScreenStackHeaderConfigProps>(headerConfigChild->getProps());
fontSize = headerProps.titleFontSize;
// headerConfigChild->getProps()
}

JNIEnv *env = facebook::jni::Environment::current();
if (env == nullptr) {
// We can basically crash here
Expand Down Expand Up @@ -79,7 +99,8 @@ class RNSScreenComponentDescriptor final
}

jfloat headerHeight =
env->CallFloatMethod(packageInstance, computeDummyLayoutID, 24);
env->CallFloatMethod(packageInstance, computeDummyLayoutID, fontSize);


layoutableShadowNode.setPadding(
{.bottom = static_cast<float>(headerHeight)});
Expand Down