Skip to content

Commit

Permalink
Adapt arkts standard on OH
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyang57 authored Oct 31, 2023
1 parent 0770142 commit 0bf887a
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 132 deletions.
4 changes: 3 additions & 1 deletion templates/openharmony/entry/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
"description": "example description",
"main": "",
"version": "1.0.0",
"dependencies": {}
"dependencies": {
"libcocos.so": "file:./src/main/cpp/types/libcocos"
}
}
27 changes: 27 additions & 0 deletions templates/openharmony/entry/src/main/cpp/types/libcocos/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ContextType } from '../../../ets/common/Constants';

interface context {
onPageShow: () => void;
onPageHide: () => void;
workerInit: () => void;
postMessage: (msgType: string, msgData: string) => void;
postSyncMessage: (msgType: string, msgData: string) => Promise<boolean | string | number>;
setPostMessageFunction: (postMessage: (msgType: string, msgData: string) => void) => void;
setPostSyncMessageFunction: (postSyncMessage: (msgType: string, msgData: string) => void) => void;
nativeEngineInit: () => void;
nativeEngineStart: () => void;
onTextChange: (param: string) => void;
onComplete: (param: string) => void;
shouldStartLoading: (viewTag: number, url: string) => void;
finishLoading: (viewTag: number, url: string) => void;
failLoading: (viewTag: number, url: string) => void;
onBackPress: () => void;
onCreate: () => void;
onDestroy: () => void;
onShow: () => void;
onHide: () => void;
resourceManagerInit: (resourceManager: any) => void;
writablePathInit: (cacheDir: string) => void;
}

export const getContext: (type: ContextType) => context;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "libcocos.so",
"types": "./index.d.ts",
"version": "",
"description": "Please describe the basic information."
}
18 changes: 9 additions & 9 deletions templates/openharmony/entry/src/main/ets/cocos/WorkerManager.ets
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@ import {Constants} from '../common/Constants'

