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