From d9d847ec91b1709c763d007841dcfde60eea016c Mon Sep 17 00:00:00 2001 From: Martin Yankov Date: Fri, 24 Aug 2018 18:13:21 +0300 Subject: [PATCH] move frame position conversion methods to ios namespace --- tns-core-modules/ui/core/view/view-common.ts | 8 --- tns-core-modules/ui/core/view/view.d.ts | 10 ---- tns-core-modules/ui/core/view/view.ios.ts | 58 ++++++++++---------- 3 files changed, 29 insertions(+), 47 deletions(-) diff --git a/tns-core-modules/ui/core/view/view-common.ts b/tns-core-modules/ui/core/view/view-common.ts index e1fbe2afe..422fc76e6 100644 --- a/tns-core-modules/ui/core/view/view-common.ts +++ b/tns-core-modules/ui/core/view/view-common.ts @@ -884,14 +884,6 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition { return undefined; } - public getPositionFromFrame(frame: any): { left, top, right, bottom } { - return undefined; - } - - public getFrameFromPosition(position: { left, top, right, bottom }, insets?: { left, top }): any { - return undefined; - } - public getLocationInWindow(): Point { return undefined; } diff --git a/tns-core-modules/ui/core/view/view.d.ts b/tns-core-modules/ui/core/view/view.d.ts index 463853fb8..168adf9bf 100644 --- a/tns-core-modules/ui/core/view/view.d.ts +++ b/tns-core-modules/ui/core/view/view.d.ts @@ -531,16 +531,6 @@ export abstract class View extends ViewBase { */ public getSafeAreaInsets(): { left, top, right, bottom }; - /** - * Transforms iOS CGRect to a position object with left, top, right and bottom. - */ - public getPositionFromFrame(frame: any): { left, top, right, bottom }; - - /** - * Transforms a position object with left, top, right and bottom to an iOS CGRect. - */ - public getFrameFromPosition(position: { left, top, right, bottom }, insets?: { left, top }): any; - /** * Returns the location of this view in the window coordinate system. */ diff --git a/tns-core-modules/ui/core/view/view.ios.ts b/tns-core-modules/ui/core/view/view.ios.ts index f70b33a81..06d640bc2 100644 --- a/tns-core-modules/ui/core/view/view.ios.ts +++ b/tns-core-modules/ui/core/view/view.ios.ts @@ -98,7 +98,7 @@ export class View extends ViewCommon { // on iOS 11+ it is possible to have a changed layout frame due to safe area insets // get the frame and adjust the position, so that onLayout works correctly const frame = this.nativeViewProtected.frame; - position = this.getPositionFromFrame(frame); + position = ios.getPositionFromFrame(frame); } this.onLayout(position.left, position.top, position.right, position.bottom); @@ -192,7 +192,7 @@ export class View extends ViewCommon { } const nativeView = this.nativeViewProtected; - const frame = this.getFrameFromPosition({ left, top, right, bottom }); + const frame = ios.getFrameFromPosition({ left, top, right, bottom }); this._setNativeViewFrame(nativeView, frame); } @@ -229,8 +229,8 @@ export class View extends ViewCommon { const insets = this.getSafeAreaInsets(); if (insets.left || insets.top) { - const position = this.getPositionFromFrame(frame); - const adjustedFrame = this.getFrameFromPosition(position, insets); + const position = ios.getPositionFromFrame(frame); + const adjustedFrame = ios.getFrameFromPosition(position, insets); return adjustedFrame; } } else { @@ -243,9 +243,9 @@ export class View extends ViewCommon { const onScreenLeft = layout.round(layout.toDevicePixels(locationOnScreen.x)); const onScreenTop = layout.round(layout.toDevicePixels(locationOnScreen.y)); - const position = this.getPositionFromFrame(frame); - const safeAreaPosition = this.getPositionFromFrame(safeArea); - const fullscreenPosition = this.getPositionFromFrame(fullscreen); + const position = ios.getPositionFromFrame(frame); + const safeAreaPosition = ios.getPositionFromFrame(safeArea); + const fullscreenPosition = ios.getPositionFromFrame(fullscreen); const adjustedPosition = position; @@ -287,26 +287,6 @@ export class View extends ViewCommon { return insets; } - public getPositionFromFrame(frame: CGRect): { left, top, right, bottom } { - const left = layout.round(layout.toDevicePixels(frame.origin.x)); - const top = layout.round(layout.toDevicePixels(frame.origin.y)); - const right = layout.round(layout.toDevicePixels(frame.origin.x + frame.size.width)); - const bottom = layout.round(layout.toDevicePixels(frame.origin.y + frame.size.height)); - - return { left, right, top, bottom }; - } - - public getFrameFromPosition(position: { left, top, right, bottom }, insets?: { left, top, right, bottom }): CGRect { - insets = insets || { left: 0, top: 0, right: 0, bottom: 0 }; - - const left = layout.round(layout.toDeviceIndependentPixels(position.left + insets.left)); - const top = layout.round(layout.toDeviceIndependentPixels(position.top + insets.top)); - const width = layout.round(layout.toDeviceIndependentPixels(position.right - position.left - insets.left - insets.right)); - const height = layout.round(layout.toDeviceIndependentPixels(position.bottom - position.top - insets.top - insets.bottom)); - - return CGRectMake(left, top, width, height); - } - public getLocationInWindow(): Point { if (!this.nativeViewProtected || !this.nativeViewProtected.window) { return undefined; @@ -772,13 +752,13 @@ export namespace ios { layoutGuide = initLayoutGuide(controller); } const safeArea = layoutGuide.layoutFrame; - let position = owner.getPositionFromFrame(safeArea); + let position = ios.getPositionFromFrame(safeArea); const safeAreaSize = safeArea.size; const hasChildViewControllers = controller.childViewControllers.count > 0; if (hasChildViewControllers) { const fullscreen = controller.view.frame; - position = owner.getPositionFromFrame(fullscreen); + position = ios.getPositionFromFrame(fullscreen); } const safeAreaWidth = layout.round(layout.toDevicePixels(safeAreaSize.width)); @@ -793,6 +773,26 @@ export namespace ios { layoutParent(owner.parent); } + export function getPositionFromFrame(frame: CGRect): { left, top, right, bottom } { + const left = layout.round(layout.toDevicePixels(frame.origin.x)); + const top = layout.round(layout.toDevicePixels(frame.origin.y)); + const right = layout.round(layout.toDevicePixels(frame.origin.x + frame.size.width)); + const bottom = layout.round(layout.toDevicePixels(frame.origin.y + frame.size.height)); + + return { left, right, top, bottom }; + } + + export function getFrameFromPosition(position: { left, top, right, bottom }, insets?: { left, top, right, bottom }): CGRect { + insets = insets || { left: 0, top: 0, right: 0, bottom: 0 }; + + const left = layout.round(layout.toDeviceIndependentPixels(position.left + insets.left)); + const top = layout.round(layout.toDeviceIndependentPixels(position.top + insets.top)); + const width = layout.round(layout.toDeviceIndependentPixels(position.right - position.left - insets.left - insets.right)); + const height = layout.round(layout.toDeviceIndependentPixels(position.bottom - position.top - insets.top - insets.bottom)); + + return CGRectMake(left, top, width, height); + } + function layoutParent(view: ViewBase): void { if (!view) { return;