Add public instance methods to View class for getting locations and sizes

Resolves #1760
This commit is contained in:
Rossen Hristov
2016-03-15 16:41:57 +02:00
parent 3cb0ce0a93
commit fe3d176dd6
5 changed files with 222 additions and 9 deletions

View File

@@ -1140,6 +1140,39 @@ export class View extends ProxyObject implements definition.View {
return undefined;
}
public getLocationInWindow(): definition.Point {
return undefined;
}
public getLocationOnScreen(): definition.Point {
return undefined;
}
public getLocationRelativeTo(otherView: definition.View): definition.Point {
var my = this.getLocationInWindow();
var other = otherView.getLocationInWindow();
if (!my || !other) {
return undefined;
}
return {
x: my.x - other.x,
y: my.y - other.y
}
}
public getActualSize(): definition.Size {
var currentBounds = this._getCurrentLayoutBounds();
if (!currentBounds) {
return undefined;
}
return {
width: utils.layout.toDeviceIndependentPixels(currentBounds.right - currentBounds.left),
height: utils.layout.toDeviceIndependentPixels(currentBounds.bottom - currentBounds.top),
}
}
public animate(animation: any): Promise<void> {
return this.createAnimation(animation).play();
}

View File

@@ -363,6 +363,32 @@ export class View extends viewCommon.View {
return false;
}
public getLocationInWindow(): viewDefinition.Point {
if (!this._nativeView || !this._nativeView.getWindowToken()) {
return undefined;
}
var nativeArray = (<any>Array).create("int", 2);
this._nativeView.getLocationInWindow(nativeArray);
return {
x: utils.layout.toDeviceIndependentPixels(nativeArray[0]),
y: utils.layout.toDeviceIndependentPixels(nativeArray[1]),
}
}
public getLocationOnScreen(): viewDefinition.Point {
if (!this._nativeView || !this._nativeView.getWindowToken()) {
return undefined;
}
var nativeArray = (<any>Array).create("int", 2);
this._nativeView.getLocationOnScreen(nativeArray);
return {
x: utils.layout.toDeviceIndependentPixels(nativeArray[0]),
y: utils.layout.toDeviceIndependentPixels(nativeArray[1]),
}
}
public static resolveSizeAndState(size: number, specSize: number, specMode: number, childMeasuredState: number): number {
var result = size;
switch (specMode) {

61
ui/core/view.d.ts vendored
View File

@@ -32,6 +32,38 @@ declare module "ui/core/view" {
export function isEventOrGesture(name: string, view: View): boolean;
/**
* The Point interface describes a two dimensional location.
* It has two properties x and y, representing the x and y coordinate of the location.
*/
export interface Point {
/**
* Represents the x coordinate of the location.
*/
x: number;
/**
* Represents the y coordinate of the location.
*/
y: number;
}
/**
* The Size interface describes abstract dimensions in two dimensional space.
* It has two properties width and height, representing the width and height values of the size.
*/
export interface Size {
/**
* Represents the width of the size.
*/
width: number;
/**
* Represents the height of the size.
*/
height: number;
}
/**
* Defines interface for an optional parameter used to create a view.
*/
@@ -433,7 +465,7 @@ declare module "ui/core/view" {
/**
* Returns the child view with the specified id.
*/
getViewById<T extends View>(id: string): T;
public getViewById<T extends View>(id: string): T;
/**
* Tries to focus the view.
@@ -483,9 +515,36 @@ declare module "ui/core/view" {
*/
on(event: "unloaded", callback: (args: observable.EventData) => void, thisArg?: any);
/**
* Animates one or more properties of the view based on the supplied options.
*/
public animate(options: animation.AnimationDefinition): Promise<void>;
/**
* Creates an Animation object based on the supplied options.
*/
public createAnimation(options: animation.AnimationDefinition): animation.Animation;
/**
* Returns the location of this view in the window coordinate system.
*/
public getLocationInWindow(): Point;
/**
* Returns the location of this view in the screen coordinate system.
*/
public getLocationOnScreen(): Point;
/**
* Returns the location of this view in the otherView's coordinate system.
*/
public getLocationRelativeTo(otherView: View): Point;
/**
* Returns the actual size of the view in device-independent pixels.
*/
public getActualSize(): Size;
// Lifecycle events
onLoaded(): void;
onUnloaded(): void;

View File

@@ -1,5 +1,6 @@
import types = require("utils/types");
import viewCommon = require("./view-common");
import viewDefinition = require("ui/core/view");
import trace = require("trace");
import utils = require("utils/utils");
import dependencyObservable = require("ui/core/dependency-observable");
@@ -252,6 +253,31 @@ export class View extends viewCommon.View {
return false;
}
public getLocationInWindow(): viewDefinition.Point {
if (!this._nativeView || !this._nativeView.window) {
return undefined;
}
var pointInWindow = this._nativeView.convertPointToView(this._nativeView.bounds.origin, null);
return {
x: utils.layout.toDeviceIndependentPixels(pointInWindow.x),
y: utils.layout.toDeviceIndependentPixels(pointInWindow.y),
}
}
public getLocationOnScreen(): viewDefinition.Point {
if (!this._nativeView || !this._nativeView.window) {
return undefined;
}
var pointInWindow = this._nativeView.convertPointToView(this._nativeView.bounds.origin, null);
var pointOnScreen = this._nativeView.window.convertPointToWindow(pointInWindow, null);
return {
x: utils.layout.toDeviceIndependentPixels(pointOnScreen.x),
y: utils.layout.toDeviceIndependentPixels(pointOnScreen.y),
}
}
private _onSizeChanged() {
this.style._sizeChanged();
}