From 1fe0238d87fce2bd47f433cfcfa5550b581898fa Mon Sep 17 00:00:00 2001 From: JackLee <809262979@qq.com> Date: Wed, 18 Mar 2026 12:38:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=B8=80=E4=BA=9B=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E9=97=B4=E9=9A=94,=E5=B0=BA=E5=AF=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/common/ContextUtils.ets | 31 ---------- entry/src/main/ets/common/UIContext.ets | 57 ----------------- entry/src/main/ets/common/Utils.ets | 60 ------------------ entry/src/main/ets/common/WindowUtils.ets | 62 ------------------- .../main/ets/entryability/EntryAbility.ets | 12 +--- .../src/main/ets/pages/CustomStyle/Button.ets | 28 ++++----- entry/src/main/ets/pages/CustomStyle/Menu.ets | 28 ++++----- entry/src/main/ets/pages/Index.ets | 8 +-- .../ets/pages/TitleTabLayout/TitleTab.ets | 6 +- 9 files changed, 32 insertions(+), 260 deletions(-) delete mode 100644 entry/src/main/ets/common/ContextUtils.ets delete mode 100644 entry/src/main/ets/common/UIContext.ets delete mode 100644 entry/src/main/ets/common/Utils.ets delete mode 100644 entry/src/main/ets/common/WindowUtils.ets diff --git a/entry/src/main/ets/common/ContextUtils.ets b/entry/src/main/ets/common/ContextUtils.ets deleted file mode 100644 index 6c466d65..00000000 --- a/entry/src/main/ets/common/ContextUtils.ets +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2025 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. - */ -// [Start Common_ContextUtils] -export class ContextUtils { - public static context: Context | undefined; - - static setContext(context: Context): void { - ContextUtils.context = context; - } - - static getContext(uiContext?: UIContext): Context | undefined { - if (uiContext) { - return uiContext.getHostContext(); - } - - return ContextUtils.context; - } -} -// [End Common_ContextUtils] \ No newline at end of file diff --git a/entry/src/main/ets/common/UIContext.ets b/entry/src/main/ets/common/UIContext.ets deleted file mode 100644 index 397907d6..00000000 --- a/entry/src/main/ets/common/UIContext.ets +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2025 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. - */ -// [Start Common_PixelUtils] -import { hilog } from '@kit.PerformanceAnalysisKit'; -import { display } from '@kit.ArkUI'; - -const DOMAIN = 0x0000; - -export class PixelUtils { - public static uiContext: UIContext | undefined; - - static setUIContext(uiContext: UIContext): void { - PixelUtils.uiContext = uiContext; - } - - static vp2px(vpValue: number, uiContext?: UIContext): number | undefined { - let _uiContext = uiContext ?? PixelUtils.uiContext; - if (!_uiContext || !_uiContext.isAvailable()) { - let displayClass = display.getDefaultDisplaySync(); - let density = displayClass.densityPixels; - return vpValue * density; - } - return _uiContext.vp2px(vpValue) - } - - static fp2px(fpValue: number, uiContext?: UIContext): number | undefined { - let _uiContext = uiContext ?? PixelUtils.uiContext; - if (!_uiContext || !_uiContext.isAvailable()) { - hilog.error(DOMAIN, 'testTag', `Can't get UIContext`); - return undefined; - } - return _uiContext.fp2px(fpValue) - } - - lpx2px(lpxValue: number, uiContext?: UIContext): number | undefined { - let _uiContext = uiContext ?? PixelUtils.uiContext; - if (!_uiContext || !_uiContext.isAvailable()) { - hilog.error(DOMAIN, 'testTag', `Can't get UIContext`); - return undefined; - } - return _uiContext.lpx2px(lpxValue) - } -} - -// [End Common_PixelUtils] \ No newline at end of file diff --git a/entry/src/main/ets/common/Utils.ets b/entry/src/main/ets/common/Utils.ets deleted file mode 100644 index 3e1f1729..00000000 --- a/entry/src/main/ets/common/Utils.ets +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2025 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. - */ -// [Start Common_Utils] -// common/Utils.ets -import { hilog } from '@kit.PerformanceAnalysisKit'; - -const DOMAIN = 0x0000; - -export class PixelUtil { - static uiContext: UIContext | undefined; - - static setUIContext(uiContext: UIContext): void { - PixelUtil.uiContext = uiContext; - } - - static removeUIContext(): void { - PixelUtil.uiContext = undefined; - } - - static vp2px(vpValue: number, uiContext?: UIContext): number | undefined { - let _uiContext = uiContext ?? PixelUtil.uiContext; - if (!_uiContext || !_uiContext.isAvailable()) { - hilog.error(DOMAIN, 'testTag', `Can't get UIContext`); - return undefined; - } - return _uiContext.vp2px(vpValue) - } - - static fp2px(fpValue: number, uiContext?: UIContext): number | undefined { - let _uiContext = uiContext ?? PixelUtil.uiContext; - if (!_uiContext || !_uiContext.isAvailable()) { - hilog.error(DOMAIN, 'testTag', `Can't get UIContext`); - return undefined; - } - return _uiContext.fp2px(fpValue) - } - - lpx2px(lpxValue: number, uiContext?: UIContext): number | undefined { - let _uiContext = uiContext ?? PixelUtil.uiContext; - if (!_uiContext || !_uiContext.isAvailable()) { - hilog.error(DOMAIN, 'testTag', `Can't get UIContext`); - return undefined; - } - return _uiContext.lpx2px(lpxValue) - } -} - -// [End Common_Utils] \ No newline at end of file diff --git a/entry/src/main/ets/common/WindowUtils.ets b/entry/src/main/ets/common/WindowUtils.ets deleted file mode 100644 index 296a3962..00000000 --- a/entry/src/main/ets/common/WindowUtils.ets +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2025 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. - */ -// [Start Common_WindowUtils] -// common/WindowUtils.ets -import { display, window } from '@kit.ArkUI'; -import { hilog } from '@kit.PerformanceAnalysisKit'; - -const DOMAIN = 0x0000; - -export class WindowUIContextUtils { - public static activeUIContext: UIContext | undefined; - - static registerWindowCallback(windowClass: window.Window): void { - try { - windowClass.on('windowEvent', (event: window.WindowEventType) => { - if (event === window.WindowEventType.WINDOW_ACTIVE) { - try { - let uiContext = windowClass.getUIContext(); - WindowUIContextUtils.activeUIContext = uiContext; - } catch (exception) { - hilog.error(DOMAIN, 'testTag', `Can't get UIContext, ${exception}`); - } - } - }); - } catch (exception) { - console.error(`Failed to unregister callback. Cause: ${exception}`); - } - } - - static unregisterWindowCallback(windowClass: window.Window): void { - windowClass.off('windowEvent'); - } - - static setActiveUIContext(uiContext: UIContext): void { - WindowUIContextUtils.activeUIContext = uiContext; - } - - static vp2px(vpValue: number, uiContext?: UIContext): number { - let _uiContext = uiContext ?? WindowUIContextUtils.activeUIContext; - if (!_uiContext || !_uiContext.isAvailable()) { - let displayClass = display.getDefaultDisplaySync(); - let density = displayClass.densityPixels; - return vpValue * density; - } - - return _uiContext.vp2px(vpValue); - } -} - -// [End Common_WindowUtils] \ No newline at end of file diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index ce9a9516..3fbde08a 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -1,8 +1,6 @@ import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window,display,AppStorageV2} from '@kit.ArkUI'; -import { WindowUIContextUtils } from '../common/WindowUtils'; -import { PixelUtils } from '../common/UIContext'; const DOMAIN = 0x0000; @@ -24,8 +22,6 @@ export default class EntryAbility extends UIAbility { // Main window is created, set main page for this ability hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); let window = windowStage.getMainWindowSync(); - // 注册主窗的回调。 - WindowUIContextUtils.registerWindowCallback(window); windowStage.loadContent('pages/Index', (err) => { if (err.code) { hilog.error(DOMAIN, 'Tag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); @@ -33,13 +29,7 @@ export default class EntryAbility extends UIAbility { } AppStorage.setOrCreate('windowStage', windowStage); let uiContext = window.getUIContext(); - PixelUtils.setUIContext(uiContext); - // 主窗获焦可能早于loadContent完成,需要在成功后设置保证有效。 - WindowUIContextUtils.setActiveUIContext(uiContext) - if (!uiContext) { - hilog.error(DOMAIN, 'testTag', `Can't get UIContext`); - return; - } + let screenWidth:number=display.getDefaultDisplaySync().width; let screenHeight:number=display.getDefaultDisplaySync().height; //定义新的宽度和高度(单位:虚拟像素) diff --git a/entry/src/main/ets/pages/CustomStyle/Button.ets b/entry/src/main/ets/pages/CustomStyle/Button.ets index c91ef441..791312b8 100644 --- a/entry/src/main/ets/pages/CustomStyle/Button.ets +++ b/entry/src/main/ets/pages/CustomStyle/Button.ets @@ -11,36 +11,36 @@ import { TitleData } from '../LayoutInterface/Layout/TabContent'; export struct EventBtn { @Param eventBtn: TitleButton | undefined = undefined; build() { - Column({ space: 2 }) { + Column({ space: 0 }) { if (this.eventBtn != undefined) { Button() - .width('35vp') - .height('35vp') + .width(35) + .height(35) .backgroundImage($r('app.media.' + this.eventBtn.eIcon)) - .backgroundImagePosition({ x: '10%', y: '10%' }) + .backgroundImagePosition({ x: '5%', y: '5%' }) .backgroundColor(Color.Transparent) .backgroundImageSize({ - width: '80%', // 图片宽度占满按钮 - height: '80%' // 图片高度占满按钮 + width: '90%', // 图片宽度占满按钮 + height: '90%' // 图片高度占满按钮 }) .onClick(()=>{ ExecuteCommand(this.eventBtn as TitleButton); }) Text() - .width('50vp') - .height('7vp') + .width(50) + .height(7) .backgroundColor(Color.Transparent) Text(this.eventBtn.eName) - .fontSize('8fp') - .width('50vp') - .height('10vp') + .fontSize(10) + .width(50) + .height(10) .textAlign(TextAlign.Center) .backgroundColor(Color.Transparent) } } - .height('50vp') - .width('50vp') - .padding('1vp') + .height(50) + .width(50) + .padding(1) } } diff --git a/entry/src/main/ets/pages/CustomStyle/Menu.ets b/entry/src/main/ets/pages/CustomStyle/Menu.ets index c1f01052..fde54387 100644 --- a/entry/src/main/ets/pages/CustomStyle/Menu.ets +++ b/entry/src/main/ets/pages/CustomStyle/Menu.ets @@ -79,37 +79,37 @@ export struct MenuBtn { } build() { - Column({ space: 2 }) { + Column({ space: 0 }) { if (this.menuBtn != undefined) { Button() .bindMenu(this.EventMenu) - .width('35vp') - .height('35vp') + .width(35) + .height(35) .backgroundImage($r('app.media.' + this.menuBtn[this.curtIndex].eIcon)) - .backgroundImagePosition({ x: '10%', y: '10%' }) + .backgroundImagePosition({ x: '5%', y: '5%' }) .backgroundColor(Color.Transparent) .backgroundImageSize({ - width: '80%', // 图片宽度占满按钮 - height: '80%' // 图片高度占满按钮 + width: '90%', // 图片宽度占满按钮 + height: '90%' // 图片高度占满按钮 }) Button() .bindMenu(this.EventMenu) - .width('50vp') - .height('7vp') + .width(50) + .height(7) .backgroundImage($r('app.media.base_chevron_down')) .backgroundImagePosition({ x: '35%', y: '-50%' }) .backgroundColor(Color.Transparent) Text(this.menuBtn[this.curtIndex].eName) - .fontSize('8fp') - .width('50vp') - .height('10vp') + .fontSize(10) + .width(50) + .height(10) .textAlign(TextAlign.Center) } } - .height('50vp') - .width('50vp') - .padding('1vp') + .height(50) + .width(50) + .padding(1) } } diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index ed72aed4..fab1a726 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -6,12 +6,6 @@ import {ModelViewTab} from './modelViewTab' import {TitleColumnSub} from './TitleTabLayout/TitleColumnSub' const DOMAIN = 0x0000; -let displayClass: display.Display | null = null; -try { - displayClass = display.getDefaultDisplaySync(); -} catch (exception) { - console.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); -} @Entry @Component struct Index { @@ -43,7 +37,7 @@ struct Index { .padding(1) Column(){ Text('状态栏').height('100%').width('100%') - }.height('5%').borderWidth('1vp') + }.height('5%').borderWidth(1) }.width('100%') .height('100%') } diff --git a/entry/src/main/ets/pages/TitleTabLayout/TitleTab.ets b/entry/src/main/ets/pages/TitleTabLayout/TitleTab.ets index c292d00b..ce3c0ab6 100644 --- a/entry/src/main/ets/pages/TitleTabLayout/TitleTab.ets +++ b/entry/src/main/ets/pages/TitleTabLayout/TitleTab.ets @@ -48,8 +48,7 @@ export struct TitleTab { Row({space:0}) { Button(TitleData.mFileModel.cmName) .height(25) - .width(60) - .padding(5) + .width(100) .bindMenu(this.FileMenu(TitleData.mFileModel.cmEvents as Array)) .type(ButtonType.Normal) .backgroundColor(Color.Brown) @@ -57,8 +56,7 @@ export struct TitleTab { Button(item.cmName) .fontWeight(index === this.titleBarFocusIndex ? FontWeight.Bold : FontWeight.Normal) .height(25) - .width(60) - .padding(5) + .width(100) .type(ButtonType.Normal) .backgroundColor(index === this.titleBarFocusIndex ? Color.Blue : Color.Brown) .onClick(() => {