mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: cleanup
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
// imported for definition purposes only
|
||||
import * as platformModule from '../platform';
|
||||
|
||||
import { FileSystemAccess } from './file-system-access';
|
||||
import { isIOS } from '../platform';
|
||||
import { profile } from '../profiling';
|
||||
|
||||
// The FileSystemAccess implementation, used through all the APIs.
|
||||
@@ -14,13 +12,6 @@ function getFileAccess(): FileSystemAccess {
|
||||
return fileAccess;
|
||||
}
|
||||
|
||||
let platform: typeof platformModule;
|
||||
function ensurePlatform() {
|
||||
if (!platform) {
|
||||
platform = require('../platform');
|
||||
}
|
||||
}
|
||||
|
||||
function createFile(info: { path: string; name: string; extension: string }) {
|
||||
const file = new File();
|
||||
file._path = info.path;
|
||||
@@ -583,9 +574,8 @@ export module knownFolders {
|
||||
|
||||
export module ios {
|
||||
function _checkPlatform(knownFolderName: string) {
|
||||
ensurePlatform();
|
||||
if (!platform.isIOS) {
|
||||
throw new Error(`The "${knownFolderName}" known folder is available on iOS only!`);
|
||||
if (!isIOS) {
|
||||
console.log(`The "${knownFolderName}" known folder is available on iOS only!`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import * as tslib from 'tslib';
|
||||
import { isIOS, isAndroid } from '../platform';
|
||||
|
||||
type ModuleLoader = (name?: string) => any;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// imported for definition purposes only
|
||||
import * as httpModule from '../../http';
|
||||
import * as imageSourceModule from '../../image-source';
|
||||
import * as platformModule from '../../platform';
|
||||
import { Screen } from '../../platform';
|
||||
import * as fsModule from '../../file-system';
|
||||
|
||||
import { getFilenameFromUrl } from './http-request-common';
|
||||
@@ -208,9 +208,8 @@ function buildJavaOptions(options: httpModule.HttpRequestOptions) {
|
||||
ensurePlatform();
|
||||
|
||||
// pass the maximum available image size to the request options in case we need a bitmap conversion
|
||||
const screen = platform.screen.mainScreen;
|
||||
javaOptions.screenWidth = screen.widthPixels;
|
||||
javaOptions.screenHeight = screen.heightPixels;
|
||||
javaOptions.screenWidth = Screen.mainScreen.widthPixels;
|
||||
javaOptions.screenHeight = Screen.mainScreen.heightPixels;
|
||||
|
||||
return javaOptions;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ImageAsset as ImageAssetDefinition, ImageAssetOptions } from '.';
|
||||
import { Observable } from '../data/observable';
|
||||
import { screen as platformScreen } from '../platform';
|
||||
import { Screen } from '../platform';
|
||||
|
||||
export class ImageAssetBase extends Observable implements ImageAssetDefinition {
|
||||
private _options: ImageAssetOptions;
|
||||
@@ -47,10 +47,8 @@ export function getAspectSafeDimensions(sourceWidth, sourceHeight, reqWidth, req
|
||||
}
|
||||
|
||||
export function getRequestedImageSize(src: { width: number; height: number }, options: ImageAssetOptions): { width: number; height: number } {
|
||||
const screen = platformScreen.mainScreen;
|
||||
|
||||
let reqWidth = options.width || Math.min(src.width, screen.widthPixels);
|
||||
let reqHeight = options.height || Math.min(src.height, screen.heightPixels);
|
||||
let reqWidth = options.width || Math.min(src.width, Screen.mainScreen.widthPixels);
|
||||
let reqHeight = options.height || Math.min(src.height, Screen.mainScreen.heightPixels);
|
||||
|
||||
if (options && options.keepAspectRatio) {
|
||||
let safeAspectSize = getAspectSafeDimensions(src.width, src.height, reqWidth, reqHeight);
|
||||
|
||||
2
packages/core/index.d.ts
vendored
2
packages/core/index.d.ts
vendored
@@ -67,7 +67,7 @@ export declare const Http: {
|
||||
export { ImageAsset, ImageAssetOptions } from './image-asset';
|
||||
export { ImageSource } from './image-source';
|
||||
export { ModuleNameResolver, ModuleListProvider, PlatformContext, _setResolver } from './module-name-resolver';
|
||||
export { isAndroid, isIOS, screen as Screen, IDevice, Device, platformNames } from './platform';
|
||||
export { isAndroid, isIOS, Screen, IDevice, Device, platformNames } from './platform';
|
||||
// Profiling
|
||||
export { InstrumentationMode, TimerInfo, profile, enable as profilingEnable, disable as profilingDisable, time as profilingTime, uptime as profilingUptime, start as profilingStart, stop as profilingStop, isRunning as profilingIsRunning, dumpProfiles as profilingDumpProfiles, resetProfiles as profilingResetProfiles, startCPUProfile as profilingStartCPU, stopCPUProfile as profilingStopCPU } from './profiling';
|
||||
export { encoding } from './text';
|
||||
|
||||
@@ -87,7 +87,7 @@ export { ImageAsset, ImageAssetOptions } from './image-asset';
|
||||
|
||||
export { ImageSource } from './image-source';
|
||||
export { ModuleNameResolver, ModuleListProvider, PlatformContext, _setResolver } from './module-name-resolver';
|
||||
export { isAndroid, isIOS, screen as Screen, IDevice, Device, platformNames } from './platform';
|
||||
export { isAndroid, isIOS, Screen, IDevice, Device, platformNames } from './platform';
|
||||
|
||||
// Profiling
|
||||
export { InstrumentationMode, TimerInfo, profile, enable as profilingEnable, disable as profilingDisable, time as profilingTime, uptime as profilingUptime, start as profilingStart, stop as profilingStop, isRunning as profilingIsRunning, dumpProfiles as profilingDumpProfiles, resetProfiles as profilingResetProfiles, startCPUProfile as profilingStartCPU, stopCPUProfile as profilingStopCPU } from './profiling';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { screen, Device } from '../platform';
|
||||
import { Screen, Device } from '../platform';
|
||||
import * as appCommonModule from '../application/application-common';
|
||||
import { PlatformContext, findMatch, stripQualifiers } from './qualifier-matcher';
|
||||
import { registerModulesFromFileSystem } from './non-bundle-workflow-compat';
|
||||
@@ -67,8 +67,8 @@ export function resolveModuleName(path: string, ext: string): string {
|
||||
|
||||
if (!resolverInstance) {
|
||||
resolverInstance = new ModuleNameResolver({
|
||||
width: screen.mainScreen.widthDIPs,
|
||||
height: screen.mainScreen.heightDIPs,
|
||||
width: Screen.mainScreen.widthDIPs,
|
||||
height: Screen.mainScreen.heightDIPs,
|
||||
os: Device.os,
|
||||
deviceType: Device.deviceType,
|
||||
});
|
||||
|
||||
@@ -3,9 +3,58 @@ import * as appModule from '../application';
|
||||
|
||||
const MIN_TABLET_PIXELS = 600;
|
||||
|
||||
export module platformNames {
|
||||
export const android = 'Android';
|
||||
export const ios = 'iOS';
|
||||
export const platformNames = {
|
||||
android: 'Android',
|
||||
ios: 'iOS',
|
||||
};
|
||||
|
||||
class MainScreen {
|
||||
private _metrics: android.util.DisplayMetrics;
|
||||
|
||||
private reinitMetrics(): void {
|
||||
if (!this._metrics) {
|
||||
this._metrics = new android.util.DisplayMetrics();
|
||||
}
|
||||
this.initMetrics();
|
||||
}
|
||||
|
||||
private initMetrics(): void {
|
||||
const nativeApp = <android.app.Application>appModule.getNativeApplication();
|
||||
nativeApp.getSystemService(android.content.Context.WINDOW_SERVICE).getDefaultDisplay().getRealMetrics(this._metrics);
|
||||
}
|
||||
|
||||
private get metrics(): android.util.DisplayMetrics {
|
||||
if (!this._metrics) {
|
||||
// NOTE: This will be memory leak but we MainScreen is singleton
|
||||
appModule.on('cssChanged', this.reinitMetrics, this);
|
||||
appModule.on(appModule.orientationChangedEvent, this.reinitMetrics, this);
|
||||
|
||||
this._metrics = new android.util.DisplayMetrics();
|
||||
this.initMetrics();
|
||||
}
|
||||
|
||||
return this._metrics;
|
||||
}
|
||||
|
||||
get widthPixels(): number {
|
||||
return this.metrics.widthPixels;
|
||||
}
|
||||
get heightPixels(): number {
|
||||
return this.metrics.heightPixels;
|
||||
}
|
||||
get scale(): number {
|
||||
return this.metrics.density;
|
||||
}
|
||||
get widthDIPs(): number {
|
||||
return this.metrics.widthPixels / this.metrics.density;
|
||||
}
|
||||
get heightDIPs(): number {
|
||||
return this.metrics.heightPixels / this.metrics.density;
|
||||
}
|
||||
}
|
||||
|
||||
export class Screen {
|
||||
static mainScreen = new MainScreen();
|
||||
}
|
||||
|
||||
class DeviceRef {
|
||||
@@ -56,7 +105,7 @@ class DeviceRef {
|
||||
|
||||
get deviceType(): 'Phone' | 'Tablet' {
|
||||
if (!this._deviceType) {
|
||||
const dips = Math.min(screen.mainScreen.widthPixels, screen.mainScreen.heightPixels) / screen.mainScreen.scale;
|
||||
const dips = Math.min(Screen.mainScreen.widthPixels, Screen.mainScreen.heightPixels) / Screen.mainScreen.scale;
|
||||
// If the device has more than 600 dips it is considered to be a tablet.
|
||||
if (dips >= MIN_TABLET_PIXELS) {
|
||||
this._deviceType = 'Tablet';
|
||||
@@ -94,56 +143,7 @@ class DeviceRef {
|
||||
}
|
||||
}
|
||||
|
||||
class MainScreen {
|
||||
private _metrics: android.util.DisplayMetrics;
|
||||
|
||||
private reinitMetrics(): void {
|
||||
if (!this._metrics) {
|
||||
this._metrics = new android.util.DisplayMetrics();
|
||||
}
|
||||
this.initMetrics();
|
||||
}
|
||||
|
||||
private initMetrics(): void {
|
||||
const nativeApp = <android.app.Application>appModule.getNativeApplication();
|
||||
nativeApp.getSystemService(android.content.Context.WINDOW_SERVICE).getDefaultDisplay().getRealMetrics(this._metrics);
|
||||
}
|
||||
|
||||
private get metrics(): android.util.DisplayMetrics {
|
||||
if (!this._metrics) {
|
||||
// NOTE: This will be memory leak but we MainScreen is singleton
|
||||
appModule.on('cssChanged', this.reinitMetrics, this);
|
||||
appModule.on(appModule.orientationChangedEvent, this.reinitMetrics, this);
|
||||
|
||||
this._metrics = new android.util.DisplayMetrics();
|
||||
this.initMetrics();
|
||||
}
|
||||
|
||||
return this._metrics;
|
||||
}
|
||||
|
||||
get widthPixels(): number {
|
||||
return this.metrics.widthPixels;
|
||||
}
|
||||
get heightPixels(): number {
|
||||
return this.metrics.heightPixels;
|
||||
}
|
||||
get scale(): number {
|
||||
return this.metrics.density;
|
||||
}
|
||||
get widthDIPs(): number {
|
||||
return this.metrics.widthPixels / this.metrics.density;
|
||||
}
|
||||
get heightDIPs(): number {
|
||||
return this.metrics.heightPixels / this.metrics.density;
|
||||
}
|
||||
}
|
||||
|
||||
export const Device = new DeviceRef();
|
||||
|
||||
export module screen {
|
||||
export const mainScreen = new MainScreen();
|
||||
}
|
||||
|
||||
export const isAndroid = true;
|
||||
export const isIOS = false;
|
||||
|
||||
12
packages/core/platform/index.d.ts
vendored
12
packages/core/platform/index.d.ts
vendored
@@ -17,10 +17,10 @@ export const isIOS: boolean;
|
||||
/*
|
||||
* Enum holding platform names.
|
||||
*/
|
||||
export module platformNames {
|
||||
export const android: string;
|
||||
export const ios: string;
|
||||
}
|
||||
export const platformNames: {
|
||||
android: string;
|
||||
ios: string;
|
||||
};
|
||||
|
||||
/*
|
||||
* An object containing device specific information.
|
||||
@@ -114,11 +114,11 @@ export interface ScreenMetrics {
|
||||
/**
|
||||
* An object describing general information about a display.
|
||||
*/
|
||||
export module screen {
|
||||
export class Screen {
|
||||
/**
|
||||
* Gets information about the main screen of the current device.
|
||||
*/
|
||||
export const mainScreen: ScreenMetrics;
|
||||
static mainScreen: ScreenMetrics;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
/* tslint:disable:class-name */
|
||||
|
||||
export module platformNames {
|
||||
export const android = 'Android';
|
||||
export const ios = 'iOS';
|
||||
}
|
||||
export const platformNames = {
|
||||
android: 'Android',
|
||||
ios: 'iOS',
|
||||
};
|
||||
|
||||
class DeviceRef {
|
||||
private _model: string;
|
||||
@@ -119,8 +119,8 @@ class MainScreen {
|
||||
|
||||
export const Device = new DeviceRef();
|
||||
|
||||
export module screen {
|
||||
export const mainScreen = new MainScreen();
|
||||
export class Screen {
|
||||
static mainScreen = new MainScreen();
|
||||
}
|
||||
|
||||
export const isIOS = true;
|
||||
|
||||
@@ -8,7 +8,7 @@ import { Color } from '../../color';
|
||||
import { Trace } from '../../trace';
|
||||
import { opacityProperty, backgroundColorProperty, rotateProperty, rotateXProperty, rotateYProperty, translateXProperty, translateYProperty, scaleXProperty, scaleYProperty, heightProperty, widthProperty, PercentLength } from '../styling/style-properties';
|
||||
import { layout } from '../../utils';
|
||||
import { Device, screen } from '../../platform';
|
||||
import { Device, Screen } from '../../platform';
|
||||
import lazy from '../../utils/lazy';
|
||||
|
||||
export * from './animation-common';
|
||||
@@ -499,10 +499,10 @@ export class Animation extends AnimationBase {
|
||||
throw new Error(`cannot animate ${propertyAnimation.property} on root view`);
|
||||
}
|
||||
const parentExtent: number = isVertical ? parent.getMeasuredHeight() : parent.getMeasuredWidth();
|
||||
toValue = PercentLength.toDevicePixels(toValue, parentExtent, parentExtent) / screen.mainScreen.scale;
|
||||
toValue = PercentLength.toDevicePixels(toValue, parentExtent, parentExtent) / Screen.mainScreen.scale;
|
||||
let nativeHeight: number = isVertical ? nativeView.getHeight() : nativeView.getWidth();
|
||||
const targetStyle: string = setLocal ? extentProperty.name : extentProperty.keyframe;
|
||||
originalValue1 = nativeHeight / screen.mainScreen.scale;
|
||||
originalValue1 = nativeHeight / Screen.mainScreen.scale;
|
||||
nativeArray[0] = originalValue1;
|
||||
nativeArray[1] = toValue;
|
||||
let extentAnimator = android.animation.ValueAnimator.ofFloat(nativeArray);
|
||||
|
||||
@@ -9,7 +9,7 @@ import { opacityProperty, backgroundColorProperty, rotateProperty, rotateXProper
|
||||
|
||||
import { iOSNativeHelper } from '../../utils/native-helper';
|
||||
|
||||
import { screen } from '../../platform';
|
||||
import { Screen } from '../../platform';
|
||||
|
||||
export * from './animation-common';
|
||||
export { KeyframeAnimation, KeyframeAnimationInfo, KeyframeDeclaration, KeyframeInfo } from './keyframe-animation';
|
||||
@@ -263,7 +263,6 @@ export class Animation extends AnimationBase {
|
||||
const style = view.style;
|
||||
const nativeView = <UIView>view.nativeViewProtected;
|
||||
const parent = view.parent as View;
|
||||
const screenScale: number = screen.mainScreen.scale;
|
||||
|
||||
let propertyNameToAnimate = animation.property;
|
||||
let subPropertyNameToAnimate;
|
||||
@@ -388,7 +387,7 @@ export class Animation extends AnimationBase {
|
||||
throw new Error(`cannot animate ${direction} on root view`);
|
||||
}
|
||||
const parentExtent: number = isHeight ? parent.getMeasuredHeight() : parent.getMeasuredWidth();
|
||||
const asNumber = PercentLength.toDevicePixels(PercentLength.parse(toValue), parentExtent, parentExtent) / screenScale;
|
||||
const asNumber = PercentLength.toDevicePixels(PercentLength.parse(toValue), parentExtent, parentExtent) / Screen.mainScreen.scale;
|
||||
let currentBounds = nativeView.layer.bounds;
|
||||
let extentX = isHeight ? currentBounds.size.width : asNumber;
|
||||
let extentY = isHeight ? asNumber : currentBounds.size.height;
|
||||
|
||||
@@ -44,7 +44,7 @@ import {
|
||||
import { Background, ad as androidBackground } from '../../styling/background';
|
||||
import { profile } from '../../../profiling';
|
||||
import { topmost } from '../../frame/frame-stack';
|
||||
import { screen } from '../../../platform';
|
||||
import { Screen } from '../../../platform';
|
||||
import { AndroidActivityBackPressedEventData, android as androidApp } from '../../../application';
|
||||
import { Device } from '../../../platform';
|
||||
import lazy from '../../../utils/lazy';
|
||||
@@ -940,9 +940,7 @@ export class View extends ViewCommon {
|
||||
}
|
||||
|
||||
[perspectiveProperty.setNative](value: number) {
|
||||
const scale = screen.mainScreen.scale;
|
||||
const distance = value * scale;
|
||||
org.nativescript.widgets.ViewHelper.setPerspective(this.nativeViewProtected, float(distance));
|
||||
org.nativescript.widgets.ViewHelper.setPerspective(this.nativeViewProtected, float(value * Screen.mainScreen.scale));
|
||||
}
|
||||
|
||||
[scaleXProperty.setNative](value: number) {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { ImageAsset } from '../../image-asset';
|
||||
import { Length } from '../styling/style-properties';
|
||||
import { knownFolders } from '../../file-system';
|
||||
|
||||
import * as platform from '../../platform';
|
||||
import { Screen } from '../../platform';
|
||||
export * from './image-common';
|
||||
|
||||
const FILE_PREFIX = 'file:///';
|
||||
@@ -87,10 +87,8 @@ export class Image extends ImageBase {
|
||||
return;
|
||||
}
|
||||
|
||||
let screen = platform.screen.mainScreen;
|
||||
|
||||
let decodeWidth = Math.min(Length.toDevicePixels(this.decodeWidth, 0), screen.widthPixels);
|
||||
let decodeHeight = Math.min(Length.toDevicePixels(this.decodeHeight, 0), screen.heightPixels);
|
||||
let decodeWidth = Math.min(Length.toDevicePixels(this.decodeWidth, 0), Screen.mainScreen.widthPixels);
|
||||
let decodeHeight = Math.min(Length.toDevicePixels(this.decodeHeight, 0), Screen.mainScreen.heightPixels);
|
||||
let keepAspectRatio = this._calculateKeepAspectRatio();
|
||||
if (value instanceof ImageAsset) {
|
||||
if (value.options) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import * as transition from '.';
|
||||
import * as platform from '../../platform';
|
||||
import { Screen } from '../../platform';
|
||||
import lazy from '../../utils/lazy';
|
||||
|
||||
const screenWidth = lazy(() => platform.screen.mainScreen.widthPixels);
|
||||
const screenHeight = lazy(() => platform.screen.mainScreen.heightPixels);
|
||||
const screenWidth = lazy(() => Screen.mainScreen.widthPixels);
|
||||
const screenHeight = lazy(() => Screen.mainScreen.heightPixels);
|
||||
|
||||
export class SlideTransition extends transition.Transition {
|
||||
private _direction: string;
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { Transition } from '.';
|
||||
import { screen } from '../../platform';
|
||||
import { Screen } from '../../platform';
|
||||
|
||||
let screenWidth = screen.mainScreen.widthDIPs;
|
||||
let screenHeight = screen.mainScreen.heightDIPs;
|
||||
let leftEdge = CGAffineTransformMakeTranslation(-screenWidth, 0);
|
||||
let rightEdge = CGAffineTransformMakeTranslation(screenWidth, 0);
|
||||
let topEdge = CGAffineTransformMakeTranslation(0, -screenHeight);
|
||||
let bottomEdge = CGAffineTransformMakeTranslation(0, screenHeight);
|
||||
let leftEdge = CGAffineTransformMakeTranslation(-Screen.mainScreen.widthDIPs, 0);
|
||||
let rightEdge = CGAffineTransformMakeTranslation(Screen.mainScreen.widthDIPs, 0);
|
||||
let topEdge = CGAffineTransformMakeTranslation(0, -Screen.mainScreen.heightDIPs);
|
||||
let bottomEdge = CGAffineTransformMakeTranslation(0, Screen.mainScreen.heightDIPs);
|
||||
|
||||
export class SlideTransition extends Transition {
|
||||
private _direction: string;
|
||||
|
||||
Reference in New Issue
Block a user