export class WorkerManager {
private static instance: WorkerManager;
private cocosWorker: any;
private cocosWorker: worker.Worker;

private constructor() {
this.cocosWorker = new worker.Worker("/entry/ets/workers/cocos_worker.ts", {type:"classic", name: "CocosWorker"});
this.cocosWorker.onerror = function (e) {
var msg = e.message;
var filename = e.filename;
var lineno = e.lineno;
var colno = e.colno;
this.cocosWorker.onerror = (e) => {
let msg = e.message;
let filename = e.filename;
let lineno = e.lineno;
let colno = e.colno;
console.error(`on Error ${msg} ${filename} ${lineno} ${colno}`);
}
}

public static getInstance(): WorkerManager {
if (AppStorage.Get(Constants.APP_KEY_WORKER_MANAGER) == null) {
if (AppStorage.Get(Constants.APP_KEY_WORKER_MANAGER) as WorkerManager == null) {
AppStorage.SetOrCreate(Constants.APP_KEY_WORKER_MANAGER, new WorkerManager);
}
return AppStorage.Get(Constants.APP_KEY_WORKER_MANAGER);
return AppStorage.Get(Constants.APP_KEY_WORKER_MANAGER) as WorkerManager;
}
public getWorker(): any {
public getWorker(): worker.Worker {
return this.cocosWorker;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
import { WorkerManager } from '../cocos/WorkerManager'
import { PortProxy } from '../common/PortProxy'
import { PortProxy } from '../common/PortProxy';

enum EventType {
PLAYING = 0,
Expand All @@ -37,29 +36,35 @@ enum EventType {
QUIT_FULLSCREEN = 1000
};

interface param {
videoTag?: number,
videoEvent?: EventType,
args?: number
}

@Observed
export class VideoInfo {
public x: number = 0;
public y: number = 0;
public w: number = 0;
public h: number = 0;
// url
public url: string = ""
public viewTag: string = ''
public visible: boolean = true
public url: string | Resource = "";
public viewTag: number = 0;
public visible: boolean = true;

public duration: number = 0;
public currentTime: number = 0;
public isFullScreen: boolean = false;
public currentProgressRate:number | string | PlaybackSpeed;
public currentProgressRate:number | string | PlaybackSpeed | null = null;

/**
* https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/arkui-ts/ts-media-components-video.md#videocontroller
*
*/
public controller: VideoController = new VideoController()

constructor(x: number, y: number, w: number, h: number, viewTag: string) {
constructor(x: number, y: number, w: number, h: number, viewTag: number) {
this.x = x;
this.y = y;
this.w = w;
Expand All @@ -72,42 +77,36 @@ export class VideoInfo {
@Component
export struct CocosVideoPlayer {
@ObjectLink videoInfo: VideoInfo;
public workPort: PortProxy;
public workPort: PortProxy | null = null;
build() {
Video({ src: this.videoInfo.url, controller: this.videoInfo.controller, currentProgressRate:this.videoInfo.currentProgressRate })
Video({ src: this.videoInfo.url, controller: this.videoInfo.controller, currentProgressRate:this.videoInfo.currentProgressRate as number | string | PlaybackSpeed })
.position({ x: this.videoInfo.x, y: this.videoInfo.y })
.width(this.videoInfo.w)
.height(this.videoInfo.h)
.controls(false)
.autoPlay(true)
.onStart(() => {
this.workPort.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.PLAYING});
this.workPort?.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.PLAYING} as param);
})
.onPause(() => {
this.workPort.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.PAUSED});
this.workPort?.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.PAUSED} as param);
})
.onFinish(() => {
this.workPort.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.COMPLETED});
this.workPort?.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.COMPLETED} as param);
})
.onPrepared((event: {
duration: number;
}) => {
this.videoInfo.duration = event.duration;
this.workPort.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.READY_TO_PLAY, args:event.duration});
.onPrepared((event) => {
this.videoInfo.duration = event?.duration as number;
this.workPort?.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.READY_TO_PLAY, args:event?.duration} as param);
})
.onUpdate((event?: {
time: number;
}) => {
this.videoInfo.currentTime = event.time
this.workPort.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.UPDATE, args:event.time});
.onUpdate((event) => {
this.videoInfo.currentTime = event?.time as number;
this.workPort?.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.UPDATE, args:event?.time} as param);
})
.onFullscreenChange((event?: {
fullscreen: boolean;
}) => {
if (!event.fullscreen) {
this.workPort.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.QUIT_FULLSCREEN});
.onFullscreenChange((event) => {
if (!event?.fullscreen) {
this.workPort?.postMessage("onVideoEvent",{videoTag:this.videoInfo.viewTag, videoEvent:EventType.QUIT_FULLSCREEN} as param);
}
this.videoInfo.isFullScreen = event.fullscreen
this.videoInfo.isFullScreen = event?.fullscreen as boolean;
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
import { WorkerManager } from '../cocos/WorkerManager'
import { PortProxy } from '../common/PortProxy'
import web from '@ohos.web.webview';

interface param {
viewTag: number,
url: string
}

@Observed
export class WebViewInfo {
// position
Expand All @@ -33,18 +39,18 @@ export class WebViewInfo {
public w: number = 0;
public h: number = 0;
// url
public url: string = ''
public url: string = '';
// tag
public viewTag: string = ''
public viewTag: number = 0;
// Whether to display
public visible: boolean = true
public visible: boolean = true;

/*
* doc : https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/arkui-ts/ts-basic-components-web.md#webcontroller
*/
public controller: WebController = new WebController()

constructor(x: number, y: number, w: number, h: number, viewTag: string) {
constructor(x: number, y: number, w: number, h: number, viewTag: number) {
this.x = x;
this.y = y;
this.w = w;
Expand All @@ -56,24 +62,24 @@ export class WebViewInfo {
@Component
export struct CocosWebView {
@ObjectLink viewInfo: WebViewInfo;
public workPort: PortProxy;
public workPort: PortProxy | null = null;
build() {
Web({ src: this.viewInfo.url, controller: this.viewInfo.controller })
.position({ x: this.viewInfo.x, y: this.viewInfo.y })
.width(this.viewInfo.w)
.height(this.viewInfo.h)
.border({ width: 1 })
.onPageBegin((event) => {
this.workPort.postMessage("onPageBegin", {viewTag:this.viewInfo.viewTag, url:event.url});
this.workPort?.postMessage("onPageBegin", {viewTag:this.viewInfo.viewTag, url:event?.url} as param);
})
.onPageEnd((event) => {
this.workPort.postMessage("onPageEnd", {viewTag:this.viewInfo.viewTag, url:event.url})
this.workPort?.postMessage("onPageEnd", {viewTag:this.viewInfo.viewTag, url:event?.url} as param);
})
.onErrorReceive((event) => {
this.workPort.postMessage("onErrorReceive", {viewTag:this.viewInfo.viewTag, url:this.viewInfo.url})
this.workPort?.postMessage("onErrorReceive", {viewTag:this.viewInfo.viewTag, url:this.viewInfo.url} as param);
})
.onHttpErrorReceive((event) => {
this.workPort.postMessage("onErrorReceive", {viewTag:this.viewInfo.viewTag, url:this.viewInfo.url})
this.workPort?.postMessage("onErrorReceive", {viewTag:this.viewInfo.viewTag, url:this.viewInfo.url} as param);
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
export struct EditBoxDialog {
private showMessage: string = ''
private inputMessage: string = ''
onTextChange: (msg: string) => void
accept: (msg: string) => void
controller: CustomDialogController
cancel: () => void
confirm: () => void
onTextChange?: (msg: string) => void
accept?: (msg: string) => void
controller?: CustomDialogController
cancel?: () => void
confirm?: () => void

build() {
Column() {
Expand All @@ -48,14 +48,14 @@ export struct EditBoxDialog {
if (this.accept) {
this.accept(this.inputMessage);
}
this.controller.close();
this.controller?.close();
})
Blank(8).width(16)
Button('完成').onClick(() => {
if (this.accept) {
this.accept(this.inputMessage);
}
this.controller.close();
this.controller?.close();
})
}.padding({ left: 8, right: 8, top: 8, bottom: 8 })
.backgroundColor(Color.Gray)
Expand Down
Loading

0 comments on commit 0bf887a

Please sign in to comment.