2020-04-19 20:57:43 +12:00
|
|
|
import ReactGA from 'react-ga';
|
2019-06-28 09:34:15 +12:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { bindActionCreators } from 'redux';
|
2021-03-30 21:27:18 +13:00
|
|
|
import { useHotkeys, useIsHotkeyPressed } from 'react-hotkeys-hook';
|
|
|
|
|
import { indexToArray, sortItems, applyFilter } from '../util/arrays';
|
|
|
|
|
import { collate } from '../util/format';
|
2019-04-18 09:03:38 +12:00
|
|
|
|
2019-06-28 09:34:15 +12:00
|
|
|
import * as uiActions from '../services/ui/actions';
|
|
|
|
|
import * as mopidyActions from '../services/mopidy/actions';
|
2021-03-30 21:27:18 +13:00
|
|
|
import * as snapcastActions from '../services/snapcast/actions';
|
|
|
|
|
|
|
|
|
|
const Hotkeys = ({
|
|
|
|
|
play_state,
|
|
|
|
|
mopidyActions,
|
|
|
|
|
uiActions,
|
|
|
|
|
snapcastActions,
|
|
|
|
|
mute,
|
|
|
|
|
play_time_position,
|
|
|
|
|
history,
|
|
|
|
|
dragging,
|
|
|
|
|
allow_reporting,
|
|
|
|
|
volume,
|
|
|
|
|
snapcast_groups,
|
|
|
|
|
snapcast_clients,
|
|
|
|
|
show_disconnected_clients,
|
|
|
|
|
}) => {
|
|
|
|
|
const prepare = ({ e, label, callback }) => {
|
2020-09-06 13:53:22 +12:00
|
|
|
const {
|
|
|
|
|
target,
|
|
|
|
|
altKey,
|
|
|
|
|
ctrlKey,
|
|
|
|
|
metaKey,
|
|
|
|
|
shiftKey,
|
2021-03-30 21:27:18 +13:00
|
|
|
key,
|
2020-09-06 13:53:22 +12:00
|
|
|
} = e;
|
2019-10-23 07:34:52 +13:00
|
|
|
// Ignore text input fields
|
|
|
|
|
if (
|
2021-03-23 21:21:00 +13:00
|
|
|
(target.nodeName === 'INPUT' && (target.type === 'text' || target.type === 'number'))
|
|
|
|
|
|| target.nodeName === 'TEXTAREA'
|
|
|
|
|
|| (target.nodeName === 'BUTTON' && key === ' ')) {
|
2019-09-05 15:36:11 +12:00
|
|
|
return;
|
|
|
|
|
}
|
2019-04-18 09:03:38 +12:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
// Ignore when there are any key modifiers. This enables us to avoid interfering
|
|
|
|
|
// with browser- and OS-default functions.
|
2020-09-06 13:53:22 +12:00
|
|
|
if (altKey || ctrlKey || metaKey || shiftKey) {
|
2019-09-05 15:36:11 +12:00
|
|
|
return;
|
|
|
|
|
}
|
2019-04-18 09:03:38 +12:00
|
|
|
|
2021-03-30 21:27:18 +13:00
|
|
|
if (allow_reporting) {
|
|
|
|
|
ReactGA.event({ category: 'Hotkey', action: e.key, label });
|
|
|
|
|
}
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
callback();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isPressed = useIsHotkeyPressed();
|
|
|
|
|
const onePressed = isPressed('1');
|
|
|
|
|
const twoPressed = isPressed('2');
|
|
|
|
|
const threePressed = isPressed('3');
|
|
|
|
|
const fourPressed = isPressed('4');
|
|
|
|
|
const fivePressed = isPressed('5');
|
|
|
|
|
|
|
|
|
|
useHotkeys('i', (e) => prepare({
|
|
|
|
|
e,
|
|
|
|
|
label: 'Hotkey info',
|
|
|
|
|
callback: () => {
|
|
|
|
|
history.push('/hotkeys');
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
useHotkeys('space,p', (e) => prepare({
|
|
|
|
|
e,
|
|
|
|
|
label: 'Play/pause',
|
|
|
|
|
callback: () => {
|
|
|
|
|
if (play_state === 'playing') {
|
|
|
|
|
mopidyActions.pause();
|
|
|
|
|
uiActions.createNotification({ content: 'pause', type: 'shortcut' });
|
|
|
|
|
} else {
|
|
|
|
|
mopidyActions.play();
|
|
|
|
|
uiActions.createNotification({ content: 'play_arrow', type: 'shortcut' });
|
2021-02-08 17:18:42 +13:00
|
|
|
}
|
2021-03-30 21:27:18 +13:00
|
|
|
},
|
|
|
|
|
}), {}, [play_state]);
|
|
|
|
|
|
|
|
|
|
useHotkeys('s', (e) => prepare({
|
|
|
|
|
e,
|
|
|
|
|
label: 'Stop',
|
|
|
|
|
callback: () => {
|
|
|
|
|
mopidyActions.stop();
|
|
|
|
|
uiActions.createNotification({ content: 'stop', type: 'shortcut' });
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
useHotkeys('r', (e) => prepare({
|
|
|
|
|
e,
|
|
|
|
|
label: 'Rewind',
|
|
|
|
|
callback: () => {
|
|
|
|
|
let new_position = play_time_position - 30000;
|
|
|
|
|
if (new_position < 0) {
|
|
|
|
|
new_position = 0;
|
2021-02-08 17:18:42 +13:00
|
|
|
}
|
2021-03-30 21:27:18 +13:00
|
|
|
mopidyActions.setTimePosition(new_position);
|
|
|
|
|
uiActions.createNotification({ content: 'fast_rewind', type: 'shortcut' });
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
useHotkeys('f', (e) => prepare({
|
|
|
|
|
e,
|
|
|
|
|
label: 'Fastforward',
|
|
|
|
|
callback: () => {
|
|
|
|
|
mopidyActions.setTimePosition(play_time_position + 30000);
|
|
|
|
|
uiActions.createNotification({ content: 'fast_forward', type: 'shortcut' });
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
useHotkeys(',', (e) => prepare({
|
|
|
|
|
e,
|
|
|
|
|
label: 'Previous',
|
|
|
|
|
callback: () => {
|
|
|
|
|
mopidyActions.previous();
|
|
|
|
|
uiActions.createNotification({ content: 'skip_previous', type: 'shortcut' });
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
useHotkeys('.', (e) => prepare({
|
|
|
|
|
e,
|
|
|
|
|
label: 'Next',
|
|
|
|
|
callback: () => {
|
|
|
|
|
mopidyActions.next();
|
|
|
|
|
uiActions.createNotification({ content: 'skip_next', type: 'shortcut' });
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
useHotkeys('=,1+=,2+=,3+=,4+=,5+=', (e, handler) => prepare({
|
|
|
|
|
e,
|
|
|
|
|
label: 'Volume up',
|
|
|
|
|
callback: () => {
|
|
|
|
|
uiActions.createNotification({ content: 'volume_up', type: 'shortcut' });
|
|
|
|
|
if (handler.key === '=') {
|
2019-09-05 15:36:11 +12:00
|
|
|
if (volume !== 'false') {
|
2021-03-30 21:27:18 +13:00
|
|
|
let nextVolume = volume + 5;
|
|
|
|
|
if (nextVolume > 100) nextVolume = 100;
|
|
|
|
|
mopidyActions.setVolume(nextVolume);
|
2019-12-03 16:53:41 +13:00
|
|
|
if (mute) {
|
|
|
|
|
mopidyActions.setMute(false);
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
2019-04-18 09:03:38 +12:00
|
|
|
}
|
2021-03-30 21:27:18 +13:00
|
|
|
} else {
|
|
|
|
|
const index = parseInt(handler.key.replace('+='), 10);
|
|
|
|
|
setSnapcastVolume(index - 1, 5);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}), {}, [volume, mute]);
|
|
|
|
|
|
|
|
|
|
useHotkeys('-,1+-,2+-,3+-,4+-,5+-', (e, handler) => prepare({
|
|
|
|
|
e,
|
|
|
|
|
label: 'Volume down',
|
|
|
|
|
callback: () => {
|
|
|
|
|
uiActions.createNotification({ content: 'volume_down', type: 'shortcut' });
|
|
|
|
|
if (handler.key === '-') {
|
2019-09-05 15:36:11 +12:00
|
|
|
if (volume !== 'false') {
|
2021-03-30 21:27:18 +13:00
|
|
|
let nextVolume = volume - 5;
|
|
|
|
|
if (nextVolume < 0) nextVolume = 0;
|
|
|
|
|
mopidyActions.setVolume(nextVolume);
|
2019-12-03 16:53:41 +13:00
|
|
|
if (mute) {
|
|
|
|
|
mopidyActions.setMute(false);
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
2019-05-05 11:06:17 +12:00
|
|
|
}
|
2021-03-30 21:27:18 +13:00
|
|
|
} else {
|
|
|
|
|
const index = parseInt(handler.key.replace('+-'), 10);
|
|
|
|
|
setSnapcastVolume(index - 1, -5);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}), {}, [volume, mute]);
|
|
|
|
|
|
|
|
|
|
useHotkeys('0', (e) => prepare({
|
|
|
|
|
e,
|
|
|
|
|
label: 'Mute/unmute',
|
|
|
|
|
callback: () => {
|
|
|
|
|
if (mute) {
|
|
|
|
|
mopidyActions.setMute(false);
|
|
|
|
|
uiActions.createNotification({ content: 'volume_up', type: 'shortcut' });
|
|
|
|
|
} else {
|
|
|
|
|
mopidyActions.setMute(true);
|
|
|
|
|
uiActions.createNotification({ content: 'volume_off', type: 'shortcut' });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}), {}, [mute]);
|
|
|
|
|
|
|
|
|
|
useHotkeys('escape', (e) => prepare({
|
|
|
|
|
e,
|
|
|
|
|
label: 'Escape',
|
|
|
|
|
callback: () => {
|
|
|
|
|
if (dragging) {
|
|
|
|
|
uiActions.dragEnd();
|
2020-09-11 13:53:54 +12:00
|
|
|
e.preventDefault();
|
2021-03-30 21:27:18 +13:00
|
|
|
} else if ($('body').hasClass('modal-open')) {
|
|
|
|
|
window.history.back();
|
2020-09-11 13:53:54 +12:00
|
|
|
e.preventDefault();
|
2021-03-30 21:27:18 +13:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}), {}, [dragging]);
|
|
|
|
|
|
|
|
|
|
const setSnapcastVolume = (index, adjustment) => {
|
|
|
|
|
const simpleGroups = indexToArray(snapcast_groups);
|
|
|
|
|
if (simpleGroups.length <= 0) return false;
|
|
|
|
|
|
|
|
|
|
let group = sortItems(simpleGroups, 'name')[index];
|
|
|
|
|
if (!group) return false;
|
|
|
|
|
group = collate(group, { clients: snapcast_clients });
|
|
|
|
|
let { clients: groupClients } = group;
|
|
|
|
|
if (!groupClients) return false;
|
|
|
|
|
if (!show_disconnected_clients) {
|
|
|
|
|
groupClients = groupClients.filter((c) => c.connected);
|
|
|
|
|
}
|
2019-09-05 15:36:11 +12:00
|
|
|
|
2021-04-04 11:52:24 +12:00
|
|
|
const groupVolume = groupClients.reduce(
|
2021-03-30 21:27:18 +13:00
|
|
|
(acc, client) => acc + (client.volume || 0),
|
|
|
|
|
0,
|
|
|
|
|
) / groupClients.length;
|
2019-12-03 16:53:41 +13:00
|
|
|
|
2021-04-04 11:52:24 +12:00
|
|
|
snapcastActions.setGroupVolume(group.id, groupVolume + adjustment, groupVolume);
|
2021-03-30 21:27:18 +13:00
|
|
|
uiActions.createNotification({ content: 'volume_up', type: 'shortcut' });
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
2019-06-28 09:34:15 +12:00
|
|
|
|
2021-03-30 21:27:18 +13:00
|
|
|
return null;
|
2019-06-28 09:34:15 +12:00
|
|
|
}
|
|
|
|
|
|
2021-02-08 17:18:42 +13:00
|
|
|
const mapStateToProps = (state) => ({
|
|
|
|
|
volume: state.mopidy.volume,
|
2019-09-05 15:36:11 +12:00
|
|
|
mute: state.mopidy.mute,
|
|
|
|
|
play_state: state.mopidy.play_state,
|
2021-02-08 17:18:42 +13:00
|
|
|
play_time_position: parseInt(state.mopidy.time_position, 10),
|
2019-09-05 15:36:11 +12:00
|
|
|
dragging: state.ui.dragger && state.ui.dragger.dragging,
|
2020-04-19 20:57:43 +12:00
|
|
|
allow_reporting: state.ui.allow_reporting,
|
2021-03-30 21:27:18 +13:00
|
|
|
snapcast_groups: state.snapcast.groups,
|
|
|
|
|
snapcast_clients: state.snapcast.clients,
|
|
|
|
|
show_disconnected_clients: state.ui.snapcast_show_disconnected_clients || false,
|
2019-09-05 15:36:11 +12:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
|
uiActions: bindActionCreators(uiActions, dispatch),
|
|
|
|
|
mopidyActions: bindActionCreators(mopidyActions, dispatch),
|
2021-03-30 21:27:18 +13:00
|
|
|
snapcastActions: bindActionCreators(snapcastActions, dispatch),
|
2019-09-05 15:36:11 +12:00
|
|
|
});
|
2019-06-28 09:34:15 +12:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(Hotkeys);
|