From f3aa3e42bdce3baa3f55adb65cd3dbb1bc8e2a6e Mon Sep 17 00:00:00 2001 From: Douglas Nassif Roma Junior Date: Thu, 9 Mar 2023 17:51:28 -0300 Subject: [PATCH] Add Android double tap zoom --- README.md | 22 ++++--- Sample/App.tsx | 2 +- Sample/ios/Podfile.lock | 2 +- .../modules/PdfRendererRecyclerView.java | 61 +++++++++++++------ .../modules/PdfRendererViewManager.java | 2 - package.json | 4 +- src/PdfRendererView.tsx | 19 +++++- 7 files changed, 74 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 16c1e2f..8307b82 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![npm version](https://img.shields.io/npm/v/react-native-pdf-renderer.svg)](https://www.npmjs.com/package/react-native-pdf-renderer) [![npm downloads](https://img.shields.io/npm/dt/react-native-pdf-renderer.svg)](#install-with-react-native-link) -⚛ A blazing fast, zero dependencies, pure native PDF Renderer for Android and iOS. +⚛ A blazing fast, zero dependencies, pure native, typed PDF Renderer for Android and iOS. |Android|iOS| |-|-| @@ -38,15 +38,17 @@ import PdfRendererView from 'react-native-pdf-renderer'; const App = () => { return ( - { - console.log(current, total); - }} - /> + + { + console.log(current, total); + }} + /> + ); } diff --git a/Sample/App.tsx b/Sample/App.tsx index 92e04a3..f5917b9 100644 --- a/Sample/App.tsx +++ b/Sample/App.tsx @@ -48,7 +48,7 @@ function App(): JSX.Element { return ( <> = mMaxZoom && factor > 1) { + return false; + } + mMatrix.postScale(factor, factor, getWidth() / 2f, getHeight() / 2f); validateMatrixLimits(); ViewCompat.postInvalidateOnAnimation(PdfRendererRecyclerView.this); @@ -259,6 +259,29 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float dX, float dY) { ViewCompat.postInvalidateOnAnimation(PdfRendererRecyclerView.this); return true; } + + @Override + public boolean onDoubleTap(@NonNull MotionEvent e) { + if (e.getPointerCount() > 1) { + return false; + } + + float[] values = new float[9]; + mMatrix.getValues(values); + + float currentZoom = values[Matrix.MSCALE_X]; + float newZoom = currentZoom > mMinZoom ? mMinZoom : mMaxZoom; + float centerX = getWidth() / 2f; + float centerY = getHeight() / 2f; + + mMatrix.setScale(newZoom, newZoom, centerX, centerY); + mMatrix.postTranslate(centerX - e.getX(), centerY - e.getY()); + + validateMatrixLimits(); + + ViewCompat.postInvalidateOnAnimation(PdfRendererRecyclerView.this); + return true; + } } class PdfRendererAdapter extends Adapter { diff --git a/android/src/main/java/com/github/douglasjunior/reactNativePdfRenderer/modules/PdfRendererViewManager.java b/android/src/main/java/com/github/douglasjunior/reactNativePdfRenderer/modules/PdfRendererViewManager.java index 4ea3e40..1f339fe 100644 --- a/android/src/main/java/com/github/douglasjunior/reactNativePdfRenderer/modules/PdfRendererViewManager.java +++ b/android/src/main/java/com/github/douglasjunior/reactNativePdfRenderer/modules/PdfRendererViewManager.java @@ -22,7 +22,6 @@ package com.github.douglasjunior.reactNativePdfRenderer.modules; -import android.util.Log; import android.view.ViewGroup; import android.widget.LinearLayout; @@ -83,7 +82,6 @@ public Map getExportedCustomBubblingEventTypeConstants() { @ReactProp(name = "source") public void setSource(ViewGroup layout, @Nullable String source) throws IOException { - Log.d("PdfRendererViewManager", "setSource"); PdfRendererRecyclerView recyclerView = (PdfRendererRecyclerView) layout.getChildAt(0); if (source != null) { diff --git a/package.json b/package.json index 716fcf9..a7a873b 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "react-native-pdf-renderer", "displayName": "React-Native Pdf Renderer", - "version": "0.0.5", - "description": "⚛ A blazing fast, zero dependencies, pure native PDF Renderer for Android and iOS.", + "version": "1.0.0", + "description": "⚛ A blazing fast, zero dependencies, pure native, typed PDF Renderer for Android and iOS.", "main": "src", "license": "MIT", "private": false, diff --git a/src/PdfRendererView.tsx b/src/PdfRendererView.tsx index 6581858..d00b62d 100644 --- a/src/PdfRendererView.tsx +++ b/src/PdfRendererView.tsx @@ -20,10 +20,11 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import React, {useCallback} from 'react'; +import React, {useCallback, useMemo} from 'react'; import { NativeSyntheticEvent, requireNativeComponent, + StyleSheet, ViewProps, } from 'react-native'; @@ -41,8 +42,20 @@ type OnPageChangeEventType = { const PdfRendererNative = requireNativeComponent('RNPdfRendererView') as any; +const styles = StyleSheet.create({ + default: { + backgroundColor: 'gray', + flex: 1, + } +}); + const PdfRendererView = (props: PdfRendererViewPropsType): JSX.Element => { - const {onPageChange, ...others} = props; + const {onPageChange, style, ...others} = props; + + const viewStyles = useMemo(() => [ + styles.default, + style, + ], [style]); const handlePageChange = useCallback( (event: NativeSyntheticEvent) => { @@ -51,7 +64,7 @@ const PdfRendererView = (props: PdfRendererViewPropsType): JSX.Element => { [onPageChange], ); - return ; + return ; }; PdfRendererView.defaultProps = {