mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 09:36:27 +00:00
Merge pull request #211 from pybricks/dlech
Add keyboard shortcuts to action buttons
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
// Copyright (c) 2020 The Pybricks Authors
|
// Copyright (c) 2020 The Pybricks Authors
|
||||||
|
|
||||||
import { Button, Intent, Position, Tooltip } from '@blueprintjs/core';
|
import { Button, Hotkey, Hotkeys, Intent, Position, Tooltip } from '@blueprintjs/core';
|
||||||
|
// importing this way due to https://github.com/palantir/blueprint/issues/3604
|
||||||
|
import { HotkeysTarget } from '@blueprintjs/core/lib/esnext/components/hotkeys/hotkeysTarget';
|
||||||
import { WithI18nProps, withI18n } from '@shopify/react-i18n';
|
import { WithI18nProps, withI18n } from '@shopify/react-i18n';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { TooltipId } from './button-i18n';
|
import { TooltipId } from './button-i18n';
|
||||||
@@ -10,6 +12,8 @@ import en from './button-i18n.en.json';
|
|||||||
export interface ActionButtonProps {
|
export interface ActionButtonProps {
|
||||||
/** A unique id for each instance. */
|
/** A unique id for each instance. */
|
||||||
readonly id: string;
|
readonly id: string;
|
||||||
|
/** Keyboard shortcut. */
|
||||||
|
readonly keyboardShortcut?: string;
|
||||||
/** Tooltip text that appears when hovering over the button. */
|
/** Tooltip text that appears when hovering over the button. */
|
||||||
readonly tooltip: TooltipId;
|
readonly tooltip: TooltipId;
|
||||||
/** Icon shown on the button. */
|
/** Icon shown on the button. */
|
||||||
@@ -22,14 +26,19 @@ export interface ActionButtonProps {
|
|||||||
|
|
||||||
type Props = ActionButtonProps & WithI18nProps;
|
type Props = ActionButtonProps & WithI18nProps;
|
||||||
|
|
||||||
|
@HotkeysTarget
|
||||||
class ActionButton extends React.Component<Props> {
|
class ActionButton extends React.Component<Props> {
|
||||||
|
private buttonRef: React.RefObject<Button> = React.createRef();
|
||||||
|
|
||||||
render(): JSX.Element {
|
render(): JSX.Element {
|
||||||
|
let tooltipText = this.props.i18n.translate(this.props.tooltip);
|
||||||
|
if (this.props.keyboardShortcut) {
|
||||||
|
tooltipText += ` (${this.props.keyboardShortcut})`;
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<Tooltip
|
<Tooltip content={tooltipText} position={Position.BOTTOM}>
|
||||||
content={this.props.i18n.translate(this.props.tooltip)}
|
|
||||||
position={Position.BOTTOM}
|
|
||||||
>
|
|
||||||
<Button
|
<Button
|
||||||
|
ref={this.buttonRef}
|
||||||
intent={Intent.PRIMARY}
|
intent={Intent.PRIMARY}
|
||||||
onClick={(): void => this.props.onAction()}
|
onClick={(): void => this.props.onAction()}
|
||||||
disabled={this.props.enabled === false}
|
disabled={this.props.enabled === false}
|
||||||
@@ -45,6 +54,29 @@ class ActionButton extends React.Component<Props> {
|
|||||||
</Tooltip>
|
</Tooltip>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderHotkeys(): React.ReactElement {
|
||||||
|
if (!this.props.keyboardShortcut) {
|
||||||
|
return <Hotkeys />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Hotkeys>
|
||||||
|
<Hotkey
|
||||||
|
global={true}
|
||||||
|
allowInInput={true}
|
||||||
|
preventDefault={true}
|
||||||
|
combo={this.props.keyboardShortcut.replaceAll('-', '+')}
|
||||||
|
label={this.props.i18n.translate(this.props.tooltip)}
|
||||||
|
onKeyDown={(): void => {
|
||||||
|
if (this.props.enabled) {
|
||||||
|
this.props.onAction();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Hotkeys>
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withI18n({ id: 'actionButton', fallback: en, translations: { en } })(
|
export default withI18n({ id: 'actionButton', fallback: en, translations: { en } })(
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ import docsIcon from './images/pybricks.svg';
|
|||||||
|
|
||||||
type StateProps = undefined;
|
type StateProps = undefined;
|
||||||
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
|
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
|
||||||
type OwnProps = Pick<ActionButtonProps, 'id'>;
|
type OwnProps = Pick<ActionButtonProps, 'id'> &
|
||||||
|
Pick<ActionButtonProps, 'keyboardShortcut'>;
|
||||||
|
|
||||||
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
|
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
|
||||||
onAction: (): Action => dispatch(toggleDocs()),
|
onAction: (): Action => dispatch(toggleDocs()),
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ import replIcon from './images/repl.svg';
|
|||||||
|
|
||||||
type StateProps = Pick<ActionButtonProps, 'enabled'>;
|
type StateProps = Pick<ActionButtonProps, 'enabled'>;
|
||||||
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
|
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
|
||||||
type OwnProps = Pick<ActionButtonProps, 'id'>;
|
type OwnProps = Pick<ActionButtonProps, 'id'> &
|
||||||
|
Pick<ActionButtonProps, 'keyboardShortcut'>;
|
||||||
|
|
||||||
const mapStateToProps = (state: RootState): StateProps => ({
|
const mapStateToProps = (state: RootState): StateProps => ({
|
||||||
enabled:
|
enabled:
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ import runIcon from './images/run.svg';
|
|||||||
|
|
||||||
type StateProps = Pick<ActionButtonProps, 'enabled'>;
|
type StateProps = Pick<ActionButtonProps, 'enabled'>;
|
||||||
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
|
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
|
||||||
type OwnProps = Pick<ActionButtonProps, 'id'>;
|
type OwnProps = Pick<ActionButtonProps, 'id'> &
|
||||||
|
Pick<ActionButtonProps, 'keyboardShortcut'>;
|
||||||
|
|
||||||
const mapStateToProps = (state: RootState): StateProps => ({
|
const mapStateToProps = (state: RootState): StateProps => ({
|
||||||
enabled:
|
enabled:
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ import downloadIcon from './images/download.svg';
|
|||||||
|
|
||||||
type StateProps = Pick<ActionButtonProps, 'enabled'>;
|
type StateProps = Pick<ActionButtonProps, 'enabled'>;
|
||||||
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
|
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
|
||||||
type OwnProps = Pick<ActionButtonProps, 'id'>;
|
type OwnProps = Pick<ActionButtonProps, 'id'> &
|
||||||
|
Pick<ActionButtonProps, 'keyboardShortcut'>;
|
||||||
|
|
||||||
const mapStateToProps = (state: RootState): StateProps => ({
|
const mapStateToProps = (state: RootState): StateProps => ({
|
||||||
enabled: state.editor.current !== null,
|
enabled: state.editor.current !== null,
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ import stopIcon from './images/stop.svg';
|
|||||||
|
|
||||||
type StateProps = Pick<ActionButtonProps, 'enabled'>;
|
type StateProps = Pick<ActionButtonProps, 'enabled'>;
|
||||||
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
|
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
|
||||||
type OwnProps = Pick<ActionButtonProps, 'id'>;
|
type OwnProps = Pick<ActionButtonProps, 'id'> &
|
||||||
|
Pick<ActionButtonProps, 'keyboardShortcut'>;
|
||||||
|
|
||||||
const mapStateToProps = (state: RootState): StateProps => ({
|
const mapStateToProps = (state: RootState): StateProps => ({
|
||||||
enabled: state.hub.runtime === HubRuntimeState.Running,
|
enabled: state.hub.runtime === HubRuntimeState.Running,
|
||||||
|
|||||||
@@ -62,6 +62,10 @@ class Terminal extends React.Component<TerminalProps> {
|
|||||||
// a control character to the terminal.
|
// a control character to the terminal.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (e.key === 'F5' || e.key === 'F6') {
|
||||||
|
// allow global handler for these keys
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ class Toolbar extends React.Component {
|
|||||||
<Navbar.Divider />
|
<Navbar.Divider />
|
||||||
<ButtonGroup>
|
<ButtonGroup>
|
||||||
<BluetoothButton id="bluetooth" />
|
<BluetoothButton id="bluetooth" />
|
||||||
<RunButton id="run" />
|
<RunButton id="run" keyboardShortcut="F5" />
|
||||||
<StopButton id="stop" />
|
<StopButton id="stop" keyboardShortcut="F6" />
|
||||||
</ButtonGroup>
|
</ButtonGroup>
|
||||||
<Navbar.Divider />
|
<Navbar.Divider />
|
||||||
<ButtonGroup>
|
<ButtonGroup>
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
"jsx": "react",
|
"jsx": "react",
|
||||||
"downlevelIteration": true,
|
"downlevelIteration": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
|
"emitDecoratorMetadata": true,
|
||||||
"noFallthroughCasesInSwitch": true
|
"noFallthroughCasesInSwitch": true
|
||||||
},
|
},
|
||||||
"include": ["src", "test"]
|
"include": ["src", "test"]
|
||||||
|
|||||||
Reference in New Issue
Block a user