• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
14
15import {Area, AreaById} from '../common/state';
16import {globals} from '../frontend/globals';
17
18export class AreaSelectionHandler {
19  private previousArea?: Area;
20
21  getAreaChange(): [boolean, AreaById|undefined] {
22    const currentSelection = globals.state.currentSelection;
23    if (currentSelection === null || currentSelection.kind !== 'AREA') {
24      return [false, undefined];
25    }
26
27    const selectedArea = globals.state.areas[currentSelection.areaId];
28    // Area is considered changed if:
29    // 1. The new area is defined and the old area undefined.
30    // 2. The new area is undefined and the old area defined (viceversa from 1).
31    // 3. Both areas are defined but their start or end times differ.
32    // 4. Both areas are defined but their tracks differ.
33    let hasAreaChanged = (!!this.previousArea !== !!selectedArea);
34    if (selectedArea && this.previousArea) {
35      // There seems to be an issue with clang-format http://shortn/_Pt98d5MCjG
36      // where `a ||= b` is formatted to `a || = b`, by inserting a space which
37      // breaks the operator.
38      // Therefore, we are using the pattern `a = a || b` instead.
39      hasAreaChanged =
40          hasAreaChanged || selectedArea.start !== this.previousArea.start;
41      hasAreaChanged =
42          hasAreaChanged || selectedArea.end !== this.previousArea.end;
43      hasAreaChanged = hasAreaChanged ||
44          selectedArea.tracks.length !== this.previousArea.tracks.length;
45      for (let i = 0; i < selectedArea.tracks.length; ++i) {
46        hasAreaChanged = hasAreaChanged ||
47            selectedArea.tracks[i] !== this.previousArea.tracks[i];
48      }
49    }
50
51    if (hasAreaChanged) {
52      this.previousArea = selectedArea;
53    }
54
55    return [hasAreaChanged, selectedArea];
56  }
57}
58