1// Copyright (C) 2021 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14import {TimeScale} from '../time_scale'; 15import {DragStrategy} from './drag_strategy'; 16 17export class BorderDragStrategy extends DragStrategy { 18 private moveStart = false; 19 20 constructor(map: TimeScale, private pixelBounds: [number, number]) { 21 super(map); 22 } 23 24 onDrag(x: number) { 25 let tStart = this.map.pxToHpTime(this.moveStart ? x : this.pixelBounds[0]); 26 let tEnd = this.map.pxToHpTime(!this.moveStart ? x : this.pixelBounds[1]); 27 if (tStart.isGreaterThan(tEnd)) { 28 this.moveStart = !this.moveStart; 29 [tEnd, tStart] = [tStart, tEnd]; 30 } 31 super.updateGlobals(tStart, tEnd); 32 this.pixelBounds = [ 33 this.map.hpTimeToPx(tStart), 34 this.map.hpTimeToPx(tEnd), 35 ]; 36 } 37 38 onDragStart(x: number) { 39 this.moveStart = 40 Math.abs(x - this.pixelBounds[0]) < Math.abs(x - this.pixelBounds[1]); 41 } 42} 43