Mike Frysinger | 598e801 | 2022-09-07 08:38:34 -0400 | [diff] [blame] | 1 | // Copyright 2022 The ChromiumOS Authors |
Jason Lin | d66e6bf | 2022-08-22 14:47:10 +1000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 5 | /** |
| 6 | * @fileoverview For supporting xterm.js and the terminal emulator. |
| 7 | */ |
| 8 | |
| 9 | // TODO(b/236205389): add tests. For example, we should enable the test in |
| 10 | // terminal_tests.js for XtermTerminal. |
| 11 | |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 12 | // TODO(b/236205389): support option smoothScrollDuration? |
| 13 | |
Mike Frysinger | 75895da | 2022-10-04 00:42:28 +0545 | [diff] [blame] | 14 | import {hterm, lib} from './deps_local.concat.js'; |
| 15 | |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 16 | import {LitElement, css, html} from './lit.js'; |
Jason Lin | c48f743 | 2022-10-13 17:28:30 +1100 | [diff] [blame] | 17 | import {FontManager, ORIGINAL_URL, TERMINAL_EMULATORS, definePrefs, |
| 18 | delayedScheduler, fontManager, getOSInfo, sleep} from './terminal_common.js'; |
Jason Lin | 82ba86c | 2022-11-09 12:12:27 +1100 | [diff] [blame] | 19 | import {TerminalContextMenu} from './terminal_context_menu.js'; |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 20 | import {ICON_COPY} from './terminal_icons.js'; |
Jason Lin | 83707c9 | 2022-09-20 19:09:41 +1000 | [diff] [blame] | 21 | import {TerminalTooltip} from './terminal_tooltip.js'; |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 22 | import {Terminal, Unicode11Addon, WebLinksAddon, WebglAddon} |
Jason Lin | 4de4f38 | 2022-09-01 14:10:18 +1000 | [diff] [blame] | 23 | from './xterm.js'; |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 24 | import {XtermInternal} from './terminal_xterm_internal.js'; |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 25 | |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 26 | |
| 27 | /** @enum {number} */ |
| 28 | export const Modifier = { |
| 29 | Shift: 1 << 0, |
| 30 | Alt: 1 << 1, |
| 31 | Ctrl: 1 << 2, |
| 32 | Meta: 1 << 3, |
| 33 | }; |
| 34 | |
| 35 | // This is just a static map from key names to key codes. It helps make the code |
| 36 | // a bit more readable. |
| 37 | const keyCodes = hterm.Parser.identifiers.keyCodes; |
| 38 | |
| 39 | /** |
| 40 | * Encode a key combo (i.e. modifiers + a normal key) to an unique number. |
| 41 | * |
| 42 | * @param {number} modifiers |
| 43 | * @param {number} keyCode |
| 44 | * @return {number} |
| 45 | */ |
| 46 | export function encodeKeyCombo(modifiers, keyCode) { |
| 47 | return keyCode << 4 | modifiers; |
| 48 | } |
| 49 | |
| 50 | const OS_DEFAULT_BINDINGS = [ |
| 51 | // Submit feedback. |
| 52 | encodeKeyCombo(Modifier.Alt | Modifier.Shift, keyCodes.I), |
| 53 | // Toggle chromevox. |
| 54 | encodeKeyCombo(Modifier.Ctrl | Modifier.Alt, keyCodes.Z), |
| 55 | // Switch input method. |
| 56 | encodeKeyCombo(Modifier.Ctrl, keyCodes.SPACE), |
| 57 | |
| 58 | // Dock window left/right. |
| 59 | encodeKeyCombo(Modifier.Alt, keyCodes.BRACKET_LEFT), |
| 60 | encodeKeyCombo(Modifier.Alt, keyCodes.BRACKET_RIGHT), |
| 61 | |
| 62 | // Maximize/minimize window. |
| 63 | encodeKeyCombo(Modifier.Alt, keyCodes.EQUAL), |
| 64 | encodeKeyCombo(Modifier.Alt, keyCodes.MINUS), |
| 65 | ]; |
| 66 | |
| 67 | |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 68 | const ANSI_COLOR_NAMES = [ |
| 69 | 'black', |
| 70 | 'red', |
| 71 | 'green', |
| 72 | 'yellow', |
| 73 | 'blue', |
| 74 | 'magenta', |
| 75 | 'cyan', |
| 76 | 'white', |
| 77 | 'brightBlack', |
| 78 | 'brightRed', |
| 79 | 'brightGreen', |
| 80 | 'brightYellow', |
| 81 | 'brightBlue', |
| 82 | 'brightMagenta', |
| 83 | 'brightCyan', |
| 84 | 'brightWhite', |
| 85 | ]; |
| 86 | |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 87 | /** |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 88 | * @typedef {{ |
| 89 | * term: !Terminal, |
| 90 | * fontManager: !FontManager, |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 91 | * xtermInternal: !XtermInternal, |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 92 | * }} |
| 93 | */ |
| 94 | export let XtermTerminalTestParams; |
| 95 | |
| 96 | /** |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 97 | * Compute a control character for a given character. |
| 98 | * |
| 99 | * @param {string} ch |
| 100 | * @return {string} |
| 101 | */ |
| 102 | function ctl(ch) { |
| 103 | return String.fromCharCode(ch.charCodeAt(0) - 64); |
| 104 | } |
| 105 | |
| 106 | /** |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 107 | * A "terminal io" class for xterm. We don't want the vanilla hterm.Terminal.IO |
| 108 | * because it always convert utf8 data to strings, which is not necessary for |
| 109 | * xterm. |
| 110 | */ |
| 111 | class XtermTerminalIO extends hterm.Terminal.IO { |
| 112 | /** @override */ |
| 113 | writeUTF8(buffer) { |
| 114 | this.terminal_.write(new Uint8Array(buffer)); |
| 115 | } |
| 116 | |
| 117 | /** @override */ |
| 118 | writelnUTF8(buffer) { |
| 119 | this.terminal_.writeln(new Uint8Array(buffer)); |
| 120 | } |
| 121 | |
| 122 | /** @override */ |
| 123 | print(string) { |
| 124 | this.terminal_.write(string); |
| 125 | } |
| 126 | |
| 127 | /** @override */ |
| 128 | writeUTF16(string) { |
| 129 | this.print(string); |
| 130 | } |
| 131 | |
| 132 | /** @override */ |
| 133 | println(string) { |
| 134 | this.terminal_.writeln(string); |
| 135 | } |
| 136 | |
| 137 | /** @override */ |
| 138 | writelnUTF16(string) { |
| 139 | this.println(string); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
Jason Lin | 83707c9 | 2022-09-20 19:09:41 +1000 | [diff] [blame] | 144 | * A custom link handler that: |
| 145 | * |
| 146 | * - Shows a tooltip with the url on a OSC 8 link. This is following what hterm |
| 147 | * is doing. Also, showing the tooltip is better for the security of the user |
| 148 | * because the link can have arbitrary text. |
| 149 | * - Uses our own way to open the window. |
| 150 | */ |
| 151 | class LinkHandler { |
| 152 | /** |
| 153 | * @param {!Terminal} term |
| 154 | */ |
| 155 | constructor(term) { |
| 156 | this.term_ = term; |
| 157 | /** @type {?TerminalTooltip} */ |
| 158 | this.tooltip_ = null; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @return {!TerminalTooltip} |
| 163 | */ |
| 164 | getTooltip_() { |
| 165 | if (!this.tooltip_) { |
| 166 | this.tooltip_ = /** @type {!TerminalTooltip} */( |
| 167 | document.createElement('terminal-tooltip')); |
| 168 | this.tooltip_.classList.add('xterm-hover'); |
| 169 | lib.notNull(this.term_.element).appendChild(this.tooltip_); |
| 170 | } |
| 171 | return this.tooltip_; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @param {!MouseEvent} ev |
| 176 | * @param {string} url |
| 177 | * @param {!Object} range |
| 178 | */ |
| 179 | activate(ev, url, range) { |
| 180 | lib.f.openWindow(url, '_blank'); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @param {!MouseEvent} ev |
| 185 | * @param {string} url |
| 186 | * @param {!Object} range |
| 187 | */ |
| 188 | hover(ev, url, range) { |
| 189 | this.getTooltip_().show(url, {x: ev.clientX, y: ev.clientY}); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @param {!MouseEvent} ev |
| 194 | * @param {string} url |
| 195 | * @param {!Object} range |
| 196 | */ |
| 197 | leave(ev, url, range) { |
| 198 | this.getTooltip_().hide(); |
| 199 | } |
| 200 | } |
| 201 | |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 202 | class Bell { |
| 203 | constructor() { |
| 204 | this.showNotification = false; |
| 205 | |
| 206 | /** @type {?Audio} */ |
| 207 | this.audio_ = null; |
| 208 | /** @type {?Notification} */ |
| 209 | this.notification_ = null; |
| 210 | this.coolDownUntil_ = 0; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Set whether a bell audio should be played. |
| 215 | * |
| 216 | * @param {boolean} value |
| 217 | */ |
| 218 | set playAudio(value) { |
| 219 | this.audio_ = value ? |
| 220 | new Audio(lib.resource.getDataUrl('hterm/audio/bell')) : null; |
| 221 | } |
| 222 | |
| 223 | ring() { |
| 224 | const now = Date.now(); |
| 225 | if (now < this.coolDownUntil_) { |
| 226 | return; |
| 227 | } |
| 228 | this.coolDownUntil_ = now + 500; |
| 229 | |
| 230 | this.audio_?.play(); |
| 231 | if (this.showNotification && !document.hasFocus() && !this.notification_) { |
| 232 | this.notification_ = new Notification( |
| 233 | `\u266A ${document.title} \u266A`, |
| 234 | {icon: lib.resource.getDataUrl('hterm/images/icon-96')}); |
| 235 | // Close the notification after a timeout. Note that this is different |
| 236 | // from hterm's behavior, but I think it makes more sense to do so. |
| 237 | setTimeout(() => { |
| 238 | this.notification_.close(); |
| 239 | this.notification_ = null; |
| 240 | }, 5000); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 245 | const A11Y_BUTTON_STYLE = ` |
| 246 | position: fixed; |
| 247 | z-index: 10; |
| 248 | right: 16px; |
| 249 | `; |
| 250 | |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 251 | // TODO: we should subscribe to the xterm.js onscroll event, and |
| 252 | // disable/enable the buttons accordingly. However, xterm.js does not seem to |
| 253 | // emit the onscroll event when the viewport is scrolled by the mouse. See |
| 254 | // https://github.com/xtermjs/xterm.js/issues/3864 |
| 255 | export class A11yButtons { |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 256 | /** |
| 257 | * @param {!Terminal} term |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 258 | * @param {!hterm.AccessibilityReader} htermA11yReader |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 259 | */ |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 260 | constructor(term, htermA11yReader) { |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 261 | this.term_ = term; |
| 262 | this.htermA11yReader_ = htermA11yReader; |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 263 | this.pageUpButton = document.createElement('button'); |
| 264 | this.pageUpButton.style.cssText = A11Y_BUTTON_STYLE; |
| 265 | this.pageUpButton.textContent = |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 266 | hterm.messageManager.get('HTERM_BUTTON_PAGE_UP'); |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 267 | this.pageUpButton.addEventListener('click', |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 268 | () => this.scrollPages_(-1)); |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 269 | |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 270 | this.pageDownButton = document.createElement('button'); |
| 271 | this.pageDownButton.style.cssText = A11Y_BUTTON_STYLE; |
| 272 | this.pageDownButton.textContent = |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 273 | hterm.messageManager.get('HTERM_BUTTON_PAGE_DOWN'); |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 274 | this.pageDownButton.addEventListener('click', |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 275 | () => this.scrollPages_(1)); |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 276 | |
| 277 | this.resetPos_(); |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 278 | |
| 279 | this.onSelectionChange_ = this.onSelectionChange_.bind(this); |
| 280 | } |
| 281 | |
| 282 | /** |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 283 | * @param {number} amount |
| 284 | */ |
| 285 | scrollPages_(amount) { |
| 286 | this.term_.scrollPages(amount); |
| 287 | this.announceScreenContent_(); |
| 288 | } |
| 289 | |
| 290 | announceScreenContent_() { |
| 291 | const activeBuffer = this.term_.buffer.active; |
| 292 | |
| 293 | let percentScrolled = 100; |
| 294 | if (activeBuffer.baseY !== 0) { |
| 295 | percentScrolled = Math.round( |
| 296 | 100 * activeBuffer.viewportY / activeBuffer.baseY); |
| 297 | } |
| 298 | |
| 299 | let currentScreenContent = hterm.messageManager.get( |
| 300 | 'HTERM_ANNOUNCE_CURRENT_SCREEN_HEADER', |
| 301 | [percentScrolled], |
| 302 | '$1% scrolled,'); |
| 303 | |
| 304 | currentScreenContent += '\n'; |
| 305 | |
| 306 | const rowEnd = Math.min(activeBuffer.viewportY + this.term_.rows, |
| 307 | activeBuffer.length); |
| 308 | for (let i = activeBuffer.viewportY; i < rowEnd; ++i) { |
| 309 | currentScreenContent += |
| 310 | activeBuffer.getLine(i).translateToString(true) + '\n'; |
| 311 | } |
| 312 | currentScreenContent = currentScreenContent.trim(); |
| 313 | |
| 314 | this.htermA11yReader_.assertiveAnnounce(currentScreenContent); |
| 315 | } |
| 316 | |
| 317 | /** |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 318 | * @param {boolean} enabled |
| 319 | */ |
| 320 | setEnabled(enabled) { |
| 321 | if (enabled) { |
| 322 | document.addEventListener('selectionchange', this.onSelectionChange_); |
| 323 | } else { |
| 324 | this.resetPos_(); |
| 325 | document.removeEventListener('selectionchange', this.onSelectionChange_); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | resetPos_() { |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 330 | this.pageUpButton.style.top = '-200px'; |
| 331 | this.pageDownButton.style.bottom = '-200px'; |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | onSelectionChange_() { |
| 335 | this.resetPos_(); |
| 336 | |
Jason Lin | 36b9fce | 2022-11-10 16:56:40 +1100 | [diff] [blame] | 337 | const selectedElement = document.getSelection().anchorNode?.parentElement; |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 338 | if (selectedElement === this.pageUpButton) { |
| 339 | this.pageUpButton.style.top = '16px'; |
| 340 | } else if (selectedElement === this.pageDownButton) { |
| 341 | this.pageDownButton.style.bottom = '16px'; |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 346 | const BACKGROUND_IMAGE_KEY = 'background-image'; |
| 347 | |
| 348 | class BackgroundImageWatcher { |
| 349 | /** |
| 350 | * @param {!hterm.PreferenceManager} prefs |
| 351 | * @param {function(string)} onChange This is called with the background image |
| 352 | * (could be empty) whenever it changes. |
| 353 | */ |
| 354 | constructor(prefs, onChange) { |
| 355 | this.prefs_ = prefs; |
| 356 | this.onChange_ = onChange; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Call once to start watching for background image changes. |
| 361 | */ |
| 362 | watch() { |
| 363 | window.addEventListener('storage', (e) => { |
| 364 | if (e.key === BACKGROUND_IMAGE_KEY) { |
| 365 | this.onChange_(this.getBackgroundImage()); |
| 366 | } |
| 367 | }); |
| 368 | this.prefs_.addObserver(BACKGROUND_IMAGE_KEY, () => { |
| 369 | this.onChange_(this.getBackgroundImage()); |
| 370 | }); |
| 371 | } |
| 372 | |
| 373 | getBackgroundImage() { |
| 374 | const image = window.localStorage.getItem(BACKGROUND_IMAGE_KEY); |
| 375 | if (image) { |
| 376 | return `url(${image})`; |
| 377 | } |
| 378 | |
| 379 | return this.prefs_.getString(BACKGROUND_IMAGE_KEY); |
| 380 | } |
| 381 | } |
| 382 | |
Jason Lin | b8f380a | 2022-10-25 13:15:56 +1100 | [diff] [blame] | 383 | let xtermTerminalStringsLoaded = false; |
| 384 | |
Jason Lin | 83707c9 | 2022-09-20 19:09:41 +1000 | [diff] [blame] | 385 | /** |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 386 | * A terminal class that 1) uses xterm.js and 2) behaves like a `hterm.Terminal` |
| 387 | * so that it can be used in existing code. |
| 388 | * |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 389 | * @extends {hterm.Terminal} |
| 390 | * @unrestricted |
| 391 | */ |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 392 | export class XtermTerminal { |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 393 | /** |
| 394 | * @param {{ |
| 395 | * storage: !lib.Storage, |
| 396 | * profileId: string, |
| 397 | * enableWebGL: boolean, |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 398 | * testParams: (!XtermTerminalTestParams|undefined), |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 399 | * }} args |
| 400 | */ |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 401 | constructor({storage, profileId, enableWebGL, testParams}) { |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 402 | this.ctrlCKeyDownHandler_ = this.ctrlCKeyDownHandler_.bind(this); |
| 403 | this.ctrlVKeyDownHandler_ = this.ctrlVKeyDownHandler_.bind(this); |
| 404 | this.zoomKeyDownHandler_ = this.zoomKeyDownHandler_.bind(this); |
| 405 | |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 406 | this.inited_ = false; |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 407 | this.profileId_ = profileId; |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 408 | /** @type {!hterm.PreferenceManager} */ |
| 409 | this.prefs_ = new hterm.PreferenceManager(storage, profileId); |
Jason Lin | c48f743 | 2022-10-13 17:28:30 +1100 | [diff] [blame] | 410 | definePrefs(this.prefs_); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 411 | this.enableWebGL_ = enableWebGL; |
| 412 | |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 413 | // TODO: we should probably pass the initial prefs to the ctor. |
Jason Lin | fc8a372 | 2022-09-07 17:49:18 +1000 | [diff] [blame] | 414 | this.term = testParams?.term || new Terminal({allowProposedApi: true}); |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 415 | this.xtermInternal_ = testParams?.xtermInternal || |
| 416 | new XtermInternal(this.term); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 417 | this.fontManager_ = testParams?.fontManager || fontManager; |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 418 | |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 419 | /** @type {?Element} */ |
| 420 | this.container_; |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 421 | this.bell_ = new Bell(); |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 422 | this.scheduleFit_ = delayedScheduler(() => this.fit_(), |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 423 | testParams ? 0 : 250); |
| 424 | |
Jason Lin | 83707c9 | 2022-09-20 19:09:41 +1000 | [diff] [blame] | 425 | this.term.loadAddon( |
| 426 | new WebLinksAddon((e, uri) => lib.f.openWindow(uri, '_blank'))); |
Jason Lin | 4de4f38 | 2022-09-01 14:10:18 +1000 | [diff] [blame] | 427 | this.term.loadAddon(new Unicode11Addon()); |
| 428 | this.term.unicode.activeVersion = '11'; |
| 429 | |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 430 | this.pendingFont_ = null; |
| 431 | this.scheduleRefreshFont_ = delayedScheduler( |
| 432 | () => this.refreshFont_(), 100); |
| 433 | document.fonts.addEventListener('loadingdone', |
| 434 | () => this.onFontLoadingDone_()); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 435 | |
| 436 | this.installUnimplementedStubs_(); |
Jason Lin | e9231bc | 2022-09-01 13:54:02 +1000 | [diff] [blame] | 437 | this.installEscapeSequenceHandlers_(); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 438 | |
Jason Lin | 34a4532 | 2022-10-12 19:10:52 +1100 | [diff] [blame] | 439 | this.term.onResize(({cols, rows}) => { |
| 440 | this.io.onTerminalResize(cols, rows); |
| 441 | if (this.prefs_.get('enable-resize-status')) { |
| 442 | this.showOverlay(`${cols} × ${rows}`); |
| 443 | } |
| 444 | }); |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 445 | // We could also use `this.io.sendString()` except for the nassh exit |
| 446 | // prompt, which only listens to onVTKeystroke(). |
| 447 | this.term.onData((data) => this.io.onVTKeystroke(data)); |
Jason Lin | 80e6913 | 2022-09-02 16:31:43 +1000 | [diff] [blame] | 448 | this.term.onBinary((data) => this.io.onVTKeystroke(data)); |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 449 | this.term.onTitleChange((title) => this.setWindowTitle(title)); |
Jason Lin | 83ef5ba | 2022-10-13 17:40:30 +1100 | [diff] [blame] | 450 | this.term.onSelectionChange(() => { |
| 451 | if (this.prefs_.get('copy-on-select')) { |
| 452 | this.copySelection_(); |
| 453 | } |
| 454 | }); |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 455 | this.term.onBell(() => this.ringBell()); |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * A mapping from key combo (see encodeKeyCombo()) to a handler function. |
| 459 | * |
| 460 | * If a key combo is in the map: |
| 461 | * |
| 462 | * - The handler instead of xterm.js will handle the keydown event. |
| 463 | * - Keyup and keypress will be ignored by both us and xterm.js. |
| 464 | * |
| 465 | * We re-generate this map every time a relevant pref value is changed. This |
| 466 | * is ok because pref changes are rare. |
| 467 | * |
| 468 | * @type {!Map<number, function(!KeyboardEvent)>} |
| 469 | */ |
| 470 | this.keyDownHandlers_ = new Map(); |
| 471 | this.scheduleResetKeyDownHandlers_ = |
| 472 | delayedScheduler(() => this.resetKeyDownHandlers_(), 250); |
| 473 | |
| 474 | this.term.attachCustomKeyEventHandler( |
| 475 | this.customKeyEventHandler_.bind(this)); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 476 | |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 477 | this.io = new XtermTerminalIO(this); |
| 478 | this.notificationCenter_ = null; |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 479 | this.htermA11yReader_ = null; |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 480 | this.a11yEnabled_ = false; |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 481 | this.a11yButtons_ = null; |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 482 | this.copyNotice_ = null; |
Jason Lin | 446f3d9 | 2022-10-13 17:34:21 +1100 | [diff] [blame] | 483 | this.scrollOnOutputListener_ = null; |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 484 | this.backgroundImageWatcher_ = new BackgroundImageWatcher(this.prefs_, |
| 485 | this.setBackgroundImage.bind(this)); |
| 486 | this.webglAddon_ = null; |
Jason Lin | a63d8ba | 2022-11-02 17:42:38 +1100 | [diff] [blame] | 487 | this.userCSSElement_ = null; |
| 488 | this.userCSSTextElement_ = null; |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 489 | |
Jason Lin | 82ba86c | 2022-11-09 12:12:27 +1100 | [diff] [blame] | 490 | this.contextMenu_ = /** @type {!TerminalContextMenu} */( |
| 491 | document.createElement('terminal-context-menu')); |
| 492 | this.contextMenu_.style.zIndex = 10; |
| 493 | this.contextMenu = { |
| 494 | setItems: (items) => this.contextMenu_.items = items, |
| 495 | }; |
| 496 | |
Jason Lin | 83707c9 | 2022-09-20 19:09:41 +1000 | [diff] [blame] | 497 | this.term.options.linkHandler = new LinkHandler(this.term); |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 498 | this.term.options.theme = { |
Jason Lin | 461ca56 | 2022-09-07 13:53:08 +1000 | [diff] [blame] | 499 | // The webgl cursor layer also paints the character under the cursor with |
| 500 | // this `cursorAccent` color. We use a completely transparent color here |
| 501 | // to effectively disable that. |
| 502 | cursorAccent: 'rgba(0, 0, 0, 0)', |
| 503 | customGlyphs: true, |
Jason Lin | 2edc25d | 2022-09-16 15:06:48 +1000 | [diff] [blame] | 504 | selectionBackground: 'rgba(174, 203, 250, .6)', |
| 505 | selectionInactiveBackground: 'rgba(218, 220, 224, .6)', |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 506 | selectionForeground: 'black', |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 507 | }; |
| 508 | this.observePrefs_(); |
Jason Lin | b8f380a | 2022-10-25 13:15:56 +1100 | [diff] [blame] | 509 | if (!xtermTerminalStringsLoaded) { |
| 510 | xtermTerminalStringsLoaded = true; |
| 511 | Terminal.strings.promptLabel = |
| 512 | hterm.messageManager.get('TERMINAL_INPUT_LABEL'); |
| 513 | Terminal.strings.tooMuchOutput = |
| 514 | hterm.messageManager.get('TERMINAL_TOO_MUCH_OUTPUT_MESSAGE'); |
| 515 | } |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 516 | } |
| 517 | |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 518 | /** @override */ |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 519 | setWindowTitle(title) { |
| 520 | document.title = title; |
| 521 | } |
| 522 | |
| 523 | /** @override */ |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 524 | ringBell() { |
| 525 | this.bell_.ring(); |
| 526 | } |
| 527 | |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 528 | /** @override */ |
| 529 | print(str) { |
| 530 | this.xtermInternal_.print(str); |
| 531 | } |
| 532 | |
| 533 | /** @override */ |
| 534 | wipeContents() { |
| 535 | this.term.clear(); |
| 536 | } |
| 537 | |
| 538 | /** @override */ |
| 539 | newLine() { |
| 540 | this.xtermInternal_.newLine(); |
| 541 | } |
| 542 | |
| 543 | /** @override */ |
| 544 | cursorLeft(number) { |
| 545 | this.xtermInternal_.cursorLeft(number ?? 1); |
| 546 | } |
| 547 | |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 548 | /** @override */ |
| 549 | setAccessibilityEnabled(enabled) { |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 550 | if (enabled === this.a11yEnabled_) { |
| 551 | return; |
| 552 | } |
| 553 | this.a11yEnabled_ = enabled; |
| 554 | |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 555 | this.a11yButtons_.setEnabled(enabled); |
| 556 | this.htermA11yReader_.setAccessibilityEnabled(enabled); |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 557 | |
| 558 | if (enabled) { |
| 559 | this.xtermInternal_.enableA11y(this.a11yButtons_.pageUpButton, |
| 560 | this.a11yButtons_.pageDownButton); |
| 561 | } else { |
| 562 | this.xtermInternal_.disableA11y(); |
| 563 | } |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 564 | } |
| 565 | |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 566 | hasBackgroundImage() { |
| 567 | return !!this.container_.style.backgroundImage; |
| 568 | } |
| 569 | |
| 570 | /** @override */ |
| 571 | setBackgroundImage(image) { |
| 572 | this.container_.style.backgroundImage = image || ''; |
| 573 | this.updateBackgroundColor_(this.prefs_.getString('background-color')); |
| 574 | } |
| 575 | |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 576 | /** |
| 577 | * Install stubs for stuff that we haven't implemented yet so that the code |
| 578 | * still runs. |
| 579 | */ |
| 580 | installUnimplementedStubs_() { |
| 581 | this.keyboard = { |
| 582 | keyMap: { |
| 583 | keyDefs: [], |
| 584 | }, |
| 585 | bindings: { |
| 586 | clear: () => {}, |
| 587 | addBinding: () => {}, |
| 588 | addBindings: () => {}, |
| 589 | OsDefaults: {}, |
| 590 | }, |
| 591 | }; |
| 592 | this.keyboard.keyMap.keyDefs[78] = {}; |
| 593 | |
| 594 | const methodNames = [ |
Joel Hockey | b89a978 | 2022-10-16 22:00:12 -0700 | [diff] [blame] | 595 | 'eraseLine', |
Joel Hockey | b89a978 | 2022-10-16 22:00:12 -0700 | [diff] [blame] | 596 | 'setCursorColumn', |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 597 | 'setCursorPosition', |
| 598 | 'setCursorVisible', |
Joel Hockey | d78374f | 2022-11-02 23:05:53 -0700 | [diff] [blame] | 599 | 'uninstallKeyboard', |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 600 | ]; |
| 601 | |
| 602 | for (const name of methodNames) { |
| 603 | this[name] = () => console.warn(`${name}() is not implemented`); |
| 604 | } |
| 605 | |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 606 | this.vt = { |
| 607 | resetParseState: () => { |
| 608 | console.warn('.vt.resetParseState() is not implemented'); |
| 609 | }, |
| 610 | }; |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 611 | } |
| 612 | |
Jason Lin | e9231bc | 2022-09-01 13:54:02 +1000 | [diff] [blame] | 613 | installEscapeSequenceHandlers_() { |
| 614 | // OSC 52 for copy. |
| 615 | this.term.parser.registerOscHandler(52, (args) => { |
| 616 | // Args comes in as a single 'clipboard;b64-data' string. The clipboard |
| 617 | // parameter is used to select which of the X clipboards to address. Since |
| 618 | // we're not integrating with X, we treat them all the same. |
| 619 | const parsedArgs = args.match(/^[cps01234567]*;(.*)/); |
| 620 | if (!parsedArgs) { |
| 621 | return true; |
| 622 | } |
| 623 | |
| 624 | let data; |
| 625 | try { |
| 626 | data = window.atob(parsedArgs[1]); |
| 627 | } catch (e) { |
| 628 | // If the user sent us invalid base64 content, silently ignore it. |
| 629 | return true; |
| 630 | } |
| 631 | const decoder = new TextDecoder(); |
| 632 | const bytes = lib.codec.stringToCodeUnitArray(data); |
| 633 | this.copyString_(decoder.decode(bytes)); |
| 634 | |
| 635 | return true; |
| 636 | }); |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 637 | |
| 638 | this.xtermInternal_.installTmuxControlModeHandler( |
| 639 | (data) => this.onTmuxControlModeLine(data)); |
| 640 | this.xtermInternal_.installEscKHandler(); |
Jason Lin | e9231bc | 2022-09-01 13:54:02 +1000 | [diff] [blame] | 641 | } |
| 642 | |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 643 | /** |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 644 | * Write data to the terminal. |
| 645 | * |
| 646 | * @param {string|!Uint8Array} data string for UTF-16 data, Uint8Array for |
| 647 | * UTF-8 data |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 648 | * @param {function()=} callback Optional callback that fires when the data |
| 649 | * was processed by the parser. |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 650 | */ |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 651 | write(data, callback) { |
| 652 | this.term.write(data, callback); |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Like `this.write()` but also write a line break. |
| 657 | * |
| 658 | * @param {string|!Uint8Array} data |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 659 | * @param {function()=} callback Optional callback that fires when the data |
| 660 | * was processed by the parser. |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 661 | */ |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 662 | writeln(data, callback) { |
| 663 | this.term.writeln(data, callback); |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 664 | } |
| 665 | |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 666 | get screenSize() { |
| 667 | return new hterm.Size(this.term.cols, this.term.rows); |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Don't need to do anything. |
| 672 | * |
| 673 | * @override |
| 674 | */ |
| 675 | installKeyboard() {} |
| 676 | |
| 677 | /** |
| 678 | * @override |
| 679 | */ |
| 680 | decorate(elem) { |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 681 | this.container_ = elem; |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 682 | elem.style.backgroundSize = '100% 100%'; |
| 683 | |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 684 | (async () => { |
| 685 | await new Promise((resolve) => this.prefs_.readStorage(resolve)); |
| 686 | // This will trigger all the observers to set the terminal options before |
| 687 | // we call `this.term.open()`. |
| 688 | this.prefs_.notifyAll(); |
| 689 | |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 690 | const screenPaddingSize = /** @type {number} */( |
| 691 | this.prefs_.get('screen-padding-size')); |
| 692 | elem.style.paddingTop = elem.style.paddingLeft = `${screenPaddingSize}px`; |
| 693 | |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 694 | this.setBackgroundImage( |
| 695 | this.backgroundImageWatcher_.getBackgroundImage()); |
| 696 | this.backgroundImageWatcher_.watch(); |
| 697 | |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 698 | this.inited_ = true; |
| 699 | this.term.open(elem); |
Jason Lin | 1be92f5 | 2023-01-23 23:50:00 +1100 | [diff] [blame] | 700 | this.xtermInternal_.addDimensionsObserver(() => this.scheduleFit_()); |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 701 | |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 702 | if (this.enableWebGL_) { |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 703 | this.reloadWebglAddon_(); |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 704 | } |
| 705 | this.term.focus(); |
| 706 | (new ResizeObserver(() => this.scheduleFit_())).observe(elem); |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 707 | this.htermA11yReader_ = new hterm.AccessibilityReader(elem); |
| 708 | this.notificationCenter_ = new hterm.NotificationCenter(document.body, |
| 709 | this.htermA11yReader_); |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 710 | |
Jason Lin | 82ba86c | 2022-11-09 12:12:27 +1100 | [diff] [blame] | 711 | elem.appendChild(this.contextMenu_); |
| 712 | |
Jason Lin | 932b743 | 2022-12-07 16:51:54 +1100 | [diff] [blame] | 713 | elem.addEventListener('dragover', (e) => e.preventDefault()); |
| 714 | elem.addEventListener('drop', |
| 715 | (e) => this.onDrop_(/** @type {!DragEvent} */(e))); |
| 716 | |
Jason Lin | 82ba86c | 2022-11-09 12:12:27 +1100 | [diff] [blame] | 717 | // Block the default context menu from popping up. |
Emil Mikulic | 2a194d0 | 2022-09-29 14:30:59 +1000 | [diff] [blame] | 718 | elem.addEventListener('contextmenu', (e) => e.preventDefault()); |
| 719 | |
| 720 | // Add a handler for pasting with the mouse. |
Jason Lin | 932b743 | 2022-12-07 16:51:54 +1100 | [diff] [blame] | 721 | elem.addEventListener('mousedown', |
| 722 | (e) => this.onMouseDown_(/** @type {!MouseEvent} */(e))); |
Emil Mikulic | 2a194d0 | 2022-09-29 14:30:59 +1000 | [diff] [blame] | 723 | |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 724 | await this.scheduleFit_(); |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 725 | this.a11yButtons_ = new A11yButtons(this.term, this.htermA11yReader_); |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 726 | |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 727 | this.onTerminalReady(); |
| 728 | })(); |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /** @override */ |
| 732 | showOverlay(msg, timeout = 1500) { |
Jason Lin | 34a4532 | 2022-10-12 19:10:52 +1100 | [diff] [blame] | 733 | this.notificationCenter_?.show(msg, {timeout}); |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /** @override */ |
| 737 | hideOverlay() { |
Jason Lin | 34a4532 | 2022-10-12 19:10:52 +1100 | [diff] [blame] | 738 | this.notificationCenter_?.hide(); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /** @override */ |
| 742 | getPrefs() { |
| 743 | return this.prefs_; |
| 744 | } |
| 745 | |
| 746 | /** @override */ |
| 747 | getDocument() { |
| 748 | return window.document; |
| 749 | } |
| 750 | |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 751 | /** @override */ |
| 752 | reset() { |
| 753 | this.term.reset(); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /** @override */ |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 757 | setProfile(profileId, callback = undefined) { |
| 758 | this.prefs_.setProfile(profileId, callback); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 759 | } |
| 760 | |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 761 | /** @override */ |
| 762 | interpret(string) { |
| 763 | this.term.write(string); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 764 | } |
| 765 | |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 766 | /** @override */ |
| 767 | focus() { |
| 768 | this.term.focus(); |
| 769 | } |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 770 | |
| 771 | /** @override */ |
| 772 | onOpenOptionsPage() {} |
| 773 | |
| 774 | /** @override */ |
| 775 | onTerminalReady() {} |
| 776 | |
Jason Lin | d04bab3 | 2022-08-22 14:48:39 +1000 | [diff] [blame] | 777 | observePrefs_() { |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 778 | // This is for this.notificationCenter_. |
| 779 | const setHtermCSSVariable = (name, value) => { |
| 780 | document.body.style.setProperty(`--hterm-${name}`, value); |
| 781 | }; |
| 782 | |
| 783 | const setHtermColorCSSVariable = (name, color) => { |
| 784 | const css = lib.notNull(lib.colors.normalizeCSS(color)); |
| 785 | const rgb = lib.colors.crackRGB(css).slice(0, 3).join(','); |
| 786 | setHtermCSSVariable(name, rgb); |
| 787 | }; |
| 788 | |
| 789 | this.prefs_.addObserver('font-size', (v) => { |
Jason Lin | 1be92f5 | 2023-01-23 23:50:00 +1100 | [diff] [blame] | 790 | this.term.options.fontSize = v; |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 791 | setHtermCSSVariable('font-size', `${v}px`); |
| 792 | }); |
| 793 | |
Jason Lin | da56aa9 | 2022-09-02 13:01:49 +1000 | [diff] [blame] | 794 | // TODO(lxj): support option "lineHeight", "scrollback". |
Jason Lin | d04bab3 | 2022-08-22 14:48:39 +1000 | [diff] [blame] | 795 | this.prefs_.addObservers(null, { |
Jason Lin | da56aa9 | 2022-09-02 13:01:49 +1000 | [diff] [blame] | 796 | 'audible-bell-sound': (v) => { |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 797 | this.bell_.playAudio = !!v; |
| 798 | }, |
| 799 | 'desktop-notification-bell': (v) => { |
| 800 | this.bell_.showNotification = v; |
Jason Lin | da56aa9 | 2022-09-02 13:01:49 +1000 | [diff] [blame] | 801 | }, |
Jason Lin | d04bab3 | 2022-08-22 14:48:39 +1000 | [diff] [blame] | 802 | 'background-color': (v) => { |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 803 | this.updateBackgroundColor_(v); |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 804 | setHtermColorCSSVariable('background-color', v); |
Jason Lin | d04bab3 | 2022-08-22 14:48:39 +1000 | [diff] [blame] | 805 | }, |
Jason Lin | d04bab3 | 2022-08-22 14:48:39 +1000 | [diff] [blame] | 806 | 'color-palette-overrides': (v) => { |
| 807 | if (!(v instanceof Array)) { |
| 808 | // For terminal, we always expect this to be an array. |
| 809 | console.warn('unexpected color palette: ', v); |
| 810 | return; |
| 811 | } |
| 812 | const colors = {}; |
| 813 | for (let i = 0; i < v.length; ++i) { |
| 814 | colors[ANSI_COLOR_NAMES[i]] = v[i]; |
| 815 | } |
| 816 | this.updateTheme_(colors); |
| 817 | }, |
Jason Lin | 1be92f5 | 2023-01-23 23:50:00 +1100 | [diff] [blame] | 818 | 'cursor-blink': (v) => { |
| 819 | this.term.options.cursorBlink = v; |
| 820 | }, |
Jason Lin | da56aa9 | 2022-09-02 13:01:49 +1000 | [diff] [blame] | 821 | 'cursor-color': (v) => this.updateTheme_({cursor: v}), |
| 822 | 'cursor-shape': (v) => { |
| 823 | let shape; |
| 824 | if (v === 'BEAM') { |
| 825 | shape = 'bar'; |
| 826 | } else { |
| 827 | shape = v.toLowerCase(); |
| 828 | } |
Jason Lin | 1be92f5 | 2023-01-23 23:50:00 +1100 | [diff] [blame] | 829 | this.term.options.cursorStyle = shape; |
Jason Lin | da56aa9 | 2022-09-02 13:01:49 +1000 | [diff] [blame] | 830 | }, |
| 831 | 'font-family': (v) => this.updateFont_(v), |
| 832 | 'foreground-color': (v) => { |
Jason Lin | 461ca56 | 2022-09-07 13:53:08 +1000 | [diff] [blame] | 833 | this.updateTheme_({foreground: v}); |
Jason Lin | da56aa9 | 2022-09-02 13:01:49 +1000 | [diff] [blame] | 834 | setHtermColorCSSVariable('foreground-color', v); |
| 835 | }, |
Jason Lin | 1be92f5 | 2023-01-23 23:50:00 +1100 | [diff] [blame] | 836 | 'line-height': (v) => { |
| 837 | this.term.options.lineHeight = v; |
| 838 | }, |
Jason Lin | 471e106 | 2022-12-08 15:39:15 +1100 | [diff] [blame] | 839 | 'scroll-on-keystroke': (v) => { |
Jason Lin | 1be92f5 | 2023-01-23 23:50:00 +1100 | [diff] [blame] | 840 | this.term.options.scrollOnUserInput = v; |
Jason Lin | 471e106 | 2022-12-08 15:39:15 +1100 | [diff] [blame] | 841 | }, |
Jason Lin | 446f3d9 | 2022-10-13 17:34:21 +1100 | [diff] [blame] | 842 | 'scroll-on-output': (v) => { |
| 843 | if (!v) { |
| 844 | this.scrollOnOutputListener_?.dispose(); |
| 845 | this.scrollOnOutputListener_ = null; |
| 846 | return; |
| 847 | } |
| 848 | if (!this.scrollOnOutputListener_) { |
| 849 | this.scrollOnOutputListener_ = this.term.onWriteParsed( |
| 850 | () => this.term.scrollToBottom()); |
| 851 | } |
| 852 | }, |
Jason Lin | a63d8ba | 2022-11-02 17:42:38 +1100 | [diff] [blame] | 853 | 'user-css': (v) => { |
| 854 | if (this.userCSSElement_) { |
| 855 | this.userCSSElement_.remove(); |
| 856 | } |
| 857 | if (v) { |
| 858 | this.userCSSElement_ = document.createElement('link'); |
| 859 | this.userCSSElement_.setAttribute('rel', 'stylesheet'); |
| 860 | this.userCSSElement_.setAttribute('href', v); |
| 861 | document.head.appendChild(this.userCSSElement_); |
| 862 | } |
| 863 | }, |
| 864 | 'user-css-text': (v) => { |
| 865 | if (!this.userCSSTextElement_) { |
| 866 | this.userCSSTextElement_ = document.createElement('style'); |
| 867 | document.head.appendChild(this.userCSSTextElement_); |
| 868 | } |
| 869 | this.userCSSTextElement_.textContent = v; |
| 870 | }, |
Jason Lin | d04bab3 | 2022-08-22 14:48:39 +1000 | [diff] [blame] | 871 | }); |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 872 | |
| 873 | for (const name of ['keybindings-os-defaults', 'pass-ctrl-n', 'pass-ctrl-t', |
| 874 | 'pass-ctrl-w', 'pass-ctrl-tab', 'pass-ctrl-number', 'pass-alt-number', |
| 875 | 'ctrl-plus-minus-zero-zoom', 'ctrl-c-copy', 'ctrl-v-paste']) { |
| 876 | this.prefs_.addObserver(name, this.scheduleResetKeyDownHandlers_); |
| 877 | } |
Jason Lin | d04bab3 | 2022-08-22 14:48:39 +1000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /** |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 881 | * Fit the terminal to the containing HTML element. |
| 882 | */ |
| 883 | fit_() { |
| 884 | if (!this.inited_) { |
| 885 | return; |
| 886 | } |
| 887 | |
| 888 | const screenPaddingSize = /** @type {number} */( |
| 889 | this.prefs_.get('screen-padding-size')); |
| 890 | |
| 891 | const calc = (size, cellSize) => { |
| 892 | return Math.floor((size - 2 * screenPaddingSize) / cellSize); |
| 893 | }; |
| 894 | |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 895 | const cellDimensions = this.xtermInternal_.getActualCellDimensions(); |
| 896 | const cols = calc(this.container_.offsetWidth, cellDimensions.width); |
| 897 | const rows = calc(this.container_.offsetHeight, cellDimensions.height); |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 898 | if (cols >= 0 && rows >= 0) { |
| 899 | this.term.resize(cols, rows); |
| 900 | } |
| 901 | } |
| 902 | |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 903 | reloadWebglAddon_() { |
| 904 | if (this.webglAddon_) { |
| 905 | this.webglAddon_.dispose(); |
| 906 | } |
| 907 | this.webglAddon_ = new WebglAddon(); |
| 908 | this.term.loadAddon(this.webglAddon_); |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * Update the background color. This will also adjust the transparency based |
| 913 | * on whether there is a background image. |
| 914 | * |
| 915 | * @param {string} color |
| 916 | */ |
| 917 | updateBackgroundColor_(color) { |
| 918 | const hasBackgroundImage = this.hasBackgroundImage(); |
| 919 | |
| 920 | // We only set allowTransparency when it is necessary becuase 1) xterm.js |
| 921 | // documentation states that allowTransparency can affect performance; 2) I |
| 922 | // find that the rendering is better with allowTransparency being false. |
| 923 | // This could be a bug with xterm.js. |
| 924 | if (!!this.term.options.allowTransparency !== hasBackgroundImage) { |
| 925 | this.term.options.allowTransparency = hasBackgroundImage; |
| 926 | if (this.enableWebGL_ && this.inited_) { |
| 927 | // Setting allowTransparency in the middle messes up webgl rendering, |
| 928 | // so we need to reload it here. |
| 929 | this.reloadWebglAddon_(); |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | if (this.hasBackgroundImage()) { |
| 934 | const css = lib.notNull(lib.colors.normalizeCSS(color)); |
| 935 | const rgb = lib.colors.crackRGB(css).slice(0, 3).join(','); |
| 936 | // Note that we still want to set the RGB part correctly even though it is |
| 937 | // completely transparent. This is because the background color without |
| 938 | // the alpha channel is used in reverse video mode. |
| 939 | color = `rgba(${rgb}, 0)`; |
| 940 | } |
| 941 | |
| 942 | this.updateTheme_({background: color}); |
| 943 | } |
| 944 | |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 945 | /** |
Jason Lin | d04bab3 | 2022-08-22 14:48:39 +1000 | [diff] [blame] | 946 | * @param {!Object} theme |
| 947 | */ |
| 948 | updateTheme_(theme) { |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 949 | const updateTheme = (target) => { |
| 950 | for (const [key, value] of Object.entries(theme)) { |
| 951 | target[key] = lib.colors.normalizeCSS(value); |
| 952 | } |
| 953 | }; |
| 954 | |
| 955 | // Must use a new theme object to trigger re-render if we have initialized. |
| 956 | if (this.inited_) { |
| 957 | const newTheme = {...this.term.options.theme}; |
| 958 | updateTheme(newTheme); |
| 959 | this.term.options.theme = newTheme; |
| 960 | return; |
Jason Lin | d04bab3 | 2022-08-22 14:48:39 +1000 | [diff] [blame] | 961 | } |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 962 | |
| 963 | updateTheme(this.term.options.theme); |
Jason Lin | d04bab3 | 2022-08-22 14:48:39 +1000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /** |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 967 | * Called when there is a "fontloadingdone" event. We need this because |
| 968 | * `FontManager.loadFont()` does not guarantee loading all the font files. |
| 969 | */ |
| 970 | async onFontLoadingDone_() { |
| 971 | // If there is a pending font, the font is going to be refresh soon, so we |
| 972 | // don't need to do anything. |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 973 | if (this.inited_ && !this.pendingFont_) { |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 974 | this.scheduleRefreshFont_(); |
| 975 | } |
| 976 | } |
| 977 | |
Jason Lin | 932b743 | 2022-12-07 16:51:54 +1100 | [diff] [blame] | 978 | /** |
| 979 | * @param {!DragEvent} e |
| 980 | */ |
| 981 | onDrop_(e) { |
| 982 | e.preventDefault(); |
| 983 | |
| 984 | // If the shift key active, try to find a "rich" text source (but not plain |
| 985 | // text). e.g. text/html is OK. This is the same behavior as hterm. |
| 986 | if (e.shiftKey) { |
| 987 | for (const type of e.dataTransfer.types) { |
| 988 | if (type !== 'text/plain' && type.startsWith('text/')) { |
| 989 | this.term.paste(e.dataTransfer.getData(type)); |
| 990 | return; |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | this.term.paste(e.dataTransfer.getData('text/plain')); |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * @param {!MouseEvent} e |
| 1000 | */ |
| 1001 | async onMouseDown_(e) { |
Jason Lin | 932b743 | 2022-12-07 16:51:54 +1100 | [diff] [blame] | 1002 | if (this.term.modes.mouseTrackingMode !== 'none') { |
| 1003 | // xterm.js is in mouse mode and will handle the event. |
| 1004 | return; |
| 1005 | } |
| 1006 | const MIDDLE = 1; |
| 1007 | const RIGHT = 2; |
| 1008 | |
| 1009 | if (e.button === RIGHT && e.ctrlKey) { |
| 1010 | this.contextMenu_.show({x: e.clientX, y: e.clientY}); |
| 1011 | return; |
| 1012 | } |
| 1013 | |
| 1014 | if (e.button === MIDDLE || (e.button === RIGHT && |
| 1015 | this.prefs_.getBoolean('mouse-right-click-paste'))) { |
| 1016 | // Paste. |
| 1017 | if (navigator.clipboard && navigator.clipboard.readText) { |
| 1018 | const text = await navigator.clipboard.readText(); |
| 1019 | this.term.paste(text); |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 1024 | copySelection_() { |
Jason Lin | e9231bc | 2022-09-01 13:54:02 +1000 | [diff] [blame] | 1025 | this.copyString_(this.term.getSelection()); |
| 1026 | } |
| 1027 | |
| 1028 | /** @param {string} data */ |
| 1029 | copyString_(data) { |
| 1030 | if (!data) { |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 1031 | return; |
| 1032 | } |
Jason Lin | e9231bc | 2022-09-01 13:54:02 +1000 | [diff] [blame] | 1033 | navigator.clipboard?.writeText(data); |
Jason Lin | 83ef5ba | 2022-10-13 17:40:30 +1100 | [diff] [blame] | 1034 | |
| 1035 | if (this.prefs_.get('enable-clipboard-notice')) { |
| 1036 | if (!this.copyNotice_) { |
| 1037 | this.copyNotice_ = document.createElement('terminal-copy-notice'); |
| 1038 | } |
| 1039 | setTimeout(() => this.showOverlay(lib.notNull(this.copyNotice_), 500), |
| 1040 | 200); |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 1041 | } |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 1042 | } |
| 1043 | |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 1044 | /** |
| 1045 | * Refresh xterm rendering for a font related event. |
| 1046 | */ |
| 1047 | refreshFont_() { |
| 1048 | // We have to set the fontFamily option to a different string to trigger the |
| 1049 | // re-rendering. Appending a space at the end seems to be the easiest |
| 1050 | // solution. Note that `clearTextureAtlas()` and `refresh()` do not work for |
| 1051 | // us. |
| 1052 | // |
| 1053 | // TODO: Report a bug to xterm.js and ask for exposing a public function for |
| 1054 | // the refresh so that we don't need to do this hack. |
| 1055 | this.term.options.fontFamily += ' '; |
| 1056 | } |
| 1057 | |
| 1058 | /** |
| 1059 | * Update a font. |
| 1060 | * |
| 1061 | * @param {string} cssFontFamily |
| 1062 | */ |
| 1063 | async updateFont_(cssFontFamily) { |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 1064 | this.pendingFont_ = cssFontFamily; |
| 1065 | await this.fontManager_.loadFont(cssFontFamily); |
| 1066 | // Sleep a bit to wait for flushing fontloadingdone events. This is not |
| 1067 | // strictly necessary, but it should prevent `this.onFontLoadingDone_()` |
| 1068 | // to refresh font unnecessarily in some cases. |
| 1069 | await sleep(30); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 1070 | |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 1071 | if (this.pendingFont_ !== cssFontFamily) { |
| 1072 | // `updateFont_()` probably is called again. Abort what we are doing. |
| 1073 | console.log(`pendingFont_ (${this.pendingFont_}) is changed` + |
| 1074 | ` (expecting ${cssFontFamily})`); |
| 1075 | return; |
| 1076 | } |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 1077 | |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 1078 | if (this.term.options.fontFamily !== cssFontFamily) { |
| 1079 | this.term.options.fontFamily = cssFontFamily; |
| 1080 | } else { |
| 1081 | // If the font is already the same, refresh font just to be safe. |
| 1082 | this.refreshFont_(); |
| 1083 | } |
| 1084 | this.pendingFont_ = null; |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 1085 | } |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 1086 | |
| 1087 | /** |
| 1088 | * @param {!KeyboardEvent} ev |
| 1089 | * @return {boolean} Return false if xterm.js should not handle the key event. |
| 1090 | */ |
| 1091 | customKeyEventHandler_(ev) { |
Jason Lin | 646dde0 | 2023-01-10 22:33:02 +1100 | [diff] [blame] | 1092 | // Without this, <alt-tab> (or <alt-shift-tab) is consumed by xterm.js |
| 1093 | // (instead the OS) when terminal is full screen. |
| 1094 | if (ev.altKey && ev.keyCode === 9) { |
| 1095 | return false; |
| 1096 | } |
| 1097 | |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 1098 | const modifiers = (ev.shiftKey ? Modifier.Shift : 0) | |
| 1099 | (ev.altKey ? Modifier.Alt : 0) | |
| 1100 | (ev.ctrlKey ? Modifier.Ctrl : 0) | |
| 1101 | (ev.metaKey ? Modifier.Meta : 0); |
| 1102 | const handler = this.keyDownHandlers_.get( |
| 1103 | encodeKeyCombo(modifiers, ev.keyCode)); |
| 1104 | if (handler) { |
| 1105 | if (ev.type === 'keydown') { |
| 1106 | handler(ev); |
| 1107 | } |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
| 1111 | return true; |
| 1112 | } |
| 1113 | |
| 1114 | /** |
| 1115 | * A keydown handler for zoom-related keys. |
| 1116 | * |
| 1117 | * @param {!KeyboardEvent} ev |
| 1118 | */ |
| 1119 | zoomKeyDownHandler_(ev) { |
| 1120 | ev.preventDefault(); |
| 1121 | |
| 1122 | if (this.prefs_.get('ctrl-plus-minus-zero-zoom') === ev.shiftKey) { |
| 1123 | // The only one with a control code. |
| 1124 | if (ev.keyCode === keyCodes.MINUS) { |
| 1125 | this.io.onVTKeystroke('\x1f'); |
| 1126 | } |
| 1127 | return; |
| 1128 | } |
| 1129 | |
| 1130 | let newFontSize; |
| 1131 | switch (ev.keyCode) { |
| 1132 | case keyCodes.ZERO: |
| 1133 | newFontSize = this.prefs_.get('font-size'); |
| 1134 | break; |
| 1135 | case keyCodes.MINUS: |
| 1136 | newFontSize = this.term.options.fontSize - 1; |
| 1137 | break; |
| 1138 | default: |
| 1139 | newFontSize = this.term.options.fontSize + 1; |
| 1140 | break; |
| 1141 | } |
| 1142 | |
Jason Lin | 1be92f5 | 2023-01-23 23:50:00 +1100 | [diff] [blame] | 1143 | this.term.options.fontSize = Math.max(1, newFontSize); |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /** @param {!KeyboardEvent} ev */ |
| 1147 | ctrlCKeyDownHandler_(ev) { |
| 1148 | ev.preventDefault(); |
| 1149 | if (this.prefs_.get('ctrl-c-copy') !== ev.shiftKey && |
| 1150 | this.term.hasSelection()) { |
| 1151 | this.copySelection_(); |
| 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | this.io.onVTKeystroke('\x03'); |
| 1156 | } |
| 1157 | |
| 1158 | /** @param {!KeyboardEvent} ev */ |
| 1159 | ctrlVKeyDownHandler_(ev) { |
| 1160 | if (this.prefs_.get('ctrl-v-paste') !== ev.shiftKey) { |
| 1161 | // Don't do anything and let the browser handles the key. |
| 1162 | return; |
| 1163 | } |
| 1164 | |
| 1165 | ev.preventDefault(); |
| 1166 | this.io.onVTKeystroke('\x16'); |
| 1167 | } |
| 1168 | |
| 1169 | resetKeyDownHandlers_() { |
| 1170 | this.keyDownHandlers_.clear(); |
| 1171 | |
| 1172 | /** |
| 1173 | * Don't do anything and let the browser handles the key. |
| 1174 | * |
| 1175 | * @param {!KeyboardEvent} ev |
| 1176 | */ |
| 1177 | const noop = (ev) => {}; |
| 1178 | |
| 1179 | /** |
| 1180 | * @param {number} modifiers |
| 1181 | * @param {number} keyCode |
| 1182 | * @param {function(!KeyboardEvent)} func |
| 1183 | */ |
| 1184 | const set = (modifiers, keyCode, func) => { |
| 1185 | this.keyDownHandlers_.set(encodeKeyCombo(modifiers, keyCode), |
| 1186 | func); |
| 1187 | }; |
| 1188 | |
| 1189 | /** |
| 1190 | * @param {number} modifiers |
| 1191 | * @param {number} keyCode |
| 1192 | * @param {function(!KeyboardEvent)} func |
| 1193 | */ |
| 1194 | const setWithShiftVersion = (modifiers, keyCode, func) => { |
| 1195 | set(modifiers, keyCode, func); |
| 1196 | set(modifiers | Modifier.Shift, keyCode, func); |
| 1197 | }; |
| 1198 | |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 1199 | // Ctrl+/ |
| 1200 | set(Modifier.Ctrl, 191, (ev) => { |
| 1201 | ev.preventDefault(); |
| 1202 | this.io.onVTKeystroke(ctl('_')); |
| 1203 | }); |
| 1204 | |
| 1205 | // Settings page. |
| 1206 | set(Modifier.Ctrl | Modifier.Shift, keyCodes.P, (ev) => { |
| 1207 | ev.preventDefault(); |
| 1208 | chrome.terminalPrivate.openOptionsPage(() => {}); |
| 1209 | }); |
| 1210 | |
| 1211 | if (this.prefs_.get('keybindings-os-defaults')) { |
| 1212 | for (const binding of OS_DEFAULT_BINDINGS) { |
| 1213 | this.keyDownHandlers_.set(binding, noop); |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | /** @param {!KeyboardEvent} ev */ |
| 1218 | const newWindow = (ev) => { |
| 1219 | ev.preventDefault(); |
| 1220 | chrome.terminalPrivate.openWindow(); |
| 1221 | }; |
| 1222 | set(Modifier.Ctrl | Modifier.Shift, keyCodes.N, newWindow); |
| 1223 | if (this.prefs_.get('pass-ctrl-n')) { |
| 1224 | set(Modifier.Ctrl, keyCodes.N, newWindow); |
| 1225 | } |
| 1226 | |
| 1227 | if (this.prefs_.get('pass-ctrl-t')) { |
| 1228 | setWithShiftVersion(Modifier.Ctrl, keyCodes.T, noop); |
| 1229 | } |
| 1230 | |
| 1231 | if (this.prefs_.get('pass-ctrl-w')) { |
| 1232 | setWithShiftVersion(Modifier.Ctrl, keyCodes.W, noop); |
| 1233 | } |
| 1234 | |
| 1235 | if (this.prefs_.get('pass-ctrl-tab')) { |
| 1236 | setWithShiftVersion(Modifier.Ctrl, keyCodes.TAB, noop); |
| 1237 | } |
| 1238 | |
| 1239 | const passCtrlNumber = this.prefs_.get('pass-ctrl-number'); |
| 1240 | |
| 1241 | /** |
| 1242 | * Set a handler for the key combo ctrl+<number>. |
| 1243 | * |
| 1244 | * @param {number} number 1 to 9 |
| 1245 | * @param {string} controlCode The control code to send if we don't want to |
| 1246 | * let the browser to handle it. |
| 1247 | */ |
| 1248 | const setCtrlNumberHandler = (number, controlCode) => { |
| 1249 | let func = noop; |
| 1250 | if (!passCtrlNumber) { |
| 1251 | func = (ev) => { |
| 1252 | ev.preventDefault(); |
| 1253 | this.io.onVTKeystroke(controlCode); |
| 1254 | }; |
| 1255 | } |
| 1256 | set(Modifier.Ctrl, keyCodes.ZERO + number, func); |
| 1257 | }; |
| 1258 | |
| 1259 | setCtrlNumberHandler(1, '1'); |
| 1260 | setCtrlNumberHandler(2, ctl('@')); |
| 1261 | setCtrlNumberHandler(3, ctl('[')); |
| 1262 | setCtrlNumberHandler(4, ctl('\\')); |
| 1263 | setCtrlNumberHandler(5, ctl(']')); |
| 1264 | setCtrlNumberHandler(6, ctl('^')); |
| 1265 | setCtrlNumberHandler(7, ctl('_')); |
| 1266 | setCtrlNumberHandler(8, '\x7f'); |
| 1267 | setCtrlNumberHandler(9, '9'); |
| 1268 | |
| 1269 | if (this.prefs_.get('pass-alt-number')) { |
| 1270 | for (let keyCode = keyCodes.ZERO; keyCode <= keyCodes.NINE; ++keyCode) { |
| 1271 | set(Modifier.Alt, keyCode, noop); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | for (const keyCode of [keyCodes.ZERO, keyCodes.MINUS, keyCodes.EQUAL]) { |
| 1276 | setWithShiftVersion(Modifier.Ctrl, keyCode, this.zoomKeyDownHandler_); |
| 1277 | } |
| 1278 | |
| 1279 | setWithShiftVersion(Modifier.Ctrl, keyCodes.C, this.ctrlCKeyDownHandler_); |
| 1280 | setWithShiftVersion(Modifier.Ctrl, keyCodes.V, this.ctrlVKeyDownHandler_); |
| 1281 | } |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 1282 | |
| 1283 | handleOnTerminalReady() {} |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 1284 | } |
| 1285 | |
Jason Lin | d66e6bf | 2022-08-22 14:47:10 +1000 | [diff] [blame] | 1286 | class HtermTerminal extends hterm.Terminal { |
| 1287 | /** @override */ |
| 1288 | decorate(div) { |
| 1289 | super.decorate(div); |
| 1290 | |
Jason Lin | c48f743 | 2022-10-13 17:28:30 +1100 | [diff] [blame] | 1291 | definePrefs(this.getPrefs()); |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 1292 | } |
Jason Lin | c48f743 | 2022-10-13 17:28:30 +1100 | [diff] [blame] | 1293 | |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 1294 | /** |
| 1295 | * This needs to be called in the `onTerminalReady()` callback. This is |
| 1296 | * awkward, but it is temporary since we will drop support for hterm at some |
| 1297 | * point. |
| 1298 | */ |
| 1299 | handleOnTerminalReady() { |
Jason Lin | d66e6bf | 2022-08-22 14:47:10 +1000 | [diff] [blame] | 1300 | const fontManager = new FontManager(this.getDocument()); |
| 1301 | fontManager.loadPowerlineCSS().then(() => { |
| 1302 | const prefs = this.getPrefs(); |
| 1303 | fontManager.loadFont(/** @type {string} */(prefs.get('font-family'))); |
| 1304 | prefs.addObserver( |
| 1305 | 'font-family', |
| 1306 | (v) => fontManager.loadFont(/** @type {string} */(v))); |
| 1307 | }); |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 1308 | |
| 1309 | const backgroundImageWatcher = new BackgroundImageWatcher(this.getPrefs(), |
| 1310 | (image) => this.setBackgroundImage(image)); |
| 1311 | this.setBackgroundImage(backgroundImageWatcher.getBackgroundImage()); |
| 1312 | backgroundImageWatcher.watch(); |
Jason Lin | d66e6bf | 2022-08-22 14:47:10 +1000 | [diff] [blame] | 1313 | } |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 1314 | |
| 1315 | /** |
| 1316 | * Write data to the terminal. |
| 1317 | * |
| 1318 | * @param {string|!Uint8Array} data string for UTF-16 data, Uint8Array for |
| 1319 | * UTF-8 data |
| 1320 | * @param {function()=} callback Optional callback that fires when the data |
| 1321 | * was processed by the parser. |
| 1322 | */ |
| 1323 | write(data, callback) { |
| 1324 | if (typeof data === 'string') { |
| 1325 | this.io.print(data); |
| 1326 | } else { |
| 1327 | this.io.writeUTF8(data); |
| 1328 | } |
| 1329 | // Hterm processes the data synchronously, so we can call the callback |
| 1330 | // immediately. |
| 1331 | if (callback) { |
| 1332 | setTimeout(callback); |
| 1333 | } |
| 1334 | } |
Jason Lin | d66e6bf | 2022-08-22 14:47:10 +1000 | [diff] [blame] | 1335 | } |
| 1336 | |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 1337 | /** |
| 1338 | * Constructs and returns a `hterm.Terminal` or a compatible one based on the |
| 1339 | * preference value. |
| 1340 | * |
| 1341 | * @param {{ |
| 1342 | * storage: !lib.Storage, |
| 1343 | * profileId: string, |
| 1344 | * }} args |
| 1345 | * @return {!Promise<!hterm.Terminal>} |
| 1346 | */ |
| 1347 | export async function createEmulator({storage, profileId}) { |
| 1348 | let config = TERMINAL_EMULATORS.get('hterm'); |
| 1349 | |
| 1350 | if (getOSInfo().alternative_emulator) { |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 1351 | // TODO: remove the url param logic. This is temporary to make manual |
| 1352 | // testing a bit easier, which is also why this is not in |
| 1353 | // './js/terminal_info.js'. |
Jason Lin | e10d6c4 | 2022-11-11 16:04:32 +1100 | [diff] [blame] | 1354 | const emulator = ORIGINAL_URL.searchParams.get('emulator'); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 1355 | // Use the default (i.e. first) one if the pref is not set or invalid. |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 1356 | config = TERMINAL_EMULATORS.get(emulator) || |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 1357 | TERMINAL_EMULATORS.values().next().value; |
| 1358 | console.log('Terminal emulator config: ', config); |
| 1359 | } |
| 1360 | |
| 1361 | switch (config.lib) { |
| 1362 | case 'xterm.js': |
| 1363 | { |
| 1364 | const terminal = new XtermTerminal({ |
| 1365 | storage, |
| 1366 | profileId, |
| 1367 | enableWebGL: config.webgl, |
| 1368 | }); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 1369 | return terminal; |
| 1370 | } |
| 1371 | case 'hterm': |
Jason Lin | d66e6bf | 2022-08-22 14:47:10 +1000 | [diff] [blame] | 1372 | return new HtermTerminal({profileId, storage}); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 1373 | default: |
| 1374 | throw new Error('incorrect emulator config'); |
| 1375 | } |
| 1376 | } |
| 1377 | |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 1378 | class TerminalCopyNotice extends LitElement { |
| 1379 | /** @override */ |
| 1380 | static get styles() { |
| 1381 | return css` |
| 1382 | :host { |
| 1383 | display: block; |
| 1384 | text-align: center; |
| 1385 | } |
| 1386 | |
| 1387 | svg { |
| 1388 | fill: currentColor; |
| 1389 | } |
| 1390 | `; |
| 1391 | } |
| 1392 | |
| 1393 | /** @override */ |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 1394 | connectedCallback() { |
| 1395 | super.connectedCallback(); |
| 1396 | if (!this.childNodes.length) { |
| 1397 | // This is not visible since we use shadow dom. But this will allow the |
| 1398 | // hterm.NotificationCenter to announce the the copy text. |
| 1399 | this.append(hterm.messageManager.get('HTERM_NOTIFY_COPY')); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | /** @override */ |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 1404 | render() { |
| 1405 | return html` |
| 1406 | ${ICON_COPY} |
| 1407 | <div>${hterm.messageManager.get('HTERM_NOTIFY_COPY')}</div> |
| 1408 | `; |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | customElements.define('terminal-copy-notice', TerminalCopyNotice); |