From 4f56d7e3f1c08dbfdc35ddac49026562c4cba19a Mon Sep 17 00:00:00 2001 From: richerfu Date: Fri, 13 Mar 2026 10:33:01 +0800 Subject: [PATCH] feat: mark resource_manager as pub --- crates/ability/README.md | 2 +- crates/ability/src/app.rs | 20 +++++++++++--------- crates/ability/src/lib.rs | 1 + crates/ability/src/resource.rs | 25 ++++++++++++++++++++++++- crates/derive/README.md | 2 +- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/crates/ability/README.md b/crates/ability/README.md index 08f4a2c..275de99 100644 --- a/crates/ability/README.md +++ b/crates/ability/README.md @@ -6,7 +6,7 @@ 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()`. The Harmony `resourceManager` instance is also initialized during `init(context)` and can be accessed through `resource_manager()`. +`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 is stored globally, so it can be accessed through `openharmony_ability::resource_manager()` or the compatibility method `app.resource_manager()`. ## License diff --git a/crates/ability/src/app.rs b/crates/ability/src/app.rs index 495036a..d33a30d 100644 --- a/crates/ability/src/app.rs +++ b/crates/ability/src/app.rs @@ -23,10 +23,14 @@ use ohos_ime_binding::IME; 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, ResourceManager, - WAKER, + get_helper, get_main_thread_env, get_permission_request_tsfn, + resource::{ + resource_manager as global_resource_manager, + set_resource_manager as set_global_resource_manager, + }, + unknown_to_permission_promise, AbilityError, AvoidArea, AvoidAreaType, Configuration, Event, + OpenHarmonyWaker, PermissionRequest, PermissionRequestCode, PermissionRequestOutput, Rect, + ResourceManager, WAKER, }; static ID: AtomicI64 = AtomicI64::new(0); @@ -104,7 +108,6 @@ 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 { @@ -161,7 +164,6 @@ impl OpenHarmonyAppInner { window_rect: Default::default(), avoid_areas: HashMap::new(), init_context: AbilityInitContext::default(), - resource_manager: None, } } @@ -230,11 +232,11 @@ impl OpenHarmonyAppInner { } pub fn resource_manager(&self) -> Option { - self.resource_manager.clone() + global_resource_manager() } pub fn set_resource_manager(&mut self, resource_manager: Option) { - self.resource_manager = resource_manager; + set_global_resource_manager(resource_manager); } pub fn exit(&self, code: i32) -> Result<()> { @@ -359,7 +361,7 @@ impl OpenHarmonyApp { } pub fn resource_manager(&self) -> Option { - self.inner.read().unwrap().resource_manager() + global_resource_manager() } #[doc(hidden)] diff --git a/crates/ability/src/lib.rs b/crates/ability/src/lib.rs index 98c5a03..1fbfee6 100644 --- a/crates/ability/src/lib.rs +++ b/crates/ability/src/lib.rs @@ -37,6 +37,7 @@ pub use webview::*; // re-export arkui and avoid the need to import it in the lib.rs pub use ohos_arkui_binding as arkui; pub use ohos_ime_binding as ime; +pub use ohos_resource_manager_binding as resource_manager; pub use ohos_xcomponent_binding as xcomponent; #[cfg(feature = "webview")] diff --git a/crates/ability/src/resource.rs b/crates/ability/src/resource.rs index 2fc4246..f58153d 100644 --- a/crates/ability/src/resource.rs +++ b/crates/ability/src/resource.rs @@ -1,10 +1,17 @@ -use std::{ops::Deref, sync::Arc}; +use std::{ + ops::Deref, + sync::{Arc, LazyLock, RwLock}, +}; 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}; +type ResourceManagerState = LazyLock>>; + +pub(crate) static RESOURCE_MANAGER: ResourceManagerState = LazyLock::new(|| RwLock::new(None)); + #[derive(Clone)] pub struct ResourceManager(Arc); @@ -28,6 +35,22 @@ impl ResourceManager { } } +/// Get global resource manager +pub fn resource_manager() -> Option { + RESOURCE_MANAGER + .read() + .ok() + .and_then(|guard| guard.as_ref().cloned()) +} + +#[doc(hidden)] +/// Set global resource manager +pub(crate) fn set_resource_manager(resource_manager: Option) { + if let Ok(mut guard) = RESOURCE_MANAGER.write() { + *guard = resource_manager; + } +} + impl Deref for ResourceManager { type Target = NativeResourceManager; diff --git a/crates/derive/README.md b/crates/derive/README.md index 0a733fd..4bb65d2 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 Harmony `resourceManager` instance is initialized alongside the other init data and is available through `app.resource_manager()`. +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, stored globally, and is available through `openharmony_ability::resource_manager()` or `app.resource_manager()`. ### webview