• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2019 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 m from 'mithril';
16import {assertExists} from '../base/logging';
17import {AppImpl} from '../core/app_impl';
18import {HotkeyGlyphs} from '../widgets/hotkey_glyphs';
19import {showModal} from '../widgets/modal';
20import {Spinner} from '../widgets/spinner';
21import {
22  KeyboardLayoutMap,
23  nativeKeyboardLayoutMap,
24  NotSupportedError,
25} from '../base/keyboard_layout_map';
26import {KeyMapping} from './viewer_page/wasd_navigation_handler';
27import {raf} from '../core/raf_scheduler';
28
29export function toggleHelp() {
30  AppImpl.instance.analytics.logEvent('User Actions', 'Show help');
31  showModal({
32    title: 'Perfetto Help',
33    content: () => m(KeyMappingsHelp),
34    buttons: [],
35  });
36}
37
38function keycap(glyph: m.Children): m.Children {
39  return m('.keycap', glyph);
40}
41
42// A fallback keyboard map based on the QWERTY keymap. Converts keyboard event
43// codes to their associated glyphs on an English QWERTY keyboard.
44class EnglishQwertyKeyboardLayoutMap implements KeyboardLayoutMap {
45  get(code: string): string {
46    // Converts 'KeyX' -> 'x'
47    return code.replace(/^Key([A-Z])$/, '$1').toLowerCase();
48  }
49}
50
51class KeyMappingsHelp implements m.ClassComponent {
52  private keyMap?: KeyboardLayoutMap;
53
54  oninit() {
55    nativeKeyboardLayoutMap()
56      .then((keyMap: KeyboardLayoutMap) => {
57        this.keyMap = keyMap;
58        raf.scheduleFullRedraw();
59      })
60      .catch((e) => {
61        if (
62          e instanceof NotSupportedError ||
63          String(e).includes('SecurityError')
64        ) {
65          // Keyboard layout is unavailable. Since showing the keyboard
66          // mappings correct for the user's keyboard layout is a nice-to-
67          // have, and users with non-QWERTY layouts are usually aware of the
68          // fact that the are using non-QWERTY layouts, we resort to showing
69          // English QWERTY mappings as a best-effort approach.
70          // The alternative would be to show key mappings for all keyboard
71          // layouts which is not feasible.
72          this.keyMap = new EnglishQwertyKeyboardLayoutMap();
73          raf.scheduleFullRedraw();
74        } else {
75          // Something unexpected happened. Either the browser doesn't conform
76          // to the keyboard API spec, or the keyboard API spec has changed!
77          throw e;
78        }
79      });
80  }
81
82  view(): m.Children {
83    return m(
84      '.help',
85      m('h2', 'Navigation'),
86      m(
87        'table',
88        m(
89          'tr',
90          m(
91            'td',
92            this.codeToKeycap(KeyMapping.KEY_ZOOM_IN),
93            '/',
94            this.codeToKeycap(KeyMapping.KEY_ZOOM_OUT),
95          ),
96          m('td', 'Zoom in/out'),
97        ),
98        m(
99          'tr',
100          m(
101            'td',
102            this.codeToKeycap(KeyMapping.KEY_PAN_LEFT),
103            '/',
104            this.codeToKeycap(KeyMapping.KEY_PAN_RIGHT),
105          ),
106          m('td', 'Pan left/right'),
107        ),
108      ),
109      m('h2', 'Mouse Controls'),
110      m(
111        'table',
112        m('tr', m('td', 'Click'), m('td', 'Select event')),
113        m('tr', m('td', 'Ctrl + Scroll wheel'), m('td', 'Zoom in/out')),
114        m('tr', m('td', 'Click + Drag'), m('td', 'Select area')),
115        m('tr', m('td', 'Shift + Click + Drag'), m('td', 'Pan left/right')),
116      ),
117      m('h2', 'Running commands from the viewer page'),
118      m(
119        'table',
120        m(
121          'tr',
122          m('td', keycap('>'), ' in the (empty) search box'),
123          m('td', 'Switch to command mode'),
124        ),
125      ),
126      m('h2', 'Making SQL queries from the viewer page'),
127      m(
128        'table',
129        m(
130          'tr',
131          m('td', keycap(':'), ' in the (empty) search box'),
132          m('td', 'Switch to query mode'),
133        ),
134        m('tr', m('td', keycap('Enter')), m('td', 'Execute query')),
135        m(
136          'tr',
137          m('td', keycap('Ctrl'), ' + ', keycap('Enter')),
138          m(
139            'td',
140            'Execute query and pin output ' +
141              '(output will not be replaced by regular query input)',
142          ),
143        ),
144      ),
145      m('h2', 'Making SQL queries from the query page'),
146      m(
147        'table',
148        m(
149          'tr',
150          m('td', keycap('Ctrl'), ' + ', keycap('Enter')),
151          m('td', 'Execute query'),
152        ),
153        m(
154          'tr',
155          m('td', keycap('Ctrl'), ' + ', keycap('Enter'), ' (with selection)'),
156          m('td', 'Execute selection'),
157        ),
158      ),
159      m('h2', 'Command Hotkeys'),
160      m(
161        'table',
162        AppImpl.instance.commands.commands
163          .filter(({defaultHotkey}) => defaultHotkey)
164          .sort((a, b) => a.name.localeCompare(b.name))
165          .map(({defaultHotkey, name}) => {
166            return m(
167              'tr',
168              m('td', m(HotkeyGlyphs, {hotkey: assertExists(defaultHotkey)})),
169              m('td', name),
170            );
171          }),
172      ),
173    );
174  }
175
176  private codeToKeycap(code: string): m.Children {
177    if (this.keyMap) {
178      return keycap(this.keyMap.get(code));
179    } else {
180      return keycap(m(Spinner));
181    }
182  }
183}
184