extra_dimensions:基于 React Native 生态的 HarmonyOS 屏幕尺寸工具库项目

【RN三方库】react-native-extra-dimensions-android鸿蒙化适配

Branch2Tags0
FilesLast commitLast update
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
9 months ago
10 months ago
10 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
9 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago

@react-native-oh-tpl/react-native-extra-dimensions

[Github Address](https://github.com/Sunhat/react-native-extra-dimensions-android)

Installation and Usage

Please check the Releases page of the third-party library for compatible version information:

Library Version Release Information Supported RN Versions
1.0.0 ExtraDimensions Releases 0.72&0.77

Enter the project directory and enter the following command:

npm

npm install @react-native-oh-tpl/react-native-extra-dimensions

yarn

yarn add @react-native-oh-tpl/react-native-extra-dimensions

The following code demonstrates the basic usage scenarios of this library:

The import library name remains unchanged when using.

import React, { useState, useEffect } from 'react';
import {
  SafeAreaView,
  ScrollView,
  StatusBar,
  StyleSheet,
  Text,
  View,
} from 'react-native';
import ExtraDimensions from 'react-native-extra-dimensions';

function App(): JSX.Element {
  const realWindowHeight = ExtraDimensions.getRealWindowHeight();
  const realWindowWidth = ExtraDimensions.getRealWindowWidth();
  const statusBarHeight = ExtraDimensions.getStatusBarHeight();
  const softMenuBarHeight = ExtraDimensions.getSoftMenuBarHeight();
  const softMenuBarEnabled = !!ExtraDimensions.isSoftMenuBarEnabled();

  return (
    <SafeAreaView style={styles.container}>
      <StatusBar barStyle="dark-content" backgroundColor="#f5f5f5" />
      <ScrollView contentContainerStyle={styles.scrollContent}>
        <View style={styles.header}>
          <Text style={styles.title}>ExtraDimensions Demo</Text>
          <Text style={styles.subtitle}>React Native for HarmonyOS</Text>
        </View>

        <View style={styles.section}>
          <Text style={styles.sectionTitle}>Screen Dimensions</Text>
          <InfoRow label="REAL_WINDOW_HEIGHT(Height)" value={`${realWindowHeight.toFixed(2)} vp`} />
          <InfoRow label="REAL_WINDOW_WIDTH(Width)" value={`${realWindowWidth.toFixed(2)} vp`} />
        </View>

        <View style={styles.section}>
          <Text style={styles.sectionTitle}>System Bar Heights</Text>
          <InfoRow label="STATUS_BAR_HEIGHT(Status Bar)" value={`${statusBarHeight.toFixed(2)} vp`} />
          <InfoRow label="SOFT_MENU_BAR_HEIGHT(Navigation Bar)" value={`${softMenuBarHeight.toFixed(2)} vp`} />
        </View>

        <View style={styles.section}>
          <Text style={styles.sectionTitle}>Navigation Bar Status</Text>
          <InfoRow 
            label="SOFT_MENU_BAR_ENABLED" 
            value={softMenuBarEnabled ? 'Enabled' : 'Disabled'} 
            highlight={softMenuBarEnabled} 
          />
        </View>

      </ScrollView>
    </SafeAreaView>
  );
}

interface InfoRowProps {
  label: string;
  value: string;
  highlight?: boolean;
}

function InfoRow({ label, value, highlight }: InfoRowProps): JSX.Element {
  return (
    <View style={styles.infoRow}>
      <Text style={styles.label}>{label}</Text>
      <Text style={[styles.value, highlight && styles.highlightValue]}>{value}</Text>
    </View>
  );
}

interface CodeBlockProps {
  code: string;
}

function CodeBlock({ code }: CodeBlockProps): JSX.Element {
  return (
    <View style={styles.codeBlock}>
      <Text style={styles.code}>{code}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#f5f5f5',
  },
  scrollContent: {
    padding: 16,
  },
  header: {
    marginBottom: 24,
    paddingVertical: 16,
    backgroundColor: '#fff',
    borderRadius: 12,
    paddingHorizontal: 20,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.1,
    shadowRadius: 4,
    elevation: 3,
  },
  title: {
    fontSize: 28,
    fontWeight: 'bold',
    color: '#333',
  },
  subtitle: {
    fontSize: 14,
    color: '#666',
    marginTop: 4,
  },
  section: {
    backgroundColor: '#fff',
    borderRadius: 12,
    padding: 16,
    marginBottom: 16,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 1 },
    shadowOpacity: 0.05,
    shadowRadius: 2,
    elevation: 2,
  },
  sectionTitle: {
    fontSize: 18,
    fontWeight: '600',
    color: '#333',
    marginBottom: 12,
  },
  infoRow: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    paddingVertical: 8,
    borderBottomWidth: 1,
    borderBottomColor: '#f0f0f0',
  },
  label: {
    fontSize: 14,
    color: '#666',
    flex: 1,
  },
  value: {
    fontSize: 16,
    fontWeight: '500',
    color: '#333',
  },
  highlightValue: {
    color: '#007AFF',
    fontWeight: '600',
  },
  codeBlock: {
    backgroundColor: '#f8f8f8',
    borderRadius: 8,
    padding: 12,
    marginTop: 8,
    borderLeftWidth: 3,
    borderLeftColor: '#007AFF',
  },
  code: {
    fontFamily: 'monospace',
    fontSize: 13,
    color: '#333',
    lineHeight: 20,
  },
});

