1/*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {Component, ElementRef, Inject, Input} from '@angular/core';
18import {FunctionUtils} from 'common/function_utils';
19import {TRACE_INFO} from 'trace/trace_info';
20import {TraceType} from 'trace/trace_type';
21import {UserOption, UserOptions} from 'viewers/common/user_options';
22import {userOptionStyle} from './styles/user_option.styles';
23
24type LogCallback = (key: string, state: boolean, name: string) => void;
25
26@Component({
27  selector: 'user-options',
28  template: `
29      <button
30        *ngFor="let option of objectKeys(userOptions)"
31        mat-flat-button
32        [color]="getUserOptionButtonColor(userOptions[option])"
33        [disabled]="userOptions[option].isUnavailable"
34        [class.not-enabled]="!userOptions[option].enabled"
35        class="user-option"
36        [style.cursor]="'pointer'"
37        (click)="onUserOptionChange(userOptions[option])">
38        <span class="user-option-label" [class.with-chip]="!!userOptions[option].chip">
39          <span> {{userOptions[option].name}} </span>
40          <div *ngIf="userOptions[option].chip" class="user-option-chip"> {{userOptions[option].chip.short}} </div>
41          <mat-icon  class="material-symbols-outlined" *ngIf="userOptions[option].icon"> {{userOptions[option].icon}} </mat-icon>
42        </span>
43      </button>
44    `,
45  styles: [userOptionStyle],
46})
47export class UserOptionsComponent {
48  objectKeys = Object.keys;
49
50  @Input() userOptions: UserOptions = {};
51  @Input() eventType = '';
52  @Input() traceType: TraceType | undefined;
53  @Input() logCallback: LogCallback = FunctionUtils.DO_NOTHING;
54
55  constructor(@Inject(ElementRef) private elementRef: ElementRef) {}
56
57  getUserOptionButtonColor(option: UserOption) {
58    return option.enabled ? 'primary' : undefined;
59  }
60
61  onUserOptionChange(option: UserOption) {
62    option.enabled = !option.enabled;
63    this.logCallback(
64      option.name,
65      option.enabled,
66      this.traceType ? TRACE_INFO[this.traceType].name : 'unknown',
67    );
68    const event = new CustomEvent(this.eventType, {
69      bubbles: true,
70      detail: {userOptions: this.userOptions},
71    });
72    this.elementRef.nativeElement.dispatchEvent(event);
73  }
74}
75