Skip to content

Commit ae1a735

Browse files
authored
V4 oh (#20891)
* ohos code optimization Signed-off-by: 冰冰冰 <[email protected]> * Change the ts file to ets; API15 upgrade; Optimization of safe zones Signed-off-by: 冰冰冰 <[email protected]> * Keyboard optimization Signed-off-by: 冰冰冰 <[email protected]> * mainability optimization Signed-off-by: 冰冰冰 <[email protected]> --------- Signed-off-by: 冰冰冰 <[email protected]>
1 parent d37e6c9 commit ae1a735

File tree

54 files changed

+532
-385
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+532
-385
lines changed

cocos/platform/ohos/CCGLViewImpl-ohos.cpp

+19-100
Original file line numberDiff line numberDiff line change
@@ -110,110 +110,29 @@ void GLViewImpl::setIMEKeyboardState(bool bOpen) {
110110
}
111111

112112
Rect GLViewImpl::getSafeAreaRect() const {
113-
Rect safeAreaRect = GLView::getSafeAreaRect();
114-
float deviceAspectRatio = 0;
115-
if(safeAreaRect.size.height > safeAreaRect.size.width) {
116-
deviceAspectRatio = safeAreaRect.size.height / safeAreaRect.size.width;
117-
} else {
118-
deviceAspectRatio = safeAreaRect.size.width / safeAreaRect.size.height;
119-
}
120-
121-
float marginX = DEFAULT_MARGIN_OHOS / _scaleX;
122-
float marginY = DEFAULT_MARGIN_OHOS / _scaleY;
123-
bool isScreenRound;
124-
if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.isRoundScreen")) {
125-
isScreenRound = function->Invoke<bool>();
113+
Rect safeAreaRect1;
114+
int left;
115+
if (auto getSafeAreaLeft = aki::JSBind::GetJSFunction("DeviceUtils.getSafeAreaLeft")) {
116+
left = getSafeAreaLeft->Invoke<int>();
126117
}
127-
bool hasSoftKeys;
128-
if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.hasSoftKeys")) {
129-
hasSoftKeys = function->Invoke<bool>();
118+
int top;
119+
if (auto getSafeAreaTop = aki::JSBind::GetJSFunction("DeviceUtils.getSafeAreaTop")) {
120+
top = getSafeAreaTop->Invoke<int>();
130121
}
131-
bool isCutoutEnabled;
132-
if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.isCutoutEnable")) {
133-
isCutoutEnabled = function->Invoke<bool>();
134-
}
135-
136-
if(isScreenRound) {
137-
// edge screen
138-
if(safeAreaRect.size.width < safeAreaRect.size.height) {
139-
safeAreaRect.origin.y += marginY * 2.f;
140-
safeAreaRect.size.height -= (marginY * 2.f);
141-
142-
safeAreaRect.origin.x += marginX;
143-
safeAreaRect.size.width -= (marginX * 2.f);
144-
} else {
145-
safeAreaRect.origin.y += marginY;
146-
safeAreaRect.size.height -= (marginY * 2.f);
147-
148-
// landscape: no changes with X-coords
149-
}
150-
} else if (deviceAspectRatio >= WIDE_SCREEN_ASPECT_RATIO_OHOS) {
151-
// almost all devices on the market have round corners
152-
float bottomMarginIfPortrait = 0;
153-
if(hasSoftKeys) {
154-
bottomMarginIfPortrait = marginY * 2.f;
155-
}
156-
157-
if(safeAreaRect.size.width < safeAreaRect.size.height) {
158-
// portrait: double margin space if device has soft menu
159-
safeAreaRect.origin.y += bottomMarginIfPortrait;
160-
safeAreaRect.size.height -= (bottomMarginIfPortrait + marginY);
161-
} else {
162-
// landscape: ignore double margin at the bottom in any cases
163-
// prepare signle margin for round corners
164-
safeAreaRect.origin.y += marginY;
165-
safeAreaRect.size.height -= (marginY * 2.f);
166-
}
167-
} else {
168-
if(hasSoftKeys && (safeAreaRect.size.width < safeAreaRect.size.height)) {
169-
// portrait: preserve only for soft system menu
170-
safeAreaRect.origin.y += marginY * 2.f;
171-
safeAreaRect.size.height -= (marginY * 2.f);
172-
}
122+
int width;
123+
if (auto getSafeAreaWidth = aki::JSBind::GetJSFunction("DeviceUtils.getSafeAreaWidth")) {
124+
width = getSafeAreaWidth->Invoke<int>();
173125
}
174-
175-
if (isCutoutEnabled) {
176-
// screen with enabled cutout area
177-
178-
int orientation;
179-
if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.getOrientation")) {
180-
orientation = function->Invoke<int>();
181-
}
182-
183-
if(static_cast<int>(GLViewImpl::Orientation::PORTRAIT) == orientation) {
184-
int result;
185-
if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.getCutoutHeight")) {
186-
result = function->Invoke<int>();
187-
}
188-
double height = result / _scaleY;
189-
safeAreaRect.origin.y += height;
190-
safeAreaRect.size.height -= height;
191-
} else if(static_cast<int>(GLViewImpl::Orientation::PORTRAIT_INVERTED) == orientation) {
192-
int result;
193-
if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.getCutoutHeight")) {
194-
result = function->Invoke<int>();
195-
}
196-
double height = result / _scaleY;
197-
safeAreaRect.size.height -= height;
198-
} else if(static_cast<int>(GLViewImpl::Orientation::LANDSCAPE) == orientation) {
199-
int result;
200-
if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.getCutoutWidth")) {
201-
result = function->Invoke<int>();
202-
}
203-
double width = result / _scaleX;
204-
safeAreaRect.size.width -= width;
205-
} else if(static_cast<int>(GLViewImpl::Orientation::LANDSCAPE_INVERTED) == orientation) {
206-
int result;
207-
if (auto function = aki::JSBind::GetJSFunction("DeviceUtils.getCutoutWidth")) {
208-
result = function->Invoke<int>();
209-
}
210-
double width = result / _scaleX;
211-
safeAreaRect.origin.x += width;
212-
safeAreaRect.size.width -= width;
213-
}
126+
int height;
127+
if (auto getSafeAreaHeight = aki::JSBind::GetJSFunction("DeviceUtils.getSafeAreaHeight")) {
128+
height = getSafeAreaHeight->Invoke<int>();
214129
}
215-
216-
return safeAreaRect;
130+
safeAreaRect1.origin.x = left / _scaleX;
131+
safeAreaRect1.origin.y = top / _scaleY;
132+
safeAreaRect1.size.width = width / _scaleX;
133+
safeAreaRect1.size.height = height / _scaleX;
134+
OHOS_LOGD("GLViewImpl getsafeAreaRect1, x:%{public}f, y:%{public}f, width:%{public}f, height:%{public}f", safeAreaRect1.origin.x, safeAreaRect1.origin.y, safeAreaRect1.size.width, safeAreaRect1.size.height);
135+
return safeAreaRect1;
217136
}
218137
NS_CC_END
219138

cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ napi_value Js_Cocos2dxHelper::initJsCocos2dxHelper(napi_env env, napi_callback_i
1313
* If you have more information that can be obtained asynchronously, add it here.
1414
*/
1515
napi_value Js_Cocos2dxHelper::initAsyncInfo(napi_env env, napi_callback_info info) {
16-
if (auto initScreenInfo = aki::JSBind::GetJSFunction("DeviceUtils.initScreenInfo")) {
17-
initScreenInfo->Invoke<void>();
18-
}
1916
return nullptr;
2017
}
2118

cocos/platform/ohos/napi/render/plugin_render.cpp

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <ace/xcomponent/native_xcomponent_key_event.h>
12
#include <stdint.h>
23
#include <unistd.h>
34

@@ -32,7 +33,7 @@ float mousePositionY = -1;
3233
bool isMouseLeftActive = false;
3334
double scrollDistance = 0;
3435

35-
std::unordered_map<int, cocos2d::EventKeyboard::KeyCode> ohKeyMap = {
36+
const std::unordered_map<OH_NativeXComponent_KeyCode, cocos2d::EventKeyboard::KeyCode> ohKeyMap = {
3637
{KEY_ESCAPE, cocos2d::EventKeyboard::KeyCode::KEY_ESCAPE},
3738
{KEY_GRAVE, cocos2d::EventKeyboard::KeyCode::KEY_GRAVE},
3839
{KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS},
@@ -62,7 +63,23 @@ std::unordered_map<int, cocos2d::EventKeyboard::KeyCode> ohKeyMap = {
6263
{KEY_DPAD_UP, cocos2d::EventKeyboard::KeyCode::KEY_DPAD_UP},
6364
{KEY_SYSRQ, cocos2d::EventKeyboard::KeyCode::KEY_PRINT},
6465
{KEY_INSERT, cocos2d::EventKeyboard::KeyCode::KEY_INSERT},
65-
{KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE}
66+
{KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE},
67+
{KEY_SCROLL_LOCK, cocos2d::EventKeyboard::KeyCode::KEY_SCROLL_LOCK},
68+
{KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS},
69+
{KEY_AT, cocos2d::EventKeyboard::KeyCode::KEY_AT},
70+
{KEY_PLUS, cocos2d::EventKeyboard::KeyCode::KEY_PLUS},
71+
{KEY_MENU, cocos2d::EventKeyboard::KeyCode::KEY_MENU},
72+
{KEY_BREAK, cocos2d::EventKeyboard::KeyCode::KEY_PAUSE},
73+
{KEY_MOVE_HOME, cocos2d::EventKeyboard::KeyCode::KEY_HOME},
74+
{KEY_MOVE_END, cocos2d::EventKeyboard::KeyCode::KEY_END},
75+
{KEY_PAGE_UP, cocos2d::EventKeyboard::KeyCode::KEY_PG_UP},
76+
{KEY_PAGE_DOWN, cocos2d::EventKeyboard::KeyCode::KEY_PG_DOWN},
77+
{KEY_NUMPAD_ADD, cocos2d::EventKeyboard::KeyCode::KEY_KP_PLUS},
78+
{KEY_NUMPAD_SUBTRACT, cocos2d::EventKeyboard::KeyCode::KEY_KP_MINUS},
79+
{KEY_NUMPAD_MULTIPLY, cocos2d::EventKeyboard::KeyCode::KEY_KP_MULTIPLY},
80+
{KEY_NUMPAD_DIVIDE, cocos2d::EventKeyboard::KeyCode::KEY_KP_DIVIDE},
81+
{KEY_NUMPAD_ENTER, cocos2d::EventKeyboard::KeyCode::KEY_KP_ENTER}
82+
6683
};
6784

6885
cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyCode ohKeyCode)
@@ -82,8 +99,9 @@ cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyC
8299
if (ohKeyCode >= KEY_A && ohKeyCode <= KEY_Z) {
83100
// A - Z
84101
return cocos2d::EventKeyboard::KeyCode(int(cocos2d::EventKeyboard::KeyCode::KEY_A) + (ohKeyCode - KEY_A));
85-
}
86-
return cocos2d::EventKeyboard::KeyCode(ohKeyCode);
102+
}
103+
OHOS_LOGW("Unmapped OH key code: %d", ohKeyCode);
104+
return cocos2d::EventKeyboard::KeyCode::KEY_NONE;
87105
}
88106

