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';
15
16import {DragStrategy} from './drag_strategy';
17
18export class BorderDragStrategy extends DragStrategy {
19  private moveStart = false;
20
21  constructor(timeScale: TimeScale, private pixelBounds: [number, number]) {
22    super(timeScale);
23  }
24
25  onDrag(x: number) {
26    let tStart =
27        this.timeScale.pxToTime(this.moveStart ? x : this.pixelBounds[0]);
28    let tEnd =
29        this.timeScale.pxToTime(!this.moveStart ? x : this.pixelBounds[1]);
30    if (tStart > tEnd) {
31      this.moveStart = !this.moveStart;
32      [tEnd, tStart] = [tStart, tEnd];
33    }
34    super.updateGlobals(tStart, tEnd);
35    this.pixelBounds =
36        [this.timeScale.timeToPx(tStart), this.timeScale.timeToPx(tEnd)];
37  }
38
39  onDragStart(x: number) {
40    this.moveStart =
41        Math.abs(x - this.pixelBounds[0]) < Math.abs(x - this.pixelBounds[1]);
42  }
43}