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