Skip to content

Commit 14d8f66

Browse files
committed
feat: add layoutScaleFactor
1 parent a3eecba commit 14d8f66

File tree

6 files changed

+19
-23
lines changed

6 files changed

+19
-23
lines changed

android/src/main/java/com/margelo/nitro/rive/HybridRiveView.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ object DefaultConfiguration {
1616
const val AUTOPLAY = true
1717
val FIT = RiveFit.CONTAIN
1818
val ALIGNMENT = RiveAlignment.CENTER
19+
val LAYOUTSCALEFACTOR = null
1920
}
2021

2122
@Keep
@@ -49,6 +50,7 @@ class HybridRiveView(val context: ThemedReactContext) : HybridRiveViewSpec() {
4950
}
5051
override var alignment: Alignment? = null
5152
override var fit: Fit? = null
53+
override var layoutScaleFactor: Double? = null
5254
//endregion
5355

5456
//region View Methods
@@ -108,6 +110,7 @@ class HybridRiveView(val context: ThemedReactContext) : HybridRiveViewSpec() {
108110
riveFile = riveFile,
109111
alignment = convertAlignment(alignment) ?: DefaultConfiguration.ALIGNMENT,
110112
fit = convertFit(fit) ?: DefaultConfiguration.FIT,
113+
layoutScaleFactor = layoutScaleFactor?.toFloat() ?: DefaultConfiguration.LAYOUTSCALEFACTOR,
111114
)
112115
view.configure(config, needsReload)
113116
needsReload = false

android/src/main/java/com/rive/RiveReactNativeView.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import app.rive.runtime.kotlin.core.File
1010
import app.rive.runtime.kotlin.core.Fit
1111
import app.rive.runtime.kotlin.core.RiveEvent
1212
import app.rive.runtime.kotlin.core.RiveOpenURLEvent
13-
import app.rive.runtime.kotlin.core.RiveTextValueRun
1413
import app.rive.runtime.kotlin.core.SMIBoolean
1514
import app.rive.runtime.kotlin.core.SMIInput
1615
import app.rive.runtime.kotlin.core.SMINumber
@@ -27,7 +26,8 @@ data class ViewConfiguration(
2726
val autoPlay: Boolean,
2827
val riveFile: File,
2928
val alignment: Alignment,
30-
val fit: Fit
29+
val fit: Fit,
30+
val layoutScaleFactor: Float?
3131
)
3232

3333
@SuppressLint("ViewConstructor")
@@ -62,6 +62,8 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
6262
} else {
6363
riveAnimationView?.alignment = config.alignment
6464
riveAnimationView?.fit = config.fit
65+
// TODO: this seems to require a reload for the view to take the new value (bug on Android)
66+
riveAnimationView?.layoutScaleFactor = config.layoutScaleFactor
6567
}
6668
viewReadyDeferred.complete(true)
6769
}
@@ -243,7 +245,7 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
243245
}
244246
}
245247

246-
248+
// TODO: this is throwing when autoplay is false
247249
// TODO: This is a temporary solution to get the state machine name as Android supports
248250
// playing multiple state machines, but in React Native we only allow playing one.
249251
/**

example/src/pages/RiveDataBindingExample.tsx

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ export default function DataBindingExample() {
136136
style={styles.rive}
137137
autoBind={false}
138138
autoPlay={true}
139-
fit={Fit.Contain}
139+
fit={Fit.Layout}
140+
layoutScaleFactor={1}
140141
file={riveFile!}
141142
hybridRef={setHybridRef}
142143
/>
@@ -150,30 +151,13 @@ const styles = StyleSheet.create({
150151
container: {
151152
flex: 1,
152153
backgroundColor: '#fff',
153-
padding: 20,
154-
},
155-
title: {
156-
fontSize: 24,
157-
fontWeight: 'bold',
158-
textAlign: 'center',
159-
marginTop: 20,
160-
marginBottom: 10,
161-
},
162-
subtitle: {
163-
fontSize: 16,
164-
color: '#666',
165-
textAlign: 'center',
166-
marginBottom: 20,
167154
},
168155
riveContainer: {
169156
flex: 1,
170-
justifyContent: 'center',
171-
alignItems: 'center',
172157
backgroundColor: '#f5f5f5',
173-
borderRadius: 10,
174-
overflow: 'hidden',
175158
},
176159
rive: {
160+
flex: 1,
177161
width: '100%',
178162
height: '100%',
179163
},

ios/HybridRiveView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ private struct DefaultConfiguration {
88
static let autoPlay = true
99
static let alignment = RiveAlignment.center
1010
static let fit = RiveFit.contain
11+
static let layoutScaleFactor = RiveRuntime.RiveViewModel.layoutScaleFactorAutomatic
1112
}
1213

1314
class HybridRiveView : HybridRiveViewSpec {
@@ -21,6 +22,7 @@ class HybridRiveView : HybridRiveViewSpec {
2122
}
2223
var alignment: Alignment?
2324
var fit: Fit?
25+
var layoutScaleFactor: Double?
2426

2527
func awaitViewReady() throws -> Promise<Bool> {
2628
return Promise.async { [self] in
@@ -90,7 +92,8 @@ class HybridRiveView : HybridRiveViewSpec {
9092
autoPlay: autoPlay ?? DefaultConfiguration.autoPlay,
9193
riveFile: file,
9294
alignment: convertAlignment(alignment) ?? DefaultConfiguration.alignment,
93-
fit: convertFit(fit) ?? DefaultConfiguration.fit
95+
fit: convertFit(fit) ?? DefaultConfiguration.fit,
96+
layoutScaleFactor: layoutScaleFactor ?? DefaultConfiguration.layoutScaleFactor
9497
)
9598

9699
try? getRiveView().configure(config, reload: needsReload)

ios/RiveReactNativeView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct ViewConfiguration {
1010
let riveFile: RiveFile
1111
let alignment: RiveRuntime.RiveAlignment
1212
let fit: RiveRuntime.RiveFit
13+
let layoutScaleFactor: Double
1314
}
1415

1516
class RiveReactNativeView: UIView, RiveStateMachineDelegate {
@@ -46,6 +47,7 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
4647
baseViewModel?.alignment = config.alignment
4748
baseViewModel?.fit = config.fit
4849
baseViewModel?.autoPlay = config.autoPlay
50+
baseViewModel?.layoutScaleFactor = config.layoutScaleFactor
4951

5052
if !isViewReady {
5153
isViewReady = true

src/specs/RiveView.nitro.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export interface RiveViewProps extends HybridViewProps {
2828
alignment?: Alignment;
2929
/** How the Rive graphic should fit within its container */
3030
fit?: Fit;
31+
/** The scale factor to apply to the Rive graphic when using Fit.Layout */
32+
layoutScaleFactor?: number;
3133
}
3234

3335
/**

0 commit comments

Comments
 (0)