OpenHarmony: file selection

This commit is contained in:
Coder2 2025-11-01 20:48:38 +08:00
parent a48cde983f
commit 9f5452fe86
No known key found for this signature in database
GPG key ID: 98C112D1F699AA4C
4 changed files with 79 additions and 12 deletions

View file

@ -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 <GLES2/gl2.h>
#include <stdlib.h>
#include <thread>
@ -18,6 +20,7 @@
#include <vulkan/vulkan_core.h>
#include <ohaudio/native_audiorenderer.h>
#include <ohaudio/native_audiostreambuilder.h>
#include <fstream>
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();

View file

@ -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;
export const sdlTextEditing: (str: string, loc: number, length: number) => number;
export const sdlDialogExecCallback: () => void;
export const sdlDialogClearSelection: () => void;
export const sdlDialogFileSelected: (path: string) => void;

View file

@ -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<string>) => {
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<void> {
context = this.getUIContext()
sdltest.sdlCallbackInit(callbackRef)
focusControl.requestFocus('mainView')
await controller.attach(true, {

View file

@ -22,9 +22,6 @@
},
{
"name": "ohos.permission.INTERNET"
},
{
"name": "ohos.permission.SYSTEM_FLOAT_WINDOW"
}
],