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
6 changes: 6 additions & 0 deletions crates/ability/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -104,6 +109,7 @@ impl<'a> Event<'a> {
Event::SurfaceDestroy => "SurfaceDestroy",
Event::Input(_) => "Input",
Event::UserEvent => "UserEvent",
Event::KeyboardEvent(_) => "KeyboardEvent",
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions crates/ability/src/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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::<i32>()?;
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,
Expand All @@ -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,
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions rust_ability/ability_rust/src/main/ets/ability/type.ets
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import { NodeContent } from "@kit.ArkUI";
export interface ApplicationLifecycle {
environmentCallback: EnvironmentCallback;
windowStageEventCallback: WindowStageEventCallback;
keyboardEventCallback: KeyboardCallback;
}

export interface EnvironmentCallback {
onConfigurationUpdated: (arg: object) => void;
onMemoryLevel: (arg: number) => void;
}

export interface KeyboardCallback {
onKeyboardHeightChange: (height: number) => void;
}

export interface WindowStageEventCallback {
onWindowStageCreate: () => void;
onWindowStageDestroy: () => void;
Expand Down