Repository navigation
fix(android): resize the render surface when the view size changes #419
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
Open
mfazekas
wants to merge
4
commits into
main
Choose a base branch
from
claude/fix-418-surface-resize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d03aabc
fix(android): resize the render surface when the view size changes
mfazekas 6ca6447
fix(android): apply layout resizes on the next frame and only when th…
mfazekas 44d8eaf
fix(android): drop a pending layout advance when the view reloads
mfazekas 9f3ac21
fix(android): drop a pending layout advance when leaving Fit.Layout
mfazekas 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.rive | ||
|
|
||
| import android.util.Log | ||
| import app.rive.core.RiveSurface | ||
| import java.lang.reflect.InvocationTargetException | ||
|
|
||
| /** | ||
| * Upstream `RiveView` resizes its surface on every size change through the | ||
| * Kotlin-internal `RiveSurface.resize`, so reflection reaches it under its | ||
| * mangled JVM name (rive-app/rive-nitro-react-native#418). | ||
| */ | ||
| internal object RiveSurfaceResize { | ||
| private const val TAG = "RiveSurfaceResize" | ||
|
|
||
| private val resizeMethod by lazy { | ||
| val signature = arrayOf(Int::class.javaPrimitiveType, Int::class.javaPrimitiveType) | ||
| val method = RiveSurface::class.java.declaredMethods.firstOrNull { | ||
| (it.name == "resize" || it.name.startsWith("resize$")) && | ||
| it.parameterTypes.contentEquals(signature) | ||
| } | ||
| if (method == null) Log.w(TAG, "RiveSurface.resize not found") | ||
| method | ||
| } | ||
|
|
||
| /** Returns false when the surface keeps its previous size. */ | ||
| fun resize(surface: RiveSurface, width: Int, height: Int): Boolean { | ||
| if (width <= 0 || height <= 0) return false | ||
| val method = resizeMethod ?: return false | ||
| return try { | ||
| method.invoke(surface, width, height) | ||
| true | ||
| } catch (e: InvocationTargetException) { | ||
| Log.w(TAG, "RiveSurface.resize(${width}x$height) failed", e.targetException) | ||
| false | ||
| } catch (e: ReflectiveOperationException) { | ||
| Log.w(TAG, "RiveSurface.resize(${width}x$height) failed", e) | ||
| false | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { useCallback, useRef, useState } from 'react'; | ||
| import { Animated, Pressable, Text, View, StyleSheet } from 'react-native'; | ||
| import { Fit, RiveView, useRiveFile } from '@rive-app/react-native'; | ||
| import { type Metadata } from '../shared/metadata'; | ||
|
|
||
| /** | ||
| * Issue #418: on the Android new backend, resizing a mounted RiveView kept | ||
| * rendering into a surface sized at creation time, so Fit.Layout content | ||
| * looked scaled and offset until remount. | ||
| * | ||
| * Toggle the height, then Remount at the same height: both should look | ||
| * identical. | ||
| */ | ||
|
|
||
| const SHORT = 200; | ||
| const TALL = 400; | ||
|
|
||
| export default function Issue418SurfaceResize() { | ||
| const { riveFile, error } = useRiveFile( | ||
| require('../../assets/rive/layout_test.riv') | ||
| ); | ||
| const [height] = useState(() => new Animated.Value(SHORT)); | ||
| const tall = useRef(false); | ||
| const [isTall, setIsTall] = useState(false); | ||
| const [generation, setGeneration] = useState(0); | ||
|
|
||
| const toggle = useCallback(() => { | ||
| tall.current = !tall.current; | ||
| setIsTall(tall.current); | ||
| Animated.timing(height, { | ||
| toValue: tall.current ? TALL : SHORT, | ||
| duration: 400, | ||
| useNativeDriver: false, | ||
| }).start(); | ||
| }, [height]); | ||
|
|
||
| return ( | ||
| <View style={styles.container}> | ||
| <Text style={styles.subtitle}> | ||
| target {isTall ? TALL : SHORT}pt · generation {generation} | ||
| </Text> | ||
| {error ? <Text style={styles.error}>{String(error)}</Text> : null} | ||
| <View style={styles.buttons}> | ||
| <Pressable style={styles.button} onPress={toggle}> | ||
| <Text style={styles.buttonText}>Toggle height</Text> | ||
| </Pressable> | ||
| <Pressable | ||
| style={styles.button} | ||
| onPress={() => setGeneration((g) => g + 1)} | ||
| > | ||
| <Text style={styles.buttonText}>Remount</Text> | ||
| </Pressable> | ||
| </View> | ||
| <Animated.View style={[styles.box, { height }]}> | ||
| {riveFile ? ( | ||
| <RiveView | ||
| key={generation} | ||
| file={riveFile} | ||
| fit={Fit.Layout} | ||
| autoPlay | ||
| style={styles.rive} | ||
| /> | ||
| ) : null} | ||
| </Animated.View> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| Issue418SurfaceResize.metadata = { | ||
| name: 'Issue #418: Surface resize', | ||
| description: | ||
| 'Android Fit.Layout content stale after the view resizes until remount', | ||
| } satisfies Metadata; | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { flex: 1, padding: 16, backgroundColor: '#fff' }, | ||
| subtitle: { fontSize: 14, color: '#666' }, | ||
| error: { color: 'red', marginTop: 8 }, | ||
| buttons: { flexDirection: 'row', gap: 8 }, | ||
| box: { marginTop: 16, backgroundColor: '#1a2a3a' }, | ||
| rive: { flex: 1 }, | ||
| button: { | ||
| backgroundColor: '#007AFF', | ||
| paddingHorizontal: 20, | ||
| paddingVertical: 10, | ||
| borderRadius: 8, | ||
| marginTop: 8, | ||
| }, | ||
| buttonText: { color: '#fff', fontWeight: '600' }, | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.