xref: /aosp_15_r20/external/perfetto/ui/src/public/omnibox.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2024 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
15export interface OmniboxManager {
16  /**
17   * Turns the omnibox into an interactive prompt for the user. Think of
18   * window.prompt() but non-modal and more integrated with the UI.
19   *
20   * @param text - The question showed to the user (e.g. "Select a process to
21   * jump to").
22   * @param choices - If defined, it shows a list of options in a select-box
23   * fashion, where the user can move with Up/Down arrows. If omitted the input
24   * is free-form, like in the case of window.prompt().
25   * @returns If `options` === undefined, returns the free-form user input. If
26   * `options` was provided, returns the selected choice. Returns undefined if
27   * the user dismisses the prompt by pressing Esc or clicking elsewhere.
28   *
29   * Example:
30   * ```ts
31   * // Free-form string
32   * const name = await prompt('Enter your name');
33   *
34   * // Simple list of choices
35   * const value = await prompt('Choose a color...', ['red', 'blue', 'green']);
36   *
37   * // Each choice is an object
38   * const value = await prompt('Choose from an enum...', {
39   *   values: [
40   *     {x: MyEnum.Foo, name: 'foo'},
41   *     {x: MyEnum.Bar, name: 'bar'},
42   *   ],
43   *   getName: (e) => e.name,
44   * );
45   * ```
46   */
47  prompt(
48    text: string,
49    choices?: ReadonlyArray<string>,
50  ): Promise<string | undefined>;
51  prompt<T>(text: string, choices: PromptChoices<T>): Promise<T | undefined>;
52}
53
54export interface PromptChoices<T> {
55  values: ReadonlyArray<T>;
56  getName: (x: T) => string;
57}
58