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 | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 17 | import {FontManager, ORIGINAL_URL, backgroundImageLocalStorageKey, 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 | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 22 | import {Terminal, CanvasAddon, Unicode11Addon, WebLinksAddon, WebglAddon} |
Joel Hockey | 0e164f0 | 2023-03-26 15:58:12 -0700 | [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. |
Jason Lin | 97a0428 | 2023-03-06 10:36:56 +1100 | [diff] [blame] | 37 | export const keyCodes = hterm.Parser.identifiers.keyCodes; |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 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 | 97a0428 | 2023-03-06 10:36:56 +1100 | [diff] [blame] | 88 | * The value is the CSI code to send when no modifier keys are pressed. |
| 89 | * |
| 90 | * @type {!Map<number, string>} |
| 91 | */ |
| 92 | const ARROW_AND_SIX_PACK_KEYS = new Map([ |
| 93 | [keyCodes.UP, '\x1b[A'], |
| 94 | [keyCodes.DOWN, '\x1b[B'], |
| 95 | [keyCodes.RIGHT, '\x1b[C'], |
| 96 | [keyCodes.LEFT, '\x1b[D'], |
| 97 | // 6-pack keys. |
| 98 | [keyCodes.INSERT, '\x1b[2~'], |
| 99 | [keyCodes.DEL, '\x1b[3~'], |
| 100 | [keyCodes.HOME, '\x1b[H'], |
| 101 | [keyCodes.END, '\x1b[F'], |
| 102 | [keyCodes.PAGE_UP, '\x1b[5~'], |
| 103 | [keyCodes.PAGE_DOWN, '\x1b[6~'], |
| 104 | ]); |
| 105 | |
| 106 | /** |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 107 | * @typedef {{ |
| 108 | * term: !Terminal, |
| 109 | * fontManager: !FontManager, |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 110 | * xtermInternal: !XtermInternal, |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 111 | * }} |
| 112 | */ |
| 113 | export let XtermTerminalTestParams; |
| 114 | |
| 115 | /** |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 116 | * Compute a control character for a given character. |
| 117 | * |
| 118 | * @param {string} ch |
| 119 | * @return {string} |
| 120 | */ |
| 121 | function ctl(ch) { |
| 122 | return String.fromCharCode(ch.charCodeAt(0) - 64); |
| 123 | } |
| 124 | |
| 125 | /** |
Jason Lin | e99299a | 2023-03-31 13:36:35 +1100 | [diff] [blame] | 126 | * Create a notification following hterm's style. |
| 127 | * |
| 128 | * @param {string} title |
| 129 | * @param {?string=} body |
| 130 | * @return {!Notification} |
| 131 | */ |
| 132 | function createNotification(title, body) { |
| 133 | const options = {icon: lib.resource.getDataUrl('hterm/images/icon-96')}; |
| 134 | if (body) { |
| 135 | options.body = body; |
| 136 | } |
| 137 | return new Notification(`\u266A ${title} \u266A`, options); |
| 138 | } |
| 139 | |
| 140 | /** |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 141 | * A "terminal io" class for xterm. We don't want the vanilla hterm.Terminal.IO |
| 142 | * because it always convert utf8 data to strings, which is not necessary for |
| 143 | * xterm. |
| 144 | */ |
| 145 | class XtermTerminalIO extends hterm.Terminal.IO { |
| 146 | /** @override */ |
| 147 | writeUTF8(buffer) { |
| 148 | this.terminal_.write(new Uint8Array(buffer)); |
| 149 | } |
| 150 | |
| 151 | /** @override */ |
| 152 | writelnUTF8(buffer) { |
| 153 | this.terminal_.writeln(new Uint8Array(buffer)); |
| 154 | } |
| 155 | |
| 156 | /** @override */ |
| 157 | print(string) { |
| 158 | this.terminal_.write(string); |
| 159 | } |
| 160 | |
| 161 | /** @override */ |
| 162 | writeUTF16(string) { |
| 163 | this.print(string); |
| 164 | } |
| 165 | |
| 166 | /** @override */ |
| 167 | println(string) { |
| 168 | this.terminal_.writeln(string); |
| 169 | } |
| 170 | |
| 171 | /** @override */ |
| 172 | writelnUTF16(string) { |
| 173 | this.println(string); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
Jason Lin | 83707c9 | 2022-09-20 19:09:41 +1000 | [diff] [blame] | 178 | * A custom link handler that: |
| 179 | * |
| 180 | * - Shows a tooltip with the url on a OSC 8 link. This is following what hterm |
| 181 | * is doing. Also, showing the tooltip is better for the security of the user |
| 182 | * because the link can have arbitrary text. |
| 183 | * - Uses our own way to open the window. |
| 184 | */ |
| 185 | class LinkHandler { |
| 186 | /** |
| 187 | * @param {!Terminal} term |
| 188 | */ |
| 189 | constructor(term) { |
| 190 | this.term_ = term; |
| 191 | /** @type {?TerminalTooltip} */ |
| 192 | this.tooltip_ = null; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @return {!TerminalTooltip} |
| 197 | */ |
| 198 | getTooltip_() { |
| 199 | if (!this.tooltip_) { |
| 200 | this.tooltip_ = /** @type {!TerminalTooltip} */( |
| 201 | document.createElement('terminal-tooltip')); |
| 202 | this.tooltip_.classList.add('xterm-hover'); |
| 203 | lib.notNull(this.term_.element).appendChild(this.tooltip_); |
| 204 | } |
| 205 | return this.tooltip_; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @param {!MouseEvent} ev |
| 210 | * @param {string} url |
| 211 | * @param {!Object} range |
| 212 | */ |
| 213 | activate(ev, url, range) { |
| 214 | lib.f.openWindow(url, '_blank'); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @param {!MouseEvent} ev |
| 219 | * @param {string} url |
| 220 | * @param {!Object} range |
| 221 | */ |
| 222 | hover(ev, url, range) { |
| 223 | this.getTooltip_().show(url, {x: ev.clientX, y: ev.clientY}); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @param {!MouseEvent} ev |
| 228 | * @param {string} url |
| 229 | * @param {!Object} range |
| 230 | */ |
| 231 | leave(ev, url, range) { |
| 232 | this.getTooltip_().hide(); |
| 233 | } |
| 234 | } |
| 235 | |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 236 | class Bell { |
| 237 | constructor() { |
| 238 | this.showNotification = false; |
| 239 | |
| 240 | /** @type {?Audio} */ |
| 241 | this.audio_ = null; |
| 242 | /** @type {?Notification} */ |
| 243 | this.notification_ = null; |
| 244 | this.coolDownUntil_ = 0; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Set whether a bell audio should be played. |
| 249 | * |
| 250 | * @param {boolean} value |
| 251 | */ |
| 252 | set playAudio(value) { |
| 253 | this.audio_ = value ? |
| 254 | new Audio(lib.resource.getDataUrl('hterm/audio/bell')) : null; |
| 255 | } |
| 256 | |
| 257 | ring() { |
| 258 | const now = Date.now(); |
| 259 | if (now < this.coolDownUntil_) { |
| 260 | return; |
| 261 | } |
| 262 | this.coolDownUntil_ = now + 500; |
| 263 | |
| 264 | this.audio_?.play(); |
| 265 | if (this.showNotification && !document.hasFocus() && !this.notification_) { |
Jason Lin | e99299a | 2023-03-31 13:36:35 +1100 | [diff] [blame] | 266 | this.notification_ = createNotification(document.title); |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 267 | // Close the notification after a timeout. Note that this is different |
| 268 | // from hterm's behavior, but I think it makes more sense to do so. |
| 269 | setTimeout(() => { |
| 270 | this.notification_.close(); |
| 271 | this.notification_ = null; |
| 272 | }, 5000); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 277 | const A11Y_BUTTON_STYLE = ` |
| 278 | position: fixed; |
| 279 | z-index: 10; |
| 280 | right: 16px; |
| 281 | `; |
| 282 | |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 283 | // TODO: we should subscribe to the xterm.js onscroll event, and |
| 284 | // disable/enable the buttons accordingly. However, xterm.js does not seem to |
| 285 | // emit the onscroll event when the viewport is scrolled by the mouse. See |
| 286 | // https://github.com/xtermjs/xterm.js/issues/3864 |
| 287 | export class A11yButtons { |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 288 | /** |
| 289 | * @param {!Terminal} term |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 290 | * @param {!hterm.AccessibilityReader} htermA11yReader |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 291 | */ |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 292 | constructor(term, htermA11yReader) { |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 293 | this.term_ = term; |
| 294 | this.htermA11yReader_ = htermA11yReader; |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 295 | this.pageUpButton = document.createElement('button'); |
| 296 | this.pageUpButton.style.cssText = A11Y_BUTTON_STYLE; |
| 297 | this.pageUpButton.textContent = |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 298 | hterm.messageManager.get('HTERM_BUTTON_PAGE_UP'); |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 299 | this.pageUpButton.addEventListener('click', |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 300 | () => this.scrollPages_(-1)); |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 301 | |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 302 | this.pageDownButton = document.createElement('button'); |
| 303 | this.pageDownButton.style.cssText = A11Y_BUTTON_STYLE; |
| 304 | this.pageDownButton.textContent = |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 305 | hterm.messageManager.get('HTERM_BUTTON_PAGE_DOWN'); |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 306 | this.pageDownButton.addEventListener('click', |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 307 | () => this.scrollPages_(1)); |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 308 | |
| 309 | this.resetPos_(); |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 310 | |
| 311 | this.onSelectionChange_ = this.onSelectionChange_.bind(this); |
| 312 | } |
| 313 | |
| 314 | /** |
Jason Lin | a8adea5 | 2022-10-25 13:14:14 +1100 | [diff] [blame] | 315 | * @param {number} amount |
| 316 | */ |
| 317 | scrollPages_(amount) { |
| 318 | this.term_.scrollPages(amount); |
| 319 | this.announceScreenContent_(); |
| 320 | } |
| 321 | |
| 322 | announceScreenContent_() { |
| 323 | const activeBuffer = this.term_.buffer.active; |
| 324 | |
| 325 | let percentScrolled = 100; |
| 326 | if (activeBuffer.baseY !== 0) { |
| 327 | percentScrolled = Math.round( |
| 328 | 100 * activeBuffer.viewportY / activeBuffer.baseY); |
| 329 | } |
| 330 | |
| 331 | let currentScreenContent = hterm.messageManager.get( |
| 332 | 'HTERM_ANNOUNCE_CURRENT_SCREEN_HEADER', |
| 333 | [percentScrolled], |
| 334 | '$1% scrolled,'); |
| 335 | |
| 336 | currentScreenContent += '\n'; |
| 337 | |
| 338 | const rowEnd = Math.min(activeBuffer.viewportY + this.term_.rows, |
| 339 | activeBuffer.length); |
| 340 | for (let i = activeBuffer.viewportY; i < rowEnd; ++i) { |
| 341 | currentScreenContent += |
| 342 | activeBuffer.getLine(i).translateToString(true) + '\n'; |
| 343 | } |
| 344 | currentScreenContent = currentScreenContent.trim(); |
| 345 | |
| 346 | this.htermA11yReader_.assertiveAnnounce(currentScreenContent); |
| 347 | } |
| 348 | |
| 349 | /** |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 350 | * @param {boolean} enabled |
| 351 | */ |
| 352 | setEnabled(enabled) { |
| 353 | if (enabled) { |
| 354 | document.addEventListener('selectionchange', this.onSelectionChange_); |
| 355 | } else { |
| 356 | this.resetPos_(); |
| 357 | document.removeEventListener('selectionchange', this.onSelectionChange_); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | resetPos_() { |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 362 | this.pageUpButton.style.top = '-200px'; |
| 363 | this.pageDownButton.style.bottom = '-200px'; |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | onSelectionChange_() { |
| 367 | this.resetPos_(); |
| 368 | |
Jason Lin | 36b9fce | 2022-11-10 16:56:40 +1100 | [diff] [blame] | 369 | const selectedElement = document.getSelection().anchorNode?.parentElement; |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 370 | if (selectedElement === this.pageUpButton) { |
| 371 | this.pageUpButton.style.top = '16px'; |
| 372 | } else if (selectedElement === this.pageDownButton) { |
| 373 | this.pageDownButton.style.bottom = '16px'; |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 378 | const BACKGROUND_IMAGE_KEY = 'background-image'; |
| 379 | |
| 380 | class BackgroundImageWatcher { |
| 381 | /** |
| 382 | * @param {!hterm.PreferenceManager} prefs |
| 383 | * @param {function(string)} onChange This is called with the background image |
| 384 | * (could be empty) whenever it changes. |
| 385 | */ |
| 386 | constructor(prefs, onChange) { |
| 387 | this.prefs_ = prefs; |
| 388 | this.onChange_ = onChange; |
Joel Hockey | 0e164f0 | 2023-03-26 15:58:12 -0700 | [diff] [blame] | 389 | this.localStorageKey_ = backgroundImageLocalStorageKey(prefs); |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Call once to start watching for background image changes. |
| 394 | */ |
| 395 | watch() { |
| 396 | window.addEventListener('storage', (e) => { |
Joel Hockey | 0e164f0 | 2023-03-26 15:58:12 -0700 | [diff] [blame] | 397 | if (e.key === this.localStorageKey_) { |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 398 | this.onChange_(this.getBackgroundImage()); |
| 399 | } |
| 400 | }); |
| 401 | this.prefs_.addObserver(BACKGROUND_IMAGE_KEY, () => { |
| 402 | this.onChange_(this.getBackgroundImage()); |
| 403 | }); |
| 404 | } |
| 405 | |
| 406 | getBackgroundImage() { |
Joel Hockey | 0e164f0 | 2023-03-26 15:58:12 -0700 | [diff] [blame] | 407 | const image = window.localStorage.getItem(this.localStorageKey_); |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 408 | if (image) { |
| 409 | return `url(${image})`; |
| 410 | } |
| 411 | |
| 412 | return this.prefs_.getString(BACKGROUND_IMAGE_KEY); |
| 413 | } |
| 414 | } |
| 415 | |
Jason Lin | b8f380a | 2022-10-25 13:15:56 +1100 | [diff] [blame] | 416 | let xtermTerminalStringsLoaded = false; |
| 417 | |
Jason Lin | 83707c9 | 2022-09-20 19:09:41 +1000 | [diff] [blame] | 418 | /** |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 419 | * A terminal class that 1) uses xterm.js and 2) behaves like a `hterm.Terminal` |
| 420 | * so that it can be used in existing code. |
| 421 | * |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 422 | * @extends {hterm.Terminal} |
| 423 | * @unrestricted |
| 424 | */ |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 425 | export class XtermTerminal { |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 426 | /** |
| 427 | * @param {{ |
| 428 | * storage: !lib.Storage, |
| 429 | * profileId: string, |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 430 | * testParams: (!XtermTerminalTestParams|undefined), |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 431 | * }} args |
| 432 | */ |
Jason Lin | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 433 | constructor({storage, profileId, testParams}) { |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 434 | this.ctrlCKeyDownHandler_ = this.ctrlCKeyDownHandler_.bind(this); |
| 435 | this.ctrlVKeyDownHandler_ = this.ctrlVKeyDownHandler_.bind(this); |
| 436 | this.zoomKeyDownHandler_ = this.zoomKeyDownHandler_.bind(this); |
| 437 | |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 438 | this.inited_ = false; |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 439 | this.profileId_ = profileId; |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 440 | /** @type {!hterm.PreferenceManager} */ |
| 441 | this.prefs_ = new hterm.PreferenceManager(storage, profileId); |
Jason Lin | c48f743 | 2022-10-13 17:28:30 +1100 | [diff] [blame] | 442 | definePrefs(this.prefs_); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 443 | |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 444 | // TODO: we should probably pass the initial prefs to the ctor. |
Jason Lin | fc8a372 | 2022-09-07 17:49:18 +1000 | [diff] [blame] | 445 | this.term = testParams?.term || new Terminal({allowProposedApi: true}); |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 446 | this.xtermInternal_ = testParams?.xtermInternal || |
| 447 | new XtermInternal(this.term); |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 448 | this.fontManager_ = testParams?.fontManager || fontManager; |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 449 | |
Jason Lin | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 450 | this.renderAddonType_ = WebglAddon; |
| 451 | if (!document.createElement('canvas').getContext('webgl')) { |
| 452 | console.warn('Webgl is not supported. Fall back to canvas renderer'); |
| 453 | this.renderAddonType_ = CanvasAddon; |
| 454 | } |
| 455 | |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 456 | /** @type {?Element} */ |
| 457 | this.container_; |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 458 | this.bell_ = new Bell(); |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 459 | this.scheduleFit_ = delayedScheduler(() => this.fit_(), |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 460 | testParams ? 0 : 250); |
| 461 | |
Jason Lin | 83707c9 | 2022-09-20 19:09:41 +1000 | [diff] [blame] | 462 | this.term.loadAddon( |
| 463 | new WebLinksAddon((e, uri) => lib.f.openWindow(uri, '_blank'))); |
Jason Lin | 4de4f38 | 2022-09-01 14:10:18 +1000 | [diff] [blame] | 464 | this.term.loadAddon(new Unicode11Addon()); |
| 465 | this.term.unicode.activeVersion = '11'; |
| 466 | |
Jason Lin | abad756 | 2022-08-22 14:49:05 +1000 | [diff] [blame] | 467 | this.pendingFont_ = null; |
| 468 | this.scheduleRefreshFont_ = delayedScheduler( |
| 469 | () => this.refreshFont_(), 100); |
| 470 | document.fonts.addEventListener('loadingdone', |
| 471 | () => this.onFontLoadingDone_()); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 472 | |
| 473 | this.installUnimplementedStubs_(); |
Jason Lin | e9231bc | 2022-09-01 13:54:02 +1000 | [diff] [blame] | 474 | this.installEscapeSequenceHandlers_(); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 475 | |
Jason Lin | 34a4532 | 2022-10-12 19:10:52 +1100 | [diff] [blame] | 476 | this.term.onResize(({cols, rows}) => { |
| 477 | this.io.onTerminalResize(cols, rows); |
| 478 | if (this.prefs_.get('enable-resize-status')) { |
| 479 | this.showOverlay(`${cols} × ${rows}`); |
| 480 | } |
| 481 | }); |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 482 | // We could also use `this.io.sendString()` except for the nassh exit |
| 483 | // prompt, which only listens to onVTKeystroke(). |
| 484 | this.term.onData((data) => this.io.onVTKeystroke(data)); |
Jason Lin | 80e6913 | 2022-09-02 16:31:43 +1000 | [diff] [blame] | 485 | this.term.onBinary((data) => this.io.onVTKeystroke(data)); |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 486 | this.term.onTitleChange((title) => this.setWindowTitle(title)); |
Jason Lin | 83ef5ba | 2022-10-13 17:40:30 +1100 | [diff] [blame] | 487 | this.term.onSelectionChange(() => { |
| 488 | if (this.prefs_.get('copy-on-select')) { |
| 489 | this.copySelection_(); |
| 490 | } |
| 491 | }); |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 492 | this.term.onBell(() => this.ringBell()); |
Jason Lin | 5690e75 | 2022-08-30 15:36:45 +1000 | [diff] [blame] | 493 | |
| 494 | /** |
| 495 | * A mapping from key combo (see encodeKeyCombo()) to a handler function. |
| 496 | * |
| 497 | * If a key combo is in the map: |
| 498 | * |
| 499 | * - The handler instead of xterm.js will handle the keydown event. |
| 500 | * - Keyup and keypress will be ignored by both us and xterm.js. |
| 501 | * |
| 502 | * We re-generate this map every time a relevant pref value is changed. This |
| 503 | * is ok because pref changes are rare. |
| 504 | * |
| 505 | * @type {!Map<number, function(!KeyboardEvent)>} |
| 506 | */ |
| 507 | this.keyDownHandlers_ = new Map(); |
| 508 | this.scheduleResetKeyDownHandlers_ = |
| 509 | delayedScheduler(() => this.resetKeyDownHandlers_(), 250); |
| 510 | |
Jason Lin | 97a0428 | 2023-03-06 10:36:56 +1100 | [diff] [blame] | 511 | this.term.attachCustomKeyEventHandler((ev) => !this.handleKeyEvent_(ev)); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 512 | |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 513 | this.io = new XtermTerminalIO(this); |
| 514 | this.notificationCenter_ = null; |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 515 | this.htermA11yReader_ = null; |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 516 | this.a11yEnabled_ = false; |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 517 | this.a11yButtons_ = null; |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 518 | this.copyNotice_ = null; |
Jason Lin | 446f3d9 | 2022-10-13 17:34:21 +1100 | [diff] [blame] | 519 | this.scrollOnOutputListener_ = null; |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 520 | this.backgroundImageWatcher_ = new BackgroundImageWatcher(this.prefs_, |
| 521 | this.setBackgroundImage.bind(this)); |
Jason Lin | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 522 | this.renderAddon_ = null; |
Jason Lin | a63d8ba | 2022-11-02 17:42:38 +1100 | [diff] [blame] | 523 | this.userCSSElement_ = null; |
| 524 | this.userCSSTextElement_ = null; |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 525 | |
Jason Lin | 82ba86c | 2022-11-09 12:12:27 +1100 | [diff] [blame] | 526 | this.contextMenu_ = /** @type {!TerminalContextMenu} */( |
| 527 | document.createElement('terminal-context-menu')); |
| 528 | this.contextMenu_.style.zIndex = 10; |
| 529 | this.contextMenu = { |
| 530 | setItems: (items) => this.contextMenu_.items = items, |
| 531 | }; |
| 532 | |
Jason Lin | 83707c9 | 2022-09-20 19:09:41 +1000 | [diff] [blame] | 533 | this.term.options.linkHandler = new LinkHandler(this.term); |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 534 | this.term.options.theme = { |
Jason Lin | 461ca56 | 2022-09-07 13:53:08 +1000 | [diff] [blame] | 535 | // The webgl cursor layer also paints the character under the cursor with |
| 536 | // this `cursorAccent` color. We use a completely transparent color here |
| 537 | // to effectively disable that. |
| 538 | cursorAccent: 'rgba(0, 0, 0, 0)', |
| 539 | customGlyphs: true, |
Jason Lin | 2edc25d | 2022-09-16 15:06:48 +1000 | [diff] [blame] | 540 | selectionBackground: 'rgba(174, 203, 250, .6)', |
| 541 | selectionInactiveBackground: 'rgba(218, 220, 224, .6)', |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 542 | selectionForeground: 'black', |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 543 | }; |
| 544 | this.observePrefs_(); |
Jason Lin | b8f380a | 2022-10-25 13:15:56 +1100 | [diff] [blame] | 545 | if (!xtermTerminalStringsLoaded) { |
| 546 | xtermTerminalStringsLoaded = true; |
| 547 | Terminal.strings.promptLabel = |
| 548 | hterm.messageManager.get('TERMINAL_INPUT_LABEL'); |
| 549 | Terminal.strings.tooMuchOutput = |
| 550 | hterm.messageManager.get('TERMINAL_TOO_MUCH_OUTPUT_MESSAGE'); |
| 551 | } |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 552 | } |
| 553 | |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 554 | /** @override */ |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 555 | setWindowTitle(title) { |
| 556 | document.title = title; |
| 557 | } |
| 558 | |
| 559 | /** @override */ |
Jason Lin | c7afb67 | 2022-10-11 15:54:17 +1100 | [diff] [blame] | 560 | ringBell() { |
| 561 | this.bell_.ring(); |
| 562 | } |
| 563 | |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 564 | /** @override */ |
| 565 | print(str) { |
| 566 | this.xtermInternal_.print(str); |
| 567 | } |
| 568 | |
| 569 | /** @override */ |
| 570 | wipeContents() { |
| 571 | this.term.clear(); |
| 572 | } |
| 573 | |
| 574 | /** @override */ |
| 575 | newLine() { |
| 576 | this.xtermInternal_.newLine(); |
| 577 | } |
| 578 | |
| 579 | /** @override */ |
| 580 | cursorLeft(number) { |
| 581 | this.xtermInternal_.cursorLeft(number ?? 1); |
| 582 | } |
| 583 | |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 584 | /** @override */ |
| 585 | setAccessibilityEnabled(enabled) { |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 586 | if (enabled === this.a11yEnabled_) { |
| 587 | return; |
| 588 | } |
| 589 | this.a11yEnabled_ = enabled; |
| 590 | |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 591 | this.a11yButtons_.setEnabled(enabled); |
| 592 | this.htermA11yReader_.setAccessibilityEnabled(enabled); |
Jason Lin | c0f14fe | 2022-10-25 15:31:29 +1100 | [diff] [blame] | 593 | |
| 594 | if (enabled) { |
| 595 | this.xtermInternal_.enableA11y(this.a11yButtons_.pageUpButton, |
| 596 | this.a11yButtons_.pageDownButton); |
| 597 | } else { |
| 598 | this.xtermInternal_.disableA11y(); |
| 599 | } |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 600 | } |
| 601 | |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 602 | hasBackgroundImage() { |
| 603 | return !!this.container_.style.backgroundImage; |
| 604 | } |
| 605 | |
| 606 | /** @override */ |
| 607 | setBackgroundImage(image) { |
| 608 | this.container_.style.backgroundImage = image || ''; |
| 609 | this.updateBackgroundColor_(this.prefs_.getString('background-color')); |
| 610 | } |
| 611 | |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 612 | /** |
| 613 | * Install stubs for stuff that we haven't implemented yet so that the code |
| 614 | * still runs. |
| 615 | */ |
| 616 | installUnimplementedStubs_() { |
| 617 | this.keyboard = { |
| 618 | keyMap: { |
| 619 | keyDefs: [], |
| 620 | }, |
| 621 | bindings: { |
| 622 | clear: () => {}, |
| 623 | addBinding: () => {}, |
| 624 | addBindings: () => {}, |
| 625 | OsDefaults: {}, |
| 626 | }, |
| 627 | }; |
| 628 | this.keyboard.keyMap.keyDefs[78] = {}; |
Joel Hockey | 965ea55 | 2023-02-19 22:08:04 -0800 | [diff] [blame] | 629 | this.keyboard.keyMap.keyDefs[84] = {}; |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 630 | |
| 631 | const methodNames = [ |
Joel Hockey | b89a978 | 2022-10-16 22:00:12 -0700 | [diff] [blame] | 632 | 'eraseLine', |
Joel Hockey | b89a978 | 2022-10-16 22:00:12 -0700 | [diff] [blame] | 633 | 'setCursorColumn', |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 634 | 'setCursorPosition', |
| 635 | 'setCursorVisible', |
Joel Hockey | d78374f | 2022-11-02 23:05:53 -0700 | [diff] [blame] | 636 | 'uninstallKeyboard', |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 637 | ]; |
| 638 | |
| 639 | for (const name of methodNames) { |
| 640 | this[name] = () => console.warn(`${name}() is not implemented`); |
| 641 | } |
| 642 | |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 643 | this.vt = { |
| 644 | resetParseState: () => { |
| 645 | console.warn('.vt.resetParseState() is not implemented'); |
| 646 | }, |
| 647 | }; |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 648 | } |
| 649 | |
Jason Lin | e9231bc | 2022-09-01 13:54:02 +1000 | [diff] [blame] | 650 | installEscapeSequenceHandlers_() { |
| 651 | // OSC 52 for copy. |
| 652 | this.term.parser.registerOscHandler(52, (args) => { |
| 653 | // Args comes in as a single 'clipboard;b64-data' string. The clipboard |
| 654 | // parameter is used to select which of the X clipboards to address. Since |
| 655 | // we're not integrating with X, we treat them all the same. |
| 656 | const parsedArgs = args.match(/^[cps01234567]*;(.*)/); |
| 657 | if (!parsedArgs) { |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | let data; |
| 662 | try { |
| 663 | data = window.atob(parsedArgs[1]); |
| 664 | } catch (e) { |
| 665 | // If the user sent us invalid base64 content, silently ignore it. |
| 666 | return true; |
| 667 | } |
| 668 | const decoder = new TextDecoder(); |
| 669 | const bytes = lib.codec.stringToCodeUnitArray(data); |
| 670 | this.copyString_(decoder.decode(bytes)); |
| 671 | |
| 672 | return true; |
| 673 | }); |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 674 | |
Jason Lin | e99299a | 2023-03-31 13:36:35 +1100 | [diff] [blame] | 675 | // URxvt perl modules. We only support "notify". |
| 676 | this.term.parser.registerOscHandler(777, (args) => { |
| 677 | // Match 'notify;<title>[;<message>]'. |
| 678 | const match = args.match(/^notify;([^;]*)(?:;(.*))?$/); |
| 679 | if (match) { |
| 680 | createNotification(match[1], match[2]); |
| 681 | } |
| 682 | |
| 683 | return true; |
| 684 | }); |
| 685 | |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 686 | this.xtermInternal_.installTmuxControlModeHandler( |
| 687 | (data) => this.onTmuxControlModeLine(data)); |
| 688 | this.xtermInternal_.installEscKHandler(); |
Jason Lin | e9231bc | 2022-09-01 13:54:02 +1000 | [diff] [blame] | 689 | } |
| 690 | |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 691 | /** |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 692 | * Write data to the terminal. |
| 693 | * |
| 694 | * @param {string|!Uint8Array} data string for UTF-16 data, Uint8Array for |
| 695 | * UTF-8 data |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 696 | * @param {function()=} callback Optional callback that fires when the data |
| 697 | * was processed by the parser. |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 698 | */ |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 699 | write(data, callback) { |
| 700 | this.term.write(data, callback); |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Like `this.write()` but also write a line break. |
| 705 | * |
| 706 | * @param {string|!Uint8Array} data |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 707 | * @param {function()=} callback Optional callback that fires when the data |
| 708 | * was processed by the parser. |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 709 | */ |
Jason Lin | 2649da2 | 2022-10-12 10:16:44 +1100 | [diff] [blame] | 710 | writeln(data, callback) { |
| 711 | this.term.writeln(data, callback); |
Jason Lin | 21d854f | 2022-08-22 14:49:59 +1000 | [diff] [blame] | 712 | } |
| 713 | |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 714 | get screenSize() { |
| 715 | return new hterm.Size(this.term.cols, this.term.rows); |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Don't need to do anything. |
| 720 | * |
| 721 | * @override |
| 722 | */ |
| 723 | installKeyboard() {} |
| 724 | |
| 725 | /** |
| 726 | * @override |
| 727 | */ |
| 728 | decorate(elem) { |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 729 | this.container_ = elem; |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 730 | elem.style.backgroundSize = '100% 100%'; |
| 731 | |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 732 | (async () => { |
| 733 | await new Promise((resolve) => this.prefs_.readStorage(resolve)); |
| 734 | // This will trigger all the observers to set the terminal options before |
| 735 | // we call `this.term.open()`. |
| 736 | this.prefs_.notifyAll(); |
| 737 | |
Jason Lin | c2504ae | 2022-09-02 13:03:31 +1000 | [diff] [blame] | 738 | const screenPaddingSize = /** @type {number} */( |
| 739 | this.prefs_.get('screen-padding-size')); |
| 740 | elem.style.paddingTop = elem.style.paddingLeft = `${screenPaddingSize}px`; |
| 741 | |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 742 | this.setBackgroundImage( |
| 743 | this.backgroundImageWatcher_.getBackgroundImage()); |
| 744 | this.backgroundImageWatcher_.watch(); |
| 745 | |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 746 | this.inited_ = true; |
| 747 | this.term.open(elem); |
Jason Lin | 1be92f5 | 2023-01-23 23:50:00 +1100 | [diff] [blame] | 748 | this.xtermInternal_.addDimensionsObserver(() => this.scheduleFit_()); |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 749 | |
Jason Lin | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 750 | this.reloadRenderAddon_(); |
Jason Lin | 8de3d28 | 2022-09-01 21:29:05 +1000 | [diff] [blame] | 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 | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 955 | reloadRenderAddon_() { |
| 956 | if (this.renderAddon_) { |
| 957 | this.renderAddon_.dispose(); |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 958 | } |
Jason Lin | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 959 | this.renderAddon_ = new this.renderAddonType_(); |
| 960 | this.term.loadAddon(this.renderAddon_); |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 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; |
Jason Lin | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 978 | if (this.inited_) { |
| 979 | // Setting allowTransparency in the middle messes up rendering, so we |
| 980 | // need to reload it here. |
| 981 | this.reloadRenderAddon_(); |
Jason Lin | ee0c1f7 | 2022-10-18 17:17:26 +1100 | [diff] [blame] | 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}) { |
Jason Lin | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 1483 | let emulator_type = 'hterm'; |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 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 |
Jason Lin | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 1487 | // testing a bit easier. |
| 1488 | if (ORIGINAL_URL.searchParams.get('emulator') !== 'hterm') { |
| 1489 | emulator_type = 'xterm.js'; |
| 1490 | } |
| 1491 | console.log('Terminal emulator type: ', emulator_type); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 1492 | } |
| 1493 | |
Jason Lin | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 1494 | if (emulator_type === 'hterm') { |
| 1495 | return new HtermTerminal({profileId, storage}); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 1496 | } |
Jason Lin | c80a23d | 2023-04-18 14:56:32 +1000 | [diff] [blame^] | 1497 | |
| 1498 | return new XtermTerminal({ |
| 1499 | storage, |
| 1500 | profileId, |
| 1501 | }); |
Jason Lin | ca61ffb | 2022-08-03 19:37:12 +1000 | [diff] [blame] | 1502 | } |
| 1503 | |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 1504 | class TerminalCopyNotice extends LitElement { |
| 1505 | /** @override */ |
| 1506 | static get styles() { |
| 1507 | return css` |
| 1508 | :host { |
| 1509 | display: block; |
| 1510 | text-align: center; |
| 1511 | } |
| 1512 | |
| 1513 | svg { |
| 1514 | fill: currentColor; |
| 1515 | } |
| 1516 | `; |
| 1517 | } |
| 1518 | |
| 1519 | /** @override */ |
Jason Lin | d3aacef | 2022-10-12 19:03:37 +1100 | [diff] [blame] | 1520 | connectedCallback() { |
| 1521 | super.connectedCallback(); |
| 1522 | if (!this.childNodes.length) { |
| 1523 | // This is not visible since we use shadow dom. But this will allow the |
| 1524 | // hterm.NotificationCenter to announce the the copy text. |
| 1525 | this.append(hterm.messageManager.get('HTERM_NOTIFY_COPY')); |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | /** @override */ |
Jason Lin | 6a402a7 | 2022-08-25 16:07:02 +1000 | [diff] [blame] | 1530 | render() { |
| 1531 | return html` |
| 1532 | ${ICON_COPY} |
| 1533 | <div>${hterm.messageManager.get('HTERM_NOTIFY_COPY')}</div> |
| 1534 | `; |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | customElements.define('terminal-copy-notice', TerminalCopyNotice); |