From 47aa03a9500a957bc1b60eb29d0d6e1056b83a91 Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Thu, 23 Jul 2020 15:16:17 -0700 Subject: [PATCH] chore: cleanup --- apps/automated/tsconfig.json | 1 + apps/playground/references.d.ts | 4 +- apps/playground/tsconfig.json | 1 + apps/ui/tsconfig.json | 1 + packages/core/file-system/index.ts | 16 +-- packages/core/globals/index.ts | 1 + .../core/http/http-request/index.android.ts | 7 +- .../core/image-asset/image-asset-common.ts | 8 +- packages/core/index.d.ts | 2 +- packages/core/index.ts | 2 +- packages/core/module-name-resolver/index.ts | 6 +- packages/core/platform/index.android.ts | 106 +++++++++--------- packages/core/platform/index.d.ts | 12 +- packages/core/platform/index.ios.ts | 12 +- packages/core/ui/animation/index.android.ts | 6 +- packages/core/ui/animation/index.ios.ts | 5 +- packages/core/ui/core/view/index.android.ts | 6 +- packages/core/ui/image/index.android.ts | 8 +- .../ui/transition/slide-transition.android.ts | 6 +- .../ui/transition/slide-transition.ios.ts | 12 +- 20 files changed, 103 insertions(+), 119 deletions(-) diff --git a/apps/automated/tsconfig.json b/apps/automated/tsconfig.json index 49320aba8..6fc4c73a2 100644 --- a/apps/automated/tsconfig.json +++ b/apps/automated/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "module": "commonjs", "target": "es5", + "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true, "noEmitHelpers": true, diff --git a/apps/playground/references.d.ts b/apps/playground/references.d.ts index 444c8f06e..77d634a29 100644 --- a/apps/playground/references.d.ts +++ b/apps/playground/references.d.ts @@ -1,2 +1,2 @@ -/// -/// +/// +/// diff --git a/apps/playground/tsconfig.json b/apps/playground/tsconfig.json index 49320aba8..6fc4c73a2 100644 --- a/apps/playground/tsconfig.json +++ b/apps/playground/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "module": "commonjs", "target": "es5", + "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true, "noEmitHelpers": true, diff --git a/apps/ui/tsconfig.json b/apps/ui/tsconfig.json index 2f7fbfc13..296f39967 100644 --- a/apps/ui/tsconfig.json +++ b/apps/ui/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "module": "commonjs", "target": "es5", + "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true, "noEmitHelpers": true, diff --git a/packages/core/file-system/index.ts b/packages/core/file-system/index.ts index 3eceb375f..66ea65fb2 100644 --- a/packages/core/file-system/index.ts +++ b/packages/core/file-system/index.ts @@ -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!`); } } diff --git a/packages/core/globals/index.ts b/packages/core/globals/index.ts index 7dfa377f2..09511beab 100644 --- a/packages/core/globals/index.ts +++ b/packages/core/globals/index.ts @@ -1,4 +1,5 @@ import * as tslib from 'tslib'; +import { isIOS, isAndroid } from '../platform'; type ModuleLoader = (name?: string) => any; diff --git a/packages/core/http/http-request/index.android.ts b/packages/core/http/http-request/index.android.ts index bf5c61a2b..b40d43d5b 100644 --- a/packages/core/http/http-request/index.android.ts +++ b/packages/core/http/http-request/index.android.ts @@ -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; } diff --git a/packages/core/image-asset/image-asset-common.ts b/packages/core/image-asset/image-asset-common.ts index f1752ae4d..9439f77eb 100644 --- a/packages/core/image-asset/image-asset-common.ts +++ b/packages/core/image-asset/image-asset-common.ts @@ -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); diff --git a/packages/core/index.d.ts b/packages/core/index.d.ts index 80cdd5c43..0cc400a60 100644 --- a/packages/core/index.d.ts +++ b/packages/core/index.d.ts @@ -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'; diff --git a/packages/core/index.ts b/packages/core/index.ts index a89f29152..eca90f35a 100644 --- a/packages/core/index.ts +++ b/packages/core/index.ts @@ -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'; diff --git a/packages/core/module-name-resolver/index.ts b/packages/core/module-name-resolver/index.ts index 74490d20b..aaa4f3486 100644 --- a/packages/core/module-name-resolver/index.ts +++ b/packages/core/module-name-resolver/index.ts @@ -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, }); diff --git a/packages/core/platform/index.android.ts b/packages/core/platform/index.android.ts index 5308ab159..da03ae99c 100644 --- a/packages/core/platform/index.android.ts +++ b/packages/core/platform/index.android.ts @@ -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 = 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 = 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; diff --git a/packages/core/platform/index.d.ts b/packages/core/platform/index.d.ts index d39ce53aa..aec73b096 100644 --- a/packages/core/platform/index.d.ts +++ b/packages/core/platform/index.d.ts @@ -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; } /** diff --git a/packages/core/platform/index.ios.ts b/packages/core/platform/index.ios.ts index 321c3a651..ba7c91697 100644 --- a/packages/core/platform/index.ios.ts +++ b/packages/core/platform/index.ios.ts @@ -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; diff --git a/packages/core/ui/animation/index.android.ts b/packages/core/ui/animation/index.android.ts index 94224f844..699fb3f5b 100644 --- a/packages/core/ui/animation/index.android.ts +++ b/packages/core/ui/animation/index.android.ts @@ -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); diff --git a/packages/core/ui/animation/index.ios.ts b/packages/core/ui/animation/index.ios.ts index fc9e602c5..fa19f9820 100644 --- a/packages/core/ui/animation/index.ios.ts +++ b/packages/core/ui/animation/index.ios.ts @@ -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 = 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; diff --git a/packages/core/ui/core/view/index.android.ts b/packages/core/ui/core/view/index.android.ts index 824c89dbd..05613d09b 100644 --- a/packages/core/ui/core/view/index.android.ts +++ b/packages/core/ui/core/view/index.android.ts @@ -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) { diff --git a/packages/core/ui/image/index.android.ts b/packages/core/ui/image/index.android.ts index d5790d600..4cb085d3f 100644 --- a/packages/core/ui/image/index.android.ts +++ b/packages/core/ui/image/index.android.ts @@ -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) { diff --git a/packages/core/ui/transition/slide-transition.android.ts b/packages/core/ui/transition/slide-transition.android.ts index 86b6e3bf8..73321ac56 100644 --- a/packages/core/ui/transition/slide-transition.android.ts +++ b/packages/core/ui/transition/slide-transition.android.ts @@ -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; diff --git a/packages/core/ui/transition/slide-transition.ios.ts b/packages/core/ui/transition/slide-transition.ios.ts index 947862e2a..2e0681e51 100644 --- a/packages/core/ui/transition/slide-transition.ios.ts +++ b/packages/core/ui/transition/slide-transition.ios.ts @@ -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;