From e0e4574904f9d7dba7bf698ae458c166188096b8 Mon Sep 17 00:00:00 2001 From: xdongs Date: Mon, 16 Jun 2025 10:47:09 +0800 Subject: [PATCH] =?UTF-8?q?pc=E9=80=9A=E7=9F=A5=E6=8E=88=E6=9D=83=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=E8=B6=85=E5=87=BA=E5=B1=8F=E5=B9=95=E5=9C=BA=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xdongs --- .../AppScope/app.json | 4 +- .../NotificationServiceExtAbility.ts | 38 ++++++++++++++-- .../entry/src/main/ets/common/constant.ets | 5 +++ .../src/main/ets/common/displayUtils.ets | 43 +++++++++++++++++++ .../entry/src/main/ets/common/utils.ets | 38 ++++++++++++++++ .../entry/src/main/ets/pages/emptyPage.ets | 7 +++ .../main/ets/pages/pcNotificationDialog.ets | 27 ++++++++++-- 7 files changed, 152 insertions(+), 10 deletions(-) create mode 100644 services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/displayUtils.ets create mode 100644 services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/emptyPage.ets diff --git a/services/dialog_ui/enable_notification_dialog/AppScope/app.json b/services/dialog_ui/enable_notification_dialog/AppScope/app.json index 5fa80604f..cf14d1595 100644 --- a/services/dialog_ui/enable_notification_dialog/AppScope/app.json +++ b/services/dialog_ui/enable_notification_dialog/AppScope/app.json @@ -2,8 +2,8 @@ "app": { "bundleName": "com.ohos.notificationdialog", "vendor": "example", - "versionCode": 1000028, - "versionName": "1.2.8", + "versionCode": 1000029, + "versionName": "1.2.9", "icon": "$media:app_icon", "label": "$string:app_name", "distributedNotificationEnabled": true, diff --git a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts index dfe0f3845..856a123cf 100644 --- a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts +++ b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts @@ -26,7 +26,8 @@ import StartOptions from '@ohos.app.ability.StartOptions'; import configPolicy from '@ohos.configPolicy'; import fs from '@ohos.file.fs'; import Constants from '../common/constant'; - +import DisplayUtils from '../common/displayUtils'; +import { shouldMoveWindow} from '../common/utils'; const TAG = 'NotificationDialog_Service '; @@ -85,6 +86,8 @@ export class EnableNotificationDialog { static PC_DIALOG_PATH = 'pages/pcNotificationDialog'; static TV_DIALOG_PATH = 'pages/tvNotificationDialog'; static CAR_DIALOG_PATH = 'pages/carNotificationDialog'; + static EMPTY_PAGE_PATH = 'pages/emptyPage'; + static MASK_COLOR = '#33000000'; static TRANSPARANT_COLOR = '#00000000'; static SCENEBOARD_BUNDLE = 'com.ohos.sceneboard'; static SYSTEMUI_BUNDLE = 'com.ohos.systemui'; @@ -172,6 +175,7 @@ export class EnableNotificationDialog { }; let subWindow = await extensionWindow.createSubWindowWithOptions('subWindowForHost' + Date(), subWindowOpts); this.subWindow = subWindow; + let moveToCenter : boolean = false; if(isPcDevice) { let hasDisalogRectInfo = false; @@ -187,10 +191,28 @@ export class EnableNotificationDialog { await this.sleep(200); } if(hasDisalogRectInfo) { + // get display + let display = DisplayUtils.getCurWindowDisplay(subWindow); + if (display == undefined) { + throw new Error('Failed to getCurWindowDisplay'); + } let windowRect = extensionWindow.properties?.uiExtensionHostWindowProxyRect; console.info(TAG, `size : ${windowRect?.left} ${windowRect?.top} ${windowRect?.width} ${windowRect?.height}`); - await subWindow.moveWindowToGlobal(windowRect?.left, windowRect?.top); - await subWindow.resize(windowRect?.width, windowRect?.height); + + // need moveToCenter + moveToCenter = shouldMoveWindow(windowRect,display); + console.info(TAG,`moveToCenter = ${moveToCenter}`); + if (moveToCenter) { + AppStorage.setOrCreate('showInDisplayCenter', true); + let widht = Constants.MAX_DIALOG_WIDTH; + let height = Constants.PC_INIT_CONTENT_HEIGHT; + await subWindow.resize(vp2px(widht), vp2px(height)); + await subWindow.moveWindowToGlobal(display.width / 2 - vp2px(widht)/2, display.height / 2 - vp2px(height)/2); + } else { + await subWindow.moveWindowToGlobal(windowRect?.left, windowRect?.top); + await subWindow.resize(windowRect?.width, windowRect?.height); + } + hasDisalogRectInfo = false; } else { console.info(TAG,'waite send windwow info fail'); @@ -207,7 +229,9 @@ export class EnableNotificationDialog { } } try { - await subWindow.setFollowParentWindowLayoutEnabled(true); + if (!moveToCenter) { + await subWindow.setFollowParentWindowLayoutEnabled(true); + } } catch (err) { console.error(TAG, `setFollowParentWindowLayoutEnabled failed! ${err.code} ${err.message}`); } @@ -217,6 +241,12 @@ export class EnableNotificationDialog { } catch (err) { console.error(TAG, 'subWindow hideNonSystemFloatingWindows failed!'); } + + if (moveToCenter) { + await session.loadContent(EnableNotificationDialog.EMPTY_PAGE_PATH, this.storage); + await session.setWindowBackgroundColor(EnableNotificationDialog.MASK_COLOR); + } + await subWindow.setWindowBackgroundColor(EnableNotificationDialog.TRANSPARANT_COLOR); await subWindow.showWindow(); } else { diff --git a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/constant.ets b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/constant.ets index 144f0a9cb..abb2085b2 100644 --- a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/constant.ets +++ b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/constant.ets @@ -172,6 +172,8 @@ export default class Constants { public static PC_RDIUS_16 = 16; public static PC_RDIUS_8 = 8; + public static PC_DOCK_HEIGHT = 120; + public static PC_INIT_CONTENT_HEIGHT = 230; public static PC_OUTLINE_COLOR = '#20FFFFFF'; @@ -188,4 +190,7 @@ export default class Constants { // ccm config path public static CCM_CONFIG_PATH = "etc/notification/notification_config.json"; + + public static TAG = 'NotificationDialog_Service '; + } diff --git a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/displayUtils.ets b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/displayUtils.ets new file mode 100644 index 000000000..dfffe4551 --- /dev/null +++ b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/displayUtils.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 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 display from '@ohos.display'; +import window from '@ohos.window'; +import Constants from './constant'; + +export default class DisplayUtils { + private constructor() { + } + + /** + * @returns display.Display if success, undefined if failed + */ + public static getCurWindowDisplay(win: window.Window): display.Display | undefined { + try { + let displayId: number | undefined = win.getWindowProperties().displayId; + console.info(Constants.TAG, `get displayId by window, displayId=${displayId}.`); + if (displayId !== undefined) { + let currentDisplay = display.getDisplayByIdSync(displayId); + console.info(Constants.TAG, `getCurWindowDisplay success.`); + return currentDisplay; + } + return undefined; + } catch (err) { + console.info(Constants.TAG, `getCurWindowDisplay failed, code=${err?.code}, msg=${err?.message}.`); + return undefined; + } + } + +} \ No newline at end of file diff --git a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/utils.ets b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/utils.ets index 4c3974fcd..2b3a0748b 100644 --- a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/utils.ets +++ b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/common/utils.ets @@ -83,3 +83,41 @@ export function sourceToVp(source: Resource): number { export function getLimitFontSize(fontSizeScale: number, sourceFontSize: number): string { return `${sourceFontSize * fontSizeScale}vp`; } + +/** + * + * @param windowRect windowRect + * @param display display + * @returns true need removeWindow + */ + export function shouldMoveWindow(windowRect: window.Rect, display: display.Display): boolean { + try { + if (windowRect === undefined) { + console.error(TAG, `windowRect is null`); + return false; + } + console.info(TAG, `displayW=${display.width}, displayH=${display.height}.` ); + + if (windowRect.width >= display.width && windowRect.height >= display.height) { + console.info(TAG, `windowRect >= display, no need move.`); + return false; + } + let windowCenterX = windowRect.left + windowRect.width / 2; + let windowCenterY = windowRect.top + windowRect.height / 2; + let minX = windowRect.width / 2; + let maxX = display.width - windowRect.width / 2; + let minY = windowRect.height / 2; + let maxY = display.height - windowRect.height / 2 - Constants.PC_DOCK_HEIGHT; + + console.info(TAG, `windowCenterX=${windowCenterX}, windowCenterY=${windowCenterY}`); + console.info(TAG, `x range=[${minX}, ${maxX}], y range=[${minY}, ${maxY}]`); + let isXOk: boolean = windowCenterX >= minX && windowCenterX <= maxX; + let isYOk: boolean = windowCenterY >= minY && windowCenterY <= maxY; + let shouldMove = !(isXOk && isYOk); + console.info(TAG, `shouldMoveWindow, shouldMove=${shouldMove}`); + return shouldMove; + } catch (err) { + console.error(TAG, `check shouldMoveWindow failed, code=${err?.code}, msg=${err?.message}.`); + return false; + } + } diff --git a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/emptyPage.ets b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/emptyPage.ets new file mode 100644 index 000000000..6e07a121b --- /dev/null +++ b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/emptyPage.ets @@ -0,0 +1,7 @@ +@Entry +@Component +struct emptyPage { + build() { + Column().height('100%').width('100%').backgroundColor($r('sys.color.mask_fourth')); + } +} \ No newline at end of file diff --git a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/pcNotificationDialog.ets b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/pcNotificationDialog.ets index 6716459c6..0b8e39bd6 100644 --- a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/pcNotificationDialog.ets +++ b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/pcNotificationDialog.ets @@ -32,6 +32,7 @@ import { MeasureOptions } from '@ohos.measure'; import { MeasureUtils } from '@ohos.arkui.UIContext'; import common from '@ohos.app.ability.common'; import uiextension from '@ohos.arkui.uiExtension'; +import DisplayUtils from '../common/displayUtils'; const TAG = 'NotificationDialog_Service '; @@ -63,14 +64,18 @@ let storage = LocalStorage.getShared(); @Component struct NotificationDialogPage { @StorageLink('isUpdate') isUpdate: number = 0; + @StorageLink('showInDisplayCenter') showInDisplayCenter: boolean = false; privacyDialogController: CustomDialogController = new CustomDialogController({ - builder: PermissionDialog({ isUpdate: $isUpdate }), + builder: PermissionDialog({ + isUpdate : $isUpdate, + showInDisplayCenter : $showInDisplayCenter + }), autoCancel: false, alignment: DialogAlignment.Center, customStyle: true, cornerRadius: Constants.PC_RDIUS_16, - maskColor: $r('sys.color.ohos_id_color_mask_thin'), + maskColor: this.showInDisplayCenter ? '#00000000' : $r('sys.color.ohos_id_color_mask_thin'), onWillDismiss: (dismissDialogAction: DismissDialogAction) => { console.info(TAG, `dialog onWillDismiss reason= : ${JSON.stringify(dismissDialogAction.reason)}`); } @@ -92,6 +97,7 @@ struct PermissionDialog { @State naviHeight: number = 0; @State isBottomPopover: boolean = false; @StorageLink('clicked') clicked: boolean = false; + @StorageLink('showInDisplayCenter') showInDisplayCenter: boolean = false; @Link @Watch('updateOnPageShow') isUpdate: number; dialog?: EnableNotificationDialog; session?: UIExtensionContentSession; @@ -192,8 +198,19 @@ struct PermissionDialog { }) } } + .onSizeChange((oldValue: SizeOptions, newValue: SizeOptions) => { + if (this.showInDisplayCenter) { + console.error(TAG, `content onSizeChange : ${newValue.height}`); + let width = vp2px(newValue.width as number); + let height = vp2px(newValue.height as number); + let display = DisplayUtils.getCurWindowDisplay(this.dialog?.subWindow); + height = height > display.height * 0.9 ? display.height * 0.9 : height; + this.dialog?.subWindow?.resize(width, height); + this.dialog?.subWindow?.moveWindowTo(display.width / 2 - width/2, display.height / 2 - height/2); + } + }) } - .constraintSize({ maxHeight: '90%' }) + .constraintSize({ maxHeight: this.showInDisplayCenter ? '100%' : '90%'}) } .borderRadius(Constants.PC_RDIUS_16) .backgroundBlurStyle(BlurStyle.COMPONENT_ULTRA_THICK) @@ -214,7 +231,9 @@ struct PermissionDialog { this.dialog = storage.get('dialog') as EnableNotificationDialog; this.session = storage.get('session') as UIExtensionContentSession; this.updateAvoidWindow(); - this.updateSubWindowSize(); + if (this.showInDisplayCenter) { + this.updateSubWindowSize(); + } try { await this.updateStatus(); await this.dialog?.dialogOpenEvent(); -- Gitee