Skip to content
Open
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
43 changes: 37 additions & 6 deletions entry/src/main/ets/pages/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { common } from '@kit.AbilityKit';
import display from '@ohos.display';
import deviceInfo from '@ohos.deviceInfo';
import promptAction from '@ohos.promptAction';
import window from '@ohos.window';
import { BusinessError } from '@kit.BasicServicesKit';

interface ServoXComponentInterface {
loadURL(url: string): void;
Expand All @@ -12,6 +14,7 @@ interface ServoXComponentInterface {
registerPromptToastCallback(callback: (msg: string) => void): void;
focusWebview(index: number): void;
initServo(options: InitOpts): void;
keyboardHeightChange(height: number): void;
}

interface InitOpts {
Expand All @@ -21,10 +24,10 @@ interface InitOpts {
}

function prompt_toast(msg: string) {
promptAction.showToast({
message: msg,
duration: 2000
});
promptAction.showToast({
message: msg,
duration: 2000
});
}

// Use the getShared API to obtain the LocalStorage instance shared by stage.
Expand All @@ -41,6 +44,7 @@ struct Index {
}

private context = getContext(this) as common.UIAbilityContext;
private mainWindow: window.Window | undefined = undefined;
@LocalStorageProp('InitialURI') InitialURI: string = "unused"
@LocalStorageProp('CommandlineArgs') CommandlineArgs: string = ""
@State urlToLoad: string = this.InitialURI
Expand All @@ -54,6 +58,32 @@ struct Index {
return true;
}

// Setup avoid area change listener for keyboard detection
async setupAvoidAreaChangeListener() {
try {
if (!this.context) {
console.warn('Context not available, cannot setup keyboard listener');
return;
}

this.mainWindow = await window.getLastWindow(this.context);

this.mainWindow.on('avoidAreaChange', (info: window.AvoidAreaOptions) => {
if (info.type === window.AvoidAreaType.TYPE_KEYBOARD) {
const keyboardHeight: number = info.area.bottomRect?.height || 0;
if (this.xComponentContext) {
this.xComponentContext.keyboardHeightChange(keyboardHeight);
} else {
console.warn('xComponentContext not available when keyboard height changed');
}
}
});
} catch (exception) {
const err = exception as BusinessError;
console.error(`Failed to setup avoid area keyboard listener. Code: ${err.code}, message: ${err.message}`);
}
}

build() {
// We originally use `Column()` here, but for some reason the column
// extends beyond the edge of the screen. This does not happen with
Expand All @@ -73,8 +103,8 @@ struct Index {
this.tablist.push(this.tablist[this.tablist.length-1]+1);
}
// yes this is correct as we always have one tab extra
// The tab extra is seperate for the initialization and will always exist.
// It is not in the tablist.
// The tab extra is seperate for the initialization and will always exist.
// It is not in the tablist.
this.currentIndex = this.tablist.length;
})
Button('⇦')
Expand Down Expand Up @@ -128,6 +158,7 @@ struct Index {
this.urlToLoad = new_url
})
this.xComponentContext.registerPromptToastCallback(prompt_toast)
this.setupAvoidAreaChangeListener()
})
}.tabBar('1')
ForEach(this.tablist, (item: number) => {
Expand Down