From 9f5452fe86d4d64471d05699d166207bf3582915 Mon Sep 17 00:00:00 2001 From: Coder2 Date: Sat, 1 Nov 2025 20:48:38 +0800 Subject: [PATCH] OpenHarmony: file selection --- ohos-project/entry/src/main/cpp/napi_init.cpp | 5 +- .../entry/src/main/cpp/sdl/Index.d.ts | 5 +- .../entry/src/main/ets/pages/Index.ets | 78 +++++++++++++++++-- ohos-project/entry/src/main/module.json5 | 3 - 4 files changed, 79 insertions(+), 12 deletions(-) diff --git a/ohos-project/entry/src/main/cpp/napi_init.cpp b/ohos-project/entry/src/main/cpp/napi_init.cpp index b457697dfe..f8f796a60b 100644 --- a/ohos-project/entry/src/main/cpp/napi_init.cpp +++ b/ohos-project/entry/src/main/cpp/napi_init.cpp @@ -11,6 +11,8 @@ #include "napi/native_api.h" #include "SDL3/SDL_log.h" #include "SDL3/SDL_hints.h" +#include "SDL3/SDL_power.h" +#include "SDL3/SDL_dialog.h" #include #include #include @@ -18,6 +20,7 @@ #include #include #include +#include static napi_value Add(napi_env env, napi_callback_info info) { @@ -119,8 +122,6 @@ int main() SDL_Log("sdl error: %s", SDL_GetError()); SDL_Window* win = SDL_CreateWindow("test", 1024, 1024, SDL_WINDOW_OPENGL); - SDL_OpenURL("https://bilibili.com"); - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "SDL Application", "test!", win); SDL_StartTextInput(win); // SDL_StopTextInput(); diff --git a/ohos-project/entry/src/main/cpp/sdl/Index.d.ts b/ohos-project/entry/src/main/cpp/sdl/Index.d.ts index 0cce95d5e2..66ecc3f030 100644 --- a/ohos-project/entry/src/main/cpp/sdl/Index.d.ts +++ b/ohos-project/entry/src/main/cpp/sdl/Index.d.ts @@ -2,4 +2,7 @@ export const sdlCallbackInit: (d) => void; export const sdlLaunchMain: (lib: string, func: string) => number; export const sdlKeyEvent: (scancode: number, type: number) => number; export const sdlTextAppend: (str: string) => number; -export const sdlTextEditing: (str: string, loc: number, length: number) => number; \ No newline at end of file +export const sdlTextEditing: (str: string, loc: number, length: number) => number; +export const sdlDialogExecCallback: () => void; +export const sdlDialogClearSelection: () => void; +export const sdlDialogFileSelected: (path: string) => void; \ No newline at end of file diff --git a/ohos-project/entry/src/main/ets/pages/Index.ets b/ohos-project/entry/src/main/ets/pages/Index.ets index d89a4ba684..1cb4bc9e08 100644 --- a/ohos-project/entry/src/main/ets/pages/Index.ets +++ b/ohos-project/entry/src/main/ets/pages/Index.ets @@ -7,6 +7,10 @@ import { pasteboard } from '@kit.BasicServicesKit'; import { common } from '@kit.AbilityKit'; import { batteryInfo } from '@kit.BasicServicesKit'; +import { picker } from '@kit.CoreFileKit'; +import { fileIo as fs } from '@kit.CoreFileKit'; +import { BusinessError } from '@kit.BasicServicesKit'; + const DOMAIN = 0x0000; let controller = inputMethod.getController() @@ -35,14 +39,14 @@ export class ArkNapiCallback { con.openLink(url) } - hasBattery(): boolean { - return batteryInfo.isBatteryPresent + hasBattery(): number { + return batteryInfo.isBatteryPresent ? 1 : 0 } - batteryCharging(): boolean { - return batteryInfo.chargingStatus == batteryInfo.BatteryChargeState.ENABLE + batteryCharging(): number { + return batteryInfo.chargingStatus == batteryInfo.BatteryChargeState.ENABLE ? 1 : 0 } - batteryCharged(): boolean { - return batteryInfo.chargingStatus == batteryInfo.BatteryChargeState.FULL + batteryCharged(): number { + return batteryInfo.chargingStatus == batteryInfo.BatteryChargeState.FULL ? 1 : 0 } batteryPercent(): number { return batteryInfo.batterySOC @@ -76,6 +80,67 @@ export class ArkNapiCallback { hilog.info(DOMAIN, 'testTag', 'text input stop') targetText = "" } + + openFileDialog(id: number, defpath: string, itemcount: number, filter: string) { + const documentSelectOptions = new picker.DocumentSelectOptions(); + documentSelectOptions.maxSelectNumber = itemcount; + documentSelectOptions.defaultFilePathUri = defpath; + + if (id == 0 || id == 1) + { + documentSelectOptions.selectMode = picker.DocumentSelectMode.FILE; + } + if (id == 2) + { + documentSelectOptions.selectMode = picker.DocumentSelectMode.FOLDER; + } + + let targetfilter: string[] = [] + let temp = filter.split('\u0002') + for (let index = 0; index < temp.length; index++) { + const element = temp[index]; + + let temp2 = element.split("|") + + // broken filter! + if (temp2.length == 1) + { + targetfilter.push(element + "|.*") + } + else + { + let targettemp = temp2[0] + "|"; + + let temp3 = temp2[1].split(";") + for (let index1 = 0; index1 < temp3.length; index1++) { + targettemp += "." + temp3[index1] + ","; + } + targetfilter.push(targettemp) + } + } + + documentSelectOptions.fileSuffixFilters = targetfilter; + documentSelectOptions.authMode = false; + documentSelectOptions.mergeMode = picker.MergeTypeMode.DEFAULT; + documentSelectOptions.isEncryptionSupported = false; + + let uris: string[] = []; + let contextt = context.getHostContext() as common.UIAbilityContext; + const documentViewPicker = new picker.DocumentViewPicker(contextt); + documentViewPicker.select(documentSelectOptions).then((documentSelectResult: Array) => { + uris = documentSelectResult; + sdltest.sdlDialogClearSelection() + for (let idx = 0; idx < uris.length; idx++) + { + let target = uris[idx].replace("file://docs", "") + sdltest.sdlDialogFileSelected(target) + console.info('documentViewPicker.select to file succeed and uris are: ' + target); + } + sdltest.sdlDialogExecCallback() + }).catch((err: BusinessError) => { + console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`); + }) + } } let callbackRef: ArkNapiCallback = new ArkNapiCallback() @@ -85,6 +150,7 @@ let callbackRef: ArkNapiCallback = new ArkNapiCallback() struct Index { async aboutToAppear(): Promise { context = this.getUIContext() + sdltest.sdlCallbackInit(callbackRef) focusControl.requestFocus('mainView') await controller.attach(true, { diff --git a/ohos-project/entry/src/main/module.json5 b/ohos-project/entry/src/main/module.json5 index 3d0d898350..8b2db800bc 100644 --- a/ohos-project/entry/src/main/module.json5 +++ b/ohos-project/entry/src/main/module.json5 @@ -22,9 +22,6 @@ }, { "name": "ohos.permission.INTERNET" - }, - { - "name": "ohos.permission.SYSTEM_FLOAT_WINDOW" } ],