From 747a30415f3c0ba17ff220f7d687d4c40e039171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E5=A4=A7=E4=BE=A0?= <794397348@qq.com> Date: Thu, 22 May 2025 18:20:48 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=B8=BF=E8=92=99=E4=B8=96=E7=95=8C?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/common/BreakpointType.ets | 39 ------ webprerenderlibrary/BuildProfile.ets | 17 +++ webprerenderlibrary/Index.ets | 1 + webprerenderlibrary/build-profile.json5 | 31 +++++ webprerenderlibrary/consumer-rules.txt | 0 webprerenderlibrary/hvigorfile.ts | 6 + webprerenderlibrary/obfuscation-rules.txt | 23 ++++ webprerenderlibrary/oh-package.json5 | 9 ++ .../src/main/ets/common/Constants.ets | 0 .../src/main/ets/common/ResourceUtil.ets | 0 .../ets/common/utils/BreakpointSystem.ets | 118 ++++++++++++++++++ .../src/main/ets/common/utils/WindowUtil.ets | 76 +++++++++++ .../src/main/ets/pages/WebPage.ets | 4 +- .../src/main/ets/pages/WebPreRenderPage.ets | 99 +++++++++++++++ webprerenderlibrary/src/main/module.json5 | 11 ++ .../main/resources/base/element/color.json | 0 .../main/resources/base/element/float.json | 0 .../main/resources/base/element/string.json | 0 .../main/resources/base/media/background.png | Bin .../main/resources/base/media/foreground.png | Bin .../resources/base/media/layered_image.json | 0 .../main/resources/base/media/startIcon.png | Bin .../main/resources/en_US/element/string.json | 0 .../src/main/resources/rawfile/config.json | 0 .../main/resources/zh_CN/element/string.json | 0 .../build-profile.json5 | 0 {entry => webprerendersample}/hvigorfile.ts | 0 .../obfuscation-rules.txt | 0 webprerendersample/oh-package-lock.json5 | 18 +++ .../oh-package.json5 | 5 +- .../WebprerendersampleAbility.ets | 2 +- .../src/main/ets/pages/Index.ets | 0 .../src/main/module.json5 | 4 +- .../resources/base/profile/main_pages.json | 0 34 files changed, 416 insertions(+), 47 deletions(-) delete mode 100644 entry/src/main/ets/common/BreakpointType.ets create mode 100644 webprerenderlibrary/BuildProfile.ets create mode 100644 webprerenderlibrary/Index.ets create mode 100644 webprerenderlibrary/build-profile.json5 create mode 100644 webprerenderlibrary/consumer-rules.txt create mode 100644 webprerenderlibrary/hvigorfile.ts create mode 100644 webprerenderlibrary/obfuscation-rules.txt create mode 100644 webprerenderlibrary/oh-package.json5 rename {entry => webprerenderlibrary}/src/main/ets/common/Constants.ets (100%) rename {entry => webprerenderlibrary}/src/main/ets/common/ResourceUtil.ets (100%) create mode 100644 webprerenderlibrary/src/main/ets/common/utils/BreakpointSystem.ets create mode 100644 webprerenderlibrary/src/main/ets/common/utils/WindowUtil.ets rename {entry => webprerenderlibrary}/src/main/ets/pages/WebPage.ets (94%) create mode 100644 webprerenderlibrary/src/main/ets/pages/WebPreRenderPage.ets create mode 100644 webprerenderlibrary/src/main/module.json5 rename {entry => webprerenderlibrary}/src/main/resources/base/element/color.json (100%) rename {entry => webprerenderlibrary}/src/main/resources/base/element/float.json (100%) rename {entry => webprerenderlibrary}/src/main/resources/base/element/string.json (100%) rename {entry => webprerenderlibrary}/src/main/resources/base/media/background.png (100%) rename {entry => webprerenderlibrary}/src/main/resources/base/media/foreground.png (100%) rename {entry => webprerenderlibrary}/src/main/resources/base/media/layered_image.json (100%) rename {entry => webprerenderlibrary}/src/main/resources/base/media/startIcon.png (100%) rename {entry => webprerenderlibrary}/src/main/resources/en_US/element/string.json (100%) rename {entry => webprerenderlibrary}/src/main/resources/rawfile/config.json (100%) rename {entry => webprerenderlibrary}/src/main/resources/zh_CN/element/string.json (100%) rename {entry => webprerendersample}/build-profile.json5 (100%) rename {entry => webprerendersample}/hvigorfile.ts (100%) rename {entry => webprerendersample}/obfuscation-rules.txt (100%) create mode 100644 webprerendersample/oh-package-lock.json5 rename {entry => webprerendersample}/oh-package.json5 (82%) rename entry/src/main/ets/entryability/EntryAbility.ets => webprerendersample/src/main/ets/entryability/WebprerendersampleAbility.ets (98%) rename {entry => webprerendersample}/src/main/ets/pages/Index.ets (100%) rename {entry => webprerendersample}/src/main/module.json5 (95%) rename {entry => webprerendersample}/src/main/resources/base/profile/main_pages.json (100%) diff --git a/entry/src/main/ets/common/BreakpointType.ets b/entry/src/main/ets/common/BreakpointType.ets deleted file mode 100644 index 2fe9c52..0000000 --- a/entry/src/main/ets/common/BreakpointType.ets +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export class BreakpointType { - public sm: T - public md: T - public lg: T - - constructor(sm: T, md: T, lg: T) { - this.sm = sm; - this.md = md; - this.lg = lg; - } - - GetValue(currentBreakpoint: string) { - if (currentBreakpoint === 'sm') { - return this.sm; - } - if (currentBreakpoint === 'md') { - return this.md; - } - if (currentBreakpoint === 'lg') { - return this.lg; - } - return undefined; - } -} diff --git a/webprerenderlibrary/BuildProfile.ets b/webprerenderlibrary/BuildProfile.ets new file mode 100644 index 0000000..3a501e5 --- /dev/null +++ b/webprerenderlibrary/BuildProfile.ets @@ -0,0 +1,17 @@ +/** + * Use these variables when you tailor your ArkTS code. They must be of the const type. + */ +export const HAR_VERSION = '1.0.0'; +export const BUILD_MODE_NAME = 'debug'; +export const DEBUG = true; +export const TARGET_NAME = 'default'; + +/** + * BuildProfile Class is used only for compatibility purposes. + */ +export default class BuildProfile { + static readonly HAR_VERSION = HAR_VERSION; + static readonly BUILD_MODE_NAME = BUILD_MODE_NAME; + static readonly DEBUG = DEBUG; + static readonly TARGET_NAME = TARGET_NAME; +} \ No newline at end of file diff --git a/webprerenderlibrary/Index.ets b/webprerenderlibrary/Index.ets new file mode 100644 index 0000000..42bf416 --- /dev/null +++ b/webprerenderlibrary/Index.ets @@ -0,0 +1 @@ +export { MainPage } from './src/main/ets/components/MainPage'; diff --git a/webprerenderlibrary/build-profile.json5 b/webprerenderlibrary/build-profile.json5 new file mode 100644 index 0000000..e6773f9 --- /dev/null +++ b/webprerenderlibrary/build-profile.json5 @@ -0,0 +1,31 @@ +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + }, + "consumerFiles": [ + "./consumer-rules.txt" + ] + } + }, + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest" + } + ] +} diff --git a/webprerenderlibrary/consumer-rules.txt b/webprerenderlibrary/consumer-rules.txt new file mode 100644 index 0000000..e69de29 diff --git a/webprerenderlibrary/hvigorfile.ts b/webprerenderlibrary/hvigorfile.ts new file mode 100644 index 0000000..4218707 --- /dev/null +++ b/webprerenderlibrary/hvigorfile.ts @@ -0,0 +1,6 @@ +import { harTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/webprerenderlibrary/obfuscation-rules.txt b/webprerenderlibrary/obfuscation-rules.txt new file mode 100644 index 0000000..272efb6 --- /dev/null +++ b/webprerenderlibrary/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/webprerenderlibrary/oh-package.json5 b/webprerenderlibrary/oh-package.json5 new file mode 100644 index 0000000..a63e49e --- /dev/null +++ b/webprerenderlibrary/oh-package.json5 @@ -0,0 +1,9 @@ +{ + "name": "webprerenderlibrary", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "Index.ets", + "author": "", + "license": "Apache-2.0", + "dependencies": {} +} diff --git a/entry/src/main/ets/common/Constants.ets b/webprerenderlibrary/src/main/ets/common/Constants.ets similarity index 100% rename from entry/src/main/ets/common/Constants.ets rename to webprerenderlibrary/src/main/ets/common/Constants.ets diff --git a/entry/src/main/ets/common/ResourceUtil.ets b/webprerenderlibrary/src/main/ets/common/ResourceUtil.ets similarity index 100% rename from entry/src/main/ets/common/ResourceUtil.ets rename to webprerenderlibrary/src/main/ets/common/ResourceUtil.ets diff --git a/webprerenderlibrary/src/main/ets/common/utils/BreakpointSystem.ets b/webprerenderlibrary/src/main/ets/common/utils/BreakpointSystem.ets new file mode 100644 index 0000000..aa7581b --- /dev/null +++ b/webprerenderlibrary/src/main/ets/common/utils/BreakpointSystem.ets @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { window } from '@kit.ArkUI'; +import type { BusinessError } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; + +const TAG: string = '[BreakpointSystem]'; + +export enum BreakpointTypeEnum { + XS = 'xs', + SM = 'sm', + MD = 'md', + LG = 'lg', + XL = 'xl', +} + +export interface BreakpointTypes { + xs?: T; + sm: T; + md: T; + lg: T; + xl?: T; +} + +export class BreakpointType { + private xs: T; + private sm: T; + private md: T; + private lg: T; + private xl: T; + + public constructor(param: BreakpointTypes) { + this.xs = param.xs || param.sm; + this.sm = param.sm; + this.md = param.md; + this.lg = param.lg; + this.xl = param.xl || param.lg; + } + + public getValue(currentBreakpoint: string): T { + if (currentBreakpoint === BreakpointTypeEnum.XS) { + return this.xs; + } + if (currentBreakpoint === BreakpointTypeEnum.SM) { + return this.sm; + } + if (currentBreakpoint === BreakpointTypeEnum.MD) { + return this.md; + } + if (currentBreakpoint === BreakpointTypeEnum.XL) { + return this.xl; + } + return this.lg; + } +} + +export class BreakpointSystem { + private static instance: BreakpointSystem; + private currentBreakpoint: BreakpointTypeEnum = BreakpointTypeEnum.MD; + + private constructor() { + } + + public static getInstance(): BreakpointSystem { + if (!BreakpointSystem.instance) { + BreakpointSystem.instance = new BreakpointSystem(); + } + return BreakpointSystem.instance; + } + + public updateCurrentBreakpoint(breakpoint: BreakpointTypeEnum): void { + if (this.currentBreakpoint !== breakpoint) { + this.currentBreakpoint = breakpoint; + AppStorage.setOrCreate('currentBreakpoint', this.currentBreakpoint); + } + } + + public onWindowSizeChange(window: window.Window): void { + this.updateWidthBp(window); + } + + public updateWidthBp(window: window.Window): void { + try { + const mainWindow: window.WindowProperties = window.getWindowProperties(); + const windowWidth: number = mainWindow.windowRect.width; + const windowWidthVp = px2vp(windowWidth); + let widthBp: BreakpointTypeEnum = BreakpointTypeEnum.MD; + if (windowWidthVp < 320) { + widthBp = BreakpointTypeEnum.XS; + } else if (windowWidthVp >= 320 && windowWidthVp < 600) { + widthBp = BreakpointTypeEnum.SM; + } else if (windowWidthVp >= 600 && windowWidthVp < 840) { + widthBp = BreakpointTypeEnum.MD; + } else if (windowWidthVp >= 840 && windowWidthVp < 1440) { + widthBp = BreakpointTypeEnum.LG; + } else { + widthBp = BreakpointTypeEnum.XL; + } + this.updateCurrentBreakpoint(widthBp); + } catch (error) { + const err: BusinessError = error as BusinessError; + hilog.error(0x0000, TAG, `UpdateBreakpoint fail, error code: ${err.code}, message: ${err.message}`); + } + } +} \ No newline at end of file diff --git a/webprerenderlibrary/src/main/ets/common/utils/WindowUtil.ets b/webprerenderlibrary/src/main/ets/common/utils/WindowUtil.ets new file mode 100644 index 0000000..0e99803 --- /dev/null +++ b/webprerenderlibrary/src/main/ets/common/utils/WindowUtil.ets @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { window } from '@kit.ArkUI'; +import type { BusinessError } from '@kit.BasicServicesKit'; +import { BreakpointSystem } from './BreakpointSystem'; +import { hilog } from '@kit.PerformanceAnalysisKit'; + +const TAG: string = '[WindowUtil]'; + +export class WindowUtil { + public static requestFullScreen(windowStage: window.WindowStage): void { + windowStage.getMainWindow((err: BusinessError, data: window.Window) => { + if (err.code) { + return; + } + const windowClass: window.Window = data; + // Realize the immersive effect. + try { + const promise: Promise = windowClass.setWindowLayoutFullScreen(true); + promise.then(() => { + hilog.info(0x0000, TAG, 'Succeeded in setting the window layout to full-screen mode.'); + }).catch((err: BusinessError) => { + hilog.info(0x0000, TAG, + `Failed to set the window layout to full-screen mode. Cause: ${err.code}, ${err.message}`); + }); + } catch { + hilog.error(0x0000, TAG, 'Failed to set the window layout to full-screen mode. '); + } + }); + } + + public static registerBreakPoint(windowStage: window.WindowStage) { + windowStage.getMainWindow((err: BusinessError, data: window.Window) => { + if (err.code) { + hilog.error(0x0000, TAG, `Failed to get main window: ${err.message}`); + return; + } + let avoidArea = data.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); + let bottomRectHeight = avoidArea.bottomRect.height; + AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); + avoidArea = data.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); + let topRectHeight = avoidArea.topRect.height; + AppStorage.setOrCreate('topRectHeight', topRectHeight); + BreakpointSystem.getInstance().updateWidthBp(data); + data.on('windowSizeChange', () => BreakpointSystem.getInstance().onWindowSizeChange(data)); + data.on('avoidAreaChange', (avoidAreaOption) => { + if (avoidAreaOption.type === window.AvoidAreaType.TYPE_SYSTEM || + avoidAreaOption.type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) { + WindowUtil.setAvoidArea(avoidAreaOption.type, avoidAreaOption.area); + } + }); + }) + } + + // Get status bar height and indicator height. + public static setAvoidArea(type: window.AvoidAreaType, area: window.AvoidArea) { + if (type === window.AvoidAreaType.TYPE_SYSTEM) { + AppStorage.setOrCreate('statusBarHeight', px2vp(area.topRect.height)); + } else { + AppStorage.setOrCreate('naviIndicatorHeight', px2vp(area.bottomRect.height)); + } + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/WebPage.ets b/webprerenderlibrary/src/main/ets/pages/WebPage.ets similarity index 94% rename from entry/src/main/ets/pages/WebPage.ets rename to webprerenderlibrary/src/main/ets/pages/WebPage.ets index 4a4cb80..86168a1 100644 --- a/entry/src/main/ets/pages/WebPage.ets +++ b/webprerenderlibrary/src/main/ets/pages/WebPage.ets @@ -17,8 +17,8 @@ import { BuilderNode, FrameNode, NodeController, UIContext } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { common } from '@kit.AbilityKit'; import { webview } from '@kit.ArkWeb'; -import { ConfigMapKey, ResourceUtil } from '../common/ResourceUtil'; -import { Constants } from '../common/Constants'; +import { ConfigMapKey, ResourceUtil } from 'webprerenderlibrary/src/main/ets/common/ResourceUtil'; +import { Constants } from 'webprerenderlibrary/src/main/ets/common/Constants'; import { BusinessError } from '@kit.BasicServicesKit'; class Data { diff --git a/webprerenderlibrary/src/main/ets/pages/WebPreRenderPage.ets b/webprerenderlibrary/src/main/ets/pages/WebPreRenderPage.ets new file mode 100644 index 0000000..b8fd40e --- /dev/null +++ b/webprerenderlibrary/src/main/ets/pages/WebPreRenderPage.ets @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { common } from '@kit.AbilityKit'; +import { ConfigMapKey, ResourceUtil } from 'webprerenderlibrary/src/main/ets/common/ResourceUtil'; +import { BreakpointType } from 'webprerenderlibrary/src/main/ets/common/BreakpointType'; +import { Constants } from 'webprerenderlibrary/src/main/ets/common/Constants'; +import { getNWeb } from './WebPage'; + +@Entry +@Component +struct WebPreRender { + @State isShowSheet: boolean = false; + @StorageProp('currentBreakpoint') curBp: string = Constants.BREAKPOINTS[1]; + @StorageProp('bottomRectHeight') bottomRectHeight: number = 0; + @StorageProp('topRectHeight') topRectHeight: number = 0; + + @Builder + mySheet() { + Column() { + NodeContainer(getNWeb(ResourceUtil.getRawFileStringByKey(getContext(this) as common.UIAbilityContext, + ConfigMapKey.GALLERY_URL))) + .height($r('app.string.full_height_width')) + .width($r('app.string.full_height_width')) + } + .padding({ + top: $r('app.string.index_tab'), + left: $r('sys.float.padding_level8'), + right: $r('sys.float.padding_level8') + }) + .width($r('app.string.full_height_width')) + .height($r('app.string.full_height_width')) + .backgroundColor($r('sys.color.comp_background_primary')) + } + + build() { + Column() { + Column() { + Text($r('app.string.title')) + .fontWeight(FontWeight.Bold) + .fontColor($r('sys.color.font_primary')) + .fontSize($r('sys.float.Title_L')) + .alignSelf(ItemAlign.Start) + .margin({ + left: this.curBp === 'sm' ? $r('sys.float.padding_level0') : + this.curBp === 'md' ? $r('sys.float.padding_level4') : $r('sys.float.padding_level8') + }) + Column() { + Button($r('app.string.text_name')) + .fontSize($r('sys.float.Subtitle_M')) + .fontColor($r('sys.color.font_on_primary')) + .width(this.curBp === 'sm' ? '100%' : $r('app.float.index_button_width_lg')) + .height($r('app.float.button_height')) + .margin({ top: $r('sys.float.padding_level6') }) + .backgroundColor($r('sys.color.comp_background_emphasize')) + .bindSheet(this.isShowSheet, this.mySheet(), { + height: new BreakpointType(748, 560, 560).GetValue(this.curBp), + width: this.curBp === 'sm' ? '100%' : 480, + preferType: SheetType.CENTER, + backgroundColor: $r('sys.color.comp_background_primary'), + onWillDisappear: () => { + this.isShowSheet = !this.isShowSheet; + } + }) + .onClick(() => { + this.isShowSheet = !this.isShowSheet; + }) + } + .width(Constants.FULL_WIDTH) + } + .backgroundColor($r('sys.color.background_secondary')) + .width(Constants.FULL_WIDTH) + .height(Constants.FULL_HEIGHT) + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + .padding({ + left: $r('sys.float.padding_level8'), + right: $r('sys.float.padding_level8'), + top: Constants.PADDING_TOP_INDEX + px2vp(this.topRectHeight), + bottom: Constants.PADDING_BOTTOM_INDEX + px2vp(this.bottomRectHeight) + }) + } + .width($r('app.string.full_height_width')) + .height($r('app.string.full_height_width')) + .backgroundColor($r('sys.color.background_secondary')) + } +} \ No newline at end of file diff --git a/webprerenderlibrary/src/main/module.json5 b/webprerenderlibrary/src/main/module.json5 new file mode 100644 index 0000000..48a8210 --- /dev/null +++ b/webprerenderlibrary/src/main/module.json5 @@ -0,0 +1,11 @@ +{ + "module": { + "name": "webprerenderlibrary", + "type": "har", + "deviceTypes": [ + "default", + "tablet", + "2in1" + ] + } +} diff --git a/entry/src/main/resources/base/element/color.json b/webprerenderlibrary/src/main/resources/base/element/color.json similarity index 100% rename from entry/src/main/resources/base/element/color.json rename to webprerenderlibrary/src/main/resources/base/element/color.json diff --git a/entry/src/main/resources/base/element/float.json b/webprerenderlibrary/src/main/resources/base/element/float.json similarity index 100% rename from entry/src/main/resources/base/element/float.json rename to webprerenderlibrary/src/main/resources/base/element/float.json diff --git a/entry/src/main/resources/base/element/string.json b/webprerenderlibrary/src/main/resources/base/element/string.json similarity index 100% rename from entry/src/main/resources/base/element/string.json rename to webprerenderlibrary/src/main/resources/base/element/string.json diff --git a/entry/src/main/resources/base/media/background.png b/webprerenderlibrary/src/main/resources/base/media/background.png similarity index 100% rename from entry/src/main/resources/base/media/background.png rename to webprerenderlibrary/src/main/resources/base/media/background.png diff --git a/entry/src/main/resources/base/media/foreground.png b/webprerenderlibrary/src/main/resources/base/media/foreground.png similarity index 100% rename from entry/src/main/resources/base/media/foreground.png rename to webprerenderlibrary/src/main/resources/base/media/foreground.png diff --git a/entry/src/main/resources/base/media/layered_image.json b/webprerenderlibrary/src/main/resources/base/media/layered_image.json similarity index 100% rename from entry/src/main/resources/base/media/layered_image.json rename to webprerenderlibrary/src/main/resources/base/media/layered_image.json diff --git a/entry/src/main/resources/base/media/startIcon.png b/webprerenderlibrary/src/main/resources/base/media/startIcon.png similarity index 100% rename from entry/src/main/resources/base/media/startIcon.png rename to webprerenderlibrary/src/main/resources/base/media/startIcon.png diff --git a/entry/src/main/resources/en_US/element/string.json b/webprerenderlibrary/src/main/resources/en_US/element/string.json similarity index 100% rename from entry/src/main/resources/en_US/element/string.json rename to webprerenderlibrary/src/main/resources/en_US/element/string.json diff --git a/entry/src/main/resources/rawfile/config.json b/webprerenderlibrary/src/main/resources/rawfile/config.json similarity index 100% rename from entry/src/main/resources/rawfile/config.json rename to webprerenderlibrary/src/main/resources/rawfile/config.json diff --git a/entry/src/main/resources/zh_CN/element/string.json b/webprerenderlibrary/src/main/resources/zh_CN/element/string.json similarity index 100% rename from entry/src/main/resources/zh_CN/element/string.json rename to webprerenderlibrary/src/main/resources/zh_CN/element/string.json diff --git a/entry/build-profile.json5 b/webprerendersample/build-profile.json5 similarity index 100% rename from entry/build-profile.json5 rename to webprerendersample/build-profile.json5 diff --git a/entry/hvigorfile.ts b/webprerendersample/hvigorfile.ts similarity index 100% rename from entry/hvigorfile.ts rename to webprerendersample/hvigorfile.ts diff --git a/entry/obfuscation-rules.txt b/webprerendersample/obfuscation-rules.txt similarity index 100% rename from entry/obfuscation-rules.txt rename to webprerendersample/obfuscation-rules.txt diff --git a/webprerendersample/oh-package-lock.json5 b/webprerendersample/oh-package-lock.json5 new file mode 100644 index 0000000..c3369b6 --- /dev/null +++ b/webprerendersample/oh-package-lock.json5 @@ -0,0 +1,18 @@ +{ + "meta": { + "stableOrder": true + }, + "lockfileVersion": 3, + "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", + "specifiers": { + "webprerenderlibrary@../webprerenderlibrary": "webprerenderlibrary@../webprerenderlibrary" + }, + "packages": { + "webprerenderlibrary@../webprerenderlibrary": { + "name": "webprerenderlibrary", + "version": "1.0.0", + "resolved": "../webprerenderlibrary", + "registryType": "local" + } + } +} \ No newline at end of file diff --git a/entry/oh-package.json5 b/webprerendersample/oh-package.json5 similarity index 82% rename from entry/oh-package.json5 rename to webprerendersample/oh-package.json5 index 248c3b7..acf55cf 100644 --- a/entry/oh-package.json5 +++ b/webprerendersample/oh-package.json5 @@ -1,10 +1,9 @@ { - "name": "entry", + "name": "webprerendersample", "version": "1.0.0", "description": "Please describe the basic information.", "main": "", "author": "", "license": "", "dependencies": {} -} - +} \ No newline at end of file diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/webprerendersample/src/main/ets/entryability/WebprerendersampleAbility.ets similarity index 98% rename from entry/src/main/ets/entryability/EntryAbility.ets rename to webprerendersample/src/main/ets/entryability/WebprerendersampleAbility.ets index 98359fc..2778ef5 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/webprerendersample/src/main/ets/entryability/WebprerendersampleAbility.ets @@ -19,7 +19,7 @@ import { hilog } from '@kit.PerformanceAnalysisKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { resourceManager } from '@kit.LocalizationKit'; import { createNWeb } from '../pages/WebPage'; -import { ConfigMapKey, ResourceUtil } from '../common/ResourceUtil'; +import { ConfigMapKey, ResourceUtil } from 'webprerenderlibrary/src/main/ets/common/ResourceUtil'; export default class EntryAbility extends UIAbility { private curBp: string = ''; diff --git a/entry/src/main/ets/pages/Index.ets b/webprerendersample/src/main/ets/pages/Index.ets similarity index 100% rename from entry/src/main/ets/pages/Index.ets rename to webprerendersample/src/main/ets/pages/Index.ets diff --git a/entry/src/main/module.json5 b/webprerendersample/src/main/module.json5 similarity index 95% rename from entry/src/main/module.json5 rename to webprerendersample/src/main/module.json5 index 428fff8..1797c65 100644 --- a/entry/src/main/module.json5 +++ b/webprerendersample/src/main/module.json5 @@ -1,6 +1,6 @@ { "module": { - "name": "entry", + "name": "webprerendersample", "type": "entry", "description": "$string:module_desc", "mainElement": "EntryAbility", @@ -21,7 +21,7 @@ "label": "$string:EntryAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", - "exported": false, + "exported": true, "skills": [ { "entities": [ diff --git a/entry/src/main/resources/base/profile/main_pages.json b/webprerendersample/src/main/resources/base/profile/main_pages.json similarity index 100% rename from entry/src/main/resources/base/profile/main_pages.json rename to webprerendersample/src/main/resources/base/profile/main_pages.json -- Gitee From f2bb7315f93d3b84dfa801063984a30d1a5ef703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E5=A4=A7=E4=BE=A0?= <794397348@qq.com> Date: Sat, 24 May 2025 19:23:32 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build-profile.json5 | 8 ++- webprerenderlibrary/.gitignore | 6 ++ webprerenderlibrary/Index.ets | 3 +- webprerenderlibrary/build-profile.json5 | 5 +- .../src/main/ets/common/utils/WindowUtil.ets | 17 +++-- .../src/main/ets/pages/WebPage.ets | 4 +- .../src/main/ets/pages/WebPreRenderPage.ets | 14 ++-- webprerendersample/oh-package.json5 | 4 +- ...ndersampleAbility.ets => EntryAbility.ets} | 69 ++----------------- .../src/main/ets/pages/Index.ets | 14 ++-- webprerendersample/src/main/module.json5 | 2 +- 11 files changed, 49 insertions(+), 97 deletions(-) create mode 100644 webprerenderlibrary/.gitignore rename webprerendersample/src/main/ets/entryability/{WebprerendersampleAbility.ets => EntryAbility.ets} (45%) diff --git a/build-profile.json5 b/build-profile.json5 index c033ce4..761c223 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -20,8 +20,8 @@ }, "modules": [ { - "name": "entry", - "srcPath": "./entry", + "name": "webprerendersample", + "srcPath": "./webprerendersample", "targets": [ { "name": "default", @@ -30,6 +30,10 @@ ] } ] + }, + { + "name": "webprerenderlibrary", + "srcPath": "./webprerenderlibrary", } ] } \ No newline at end of file diff --git a/webprerenderlibrary/.gitignore b/webprerenderlibrary/.gitignore new file mode 100644 index 0000000..e2713a2 --- /dev/null +++ b/webprerenderlibrary/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/webprerenderlibrary/Index.ets b/webprerenderlibrary/Index.ets index 42bf416..6bb0a6b 100644 --- a/webprerenderlibrary/Index.ets +++ b/webprerenderlibrary/Index.ets @@ -1 +1,2 @@ -export { MainPage } from './src/main/ets/components/MainPage'; +export { WebPreRenderPage } from './src/main/ets/pages/WebPreRenderPage'; +export { WindowUtil } from './src/main/ets/common/utils/WindowUtil'; \ No newline at end of file diff --git a/webprerenderlibrary/build-profile.json5 b/webprerenderlibrary/build-profile.json5 index e6773f9..312d38e 100644 --- a/webprerenderlibrary/build-profile.json5 +++ b/webprerenderlibrary/build-profile.json5 @@ -8,7 +8,7 @@ "arkOptions": { "obfuscation": { "ruleOptions": { - "enable": false, + "enable": true, "files": [ "./obfuscation-rules.txt" ] @@ -23,9 +23,6 @@ "targets": [ { "name": "default" - }, - { - "name": "ohosTest" } ] } diff --git a/webprerenderlibrary/src/main/ets/common/utils/WindowUtil.ets b/webprerenderlibrary/src/main/ets/common/utils/WindowUtil.ets index 0e99803..84b20e5 100644 --- a/webprerenderlibrary/src/main/ets/common/utils/WindowUtil.ets +++ b/webprerenderlibrary/src/main/ets/common/utils/WindowUtil.ets @@ -17,6 +17,9 @@ import { window } from '@kit.ArkUI'; import type { BusinessError } from '@kit.BasicServicesKit'; import { BreakpointSystem } from './BreakpointSystem'; import { hilog } from '@kit.PerformanceAnalysisKit'; +import { createNWeb } from '../../pages/WebPage'; +import { ConfigMapKey, ResourceUtil } from '../ResourceUtil'; +import { common, UIAbility } from '@kit.AbilityKit'; const TAG: string = '[WindowUtil]'; @@ -42,18 +45,12 @@ export class WindowUtil { }); } - public static registerBreakPoint(windowStage: window.WindowStage) { + public static registerBreakPoint(windowStage: window.WindowStage,ability:UIAbility) { windowStage.getMainWindow((err: BusinessError, data: window.Window) => { if (err.code) { hilog.error(0x0000, TAG, `Failed to get main window: ${err.message}`); return; } - let avoidArea = data.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); - let bottomRectHeight = avoidArea.bottomRect.height; - AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); - avoidArea = data.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); - let topRectHeight = avoidArea.topRect.height; - AppStorage.setOrCreate('topRectHeight', topRectHeight); BreakpointSystem.getInstance().updateWidthBp(data); data.on('windowSizeChange', () => BreakpointSystem.getInstance().onWindowSizeChange(data)); data.on('avoidAreaChange', (avoidAreaOption) => { @@ -62,15 +59,17 @@ export class WindowUtil { WindowUtil.setAvoidArea(avoidAreaOption.type, avoidAreaOption.area); } }); + createNWeb(ResourceUtil.getRawFileStringByKey(getContext(ability) as common.UIAbilityContext, + ConfigMapKey.GALLERY_URL), data.getUIContext()); }) } // Get status bar height and indicator height. public static setAvoidArea(type: window.AvoidAreaType, area: window.AvoidArea) { if (type === window.AvoidAreaType.TYPE_SYSTEM) { - AppStorage.setOrCreate('statusBarHeight', px2vp(area.topRect.height)); + AppStorage.setOrCreate('topRectHeight', px2vp(area.topRect.height)); } else { - AppStorage.setOrCreate('naviIndicatorHeight', px2vp(area.bottomRect.height)); + AppStorage.setOrCreate('bottomRectHeight', px2vp(area.bottomRect.height)); } } } \ No newline at end of file diff --git a/webprerenderlibrary/src/main/ets/pages/WebPage.ets b/webprerenderlibrary/src/main/ets/pages/WebPage.ets index 86168a1..b0c4c52 100644 --- a/webprerenderlibrary/src/main/ets/pages/WebPage.ets +++ b/webprerenderlibrary/src/main/ets/pages/WebPage.ets @@ -17,9 +17,9 @@ import { BuilderNode, FrameNode, NodeController, UIContext } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { common } from '@kit.AbilityKit'; import { webview } from '@kit.ArkWeb'; -import { ConfigMapKey, ResourceUtil } from 'webprerenderlibrary/src/main/ets/common/ResourceUtil'; -import { Constants } from 'webprerenderlibrary/src/main/ets/common/Constants'; import { BusinessError } from '@kit.BasicServicesKit'; +import { ConfigMapKey, ResourceUtil } from '../common/ResourceUtil'; +import { Constants } from '../common/Constants'; class Data { public url: string = diff --git a/webprerenderlibrary/src/main/ets/pages/WebPreRenderPage.ets b/webprerenderlibrary/src/main/ets/pages/WebPreRenderPage.ets index b8fd40e..72fb1d2 100644 --- a/webprerenderlibrary/src/main/ets/pages/WebPreRenderPage.ets +++ b/webprerenderlibrary/src/main/ets/pages/WebPreRenderPage.ets @@ -14,14 +14,14 @@ */ import { common } from '@kit.AbilityKit'; -import { ConfigMapKey, ResourceUtil } from 'webprerenderlibrary/src/main/ets/common/ResourceUtil'; -import { BreakpointType } from 'webprerenderlibrary/src/main/ets/common/BreakpointType'; -import { Constants } from 'webprerenderlibrary/src/main/ets/common/Constants'; +import { Constants } from '../common/Constants'; +import { ConfigMapKey, ResourceUtil } from '../common/ResourceUtil'; +import { BreakpointType } from '../common/utils/BreakpointSystem'; import { getNWeb } from './WebPage'; @Entry @Component -struct WebPreRender { +export struct WebPreRenderPage { @State isShowSheet: boolean = false; @StorageProp('currentBreakpoint') curBp: string = Constants.BREAKPOINTS[1]; @StorageProp('bottomRectHeight') bottomRectHeight: number = 0; @@ -66,7 +66,7 @@ struct WebPreRender { .margin({ top: $r('sys.float.padding_level6') }) .backgroundColor($r('sys.color.comp_background_emphasize')) .bindSheet(this.isShowSheet, this.mySheet(), { - height: new BreakpointType(748, 560, 560).GetValue(this.curBp), + height: new BreakpointType({sm:748, md:560, lg:560}).getValue(this.curBp), width: this.curBp === 'sm' ? '100%' : 480, preferType: SheetType.CENTER, backgroundColor: $r('sys.color.comp_background_primary'), @@ -88,8 +88,8 @@ struct WebPreRender { .padding({ left: $r('sys.float.padding_level8'), right: $r('sys.float.padding_level8'), - top: Constants.PADDING_TOP_INDEX + px2vp(this.topRectHeight), - bottom: Constants.PADDING_BOTTOM_INDEX + px2vp(this.bottomRectHeight) + top: Constants.PADDING_TOP_INDEX + this.topRectHeight, + bottom: Constants.PADDING_BOTTOM_INDEX + this.bottomRectHeight }) } .width($r('app.string.full_height_width')) diff --git a/webprerendersample/oh-package.json5 b/webprerendersample/oh-package.json5 index acf55cf..2a4c88b 100644 --- a/webprerendersample/oh-package.json5 +++ b/webprerendersample/oh-package.json5 @@ -5,5 +5,7 @@ "main": "", "author": "", "license": "", - "dependencies": {} + "dependencies": { + "webprerenderlibrary": "file:../webprerenderlibrary" + } } \ No newline at end of file diff --git a/webprerendersample/src/main/ets/entryability/WebprerendersampleAbility.ets b/webprerendersample/src/main/ets/entryability/EntryAbility.ets similarity index 45% rename from webprerendersample/src/main/ets/entryability/WebprerendersampleAbility.ets rename to webprerendersample/src/main/ets/entryability/EntryAbility.ets index 2778ef5..9b46fa0 100644 --- a/webprerendersample/src/main/ets/entryability/WebprerendersampleAbility.ets +++ b/webprerendersample/src/main/ets/entryability/EntryAbility.ets @@ -13,37 +13,12 @@ * limitations under the License. */ -import { common, Configuration, ConfigurationConstant, UIAbility } from '@kit.AbilityKit'; -import { display, window } from '@kit.ArkUI'; +import { Configuration, ConfigurationConstant, UIAbility } from '@kit.AbilityKit'; +import { window } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; -import { BusinessError } from '@kit.BasicServicesKit'; -import { resourceManager } from '@kit.LocalizationKit'; -import { createNWeb } from '../pages/WebPage'; -import { ConfigMapKey, ResourceUtil } from 'webprerenderlibrary/src/main/ets/common/ResourceUtil'; +import { WindowUtil } from 'webprerenderlibrary'; -export default class EntryAbility extends UIAbility { - private curBp: string = ''; - - private updateBreakpoint(windowWidth: number): void { - try { - let windowWidthVp = windowWidth / display.getDefaultDisplaySync().densityPixels; - let newBp: string = ''; - if (windowWidthVp < 600) { - newBp = 'sm'; - } else if (windowWidthVp < 840) { - newBp = 'md'; - } else { - newBp = 'lg'; - } - if (this.curBp !== newBp) { - this.curBp = newBp; - AppStorage.setOrCreate('currentBreakpoint', this.curBp); - } - } catch (error) { - hilog.error(0x0000, 'updateBreakpoint', '%{public}s', - `updateBreakpoint fail, ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); - } - } +export default class WebprerendersampleAbility extends UIAbility { onCreate(): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); @@ -67,46 +42,14 @@ export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - - let resourceManager: resourceManager.ResourceManager = this.context.resourceManager; - AppStorage.setOrCreate('resourceManager', resourceManager); windowStage.loadContent('pages/Index', (err) => { if (err.code) { hilog.error(0x0000, 'testTag', '%{public}s', `Failed to load the content. Cause: ${err.code}`); return; } hilog.info(0x0000, 'testTag', '%{public}s', `Succeed in loading the content.`); - - windowStage.getMainWindow().then((windowObj: window.Window) => { - try { - let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; - let avoidArea = windowObj.getWindowAvoidArea(type); - let bottomRectHeight = avoidArea.bottomRect.height; - AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); - type = window.AvoidAreaType.TYPE_SYSTEM; - avoidArea = windowObj.getWindowAvoidArea(type); - let topRectHeight = avoidArea.topRect.height; - AppStorage.setOrCreate('topRectHeight', topRectHeight); - this.updateBreakpoint(windowObj.getWindowProperties().windowRect.width); - - windowObj.on('windowSizeChange', (windowSize) => { - this.updateBreakpoint(windowSize.width); - }); - - windowObj.setWindowLayoutFullScreen(true).then(() => { - hilog.info(0x0000, 'testTag', '%{public}s', 'Succeeded in setting the window layout to full-screen mode.'); - }).catch(() => { - hilog.error(0x0000, 'testTag', '%{public}s', 'Failed to set the window layout to full-screen mode.'); - }); - - createNWeb(ResourceUtil.getRawFileStringByKey(getContext(this) as common.UIAbilityContext, - ConfigMapKey.GALLERY_URL), windowObj.getUIContext()); - } catch (err) { - hilog.error(0x0000, 'testTag', `Set window properties err. Cause: ${err.code}`); - } - }).catch((err: BusinessError) => { - hilog.error(0x0000, 'testTag', `Failed to get main window. Cause:`, err.message); - }); + WindowUtil.requestFullScreen(windowStage) + WindowUtil.registerBreakPoint(windowStage,this) }); } diff --git a/webprerendersample/src/main/ets/pages/Index.ets b/webprerendersample/src/main/ets/pages/Index.ets index af52f75..8b0ab46 100644 --- a/webprerendersample/src/main/ets/pages/Index.ets +++ b/webprerendersample/src/main/ets/pages/Index.ets @@ -14,10 +14,10 @@ */ import { common } from '@kit.AbilityKit'; -import { ConfigMapKey, ResourceUtil } from '../common/ResourceUtil'; -import { BreakpointType } from '../common/BreakpointType'; -import { Constants } from '../common/Constants'; -import { getNWeb } from './WebPage'; +import { ConfigMapKey, ResourceUtil } from 'webprerenderlibrary/src/main/ets/common/ResourceUtil'; +import { Constants } from 'webprerenderlibrary/src/main/ets/common/Constants'; +import { getNWeb } from 'webprerenderlibrary/src/main/ets/pages/WebPage'; +import { BreakpointType } from 'webprerenderlibrary/src/main/ets/common/utils/BreakpointSystem'; @Entry @Component @@ -66,7 +66,7 @@ struct WebPreRender { .margin({ top: $r('sys.float.padding_level6') }) .backgroundColor($r('sys.color.comp_background_emphasize')) .bindSheet(this.isShowSheet, this.mySheet(), { - height: new BreakpointType(748, 560, 560).GetValue(this.curBp), + height: new BreakpointType({sm:748, md:560, lg:560}).getValue(this.curBp), width: this.curBp === 'sm' ? '100%' : 480, preferType: SheetType.CENTER, backgroundColor: $r('sys.color.comp_background_primary'), @@ -88,8 +88,8 @@ struct WebPreRender { .padding({ left: $r('sys.float.padding_level8'), right: $r('sys.float.padding_level8'), - top: Constants.PADDING_TOP_INDEX + px2vp(this.topRectHeight), - bottom: Constants.PADDING_BOTTOM_INDEX + px2vp(this.bottomRectHeight) + top: Constants.PADDING_TOP_INDEX + this.topRectHeight, + bottom: Constants.PADDING_BOTTOM_INDEX + this.bottomRectHeight }) } .width($r('app.string.full_height_width')) diff --git a/webprerendersample/src/main/module.json5 b/webprerendersample/src/main/module.json5 index 1797c65..77e38e2 100644 --- a/webprerendersample/src/main/module.json5 +++ b/webprerendersample/src/main/module.json5 @@ -14,7 +14,7 @@ "pages": "$profile:main_pages", "abilities": [ { - "name": "EntryAbility", + "name": "WebprerendersampleAbility", "srcEntry": "./ets/entryability/EntryAbility.ets", "description": "$string:EntryAbility_desc", "icon": "$media:layered_image", -- Gitee