Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/ability/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 11 additions & 9 deletions crates/ability/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -104,7 +108,6 @@ pub struct OpenHarmonyAppInner {
pub(crate) window_rect: Rect,
pub(crate) avoid_areas: HashMap<AvoidAreaType, AvoidArea>,
pub(crate) init_context: AbilityInitContext,
pub(crate) resource_manager: Option<ResourceManager>,
}

impl PartialEq for OpenHarmonyAppInner {
Expand Down Expand Up @@ -161,7 +164,6 @@ impl OpenHarmonyAppInner {
window_rect: Default::default(),
avoid_areas: HashMap::new(),
init_context: AbilityInitContext::default(),
resource_manager: None,
}
}

Expand Down Expand Up @@ -230,11 +232,11 @@ impl OpenHarmonyAppInner {
}

pub fn resource_manager(&self) -> Option<ResourceManager> {
self.resource_manager.clone()
global_resource_manager()
}

pub fn set_resource_manager(&mut self, resource_manager: Option<ResourceManager>) {
self.resource_manager = resource_manager;
set_global_resource_manager(resource_manager);
}

pub fn exit(&self, code: i32) -> Result<()> {
Expand Down Expand Up @@ -359,7 +361,7 @@ impl OpenHarmonyApp {
}

pub fn resource_manager(&self) -> Option<ResourceManager> {
self.inner.read().unwrap().resource_manager()
global_resource_manager()
}

#[doc(hidden)]
Expand Down
1 change: 1 addition & 0 deletions crates/ability/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
25 changes: 24 additions & 1 deletion crates/ability/src/resource.rs
Original file line number Diff line number Diff line change
@@ -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<RwLock<Option<ResourceManager>>>;

pub(crate) static RESOURCE_MANAGER: ResourceManagerState = LazyLock::new(|| RwLock::new(None));

#[derive(Clone)]
pub struct ResourceManager(Arc<NativeResourceManager>);

Expand All @@ -28,6 +35,22 @@ impl ResourceManager {
}
}

/// Get global resource manager
pub fn resource_manager() -> Option<ResourceManager> {
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<ResourceManager>) {
if let Ok(mut guard) = RESOURCE_MANAGER.write() {
*guard = resource_manager;
}
}

impl Deref for ResourceManager {
type Target = NativeResourceManager;

Expand Down
2 changes: 1 addition & 1 deletion crates/derive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading