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