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
10 changes: 10 additions & 0 deletions crates/ability/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,16 @@ impl OpenHarmonyApp {

self.back_press_interceptor.replace(Some(static_handler));
}

/// Get back press interceptor result
/// Returns true to intercept back press, false to pass through
pub fn get_back_press_interceptor(&self) -> bool {
self.back_press_interceptor
.borrow_mut()
.as_mut()
.map(|h| h())
.unwrap_or(true)
}
}

impl Default for OpenHarmonyApp {
Expand Down
14 changes: 0 additions & 14 deletions crates/ability/src/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub struct EnvironmentCallback<'a> {
pub struct WindowStageEventCallback<'a> {
pub on_window_stage_create: Function<'a, (), ()>,
pub on_window_stage_destroy: Function<'a, (), ()>,
pub on_back_press_intercept: Function<'a, (), bool>,
pub on_ability_create: Function<'a, (), ()>,
pub on_ability_destroy: Function<'a, (), ()>,
pub on_ability_save_state: Function<'a, (), ()>,
Expand Down Expand Up @@ -232,18 +231,6 @@ pub fn create_lifecycle_handle<'a>(
Ok(())
})?;

let on_back_press_intercept_app = app.clone();
let on_back_press_intercept =
env.create_function_from_closure("on_back_press_intercept", move |_ctx| {
let intercept = on_back_press_intercept_app
.back_press_interceptor
.borrow_mut()
.as_mut()
.map(|h| h())
.unwrap_or(true);
Ok(intercept)
})?;

let on_ability_create_app = app.clone();
let on_ability_create = env.create_function_from_closure("on_ability_create", move |_ctx| {
if let Some(ref mut h) = *on_ability_create_app.event_loop.borrow_mut() {
Expand Down Expand Up @@ -306,7 +293,6 @@ pub fn create_lifecycle_handle<'a>(
window_stage_event_callback: WindowStageEventCallback {
on_window_stage_create,
on_window_stage_destroy,
on_back_press_intercept,
on_ability_create,
on_ability_destroy,
on_ability_save_state,
Expand Down
8 changes: 8 additions & 0 deletions crates/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ pub fn ability(attr: TokenStream, item: TokenStream) -> TokenStream {

#protocol_registrations_apply

/// Get back press interceptor result
/// Can be called from ArkTS page lifecycle (onBackPress)
/// Returns true to intercept back press, false to pass through
#[napi_derive_ohos::napi]
pub fn on_back_press_intercept() -> bool {
(*APP).get_back_press_interceptor()
}

#[napi_derive_ohos::napi]
pub fn init<'a>(
env: &'a napi_ohos::Env,
Expand Down
4 changes: 4 additions & 0 deletions package/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.4.0-beta.4
- Fix onBackPress trigger logic

---
# 0.4.0-beta.3
- Add avoidArea event
- Add onBackPress event
Expand Down
2 changes: 1 addition & 1 deletion package/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@ohos-rs/ability",
"description": "Adaptor for OpenHarmony/HarmonyNext Application with Rust",
"main": "index.ets",
"version": "0.4.0-beta.3",
"version": "0.4.0-beta.4",
"repository": "https://github.com/harmony-contrib/openharmony-ability.git",
"dependencies": {},
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,4 @@ export class RustAbility extends UIAbility {
wantParam[STATE_KEY] = ret as string;
return AbilityConstant.OnSaveResult.RECOVERY_AGREE;
}

onBackPress(): boolean {
const intercept = this.lifecycle?.windowStageEventCallback.onBackPressIntercept();
return intercept ?? true;
}
}
1 change: 0 additions & 1 deletion rust_ability/ability_rust/src/main/ets/ability/type.ets
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export interface KeyboardCallback {
export interface WindowStageEventCallback {
onWindowStageCreate: () => void;
onWindowStageDestroy: () => void;
onBackPressIntercept: () => boolean;
onAbilityCreate: (arg: string) => void;
onAbilityDestroy: () => void;
onAbilitySaveState: () => string;
Expand Down
17 changes: 17 additions & 0 deletions rust_ability/ability_rust/src/main/ets/components/MainPage.ets
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import { DefaultXComponent } from "./DefaultXComponent";
import { Loadable } from "../helper/loadable";

export const RouteName = "RustAbility";

@Entry({ routeName: RouteName })
@Component
struct Index {
@StorageProp("moduleName") name: string = "";
private nativeModule: ESObject = null;

async aboutToAppear(): Promise<void> {
const moduleName = `lib${this.name}.so`;
this.nativeModule = await Loadable.load(moduleName, "async");
}

onBackPress(): boolean {
// Call the back press interceptor from Rust
if (typeof this.nativeModule?.on_back_press_intercept === "function") {
return this.nativeModule.on_back_press_intercept();
}
return false;
}

build() {
Row() {
Column() {
Expand Down