This repository was archived by the owner on Feb 11, 2026. It is now read-only.
forked from react/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactScrollViewCommandHelper.java
More file actions
executable file
·98 lines (84 loc) · 3 KB
/
Copy pathReactScrollViewCommandHelper.java
File metadata and controls
executable file
·98 lines (84 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.views.scroll;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.PixelUtil;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Helper for view managers to handle commands like 'scrollTo'.
* Shared by {@link ReactScrollViewManager} and {@link ReactHorizontalScrollViewManager}.
*/
public class ReactScrollViewCommandHelper {
public static final int COMMAND_SCROLL_TO = 1;
public static final int COMMAND_SCROLL_TO_END = 2;
public static final int COMMAND_FLASH_SCROLL_INDICATORS = 3;
public interface ScrollCommandHandler<T> {
void scrollTo(T scrollView, ScrollToCommandData data);
void scrollToEnd(T scrollView, ScrollToEndCommandData data);
void flashScrollIndicators(T scrollView);
}
public static class ScrollToCommandData {
public final int mDestX, mDestY;
public final boolean mAnimated;
ScrollToCommandData(int destX, int destY, boolean animated) {
mDestX = destX;
mDestY = destY;
mAnimated = animated;
}
}
public static class ScrollToEndCommandData {
public final boolean mAnimated;
ScrollToEndCommandData(boolean animated) {
mAnimated = animated;
}
}
public static Map<String,Integer> getCommandsMap() {
return MapBuilder.of(
"scrollTo",
COMMAND_SCROLL_TO,
"scrollToEnd",
COMMAND_SCROLL_TO_END,
"flashScrollIndicators",
COMMAND_FLASH_SCROLL_INDICATORS);
}
public static <T> void receiveCommand(
ScrollCommandHandler<T> viewManager,
T scrollView,
int commandType,
@Nullable ReadableArray args) {
Assertions.assertNotNull(viewManager);
Assertions.assertNotNull(scrollView);
Assertions.assertNotNull(args);
switch (commandType) {
case COMMAND_SCROLL_TO: {
int destX = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(0)));
int destY = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(1)));
boolean animated = args.getBoolean(2);
viewManager.scrollTo(scrollView, new ScrollToCommandData(destX, destY, animated));
return;
}
case COMMAND_SCROLL_TO_END: {
boolean animated = args.getBoolean(0);
viewManager.scrollToEnd(scrollView, new ScrollToEndCommandData(animated));
return;
}
case COMMAND_FLASH_SCROLL_INDICATORS:
viewManager.flashScrollIndicators(scrollView);
return;
default:
throw new IllegalArgumentException(String.format(
"Unsupported command %d received by %s.",
commandType,
viewManager.getClass().getSimpleName()));
}
}
}