diff --git a/packages/nativescript-keyboard/index.android.ts b/packages/nativescript-keyboard/index.android.ts index af400ee..fefafaa 100644 --- a/packages/nativescript-keyboard/index.android.ts +++ b/packages/nativescript-keyboard/index.android.ts @@ -1,28 +1,38 @@ -import { Utils } from "@nativescript/core"; -import { KeyboardBase } from "./common"; - +import { Utils } from '@nativescript/core'; +import { KeyboardBase } from './common'; export class Keyboard extends KeyboardBase { - private listener: any; - + private listener: any; - onChangeVisibility(callback: (isOpen: boolean) => void) { - /* let windowVisibleDisplayFrame = new android.graphics.Rect(); + onChangeVisibility(callback: (isOpen: boolean, params: { height: number }) => void) { + /* let windowVisibleDisplayFrame = new android.graphics.Rect(); let lastVisibleDecorViewHeight: number; */ - //https://stackoverflow.com/questions/25216749/soft-keyboard-open-and-close-listener-in-an-activity-in-android - const rootView: android.view.View = Utils.android.getCurrentActivity()?.getWindow()?.getDecorView(); - if (rootView) { - this.listener = new android.view.ViewTreeObserver.OnGlobalLayoutListener({ - onGlobalLayout() { - let r = new android.graphics.Rect(); - rootView.getWindowVisibleDisplayFrame(r) - const height = rootView.getHeight() - if (height - r.bottom > height * 0.1399) { - callback(true); - } else { - callback(false); - } - /* rootView.getWindowVisibleDisplayFrame(windowVisibleDisplayFrame); + //https://stackoverflow.com/questions/25216749/soft-keyboard-open-and-close-listener-in-an-activity-in-android + const rootView: android.view.View = Utils.android.getCurrentActivity()?.getWindow()?.getDecorView(); + if (rootView) { + this.listener = new android.view.ViewTreeObserver.OnGlobalLayoutListener({ + onGlobalLayout() { + const r = new android.graphics.Rect(); + // rootView.getWindowVisibleDisplayFrame(r) + const height = rootView.getHeight(); + const view = Utils.android.getCurrentActivity().getWindow().getDecorView(); + view.getWindowVisibleDisplayFrame(r); + const screenHeight = view.getHeight(); + const heightDifference = screenHeight - (r.bottom - r.top); + + console.log('heightDifference'); + console.log(heightDifference); + console.log(height); + console.log(r.bottom); + console.log(height - r.bottom); + console.log(height * 0.1399); + + if (height - r.bottom > height * 0.1399) { + callback(true, { height: 0 }); + } else { + callback(false, { height: 0 }); + } + /* rootView.getWindowVisibleDisplayFrame(windowVisibleDisplayFrame); const visibleDecorViewHeight = windowVisibleDisplayFrame.height(); if (lastVisibleDecorViewHeight == -1) lastVisibleDecorViewHeight = visibleDecorViewHeight + 101; @@ -34,20 +44,19 @@ export class Keyboard extends KeyboardBase { } } lastVisibleDecorViewHeight = visibleDecorViewHeight; */ - } - }) - rootView.getViewTreeObserver().addOnGlobalLayoutListener(this.listener) - } else { - console.log("Error on get RootView onChangeVisibility"); - } - } - offChangeVisibility() { - const rootView: android.view.View = Utils.android.getCurrentActivity()?.getWindow()?.getDecorView(); - rootView?.getViewTreeObserver().removeOnGlobalLayoutListener(this.listener); + }, + }); + rootView.getViewTreeObserver().addOnGlobalLayoutListener(this.listener); + } else { + console.log('Error on get RootView onChangeVisibility'); } - isOpen() { - const imm: android.view.inputmethod.InputMethodManager = Utils.android.getCurrentActivity().getSystemService(android.content.Context.INPUT_METHOD_SERVICE); - return imm.isAcceptingText(); - }; - -} \ No newline at end of file + } + offChangeVisibility() { + const rootView: android.view.View = Utils.android.getCurrentActivity()?.getWindow()?.getDecorView(); + rootView?.getViewTreeObserver().removeOnGlobalLayoutListener(this.listener); + } + isOpen() { + const imm: android.view.inputmethod.InputMethodManager = Utils.android.getCurrentActivity().getSystemService(android.content.Context.INPUT_METHOD_SERVICE); + return imm.isAcceptingText(); + } +} diff --git a/packages/nativescript-keyboard/index.ios.ts b/packages/nativescript-keyboard/index.ios.ts index b8d7ec3..9637d57 100644 --- a/packages/nativescript-keyboard/index.ios.ts +++ b/packages/nativescript-keyboard/index.ios.ts @@ -1,31 +1,30 @@ -import { Application } from "@nativescript/core"; -import { KeyboardBase } from "./common"; - +import { Application } from '@nativescript/core'; +import { KeyboardBase } from './common'; export class Keyboard extends KeyboardBase { - private openListener: any; - private closeListener: any; - - onChangeVisibility(callback: (isOpen: boolean) => void) { - this.openListener = Application.ios.addNotificationObserver(UIKeyboardDidShowNotification, () => { - callback(true); - }) - this.closeListener = Application.ios.addNotificationObserver(UIKeyboardDidHideNotification, () => { - callback(false); - }) - } - offChangeVisibility() { - Application.ios.removeNotificationObserver(this.openListener, UIKeyboardDidShowNotification) - Application.ios.removeNotificationObserver(this.closeListener, UIKeyboardDidHideNotification) - } - isOpen() { - //TODO: implement - //@ts-ignore - console.log(UIApplication.sharedApplication.windows.containsObject(NSClassFromString("UIRemoteKeyboardWindow"))); + private openListener: any; + private closeListener: any; - //@ts-ignore - return UIApplication.sharedApplication.windows.containsObject(NSClassFromString("UIRemoteKeyboardWindow")) - // NSClassFromString("UIRemoteKeyboardWindow"), (Frame.topmost().nativeViewProtected as UIView).window.contains(where: { $0.isKind(of: keyboardWindowClass) }) - }; + onChangeVisibility(callback: (isOpen: boolean, params: { height: number }) => void) { + this.openListener = Application.ios.addNotificationObserver(UIKeyboardWillShowNotification, (notification) => { + const keyboardRectangle = notification.userInfo.objectForKey('UIKeyboardFrameEndUserInfoKey') as NSValue; + callback(true, { height: keyboardRectangle?.CGRectValue?.size?.height ?? 0 }); + }); + this.closeListener = Application.ios.addNotificationObserver(UIKeyboardWillHideNotification, () => { + callback(false, { height: 0 }); + }); + } + offChangeVisibility() { + Application.ios.removeNotificationObserver(this.openListener, UIKeyboardWillShowNotification); + Application.ios.removeNotificationObserver(this.closeListener, UIKeyboardWillHideNotification); + } + isOpen() { + //TODO: implement + //@ts-ignore + console.log(UIApplication.sharedApplication.windows.containsObject(NSClassFromString('UIRemoteKeyboardWindow'))); -} \ No newline at end of file + //@ts-ignore + return UIApplication.sharedApplication.windows.containsObject(NSClassFromString('UIRemoteKeyboardWindow')); + // NSClassFromString("UIRemoteKeyboardWindow"), (Frame.topmost().nativeViewProtected as UIView).window.contains(where: { $0.isKind(of: keyboardWindowClass) }) + } +}