diff --git a/build-profile.json5 b/build-profile.json5 index c033ce4278fb0641d84107c3c134047ac487368e..761c2232f257bd323c4cd780a204fd51c2ad3fe4 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/entry/src/main/ets/common/BreakpointType.ets b/entry/src/main/ets/common/BreakpointType.ets deleted file mode 100644 index 2fe9c5213aacde5ebd67fe16f7c7bee2972f54f3..0000000000000000000000000000000000000000 --- 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/.gitignore b/webprerenderlibrary/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /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/BuildProfile.ets b/webprerenderlibrary/BuildProfile.ets new file mode 100644 index 0000000000000000000000000000000000000000..3a501e5ddee8ea6d28961648fc7dd314a5304bd4 --- /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 0000000000000000000000000000000000000000..6bb0a6b6dd38222c319bc7b5e4b6288443a45d38 --- /dev/null +++ b/webprerenderlibrary/Index.ets @@ -0,0 +1,2 @@ +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 new file mode 100644 index 0000000000000000000000000000000000000000..312d38eb08629793b3484c7373213f22840c8d82 --- /dev/null +++ b/webprerenderlibrary/build-profile.json5 @@ -0,0 +1,28 @@ +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": true, + "files": [ + "./obfuscation-rules.txt" + ] + }, + "consumerFiles": [ + "./consumer-rules.txt" + ] + } + }, + }, + ], + "targets": [ + { + "name": "default" + } + ] +} diff --git a/webprerenderlibrary/consumer-rules.txt b/webprerenderlibrary/consumer-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/webprerenderlibrary/hvigorfile.ts b/webprerenderlibrary/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..42187071482d292588ad40babeda74f7b8d97a23 --- /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 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /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/entry/oh-package.json5 b/webprerenderlibrary/oh-package.json5 similarity index 59% rename from entry/oh-package.json5 rename to webprerenderlibrary/oh-package.json5 index 248c3b7541a589682a250f86a6d3ecf7414d2d6a..a63e49e85474617db32f4ee51af03e3098079a6d 100644 --- a/entry/oh-package.json5 +++ b/webprerenderlibrary/oh-package.json5 @@ -1,10 +1,9 @@ { - "name": "entry", + "name": "webprerenderlibrary", "version": "1.0.0", "description": "Please describe the basic information.", - "main": "", + "main": "Index.ets", "author": "", - "license": "", + "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 0000000000000000000000000000000000000000..aa7581b2047323eac83c8d9a031e21faabc55bd2 --- /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 0000000000000000000000000000000000000000..84b20e5a1cd85e6ce4dfa553b697ae233990ea34 --- /dev/null +++ b/webprerenderlibrary/src/main/ets/common/utils/WindowUtil.ets @@ -0,0 +1,75 @@ +/* + * 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'; +import { createNWeb } from '../../pages/WebPage'; +import { ConfigMapKey, ResourceUtil } from '../ResourceUtil'; +import { common, UIAbility } from '@kit.AbilityKit'; + +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,ability:UIAbility) { + windowStage.getMainWindow((err: BusinessError, data: window.Window) => { + if (err.code) { + hilog.error(0x0000, TAG, `Failed to get main window: ${err.message}`); + return; + } + 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); + } + }); + 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('topRectHeight', px2vp(area.topRect.height)); + } else { + AppStorage.setOrCreate('bottomRectHeight', 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 100% rename from entry/src/main/ets/pages/WebPage.ets rename to webprerenderlibrary/src/main/ets/pages/WebPage.ets index 4a4cb80290ca402b67abd0d8e90b54174cdbea99..b0c4c52c57def6260f925f077ba39d36c2e2865a 100644 --- a/entry/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 { BusinessError } from '@kit.BasicServicesKit'; import { ConfigMapKey, ResourceUtil } from '../common/ResourceUtil'; import { Constants } from '../common/Constants'; -import { BusinessError } from '@kit.BasicServicesKit'; class Data { public url: string = diff --git a/entry/src/main/ets/pages/Index.ets b/webprerenderlibrary/src/main/ets/pages/WebPreRenderPage.ets similarity index 91% rename from entry/src/main/ets/pages/Index.ets rename to webprerenderlibrary/src/main/ets/pages/WebPreRenderPage.ets index af52f756c08a33d63cb2c6e0e7950ef084374faf..72fb1d29cc8902031062c095bf2b00eece6be88b 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/webprerenderlibrary/src/main/ets/pages/WebPreRenderPage.ets @@ -14,14 +14,14 @@ */ import { common } from '@kit.AbilityKit'; -import { ConfigMapKey, ResourceUtil } from '../common/ResourceUtil'; -import { BreakpointType } from '../common/BreakpointType'; 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/webprerenderlibrary/src/main/module.json5 b/webprerenderlibrary/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..48a8210d9d95fc4b075c7004512c57c56b9c397a --- /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 0000000000000000000000000000000000000000..c3369b6b427564e70f342f8789381dab4497f0e0 --- /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/webprerendersample/oh-package.json5 b/webprerendersample/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2a4c88b30223923de7c60ecf0a60d41041a9d48d --- /dev/null +++ b/webprerendersample/oh-package.json5 @@ -0,0 +1,11 @@ +{ + "name": "webprerendersample", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "webprerenderlibrary": "file:../webprerenderlibrary" + } +} \ No newline at end of file diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/webprerendersample/src/main/ets/entryability/EntryAbility.ets similarity index 45% rename from entry/src/main/ets/entryability/EntryAbility.ets rename to webprerendersample/src/main/ets/entryability/EntryAbility.ets index 98359fc01157b5505f5527efae3df142eda6fbe6..9b46fa0bd4daca15c24c4451d44cc828e76f7713 100644 --- a/entry/src/main/ets/entryability/EntryAbility.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 '../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 new file mode 100644 index 0000000000000000000000000000000000000000..8b0ab46fe93ebc8dcc8111c7dc4801eac7f8466a --- /dev/null +++ b/webprerendersample/src/main/ets/pages/Index.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 { 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 +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({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'), + 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 + this.topRectHeight, + bottom: Constants.PADDING_BOTTOM_INDEX + 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/entry/src/main/module.json5 b/webprerendersample/src/main/module.json5 similarity index 91% rename from entry/src/main/module.json5 rename to webprerendersample/src/main/module.json5 index 428fff8e5c4a8532d2b9fcab40d43dfab4779a7c..77e38e22fdc32c69a04c3ec62829fa3eb20085b4 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", @@ -14,14 +14,14 @@ "pages": "$profile:main_pages", "abilities": [ { - "name": "EntryAbility", + "name": "WebprerendersampleAbility", "srcEntry": "./ets/entryability/EntryAbility.ets", "description": "$string:EntryAbility_desc", "icon": "$media:layered_image", "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