Skip to content

Commit

Permalink
add channel switcher (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtinth committed Aug 20, 2023
1 parent 5c78e7d commit c333901
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/core/MainView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { AppServices } from './AppServices'
import MIDIEmitter from './MIDIEmitter'
import { tw } from 'twind'
import { StatusBar } from './StatusBar'
import { switchOutputChannel, OutputChannelSwitcher } from './switchOutputChannel'
import { showInformationMessage } from './showInformationMessage'

export class MainView extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -49,6 +51,16 @@ export class MainView extends React.Component {
if (e.metaKey) {
if (e.keyCode >= 0x30 && e.keyCode <= 0x39) {
MIDI.send([0xc0, e.keyCode === 0x30 ? 9 : e.keyCode - 0x31])
showInformationMessage(
'Program change to #' + (e.keyCode === 0x30 ? 10 : e.keyCode - 0x30)
)
e.preventDefault()
return
}
}
if (e.ctrlKey) {
if (e.keyCode >= 0x30 && e.keyCode <= 0x39) {
switchOutputChannel(e.keyCode === 0x30 ? 9 : e.keyCode - 0x31)
e.preventDefault()
return
}
Expand Down Expand Up @@ -204,6 +216,7 @@ export class MainView extends React.Component {
)}
</Observer>
<AppServices features={this.props.features} store={this.store} />
<OutputChannelSwitcher />
</div>
)
}
Expand Down
17 changes: 17 additions & 0 deletions src/core/showInformationMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { tw } from "twind"

export function showInformationMessage (text: string) {
const message = document.createElement('div')
message.className = tw`absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 pointer-events-none bg-black text-white text-xl p-4`
message.style.opacity = '1'
message.style.zIndex = '999'
message.style.transition = 'opacity 0.5s ease-in-out'
message.innerText = text
document.body.appendChild(message)
setTimeout(() => {
message.style.opacity = '0'
setTimeout(() => {
document.body.removeChild(message)
}, 500)
}, 1000)
}
18 changes: 18 additions & 0 deletions src/core/switchOutputChannel.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React, { useEffect } from 'react';
import { useConfiguration } from './AppConfigurationHooks';
import { showInformationMessage } from './showInformationMessage';
import { StatusBarItem } from './StatusBar';

// HACK: Allow switching output channel from keyboard.
export let switchOutputChannel = () => { };

export function OutputChannelSwitcher() {
const mainChannel = useConfiguration('midi.output.channel');
useEffect(() => {
switchOutputChannel = (channel) => {
mainChannel.setValue(String(channel));
showInformationMessage('Switched output channel to ch.' + (channel + 1));
};
}, [mainChannel.setValue]);
return <StatusBarItem>ch.{+mainChannel.value + 1}</StatusBarItem>;
}

0 comments on commit c333901

Please sign in to comment.