diff --git a/Cargo.lock b/Cargo.lock index a7cc73f..9ae439d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -346,6 +346,25 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "225be105b169f45a2bca86498988c1877568d82542c013b345d605eb915e48ae" +[[package]] +name = "ohos-resource-manager-binding" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33793d5fc06cf356b3d25aa6228785e62c9f2a0e913a2c285b1eb8efb7185b5e" +dependencies = [ + "napi-ohos", + "ohos-resource-manager-sys", +] + +[[package]] +name = "ohos-resource-manager-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97398d3f86b80279f062bd872e04be851fc9e0eb1dba4dcaabef872201517ff6" +dependencies = [ + "napi-sys-ohos", +] + [[package]] name = "ohos-web-binding" version = "0.1.1" @@ -436,6 +455,7 @@ dependencies = [ "ohos-arkui-binding", "ohos-display-binding", "ohos-ime-binding", + "ohos-resource-manager-binding", "ohos-web-binding", "ohos-xcomponent-binding", ] diff --git a/Cargo.toml b/Cargo.toml index fba457f..3d70163 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,5 +20,6 @@ ohos-xcomponent-binding = { version = "0.2" } ohos-ime-binding = { version = "0.1" } ohos-web-binding = { version = "0.1" } ohos-display-binding = { version = "0.0.1" } +ohos-resource-manager-binding = { version = "0.2" } -http = { version = "1.1" } \ No newline at end of file +http = { version = "1.1" } diff --git a/crates/ability/Cargo.toml b/crates/ability/Cargo.toml index b12db9f..9f15e4a 100644 --- a/crates/ability/Cargo.toml +++ b/crates/ability/Cargo.toml @@ -23,6 +23,7 @@ ohos-arkui-binding = { workspace = true, features = ["napi"] } ohos-xcomponent-binding = { workspace = true } ohos-ime-binding = { workspace = true } ohos-display-binding = { workspace = true } +ohos-resource-manager-binding = { workspace = true } # for webview feature ohos-web-binding = { workspace = true, optional = true } diff --git a/crates/ability/README.md b/crates/ability/README.md index e420a9f..08f4a2c 100644 --- a/crates/ability/README.md +++ b/crates/ability/README.md @@ -6,8 +6,8 @@ openharmony-ability is the Rust runtime crate in this repository. It provides li ## Runtime Context -`NativeAbility` passes the ArkTS init context into native code during `init(context)`. In the Rust runtime, `OpenHarmonyApp` can read `moduleName`, `basePath`, `prefPath`, and `preferredLocales` via `init_context()`, `module_name()`, `base_path()`, `pref_path()`, and `preferred_locales()`. +`NativeAbility` passes the ArkTS init context into native code during `init(context)`. In the Rust runtime, `OpenHarmonyApp` can read `moduleName`, `basePath`, `prefPath`, and `preferredLocales` via `init_context()`, `module_name()`, `base_path()`, `pref_path()`, and `preferred_locales()`. The Harmony `resourceManager` instance is also initialized during `init(context)` and can be accessed through `resource_manager()`. ## License -This project is licensed under the [MIT license](https://github.com/harmony-contrib/openharmony-ability/blob/main/LICENSE) \ No newline at end of file +This project is licensed under the [MIT license](https://github.com/harmony-contrib/openharmony-ability/blob/main/LICENSE) diff --git a/crates/ability/src/app.rs b/crates/ability/src/app.rs index 1ef60eb..495036a 100644 --- a/crates/ability/src/app.rs +++ b/crates/ability/src/app.rs @@ -25,7 +25,8 @@ use ohos_xcomponent_binding::RawWindow; use crate::{ get_helper, get_main_thread_env, get_permission_request_tsfn, unknown_to_permission_promise, AbilityError, AvoidArea, AvoidAreaType, Configuration, Event, OpenHarmonyWaker, - PermissionRequest, PermissionRequestCode, PermissionRequestOutput, Rect, WAKER, + PermissionRequest, PermissionRequestCode, PermissionRequestOutput, Rect, ResourceManager, + WAKER, }; static ID: AtomicI64 = AtomicI64::new(0); @@ -75,6 +76,21 @@ pub struct AbilityInitContext { pub module_name: Option, } +impl AbilityInitContext { + pub fn from_object(context: Option<&Object<'_>>) -> Result { + let Some(context) = context else { + return Ok(Self::default()); + }; + + Ok(Self { + base_path: context.get("basePath")?, + pref_path: context.get("prefPath")?, + preferred_locales: context.get("preferredLocales")?, + module_name: context.get("moduleName")?, + }) + } +} + #[derive(Clone)] pub struct OpenHarmonyAppInner { pub(crate) raw_window: Option, @@ -88,6 +104,7 @@ pub struct OpenHarmonyAppInner { pub(crate) window_rect: Rect, pub(crate) avoid_areas: HashMap, pub(crate) init_context: AbilityInitContext, + pub(crate) resource_manager: Option, } impl PartialEq for OpenHarmonyAppInner { @@ -144,6 +161,7 @@ impl OpenHarmonyAppInner { window_rect: Default::default(), avoid_areas: HashMap::new(), init_context: AbilityInitContext::default(), + resource_manager: None, } } @@ -211,6 +229,14 @@ impl OpenHarmonyAppInner { self.init_context = context; } + pub fn resource_manager(&self) -> Option { + self.resource_manager.clone() + } + + pub fn set_resource_manager(&mut self, resource_manager: Option) { + self.resource_manager = resource_manager; + } + pub fn exit(&self, code: i32) -> Result<()> { let ret = unsafe { get_helper() }; if let Some(h) = ret.borrow().as_ref() { @@ -332,6 +358,18 @@ impl OpenHarmonyApp { self.init_context().preferred_locales } + pub fn resource_manager(&self) -> Option { + self.inner.read().unwrap().resource_manager() + } + + #[doc(hidden)] + pub fn set_resource_manager(&self, resource_manager: Option) { + self.inner + .write() + .unwrap() + .set_resource_manager(resource_manager); + } + pub fn show_keyboard(&self) { let _guard = self .is_keyboard_show diff --git a/crates/ability/src/lib.rs b/crates/ability/src/lib.rs index d088e94..98c5a03 100644 --- a/crates/ability/src/lib.rs +++ b/crates/ability/src/lib.rs @@ -9,6 +9,7 @@ mod input; mod lifecycle; mod memory; mod render; +mod resource; mod stage; mod waker; @@ -26,6 +27,7 @@ pub use input::*; pub use lifecycle::*; pub use memory::*; pub use render::*; +pub use resource::*; pub use stage::*; pub use waker::*; diff --git a/crates/ability/src/resource.rs b/crates/ability/src/resource.rs new file mode 100644 index 0000000..2fc4246 --- /dev/null +++ b/crates/ability/src/resource.rs @@ -0,0 +1,37 @@ +use std::{ops::Deref, sync::Arc}; + +use napi_ohos::{bindgen_prelude::Object, Env, Result}; +use ohos_resource_manager_binding::ResourceManager as NativeResourceManager; +pub use ohos_resource_manager_binding::ScreenDensity as ResourceScreenDensity; +pub use ohos_resource_manager_binding::{IconType, RawDir, RawFile, RawFile64, RawFileError}; + +#[derive(Clone)] +pub struct ResourceManager(Arc); + +impl ResourceManager { + pub fn new(env: Env, resource_manager: Object) -> Self { + Self(Arc::new(NativeResourceManager::new(env, resource_manager))) + } + + pub fn from_init_context(env: Env, context: Option<&Object<'_>>) -> Result> { + let Some(context) = context else { + return Ok(None); + }; + + Ok(context + .get::("resourceManager")? + .map(|resource_manager| Self::new(env, resource_manager))) + } + + pub fn inner(&self) -> &NativeResourceManager { + self.0.as_ref() + } +} + +impl Deref for ResourceManager { + type Target = NativeResourceManager; + + fn deref(&self) -> &Self::Target { + self.inner() + } +} diff --git a/crates/derive/README.md b/crates/derive/README.md index bdca925..0a733fd 100644 --- a/crates/derive/README.md +++ b/crates/derive/README.md @@ -44,7 +44,7 @@ fn openharmony_app(app: OpenHarmonyApp) { ### Init Context -The generated `init(context)` forwards ArkTS init data into native code. In the Rust runtime, you can read it through `app.init_context()`, `app.module_name()`, `app.base_path()`, `app.pref_path()`, and `app.preferred_locales()`. +The generated `init(context)` forwards ArkTS init data into native code. In the Rust runtime, you can read it through `app.init_context()`, `app.module_name()`, `app.base_path()`, `app.pref_path()`, and `app.preferred_locales()`. The Harmony `resourceManager` instance is initialized alongside the other init data and is available through `app.resource_manager()`. ### webview @@ -96,4 +96,4 @@ fn openharmony_app(app: OpenHarmonyApp) { } }); } -``` \ No newline at end of file +``` diff --git a/crates/derive/src/lib.rs b/crates/derive/src/lib.rs index e4c9d77..fd24b22 100644 --- a/crates/derive/src/lib.rs +++ b/crates/derive/src/lib.rs @@ -151,9 +151,13 @@ pub fn ability(attr: TokenStream, item: TokenStream) -> TokenStream { #[napi_derive_ohos::napi] pub fn init<'a>( env: &'a napi_ohos::Env, - context: Option, + #[napi(ts_arg_type = "AbilityInitContext")] + context: Option>, ) -> napi_ohos::Result> { - (*APP).set_init_context(context.unwrap_or_default()); + let init_context = openharmony_ability::AbilityInitContext::from_object(context.as_ref())?; + let resource_manager = openharmony_ability::ResourceManager::from_init_context(*env, context.as_ref())?; + (*APP).set_init_context(init_context); + (*APP).set_resource_manager(resource_manager); let lifecycle_handle = openharmony_ability::create_lifecycle_handle(env, (*APP).clone())?; #fn_name((*APP).clone()); Ok(lifecycle_handle) diff --git a/native_ability/src/main/ets/ability/NativeAbility.ets b/native_ability/src/main/ets/ability/NativeAbility.ets index dcaf939..f7097e7 100644 --- a/native_ability/src/main/ets/ability/NativeAbility.ets +++ b/native_ability/src/main/ets/ability/NativeAbility.ets @@ -59,6 +59,7 @@ export class NativeAbility extends UIAbility { prefPath: context?.filesDir ?? "", preferredLocales: context?.config?.language ?? "", moduleName, + resourceManager: context?.resourceManager, }; } diff --git a/native_ability/src/main/ets/ability/type.ets b/native_ability/src/main/ets/ability/type.ets index 4887250..7e82db0 100644 --- a/native_ability/src/main/ets/ability/type.ets +++ b/native_ability/src/main/ets/ability/type.ets @@ -1,4 +1,5 @@ import { NodeContent } from "@kit.ArkUI"; +import resourceManager from "@ohos.resourceManager"; export interface ApplicationLifecycle { environmentCallback: EnvironmentCallback; @@ -66,6 +67,7 @@ export interface AbilityInitContext { prefPath?: string; preferredLocales?: string; moduleName?: string; + resourceManager?: resourceManager.ResourceManager; } export interface Module { diff --git a/native_ability/src/main/ets/webview/DefaultWebview.ets b/native_ability/src/main/ets/webview/DefaultWebview.ets index a931413..40f9030 100644 --- a/native_ability/src/main/ets/webview/DefaultWebview.ets +++ b/native_ability/src/main/ets/webview/DefaultWebview.ets @@ -109,12 +109,15 @@ export class RustWebviewNodeController extends NodeController { return getCookies(url) as string; }; const loadUrl = (url: string, header?: Record) => { - const headers = Object.keys((header || {}) as Record).reduce((t, i) => { - if (!!header![i]) { - t.push({ headerKey: i, headerValue: header![i] }); - } - return t; - }, [] as Array); + const headers = Object.keys((header || {}) as Record).reduce( + (t, i) => { + if (!!header![i]) { + t.push({ headerKey: i, headerValue: header![i] }); + } + return t; + }, + [] as Array, + ); controller.loadUrl(url, headers); }; diff --git a/package/CHANGELOG.md b/package/CHANGELOG.md index 135810f..1b051a1 100644 --- a/package/CHANGELOG.md +++ b/package/CHANGELOG.md @@ -1,3 +1,7 @@ +# 0.4.0-beta.5 +- Add ResourceManager when init + +--- # 0.4.0-beta.4 - Fix onBackPress trigger logic diff --git a/package/oh-package.json5 b/package/oh-package.json5 index b366cb9..0333edc 100644 --- a/package/oh-package.json5 +++ b/package/oh-package.json5 @@ -4,7 +4,7 @@ "name": "@ohos-rs/ability", "description": "Adaptor for OpenHarmony/HarmonyNext native abilities", "main": "index.ets", - "version": "0.4.0-beta.4", + "version": "0.4.0-beta.5", "repository": "https://github.com/harmony-contrib/openharmony-ability.git", "dependencies": {}, "keywords": [