11/*
2- Copyright © 2017 Kerry Shetline, kerry@shetline.com.
2+ Copyright © 2017-2018 Kerry Shetline, kerry@shetline.com.
33
44 This code is free software: you can redistribute it and/or modify
55 it under the terms of the GNU General Public License as published by
@@ -26,7 +26,7 @@ import {
2626 ASTEROID_BASE , COMET_BASE , EARTH , FIRST_PLANET , HALF_MINUTE , ISkyObserver , LAST_PLANET , NO_MATCH , SkyObserver , SolarSystem ,
2727 StarCatalog , UT_to_TDB
2828} from 'ks-astronomy' ;
29- import { abs , ceil , max , Point , sqrt } from 'ks-math' ;
29+ import { max , Point , sqrt } from 'ks-math' ;
3030import { FontMetrics , getFontMetrics , isSafari } from 'ks-util' ;
3131import * as _ from 'lodash' ;
3232import { KsDateTime } from 'ks-date-time-zone' ;
@@ -36,8 +36,7 @@ import { Subscription, BehaviorSubject, Observable } from 'rxjs';
3636export const PROPERTY_ADDITIONALS = 'additionals' ;
3737export enum ADDITIONALS { NONE , ALL_ASTEROIDS , ALL_COMETS , ALL }
3838
39- const MAX_RESIZE_TOLERANCE = 4 ;
40- const MAX_RESIZE_CYCLES = 3 ;
39+ const FLICK_REJECTION_THRESHOLD = 250 ;
4140
4241export interface DrawingContext {
4342 context : CanvasRenderingContext2D ;
@@ -78,8 +77,9 @@ export abstract class GenericView implements AfterViewInit {
7877 protected canTouchZoom = false ;
7978 protected initialZoomSpread = 0 ; // 0 means not zooming.
8079 protected goodDragStart = false ;
80+ protected dragStartTime = 0 ;
8181 protected throttledRedraw : ( ) => void ;
82- protected debouncedResize : ( ) => void ;
82+ protected throttledResize : ( ) => void ;
8383 protected dragging = false ;
8484 protected excludedPlanets : number [ ] = [ EARTH ] ;
8585 protected isSafari = false ;
@@ -88,10 +88,6 @@ export abstract class GenericView implements AfterViewInit {
8888 protected lastDrawingContext : DrawingContext ;
8989 protected planetsToDraw : number [ ] = [ ] ;
9090 protected additional : ADDITIONALS | string = ADDITIONALS . NONE ;
91- protected waitingForResizeToSettle = false ;
92- protected resizeTolerance = 0 ;
93- protected lastSizeDiff = 0 ;
94- protected resizeCycles = 0 ;
9591
9692 protected sanitizedHandCursor : SafeStyle ;
9793 protected sanitizedLabelCrosshair : SafeStyle ;
@@ -130,6 +126,10 @@ export abstract class GenericView implements AfterViewInit {
130126 this . draw ( ) ;
131127 } , 100 ) ;
132128
129+ this . throttledResize = _ . throttle ( ( ) => {
130+ this . doResize ( ) ;
131+ } , 100 ) ;
132+
133133 appService . getCurrentTabUpdates ( ( currentTab : CurrentTab ) => {
134134 if ( this . tabId === currentTab )
135135 setTimeout ( ( ) => this . onResize ( ) ) ;
@@ -158,64 +158,17 @@ export abstract class GenericView implements AfterViewInit {
158158 }
159159
160160 onResize ( ) : void {
161- this . waitingForResizeToSettle = false ;
162- this . resizeCycles = 0 ;
163- this . onResizeAux ( ) ;
164- }
165-
166- protected onResizeAux ( ) : void {
167- if ( ! this . debouncedResize ) {
168- this . debouncedResize = _ . debounce ( ( ) => {
169- this . doResize ( ) ;
170-
171- setTimeout ( ( ) => {
172- // Ideally this.wrapper.clientWidth and this.canvas.clientWidth are equal after resizing,
173- // but in Firefox (and possibly other browsers) they don't match exactly even after an extra
174- // cycle of resizing. Below we try to dynamically figure out how much tolerance in width
175- // difference to allow for.
176- const sizeDiff = abs ( this . wrapper . clientWidth - this . canvas . clientWidth ) ;
177- let resizeAgain = true ;
178-
179- if ( sizeDiff > this . resizeTolerance ) {
180- if ( this . waitingForResizeToSettle && sizeDiff <= MAX_RESIZE_TOLERANCE ) {
181- if ( sizeDiff === this . lastSizeDiff ) {
182- if ( ++ this . resizeCycles === MAX_RESIZE_CYCLES ) {
183- this . resizeTolerance = sizeDiff ;
184- resizeAgain = false ;
185- this . waitingForResizeToSettle = false ;
186- }
187- }
188- else
189- this . resizeCycles = 0 ;
190- }
191-
192- this . lastSizeDiff = sizeDiff ;
193-
194- if ( resizeAgain ) {
195- this . waitingForResizeToSettle = true ;
196- this . onResizeAux ( ) ;
197- }
198- }
199- } , 50 ) ;
200- } , 50 ) ;
201- }
202-
203161 this . marqueeText = '' ;
204- this . debouncedResize ( ) ;
162+ this . throttledResize ( ) ;
205163 }
206164
207165 private doResize ( ) : void {
208- const top = ceil ( this . canvas . getBoundingClientRect ( ) . top ) ;
209- const marqueeHeight = this . marquee . getBoundingClientRect ( ) . height ;
210-
211166 this . width = this . wrapper . clientWidth ;
212- // Using the document's clientHeight instead of the window's innerHeight accounts for possible scroll bar.
213- this . height = max ( window . document . documentElement . clientHeight - top - marqueeHeight - 12 , 250 ) ;
214-
167+ this . height = this . wrapper . clientHeight ;
215168 this . canvas . width = this . width ;
216169 this . canvas . height = this . height ;
217170 this . canvas . style . height = this . height + 'px' ;
218- this . wrapper . style . height = this . height + 'px' ;
171+ this . canvas . style . height = this . height + 'px' ;
219172
220173 this . draw ( ) ;
221174 }
@@ -249,6 +202,7 @@ export abstract class GenericView implements AfterViewInit {
249202
250203 if ( this . isInsideView ( ) ) {
251204 this . goodDragStart = true ;
205+ this . dragStartTime = performance . now ( ) ;
252206 this . draw ( ) ;
253207 event . preventDefault ( ) ;
254208
@@ -275,6 +229,7 @@ export abstract class GenericView implements AfterViewInit {
275229 }
276230
277231 onTouchMove ( event : TouchEvent ) : void {
232+ const notAFlick = performance . now ( ) > this . dragStartTime + FLICK_REJECTION_THRESHOLD ;
278233 const pt0 = this . getXYForTouchEvent ( event ) ;
279234 const pt = _ . clone ( pt0 ) ;
280235 let pt1 ;
@@ -285,7 +240,7 @@ export abstract class GenericView implements AfterViewInit {
285240 pt . y = ( pt0 . y + pt1 . y ) / 2 ;
286241 }
287242
288- if ( this . goodDragStart )
243+ if ( this . goodDragStart && notAFlick && event . touches . length === 1 || this . canTouchZoom )
289244 this . handleMouseMove ( pt . x , pt . y , true ) ;
290245
291246 if ( this . initialZoomSpread ) {
@@ -301,7 +256,7 @@ export abstract class GenericView implements AfterViewInit {
301256 this . initialZoomSpread = 0 ;
302257 }
303258
304- if ( this . isInsideView ( ) )
259+ if ( this . isInsideView ( ) && notAFlick )
305260 event . preventDefault ( ) ;
306261 }
307262
@@ -352,8 +307,6 @@ export abstract class GenericView implements AfterViewInit {
352307 if ( event . touches . length === 1 )
353308 this . onTouchStart ( event ) ;
354309 else if ( event . touches . length === 0 ) {
355- this . resetCursor ( ) ;
356- this . draw ( ) ;
357310 this . dragging = false ;
358311 event . preventDefault ( ) ;
359312 }
0 commit comments