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

Implement sounds in Boson #28

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .erb/configs/webpack.config.renderer.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ const configuration: webpack.Configuration = {
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
// Sounds
{
test: /\.(ogg|mp3|wav)$/i,
type: 'asset/resource',
},
// Images
{
test: /\.(png|jpg|jpeg|gif)$/i,
Expand Down
5 changes: 5 additions & 0 deletions .erb/configs/webpack.config.renderer.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ const configuration: webpack.Configuration = {
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
// Sounds
{
test: /\.(ogg|mp3|wav)$/i,
type: 'asset/resource',
},
// Images
{
test: /\.(png|jpg|jpeg|gif)$/i,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"react-highlight-words": "^0.20.0",
"react-router-dom": "^6.12.1",
"react-virtuoso": "^4.3.10",
"sound-play": "^1.1.0",
"unique-names-generator": "^4.7.1"
},
"devDependencies": {
Expand All @@ -111,6 +112,7 @@
"@types/react-dom": "^18.2.4",
"@types/react-highlight-words": "^0.16.4",
"@types/react-test-renderer": "^18.0.0",
"@types/sound-play": "^1.1.0",
"@types/terser-webpack-plugin": "^5.2.0",
"@types/webpack-bundle-analyzer": "^4.6.0",
"@typescript-eslint/eslint-plugin": "^5.59.9",
Expand Down
9 changes: 9 additions & 0 deletions src/audio.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module '*.mp3' {
const src: string;
export default src;
}

declare module '*.wav' {
const src: string;
export default src;
}
10 changes: 7 additions & 3 deletions src/main/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { config, store, subscriptions as storeSubscriptions } from './store';
import connectAndAuthorise from './tron/tools';
import { tron } from './tron/tron';
import { CommandStatus } from './tron/types';
import { playSound } from './utils';

export default function loadEvents() {
// app events
Expand Down Expand Up @@ -139,7 +140,7 @@ export default function loadEvents() {
keytar.setPassword('boson', key, value);
});

// tools
// Tools
ipcMain.on('tools:get-uuid', async (event) => {
event.returnValue = randomUUID();
});
Expand All @@ -157,9 +158,12 @@ export default function loadEvents() {
return stdout;
}
);
ipcMain.handle('tools:open-in-browser', async (event, path: string) =>
shell.openExternal(path)
ipcMain.handle('tools:open-in-browser', async (event, extPath: string) =>
shell.openExternal(extPath)
);
ipcMain.handle('tools:play-sound', async (event, type: string) => {
playSound(type);
});

// Dialogs
ipcMain.handle(
Expand Down
2 changes: 1 addition & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import loadEvents from './events';
import MenuBuilder from './menu/menu';
import { config, store } from './store';
import { WindowNames, WindowParams } from './types';
import { resolveHtmlPath } from './util';
import { resolveHtmlPath } from './utils';

// For now disable log rotation
log.transports.file.maxSize = 0;
Expand Down
21 changes: 21 additions & 0 deletions src/main/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { app, BrowserWindow, Menu, MenuItemConstructorOptions } from 'electron';
import { createWindow } from '../main';
import { store } from '../store';
import {
checkForUpdates,
clearLogs,
Expand Down Expand Up @@ -37,6 +38,15 @@ export default class MenuBuilder {
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);

store.onDidChange('audio', (newValue) => {
if (!newValue) return;

const mutedSoundsItem = menu.getMenuItemById('mute-sounds');
if (mutedSoundsItem) {
mutedSoundsItem.checked = newValue.muted;
}
});

return menu;
}

Expand Down Expand Up @@ -74,6 +84,17 @@ export default class MenuBuilder {
click: () => createWindow('preferences'),
},
{ type: 'separator' },
{
id: 'mute-sounds',
label: 'Mute sounds',
type: 'checkbox',
checked: store.get('audio.muted'),
click: () => {
const current = store.get('audio.muted') as boolean;
store.set('audio.muted', !current);
},
accelerator: 'Command+Shift+M',
},
{
label: 'Save window positions',
click: saveWindows,
Expand Down
3 changes: 3 additions & 0 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ const ElectronAPI = {
openInApplication: (command: string) => {
return ipcRenderer.invoke('tools:open-in-application', command);
},
playSound: (type: string) => {
return ipcRenderer.invoke('tools:play-sound', type);
},
},
dialog: {
showMessageBox: async (
Expand Down
12 changes: 11 additions & 1 deletion src/main/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ const store = new Store({
st.set('hal.allowGotoFieldAutoMode', true);
st.delete('hal.useAutoMode' as keyof typeof userConfig);
},
'>=0.3.3': (st) => {
st.set('audio', {
mode: 'on',
muted: false,
minimal: ['error'],
sounds: {
error: 'error.wav',
},
});
},
},
});

Expand All @@ -49,4 +59,4 @@ if (!(store.get('windows.openWindows') as string[]).includes('main')) {

const subscriptions = new Map<string, () => EventEmitter>();

export { store, defaultConfig, subscriptions };
export { defaultConfig, store, subscriptions };
8 changes: 8 additions & 0 deletions src/main/store/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
"chat": {
"notifications": true
},
"audio": {
"mode": "on",
"muted": false,
"minimal": ["error"],
"sounds": {
"error": "error.wav"
}
},
"maxLogMessages": 50000,
"updateChannel": "stable",
"profiles": {}
Expand Down
6 changes: 6 additions & 0 deletions src/main/tron/tron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
uniqueNamesGenerator,
} from 'unique-names-generator';
import { config, store } from '../store';
import { playSound } from '../utils';
import Command from './command';
import parseLine from './keywords';
import Reply from './reply';
Expand Down Expand Up @@ -354,6 +355,11 @@ export class TronConnection {
keywords
);

// If message is error, play sound.
if (reply.code === ReplyCode.Error || reply.code === ReplyCode.Failed) {
playSound('error');
}

// Update reply date to match TCC TAI.
reply.date += this.taiOffset * 1000;

Expand Down
16 changes: 0 additions & 16 deletions src/main/util.ts

This file was deleted.

45 changes: 45 additions & 0 deletions src/main/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint import/prefer-default-export: off */
import path from 'path';
import sound from 'sound-play';
import { URL } from 'url';
import { store } from './store';

export function resolveHtmlPath(htmlFileName: string) {
if (process.env.NODE_ENV === 'development') {
const port = process.env.PORT || 1212;
const url = new URL(`http://localhost:${port}/${htmlFileName}`);
url.pathname = htmlFileName;
return url.href;
}
return `file://${path.resolve(
__dirname,
`../renderer/index.html?${htmlFileName}`
)}`;
}

export interface PlaySoundOpts {
overrideMode: boolean;
}

export function playSound(type: string, opts?: PlaySoundOpts) {
const { overrideMode = false } = opts || {};

const file = store.get(`audio.sounds.${type}`, null);
if (!file) return;

const mode: string = store.get('audio.mode');
const minimals: string[] = store.get('audio.minimal');
const muted = store.get('audio.muted');

if (!overrideMode) {
if (muted) return;
if (mode === 'off') return;
if (mode === 'minimal' && !minimals.includes(type)) return;
}

if (path.isAbsolute(file)) {
sound.play(file);
} else {
sound.play(path.join(__dirname, '../sounds', file));
}
}
18 changes: 14 additions & 4 deletions src/renderer/Preferences/Components/BooleanOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ export default function BooleanOption(props: BooleanOptionProps) {
const { title, param, description } = props;

return (
<Grid container pt={1} minHeight={50} alignContent='center'>
<Grid item xs={9}>
<TypographyTitle>{title}</TypographyTitle>
<TypographyDescription>{description}</TypographyDescription>
<Grid
container
pt={1}
minHeight={50}
alignItems='center'
alignContent='center'
>
<Grid item xs={9} alignContent='center' alignItems='center'>
<TypographyTitle gutterBottom={description !== undefined}>
{title}
</TypographyTitle>
{description && (
<TypographyDescription>{description}</TypographyDescription>
)}
</Grid>
<Grid
item
Expand Down
10 changes: 7 additions & 3 deletions src/renderer/Preferences/Components/TypographyTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ type PreferencesTypographyType = {
children?: React.ReactNode;
};

export function TypographyTitle(props: PreferencesTypographyType) {
const { children } = props;
type PreferencesTypographyTitleType = PreferencesTypographyType & {
gutterBottom?: boolean;
};

export function TypographyTitle(props: PreferencesTypographyTitleType) {
const { children, gutterBottom = true } = props;

return (
<Typography
Expand All @@ -25,7 +29,7 @@ export function TypographyTitle(props: PreferencesTypographyType) {
userSelect: 'none',
alignSelf: 'center',
})}
gutterBottom
gutterBottom={gutterBottom}
>
{children}
</Typography>
Expand Down
49 changes: 48 additions & 1 deletion src/renderer/Preferences/Panes/InterfacePane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Grid from '@mui/system/Unstable_Grid';
import React from 'react';
import { ColorModeValues } from 'renderer/App';
import { useStore } from 'renderer/hooks';
import BooleanOption from '../Components/BooleanOption';
import Pane from '../Components/Pane';
import PreferencesFormControlLabel from '../Components/PreferencesFormControlLabel';
import PreferencesRadioGroup from '../Components/PreferencesRadioGroup';
Expand All @@ -31,7 +32,9 @@ import {

function ThemeMode() {
const theme = useTheme();
const [themeMode, setThemeMode] = React.useState<ColorModeValues>('system');
const [themeMode, setThemeMode] = React.useState<ColorModeValues>(
window.electron.store.get('interface.mode') || 'system'
);

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setThemeMode(event.target.value as ColorModeValues);
Expand Down Expand Up @@ -69,6 +72,48 @@ function ThemeMode() {
);
}

function AudioMode() {
const key = 'audio.mode';
const [audioMode, setAudioMode] = React.useState<string>(
window.electron.store.get(key) || 'on'
);

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setAudioMode(event.target.value);
window.electron.store.set(key, event.target.value);
};

return (
<Stack>
<Typography variant='button' color='text.secondary' fontSize='13px'>
Sounds
</Typography>
<FormControl sx={{ paddingTop: 1 }}>
<PreferencesRadioGroup value={audioMode} onChange={handleChange}>
<PreferencesFormControlLabel
value='on'
control={<Radio />}
label='On'
/>
<PreferencesFormControlLabel
value='minimal'
control={<Radio />}
label='Minimal'
/>
<PreferencesFormControlLabel
value='off'
control={<Radio />}
label='Off'
/>
</PreferencesRadioGroup>
</FormControl>
<Box px={1} pr={2}>
<BooleanOption param='audio.muted' title='Mute all sounds' />
</Box>
</Stack>
);
}

function WindowManagement() {
const [saveOnlyOnRequest] = useStore<boolean>('interface.saveOnlyOnRequest');

Expand Down Expand Up @@ -157,6 +202,8 @@ export default function InterfacePane() {
<Stack width='100%' direction='column'>
<ThemeMode />
<Divider sx={{ my: 4 }} />
<AudioMode />
<Divider sx={{ my: 4 }} />
<WindowManagement />
</Stack>
</Grid>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/Preferences/Preferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function MenuItemPreferences(props: {
name: string;
title: string;
selectedPane: string;
setSelectedPane: (string) => void;
setSelectedPane: (arg0: string) => void;
}) {
const { name, title, selectedPane, setSelectedPane } = props;
const DEFAULT_PANE = 'connection';
Expand Down
Binary file added src/sounds/error.wav
Binary file not shown.
Loading