89107
void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window)

tests/cpp-tests/proj.ohos/AppScope/app.json5

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"app": {
3-
"bundleName": "ohos.cocos4.0.cpp.tests",
3+
"bundleName": "ohos.cocosV4.cpp.tests",
44
"vendor": "example",
55
"versionCode": 1000000,
66
"versionName": "1.0.0",

tests/cpp-tests/proj.ohos/AppScope/resources/base/element/string.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"string": [
33
{
44
"name": "app_name",
5-
"value": "4.0_cpp_tests"
5+
"value": "v4_cpp_tests"
66
}
77
]
88
}

tests/cpp-tests/proj.ohos/build-profile.json5

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"name": "default",
66
"signingConfig": "default",
7-
"compatibleSdkVersion": "5.0.0(12)",
7+
"compatibleSdkVersion": "5.0.3(15)",
88
"runtimeOS": "HarmonyOS"
99
}
1010
],
@@ -13,13 +13,13 @@
1313
"name": "default",
1414
"type": "HarmonyOS",
1515
"material": {
16-
"certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_cVH5E_jhivWpH1G1xxY8OLXUTNTmD8SODpqOIrtme5E=.cer",
17-
"storePassword": "0000001B5CB8776478C1411B593CFD4F39489A3E411CE13DFDF53328C6F724DA172111B95C7E99C2272438",
16+
"certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_wt6HgzvadMBtjH4MDdobSA_pas_3leTTVGu_d5JlEos=.cer",
1817
"keyAlias": "debugKey",
19-
"keyPassword": "0000001BB51E522040B6BCCB099EEB3736F242F7E814CE1C7E67420E0706EFFA080DFFBE827CA906DD1463",
20-
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_cVH5E_jhivWpH1G1xxY8OLXUTNTmD8SODpqOIrtme5E=.p7b",
18+
"keyPassword": "0000001AEED126A304DC1E289FD52B1F592744C4C3550D453E006CCED8BC763E8B1A5E8B6517106267C4",
19+
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_wt6HgzvadMBtjH4MDdobSA_pas_3leTTVGu_d5JlEos=.p7b",
2120
"signAlg": "SHA256withECDSA",
22-
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_cVH5E_jhivWpH1G1xxY8OLXUTNTmD8SODpqOIrtme5E=.p12"
21+
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_wt6HgzvadMBtjH4MDdobSA_pas_3leTTVGu_d5JlEos=.p12",
22+
"storePassword": "0000001A0FAFA8D8D21BFB7BE7FE4B7B677FC3B094772078195F4309E978FC88E4D6889ED9DCD22441A7"
2323
}
2424
}
2525
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import window from '@ohos.window';
2+
import UIAbility from '@ohos.app.ability.UIAbility';
3+
import nativeRender from "libnativerender.so";
4+
import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities"
5+
import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities"
6+
import { BusinessError } from '@kit.BasicServicesKit';
7+
import Want from '@ohos.app.ability.Want';
8+
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
9+
10+
const nativeAppLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE);
11+
const rawFileUtils: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.RAW_FILE_UTILS);
12+
13+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
14+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
15+
export default class MainAbility extends UIAbility {
16+
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
17+
nativeAppLifecycle.onCreate();
18+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, this.context);
19+
console.info('[LIFECYCLE-App] onCreate')
20+
}
21+
22+
onDestroy() {
23+
nativeAppLifecycle.onDestroy();
24+
console.info('[LIFECYCLE-App] onDestroy')
25+
}
26+
27+
onWindowStageCreate(windowStage: window.WindowStage): void {
28+
// Main window is created, set main page for this ability
29+
windowStage.loadContent('pages/Index', (err:BusinessError, data) => {
30+
if (err.code) {
31+
return;
32+
}
33+
rawFileUtils.nativeResourceManagerInit(this.context.resourceManager);
34+
rawFileUtils.writablePathInit(this.context.filesDir);
35+
});
36+
37+
windowStage.getMainWindow().then((windowIns: window.Window) => {
38+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW, windowIns);
39+
// Set whether to display the status bar and navigation bar. If they are not displayed, [] is displayed.
40+
let systemBarPromise = windowIns.setWindowSystemBarEnable([]);
41+
// Whether the window layout is displayed in full screen mode
42+
let fullScreenPromise = windowIns.setWindowLayoutFullScreen(true);
43+
// Sets whether the screen is always on.
44+
let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true);
45+
Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => {
46+
console.info('Succeeded in setting the window');
47+
}).catch((err: BusinessError) => {
48+
console.error('Failed to set the window, cause ' + JSON.stringify(err));
49+
});
50+
51+
try {
52+
DeviceUtils.calculateSafeArea(windowIns.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT), windowIns.getWindowProperties().windowRect);
53+
windowIns.on('avoidAreaChange', (data) => {
54+
console.info('getSafeAreaRect Succeeded in enabling the listener for system avoid area changes. type:' +
55+
JSON.stringify(data.type) + ', area: ' + JSON.stringify(data.area));
56+
57+
if(data.type == window.AvoidAreaType.TYPE_SYSTEM_GESTURE || data.type == window.AvoidAreaType.TYPE_KEYBOARD) {
58+
return;
59+
}
60+
61+
let mainWindow: window.Window = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW);
62+
DeviceUtils.calculateSafeArea(data.area, mainWindow.getWindowProperties().windowRect);
63+
});
64+
} catch (exception) {
65+
console.error(`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}`);
66+
}
67+
})
68+
69+
windowStage.on("windowStageEvent", (data:window.WindowStageEventType) => {
70+
let stageEventType: window.WindowStageEventType = data;
71+
switch (stageEventType) {
72+
case window.WindowStageEventType.RESUMED:
73+
console.info('[LIFECYCLE-App] onShow_RESUMED')
74+
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){
75+
nativeAppLifecycle.onShow();
76+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false);
77+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
78+
}
79+
break;
80+
case window.WindowStageEventType.PAUSED:
81+
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){
82+
console.info('[LIFECYCLE-App] onHide_PAUSED')
83+
nativeAppLifecycle.onHide();
84+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false);
85+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
86+
}
87+
break;
88+
default:
89+
break;
90+
}
91+
});
92+
}
93+
94+
onWindowStageDestroy() {
95+
// Main window is destroyed, release UI related resources
96+
}
97+
98+
onForeground() {
99+
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){
100+
// Ability has brought to foreground
101+
console.info('[LIFECYCLE-App] onShow')
102+
nativeAppLifecycle.onShow();
103+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false);
104+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
105+
}
106+
}
107+
108+
onBackground() {
109+
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){
110+
// Ability has back to background
111+
console.info('[LIFECYCLE-App] onHide')
112+
nativeAppLifecycle.onHide();
113+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false);
114+
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
115+
}
116+
}
117+
};

0 commit comments

Comments
 (0)