diff --git a/crates/ability/src/event.rs b/crates/ability/src/event.rs index 17f6a33..a82e1b1 100644 --- a/crates/ability/src/event.rs +++ b/crates/ability/src/event.rs @@ -78,6 +78,11 @@ pub enum Event<'a> { /// IME Input(InputEvent), + /// keyboard event + /// alias onKeyboardHeightChange + /// https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-window-window#onkeyboardheightchange7 + KeyboardEvent(i32), + UserEvent, } @@ -104,6 +109,7 @@ impl<'a> Event<'a> { Event::SurfaceDestroy => "SurfaceDestroy", Event::Input(_) => "Input", Event::UserEvent => "UserEvent", + Event::KeyboardEvent(_) => "KeyboardEvent", } } } diff --git a/crates/ability/src/lifecycle.rs b/crates/ability/src/lifecycle.rs index deb7960..0f2fc14 100644 --- a/crates/ability/src/lifecycle.rs +++ b/crates/ability/src/lifecycle.rs @@ -29,10 +29,16 @@ pub struct WindowStageEventCallback<'a> { pub on_window_rect_change: Function<'a, Object<'a>, ()>, } +#[napi(object)] +pub struct KeyboardCallback<'a> { + pub on_keyboard_height_change: Function<'a, i32, ()>, +} + #[napi(object)] pub struct ApplicationLifecycle<'a> { pub environment_callback: EnvironmentCallback<'a>, pub window_stage_event_callback: WindowStageEventCallback<'a>, + pub keyboard_event_callback: KeyboardCallback<'a>, } /// create lifecycle object and return to arkts @@ -237,6 +243,16 @@ pub fn create_lifecycle_handle<'a>( Ok(()) })?; + let keyboard_event_callback_app = app.clone(); + let keyboard_event_callback = + env.create_function_from_closure("keyboard_event_callback", move |ctx| { + let event_type = ctx.first_arg::()?; + if let Some(ref mut h) = *keyboard_event_callback_app.event_loop.borrow_mut() { + h(Event::KeyboardEvent(event_type)) + } + Ok(()) + })?; + Ok(ApplicationLifecycle { environment_callback: EnvironmentCallback { on_configuration_updated, @@ -253,5 +269,8 @@ pub fn create_lifecycle_handle<'a>( on_window_size_change: window_resize, on_window_stage_event: window_stage_event, }, + keyboard_event_callback: KeyboardCallback { + on_keyboard_height_change: keyboard_event_callback, + }, }) } diff --git a/rust_ability/ability_rust/src/main/ets/ability/RustAbility.ets b/rust_ability/ability_rust/src/main/ets/ability/RustAbility.ets index c8f8f5e..09fae58 100644 --- a/rust_ability/ability_rust/src/main/ets/ability/RustAbility.ets +++ b/rust_ability/ability_rust/src/main/ets/ability/RustAbility.ets @@ -61,6 +61,11 @@ export class RustAbility extends UIAbility { this.lifecycle?.windowStageEventCallback.onWindowStageEvent(event); }); + let win = await windowStage.getMainWindow(); + win.on("keyboardHeightChange", (height) => { + this.lifecycle?.keyboardEventCallback.onKeyboardHeightChange(height); + }); + if (this.defaultPage) { await windowStage.loadContentByName(Entry.RouteName); } diff --git a/rust_ability/ability_rust/src/main/ets/ability/type.ets b/rust_ability/ability_rust/src/main/ets/ability/type.ets index 0498890..7301e7d 100644 --- a/rust_ability/ability_rust/src/main/ets/ability/type.ets +++ b/rust_ability/ability_rust/src/main/ets/ability/type.ets @@ -3,6 +3,7 @@ import { NodeContent } from "@kit.ArkUI"; export interface ApplicationLifecycle { environmentCallback: EnvironmentCallback; windowStageEventCallback: WindowStageEventCallback; + keyboardEventCallback: KeyboardCallback; } export interface EnvironmentCallback { @@ -10,6 +11,10 @@ export interface EnvironmentCallback { onMemoryLevel: (arg: number) => void; } +export interface KeyboardCallback { + onKeyboardHeightChange: (height: number) => void; +} + export interface WindowStageEventCallback { onWindowStageCreate: () => void; onWindowStageDestroy: () => void;