Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If bell() gets called emit bell event and add default bell sound #878

Merged
merged 24 commits into from
Aug 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
245f626
If bell() gets called emit bell event and add default bell sound
npezza93 Aug 10, 2017
0eab4d6
Rename Bell.ts to Sounds that has BellSound constant
npezza93 Aug 10, 2017
fd51d0a
Preload bell audio
npezza93 Aug 10, 2017
6b46e08
Move preloading bell audio to open() and append it the the terminal e…
npezza93 Aug 10, 2017
8a1f365
Add ability to have a visual bell
npezza93 Aug 12, 2017
44ed0b9
Dont hardcode as many color codes in xterm.ts
npezza93 Aug 12, 2017
8c94ef8
Clear out any existing vidualBellTimers. Set colors back to all white.
npezza93 Aug 13, 2017
3027b1d
Set the visual bell color to a light gray
npezza93 Aug 13, 2017
96c9d50
Fix some quote lints
npezza93 Aug 13, 2017
d8717d6
Add visual bell styling for underline and bar cursor types
npezza93 Aug 14, 2017
2f1380a
Make bellStyle singular. Preload sound on option setting. Use strings…
npezza93 Aug 16, 2017
c41bd81
Swap out bell sound for a free one
npezza93 Aug 16, 2017
3beb1f5
Add return types on new private functions
npezza93 Aug 16, 2017
1d6f71d
Fix bad merge
npezza93 Aug 17, 2017
0bb5ff8
Fix lints
npezza93 Aug 17, 2017
c88f501
Add a bellStyle dropdown in the demo
npezza93 Aug 17, 2017
8a6bff4
Remove bellAudioElement if it is not used. Remove extra import
npezza93 Aug 17, 2017
68cd79d
Set bellStyle to a string type instead of an array of strings
npezza93 Aug 17, 2017
b0164e1
Update bell sound
npezza93 Aug 19, 2017
668b25e
Fix license info and lint
Tyriar Aug 19, 2017
147fef7
Use removeChild instead of remove for compatibility
npezza93 Aug 19, 2017
1211435
Change opacity on visual bell instead of background-color
npezza93 Aug 19, 2017
42c3eff
Update css transitions
npezza93 Aug 19, 2017
1f930c1
Add bellAudioElement to helperContainer instead of element
npezza93 Aug 19, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ <h2>Options</h2>
</select>
</label>
</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>
<label>scrollback <input type="number" id="option-scrollback" value="1000" /></label>
</p>
Expand Down
6 changes: 5 additions & 1 deletion demo/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var terminalContainer = document.getElementById('terminal-container'),
cursorBlink: document.querySelector('#option-cursor-blink'),
cursorStyle: document.querySelector('#option-cursor-style'),
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'),
rowsElement = document.getElementById('rows');
Expand Down Expand Up @@ -53,6 +54,9 @@ optionElements.cursorBlink.addEventListener('change', function () {
optionElements.cursorStyle.addEventListener('change', function () {
term.setOption('cursorStyle', optionElements.cursorStyle.value);
});
optionElements.bellStyle.addEventListener('change', function () {
term.setOption('bellStyle', optionElements.bellStyle.value);
});
optionElements.scrollback.addEventListener('change', function () {
term.setOption('scrollback', parseInt(optionElements.scrollback.value, 10));
});
Expand Down
9 changes: 1 addition & 8 deletions src/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,7 @@ export class InputHandler implements IInputHandler {
* Bell (Ctrl-G).
*/
public bell(): void {
if (!this._terminal.options.visualBell) {
return;
}
this._terminal.element.style.borderColor = 'white';
setTimeout(() => this._terminal.element.style.borderColor = '', 10);
if (this._terminal.options.popOnBell) {
this._terminal.focus();
}
this._terminal.bell();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface IInputHandlingTerminal extends IEventEmitter {
viewport: IViewport;
selectionManager: ISelectionManager;

bell(): void;
focus(): void;
convertEol: boolean;
updateRange(y: number): void;
Expand All @@ -113,6 +114,8 @@ export interface IInputHandlingTerminal extends IEventEmitter {
}

export interface ITerminalOptions {
bellSound?: string;
bellStyle?: string;
cancelEvents?: boolean;
colors?: string[];
cols?: number;
Expand All @@ -123,14 +126,12 @@ export interface ITerminalOptions {
disableStdin?: boolean;
geometry?: [number, number];
handler?: (data: string) => void;
popOnBell?: boolean;
rows?: number;
screenKeys?: boolean;
scrollback?: number;
tabStopWidth?: number;
termName?: string;
useFlowControl?: boolean;
visualBell?: boolean;
}

export interface IBuffer {
Expand Down
48 changes: 40 additions & 8 deletions src/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { CHARSETS } from './Charsets';
import { getRawByteCoords } from './utils/Mouse';
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 { BellSound } from './utils/Sounds';

// Declare for RequireJS in loadAddon
declare var define: any;
Expand Down Expand Up @@ -147,8 +148,8 @@ const DEFAULT_OPTIONS: ITerminalOptions = {
geometry: [80, 24],
cursorBlink: false,
cursorStyle: 'block',
visualBell: false,
popOnBell: false,
bellSound: BellSound,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cool if this gets lazy loaded using dynamic imports microsoft/TypeScript#12364. I'm not sure how well this works on the web yet, I guess we can defer this unless you want to have a look? #894

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a stab at this but was getting some errors so I think I'll punt this to a different PR if that's alright.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it might not be so easy 👍

bellStyle: null,
scrollback: 1000,
screenKeys: false,
debug: false,
Expand Down Expand Up @@ -178,6 +179,8 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
private helperContainer: HTMLElement;
private compositionView: HTMLElement;
private charSizeStyleElement: HTMLStyleElement;
private bellAudioElement: HTMLAudioElement;
private visualBellTimer: number;

public browser: IBrowser = <any>Browser;

Expand Down Expand Up @@ -482,6 +485,7 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
this.viewport.syncScrollArea();
break;
case 'tabStopWidth': this.setupStops(); break;
case 'bellStyle': this.preloadBellSound(); break;
}
}

Expand Down Expand Up @@ -689,6 +693,9 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
this.viewportScrollArea.classList.add('xterm-scroll-area');
this.viewportElement.appendChild(this.viewportScrollArea);

// preload audio
this.preloadBellSound();

// Create the selection container.
this.selectionContainer = document.createElement('div');
this.selectionContainer.classList.add('xterm-selection');
Expand Down Expand Up @@ -1874,12 +1881,16 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
* Note: We could do sweet things with webaudio here
*/
public bell(): void {
if (!this.options.visualBell) return;
this.element.style.borderColor = 'white';
setTimeout(() => {
this.element.style.borderColor = '';
}, 10);
if (this.options.popOnBell) this.focus();
this.emit('bell');
if (this.soundBell()) this.bellAudioElement.play();

if (this.visualBell()) {
this.element.classList.add('visual-bell-active');
clearTimeout(this.visualBellTimer);
this.visualBellTimer = window.setTimeout(() => {
this.element.classList.remove('visual-bell-active');
}, 200);
}
}

/**
Expand Down Expand Up @@ -2271,6 +2282,27 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT

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);
}
}
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ export type BooleanOption =
'cursorBlink' |
'debug' |
'disableStdin' |
'popOnBell' |
'screenKeys' |
'useFlowControl' |
'visualBell';
'useFlowControl';
export type StringOption =
'cursorStyle' |
'bellStyle' |
'termName';
export type StringArrayOption = 'colors';
export type NumberOption =
Expand Down
9 changes: 9 additions & 0 deletions src/utils/Sounds.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions src/utils/TestUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export class MockInputHandlingTerminal implements IInputHandlingTerminal {
throw new Error('Method not implemented.');
}
convertEol: boolean;
bell(): void {
throw new Error('Method not implemented.');
}

updateRange(y: number): void {
throw new Error('Method not implemented.');
}
Expand Down
10 changes: 10 additions & 0 deletions src/xterm.css
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@

.terminal .terminal-cursor {
position: relative;
transition: opacity 150ms ease;
}

.terminal:not(.focus) .terminal-cursor {
Expand All @@ -108,6 +109,7 @@
content: '';
position: absolute;
background-color: #fff;
transition: opacity 150ms ease;
}

.terminal.focus.xterm-cursor-style-bar:not(.xterm-cursor-blink-on) .terminal-cursor::before {
Expand All @@ -124,6 +126,14 @@
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 {
background: #000;
color: #FFF;
Expand Down