export default App;
Auto-link Support RN Framework Version
0.0.1 Yes 0.72

Projects using AutoLink need to be configured according to this document. Autolink framework guide: https://gitcode.com/openharmony-sig/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md

If your version supports Autolink and the project has already integrated Autolink, you can skip the ManualLink configuration.

ManualLink: This step is a guide for manually configuring native dependencies

First, you need to open the HarmonyOS project harmony in the project using DevEco Studio.

1. Add the overrides field to the project root directory's oh-package.json5

{
  ...
  "overrides": {
    	"@rnoh/react-native-openharmony" : "./react_native_openharmony"
  }
}

2. Import native code

There are currently two methods:

  1. Import via har package (this method will be deprecated when the IDE improves related functions, currently this is the preferred method);
  2. Directly link source code.

Method 1: Import via har package

The har package is located in the `harmony` folder of the third-party library installation path.

Open entry/oh-package.json5 and add the following dependencies:

"dependencies": {
    "@rnoh/react-native-openharmony": "file:../react_native_openharmony",
    "extra-dimensions": "file:../../node_modules/ohosextradimensions/harmony/extra-dimensions.har"
  }

Click the sync button in the upper right corner

Or execute in the terminal:

cd entry
ohpm install

Method 2: Directly link source code

To use directly linking source code, please refer to [Direct Source Code Linking Guide](/zh-cn/link-source-code.md)

3. Configure CMakeLists and import ExtraDimensionsPackage

Open entry/src/main/cpp/CMakeLists.txt and add:

project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
+ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
set(LOG_VERBOSITY_LEVEL 1)
set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
set(WITH_HITRACE_SYSTRACE 1) # for other CMakeLists.txt files to use
add_compile_definitions(WITH_HITRACE_SYSTRACE)

add_subdirectory("${RNOH_CPP_DIR}" ./rn)

# RNOH_BEGIN: manual_package_linking_1
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULES}/@react-native-oh-tpl/react-native-extra-dimensions/src/main/ets/cpp" ./extra_dimensions)
# RNOH_END: manual_package_linking_1

file(GLOB GENERATED_CPP_FILES "./generated/*.cpp")

add_library(rnoh_app SHARED
    ${GENERATED_CPP_FILES}
    "./PackageProvider.cpp"
    "${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
)
target_link_libraries(rnoh_app PUBLIC rnoh)

# RNOH_BEGIN: manual_package_linking_2
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_extra_dimensions)
# RNOH_END: link_packages

Open entry/src/main/cpp/PackageProvider.cpp and add:

#include "RNOH/PackageProvider.h"
#include "generated/RNOHGeneratedPackage.h"
#include "SamplePackage.h"
+ #include "RNOHExtraDimensionsPackage.h"

using namespace rnoh;

std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
    return {
        std::make_shared<RNOHGeneratedPackage>(ctx),
        std::make_shared<SamplePackage>(ctx),
+       std::make_shared<RNOHExtraDimensionsPackage>(ctx),
    };
}

4. Import ExtraDimensionsPackage on the ArkTs side

Open entry/src/main/ets/RNPackagesFactory.ts and add:

  ...
+ import { ExtraDimensionsPackage } from '@react-native-oh-tpl/react-native-extra-dimensions/ts';

export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
  return [
    new SamplePackage(ctx),
+   new ExtraDimensionsPackage(ctx),
  ];
}

Run

Click the sync button in the upper right corner

Or execute in the terminal:

cd entry
ohpm install

Then compile and run.

Constraints and Limitations

Compatibility

To use this library, you need to use the correct React-Native and RNOH versions. In addition, you also need to use the matching DevEco Studio and phone ROM.

Validated on the following versions:

  1. RNOH: 0.72.96; SDK: HarmonyOS 6.0.0.47; IDE: DevEco Studio 6.0.0.858 ROM: 6.0.0.112;
  2. RNOH: 0.72.28; SDK: HarmonyOS 6.0.0.47; IDE: DevEco Studio 6.0.0.858 ROM: 6.0.0.112;
  3. RNOH: 0.77.18; SDK: HarmonyOS 6.0.0.47; IDE: DevEco Studio 6.0.0.858 ROM: 6.0.0.112;

Features

The following are the currently supported component properties:

The "Platform" column indicates the platforms supported by the property in the original third-party library.

The "HarmonyOS Support" column: yes means the HarmonyOS platform supports this property; no means it is not supported; partially means partial support. The usage method is cross-platform consistent, and the effect corresponds to iOS or Android.

API List

Name Description Type Required Platform HarmonyOS Support
getRealWindowHeight Get real window height number No Android Yes
getRealWindowWidth Get real window width number No Android Yes
getStatusBarHeight Get status bar height number No Android Yes
getSoftMenuBarHeight Get soft navigation bar height number No Android Yes
isSoftMenuBarEnabled Check if soft navigation bar is enabled boolean No Android Yes

Known Issues

Others

License

This project is based on The MIT License (MIT). Feel free to enjoy and contribute to the open source.

Introduction

【RN三方库】react-native-extra-dimensions-android鸿蒙化适配

Customize your domain