From b34b7c47dc535ca00af1b5f228728a851019951c Mon Sep 17 00:00:00 2001 From: yangpeng85 Date: Sat, 30 Jul 2022 16:40:42 +0800 Subject: [PATCH] support show/hide Wallpaper component when window show/hide Signed-off-by: yangpeng85 --- .../ets/com/ohos/model/screenLockModel.ts | 2 ++ .../ets/com/ohos/view/component/wallpaper.ets | 9 ++++-- .../ets/com/ohos/vm/wallpaperViewModel.ts | 30 ++++++++++++++++--- .../pc/src/main/ets/pages/slidescreenlock.ets | 6 ++-- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/features/screenlock/src/main/ets/com/ohos/model/screenLockModel.ts b/features/screenlock/src/main/ets/com/ohos/model/screenLockModel.ts index d383ff9..53d1a9d 100644 --- a/features/screenlock/src/main/ets/com/ohos/model/screenLockModel.ts +++ b/features/screenlock/src/main/ets/com/ohos/model/screenLockModel.ts @@ -67,6 +67,7 @@ export default class ScreenLockModel { } showScreenLockWindow(callback: Callback) { + AppStorage.SetOrCreate('isWallpaperShow', true); windowManager.find(Constants.WIN_NAME).then((win) => { win.show().then(() => { Log.showInfo(TAG, `window show`); @@ -82,6 +83,7 @@ export default class ScreenLockModel { win.hide().then(() => { Log.showInfo(TAG, `window hide`); callback(); + AppStorage.SetOrCreate('isWallpaperShow', false); }) }) } diff --git a/features/wallpapercomponent/src/main/ets/com/ohos/view/component/wallpaper.ets b/features/wallpapercomponent/src/main/ets/com/ohos/view/component/wallpaper.ets index e6806c3..9d52c98 100644 --- a/features/wallpapercomponent/src/main/ets/com/ohos/view/component/wallpaper.ets +++ b/features/wallpapercomponent/src/main/ets/com/ohos/view/component/wallpaper.ets @@ -24,12 +24,17 @@ export default struct Wallpaper { @State mViewModel: ViewModel = new ViewModel() aboutToAppear() { - Log.showInfo(TAG, `aboutToAppear`) + Log.showInfo(TAG, "aboutToAppear") this.mViewModel.ViewModelInit() } + aboutToDisappear() { + Log.showInfo(TAG, "aboutToDisappear") + this.mViewModel.ViewModelDestroy() + } + build() { - Image(this.mViewModel.screenlockWallpaper) + Image(this.mViewModel.getWallpaperData()) .width(Constants.FULL_CONTAINER_WIDTH) .height(Constants.FULL_CONTAINER_HEIGHT) .objectFit(ImageFit.Cover) diff --git a/features/wallpapercomponent/src/main/ets/com/ohos/vm/wallpaperViewModel.ts b/features/wallpapercomponent/src/main/ets/com/ohos/vm/wallpaperViewModel.ts index 54ca48b..ee410b2 100644 --- a/features/wallpapercomponent/src/main/ets/com/ohos/vm/wallpaperViewModel.ts +++ b/features/wallpapercomponent/src/main/ets/com/ohos/vm/wallpaperViewModel.ts @@ -13,19 +13,29 @@ * limitations under the License. */ +import image from '@ohos.multimedia.image'; import WallpaperMar from '@ohos.wallpaper' import Log from '../../../../../../../../common/src/main/ets/default/Log' const TAG = 'ScreenLock-WallpaperViewModel' export default class WallpaperViewModel { - screenlockWallpaper: string = '' + private wallpaperData: image.PixelMap = undefined - ViewModelInit(): void{ - Log.showDebug(TAG, 'ViewModelInit'); + ViewModelInit(): void { + Log.showDebug(TAG, "ViewModelInit"); this.getScreenLockWallpaper() } + ViewModelDestroy(): void { + Log.showDebug(TAG, "ViewModelDestroy"); + this.freeScreenLockWallpaper(); + } + + getWallpaperData() { + return this.wallpaperData; + } + private getScreenLockWallpaper() { Log.showInfo(TAG, 'getScreenLockWallpaper'); WallpaperMar.getPixelMap(WallpaperMar.WallpaperType.WALLPAPER_LOCKSCREEN, (error, data) => { @@ -33,8 +43,20 @@ export default class WallpaperViewModel { Log.showError(TAG, 'getScreenLockWallpaper error:' + JSON.stringify(error)); } else { Log.showDebug(TAG, 'getScreenLockWallpaper data:' + JSON.stringify(data)); - this.screenlockWallpaper = data + this.wallpaperData = data } }) } + + private freeScreenLockWallpaper() { + Log.showInfo(TAG, 'free ScreenLockWallpaper'); + if (typeof this.wallpaperData === 'undefined' || this.wallpaperData == null) { + return; + } + this.wallpaperData.release().then(() => { + Log.showDebug(TAG, 'release succeed'); + }).catch((err) => { + Log.showDebug(TAG, `release failed ${err}`); + }) + } } diff --git a/product/pc/src/main/ets/pages/slidescreenlock.ets b/product/pc/src/main/ets/pages/slidescreenlock.ets index 44e688b..879ceeb 100644 --- a/product/pc/src/main/ets/pages/slidescreenlock.ets +++ b/product/pc/src/main/ets/pages/slidescreenlock.ets @@ -13,7 +13,6 @@ * limitations under the License. */ -import Trace from '../../../../../../common/src/main/ets/default/Trace' import Log from '../../../../../../common/src/main/ets/default/Log' import Accounts from '../../../../../../features/screenlock/src/main/ets/com/ohos/view/component/accounts' import DateTime from '../../../../../../features/datetimecomponent/src/main/ets/com/ohos/view/component/dateTime' @@ -34,6 +33,7 @@ export default struct SlideScreenlock { @StorageLink('batteryCharging') @Watch('onCharging') batteryCharging: boolean = false @Prop @Watch("onStatusChange") pageStatus: number @StorageLink('deviceStatus') @Watch('onDeviceStatusChange') deviceStatus: string = "" + @StorageLink('isWallpaperShow') isWallpaperShow: boolean = true private mHeightPx : number = 44 aboutToAppear() { @@ -56,7 +56,9 @@ export default struct SlideScreenlock { build() { Stack({ alignContent: Alignment.Bottom }) { Column() { - Wallpaper() + if (this.isWallpaperShow === true) { + Wallpaper() + } } .width(Constants.FULL_CONTAINER_WIDTH) .height(Constants.FULL_CONTAINER_HEIGHT)