Merge pull request #878 from npezza93/add-bell-sound

If bell() gets called emit bell event and add default bell sound
This commit is contained in:
Daniel Imms
2017-08-19 13:05:01 -07:00
committed by GitHub
9 changed files with 85 additions and 22 deletions
+11
View File
@@ -38,6 +38,17 @@
</select> </select>
</label> </label>
</p> </p>
<p>
<label>
bellStyle
<select id="option-bell-style">
<option value="">none</option>
<option value="sound">sound</option>
<option value="visual">visual</option>
<option value="both">both</option>
</select>
</label>
</p>
<p> <p>
<label>scrollback <input type="number" id="option-scrollback" value="1000" /></label> <label>scrollback <input type="number" id="option-scrollback" value="1000" /></label>
</p> </p>
+5 -1
View File
@@ -15,7 +15,8 @@ var terminalContainer = document.getElementById('terminal-container'),
cursorBlink: document.querySelector('#option-cursor-blink'), cursorBlink: document.querySelector('#option-cursor-blink'),
cursorStyle: document.querySelector('#option-cursor-style'), cursorStyle: document.querySelector('#option-cursor-style'),
scrollback: document.querySelector('#option-scrollback'), scrollback: document.querySelector('#option-scrollback'),
tabstopwidth: document.querySelector('#option-tabstopwidth') tabstopwidth: document.querySelector('#option-tabstopwidth'),
bellStyle: document.querySelector('#option-bell-style')
}, },
colsElement = document.getElementById('cols'), colsElement = document.getElementById('cols'),
rowsElement = document.getElementById('rows'); rowsElement = document.getElementById('rows');
@@ -53,6 +54,9 @@ optionElements.cursorBlink.addEventListener('change', function () {
optionElements.cursorStyle.addEventListener('change', function () { optionElements.cursorStyle.addEventListener('change', function () {
term.setOption('cursorStyle', optionElements.cursorStyle.value); term.setOption('cursorStyle', optionElements.cursorStyle.value);
}); });
optionElements.bellStyle.addEventListener('change', function () {
term.setOption('bellStyle', optionElements.bellStyle.value);
});
optionElements.scrollback.addEventListener('change', function () { optionElements.scrollback.addEventListener('change', function () {
term.setOption('scrollback', parseInt(optionElements.scrollback.value, 10)); term.setOption('scrollback', parseInt(optionElements.scrollback.value, 10));
}); });
+1 -8
View File
@@ -106,14 +106,7 @@ export class InputHandler implements IInputHandler {
* Bell (Ctrl-G). * Bell (Ctrl-G).
*/ */
public bell(): void { public bell(): void {
if (!this._terminal.options.visualBell) { this._terminal.bell();
return;
}
this._terminal.element.style.borderColor = 'white';
setTimeout(() => this._terminal.element.style.borderColor = '', 10);
if (this._terminal.options.popOnBell) {
this._terminal.focus();
}
} }
/** /**
+3 -2
View File
@@ -87,6 +87,7 @@ export interface IInputHandlingTerminal extends IEventEmitter {
viewport: IViewport; viewport: IViewport;
selectionManager: ISelectionManager; selectionManager: ISelectionManager;
bell(): void;
focus(): void; focus(): void;
convertEol: boolean; convertEol: boolean;
updateRange(y: number): void; updateRange(y: number): void;
@@ -113,6 +114,8 @@ export interface IInputHandlingTerminal extends IEventEmitter {
} }
export interface ITerminalOptions { export interface ITerminalOptions {
bellSound?: string;
bellStyle?: string;
cancelEvents?: boolean; cancelEvents?: boolean;
colors?: string[]; colors?: string[];
cols?: number; cols?: number;
@@ -123,14 +126,12 @@ export interface ITerminalOptions {
disableStdin?: boolean; disableStdin?: boolean;
geometry?: [number, number]; geometry?: [number, number];
handler?: (data: string) => void; handler?: (data: string) => void;
popOnBell?: boolean;
rows?: number; rows?: number;
screenKeys?: boolean; screenKeys?: boolean;
scrollback?: number; scrollback?: number;
tabStopWidth?: number; tabStopWidth?: number;
termName?: string; termName?: string;
useFlowControl?: boolean; useFlowControl?: boolean;
visualBell?: boolean;
} }
export interface IBuffer { export interface IBuffer {
+40 -8
View File
@@ -39,6 +39,7 @@ import { CHARSETS } from './Charsets';
import { getRawByteCoords } from './utils/Mouse'; import { getRawByteCoords } from './utils/Mouse';
import { CustomKeyEventHandler, Charset, LinkMatcherHandler, LinkMatcherValidationCallback, CharData, LineData, Option, StringOption, BooleanOption, StringArrayOption, NumberOption, GeometryOption, HandlerOption } from './Types'; import { CustomKeyEventHandler, Charset, LinkMatcherHandler, LinkMatcherValidationCallback, CharData, LineData, Option, StringOption, BooleanOption, StringArrayOption, NumberOption, GeometryOption, HandlerOption } from './Types';
import { ITerminal, IBrowser, ITerminalOptions, IInputHandlingTerminal, ILinkMatcherOptions, IViewport, ICompositionHelper } from './Interfaces'; import { ITerminal, IBrowser, ITerminalOptions, IInputHandlingTerminal, ILinkMatcherOptions, IViewport, ICompositionHelper } from './Interfaces';
import { BellSound } from './utils/Sounds';
// Declare for RequireJS in loadAddon // Declare for RequireJS in loadAddon
declare var define: any; declare var define: any;
@@ -147,8 +148,8 @@ const DEFAULT_OPTIONS: ITerminalOptions = {
geometry: [80, 24], geometry: [80, 24],
cursorBlink: false, cursorBlink: false,
cursorStyle: 'block', cursorStyle: 'block',
visualBell: false, bellSound: BellSound,
popOnBell: false, bellStyle: null,
scrollback: 1000, scrollback: 1000,
screenKeys: false, screenKeys: false,
debug: false, debug: false,
@@ -178,6 +179,8 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
private helperContainer: HTMLElement; private helperContainer: HTMLElement;
private compositionView: HTMLElement; private compositionView: HTMLElement;
private charSizeStyleElement: HTMLStyleElement; private charSizeStyleElement: HTMLStyleElement;
private bellAudioElement: HTMLAudioElement;
private visualBellTimer: number;
public browser: IBrowser = <any>Browser; public browser: IBrowser = <any>Browser;
@@ -482,6 +485,7 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
this.viewport.syncScrollArea(); this.viewport.syncScrollArea();
break; break;
case 'tabStopWidth': this.setupStops(); break; case 'tabStopWidth': this.setupStops(); break;
case 'bellStyle': this.preloadBellSound(); break;
} }
} }
@@ -689,6 +693,9 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
this.viewportScrollArea.classList.add('xterm-scroll-area'); this.viewportScrollArea.classList.add('xterm-scroll-area');
this.viewportElement.appendChild(this.viewportScrollArea); this.viewportElement.appendChild(this.viewportScrollArea);
// preload audio
this.preloadBellSound();
// Create the selection container. // Create the selection container.
this.selectionContainer = document.createElement('div'); this.selectionContainer = document.createElement('div');
this.selectionContainer.classList.add('xterm-selection'); this.selectionContainer.classList.add('xterm-selection');
@@ -1874,12 +1881,16 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
* Note: We could do sweet things with webaudio here * Note: We could do sweet things with webaudio here
*/ */
public bell(): void { public bell(): void {
if (!this.options.visualBell) return; this.emit('bell');
this.element.style.borderColor = 'white'; if (this.soundBell()) this.bellAudioElement.play();
setTimeout(() => {
this.element.style.borderColor = ''; if (this.visualBell()) {
}, 10); this.element.classList.add('visual-bell-active');
if (this.options.popOnBell) this.focus(); clearTimeout(this.visualBellTimer);
this.visualBellTimer = window.setTimeout(() => {
this.element.classList.remove('visual-bell-active');
}, 200);
}
} }
/** /**
@@ -2271,6 +2282,27 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
return matchColorCache[hash] = li; return matchColorCache[hash] = li;
} }
private visualBell(): boolean {
return this.options.bellStyle === 'visual' ||
this.options.bellStyle === 'both';
}
private soundBell(): boolean {
return this.options.bellStyle === 'sound' ||
this.options.bellStyle === 'both';
}
private preloadBellSound(): void {
if (this.soundBell()) {
this.bellAudioElement = document.createElement('audio');
this.bellAudioElement.setAttribute('preload', 'auto');
this.bellAudioElement.setAttribute('src', this.options.bellSound);
this.helperContainer.appendChild(this.bellAudioElement);
} else if (this.bellAudioElement) {
this.helperContainer.removeChild(this.bellAudioElement);
}
}
} }
/** /**
+2 -3
View File
@@ -26,12 +26,11 @@ export type BooleanOption =
'cursorBlink' | 'cursorBlink' |
'debug' | 'debug' |
'disableStdin' | 'disableStdin' |
'popOnBell' |
'screenKeys' | 'screenKeys' |
'useFlowControl' | 'useFlowControl';
'visualBell';
export type StringOption = export type StringOption =
'cursorStyle' | 'cursorStyle' |
'bellStyle' |
'termName'; 'termName';
export type StringArrayOption = 'colors'; export type StringArrayOption = 'colors';
export type NumberOption = export type NumberOption =
+9
View File
@@ -0,0 +1,9 @@
/**
* @license MIT
*/
// Source: https://freesound.org/people/altemark/sounds/45759/
// This sound is released under the Creative Commons Attribution 3.0 Unported
// (CC BY 3.0) license. It was created by 'altemark'. No modifications have been
// made, apart from the conversion to base64.
export const BellSound = 'data:audio/wav;base64,UklGRigBAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQQBAADpAFgCwAMlBZoG/wdmCcoKRAypDQ8PbRDBEQQTOxRtFYcWlBePGIUZXhoiG88bcBz7HHIdzh0WHlMeZx51HmkeUx4WHs8dah0AHXwc3hs9G4saxRnyGBIYGBcQFv8U4RPAEoYRQBACD70NWwwHC6gJOwjWBloF7gOBAhABkf8b/qv8R/ve+Xf4Ife79W/0JfPZ8Z/wde9N7ijtE+wU6xvqM+lb6H7nw+YX5mrlxuQz5Mzje+Ma49fioeKD4nXiYeJy4pHitOL04j/jn+MN5IPkFOWs5U3mDefM55/ogOl36m7rdOyE7abuyu8D8Unyj/Pg9D/2qfcb+Yn6/vuK/Qj/lAAlAg==';
+4
View File
@@ -97,6 +97,10 @@ export class MockInputHandlingTerminal implements IInputHandlingTerminal {
throw new Error('Method not implemented.'); throw new Error('Method not implemented.');
} }
convertEol: boolean; convertEol: boolean;
bell(): void {
throw new Error('Method not implemented.');
}
updateRange(y: number): void { updateRange(y: number): void {
throw new Error('Method not implemented.'); throw new Error('Method not implemented.');
} }
+10
View File
@@ -91,6 +91,7 @@
.terminal .terminal-cursor { .terminal .terminal-cursor {
position: relative; position: relative;
transition: opacity 150ms ease;
} }
.terminal:not(.focus) .terminal-cursor { .terminal:not(.focus) .terminal-cursor {
@@ -108,6 +109,7 @@
content: ''; content: '';
position: absolute; position: absolute;
background-color: #fff; background-color: #fff;
transition: opacity 150ms ease;
} }
.terminal.focus.xterm-cursor-style-bar:not(.xterm-cursor-blink-on) .terminal-cursor::before { .terminal.focus.xterm-cursor-style-bar:not(.xterm-cursor-blink-on) .terminal-cursor::before {
@@ -124,6 +126,14 @@
height: 1px; height: 1px;
} }
.terminal.focus.xterm-cursor-style-block.visual-bell-active .terminal-cursor {
opacity: 0.5;
}
.terminal.focus.xterm-cursor-style-bar.visual-bell-active .terminal-cursor::before,
.terminal.focus.xterm-cursor-style-underline.visual-bell-active .terminal-cursor::before {
opacity: 0;
}
.terminal .composition-view { .terminal .composition-view {
background: #000; background: #000;
color: #FFF; color: #FFF;