Skip to content

Commit c8a08ce

Browse files
authored
fix: OverKeyboardView rotation (fabric) (#918)
## 📜 Description Fixed wrong dimensions when phone gets rotated. ## 💡 Motivation and Context We need to update shadow node layout whenever screen dimensions changes, so in this PR I'm doing this. I use `onSizeChanged` method as a lifecycle method for updating layout. Technically I could use `onLayout`, but it's highly undesirable to block `onLayout` method, so it's better to do it in `onSizeChanged`. Fixes Android issue described in #915 ## 📢 Changelog <!-- High level overview of important changes --> <!-- For example: fixed status bar manipulation; added new types declarations; --> <!-- If your changes don't affect one of platform/language below - then remove this platform/language --> ### Android - moved state wrapper to `hostView`; - manage `ShadowNode` layout inside `hostView`. ## 🤔 How Has This Been Tested? Tested manually on Medium Phone API 35. ## 📸 Screenshots (if appropriate): [okv-rotation.webm](https://github.com/user-attachments/assets/6b49d0b3-e266-47e7-89af-1a8a3b4de6ce) ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
1 parent 0071eb0 commit c8a08ce

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

android/src/main/java/com/reactnativekeyboardcontroller/views/overlay/OverKeyboardViewGroup.kt

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ class OverKeyboardHostView(
3030
private var windowManager: WindowManager = reactContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
3131
private var hostView: OverKeyboardRootViewGroup = OverKeyboardRootViewGroup(reactContext)
3232

33-
internal var stateWrapper: StateWrapper? = null
33+
var stateWrapper: StateWrapper?
34+
get() = hostView.stateWrapper
35+
set(stateWrapper) {
36+
hostView.stateWrapper = stateWrapper
37+
}
3438

3539
init {
3640
hostView.eventDispatcher = dispatcher
@@ -92,24 +96,14 @@ class OverKeyboardHostView(
9296
PixelFormat.TRANSLUCENT,
9397
)
9498

95-
stretchTo(fullScreen = true)
9699
windowManager.addView(hostView, layoutParams)
97100
}
98101

99102
fun hide() {
100103
if (hostView.isAttached) {
101104
windowManager.removeView(hostView)
102-
stretchTo(fullScreen = false)
103105
}
104106
}
105-
106-
private fun stretchTo(fullScreen: Boolean) {
107-
val displaySize = reactContext.getDisplaySize()
108-
val newStateData: WritableMap = WritableNativeMap()
109-
newStateData.putDouble("screenWidth", if (fullScreen) displaySize.x.toFloat().dp else 0.0)
110-
newStateData.putDouble("screenHeight", if (fullScreen) displaySize.y.toFloat().dp else 0.0)
111-
stateWrapper?.updateState(newStateData)
112-
}
113107
}
114108

115109
@SuppressLint("ViewConstructor")
@@ -120,6 +114,7 @@ class OverKeyboardRootViewGroup(
120114
private val jsTouchDispatcher: JSTouchDispatcher = JSTouchDispatcher(this)
121115
private var jsPointerDispatcher: JSPointerDispatcherCompat? = null
122116
internal var eventDispatcher: EventDispatcher? = null
117+
internal var stateWrapper: StateWrapper? = null
123118
internal var isAttached = false
124119

125120
init {
@@ -131,13 +126,27 @@ class OverKeyboardRootViewGroup(
131126
// region life cycles
132127
override fun onAttachedToWindow() {
133128
super.onAttachedToWindow()
129+
val displaySize = reactContext.getDisplaySize()
130+
stretchTo(width = displaySize.x, height = displaySize.y)
134131
isAttached = true
135132
}
136133

137134
override fun onDetachedFromWindow() {
138135
super.onDetachedFromWindow()
136+
stretchTo(width = 0, height = 0)
139137
isAttached = false
140138
}
139+
140+
override fun onSizeChanged(
141+
width: Int,
142+
height: Int,
143+
oldWidth: Int,
144+
oldHeight: Int,
145+
) {
146+
super.onSizeChanged(width, height, oldWidth, oldHeight)
147+
stretchTo(width, height)
148+
}
149+
141150
// endregion
142151

143152
// region Touch events handling
@@ -216,4 +225,14 @@ class OverKeyboardRootViewGroup(
216225
reactContext.reactApplicationContext.handleException(RuntimeException(t))
217226
}
218227
// endregion
228+
229+
private fun stretchTo(
230+
width: Int,
231+
height: Int,
232+
) {
233+
val newStateData: WritableMap = WritableNativeMap()
234+
newStateData.putDouble("screenWidth", width.toFloat().dp)
235+
newStateData.putDouble("screenHeight", height.toFloat().dp)
236+
stateWrapper?.updateState(newStateData)
237+
}
219238
}

0 commit comments

Comments
 (0)