Skip to content
This repository was archived by the owner on Jun 5, 2019. It is now read-only.

Commit 0769bd7

Browse files
authored
Adds keyboard support to the renderer. (#19)
1 parent b3bfedd commit 0769bd7

File tree

6 files changed

+88
-32
lines changed

6 files changed

+88
-32
lines changed

package-lock.json

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,20 @@
4343
"mobx": "3.2.1",
4444
"mobx-react": "4.2.2",
4545
"mobx-state-tree": "0.9.2",
46+
"mousetrap": "1.6.1",
4647
"ramda": "0.24.1",
4748
"react": "15.6.1",
4849
"react-dom": "15.6.1",
4950
"rebass": "1.0.0"
5051
},
5152
"description": "An electron starter project.",
5253
"devDependencies": {
53-
"@types/react": "^15.0.38",
54-
"@types/react-dom": "^15.5.1",
54+
"@types/mousetrap": "1.5.34",
55+
"@types/react": "15.0.38",
56+
"@types/react-dom": "15.5.1",
5557
"ava": "0.21.0",
5658
"electron": "1.7.4",
57-
"electron-builder": "^19.16.0",
59+
"electron-builder": "19.16.0",
5860
"fuse-box": "2.2.0",
5961
"husky": "0.14.3",
6062
"lint-staged": "4.0.1",
@@ -96,5 +98,5 @@
9698
"postinstall": "Used by electron-builder to build native dependencies.",
9799
"start": "Starts the app in dev mode."
98100
},
99-
"version": "0.9.0"
100-
}
101+
"version": "0.10.0"
102+
}
Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as React from 'react'
22
import { Tabs, TabItem } from 'rebass'
3+
import { bindKey, unbindKey } from '../../platform'
34

45
export type SampleTabType = 'one' | 'two' | 'three'
6+
57
export interface SampleTabsProps {
68
tab: SampleTabType
79
onChangeTab: (tab: SampleTabType) => void
@@ -10,30 +12,39 @@ export interface SampleTabsProps {
1012
const style = { WebkitAppRegion: 'drag' }
1113
const tabStyle = { cursor: 'pointer', WebkitAppRegion: 'no-drag' }
1214

13-
export function SampleTabs(props: SampleTabsProps) {
14-
return (
15-
<Tabs px={3} bg="gray1" style={style}>
16-
<TabItem
17-
style={tabStyle}
18-
onClick={() => props.onChangeTab('one')}
19-
active={props.tab === 'one'}
20-
>
21-
Random Dog
22-
</TabItem>
23-
<TabItem
24-
style={tabStyle}
25-
onClick={() => props.onChangeTab('two')}
26-
active={props.tab === 'two'}
27-
>
28-
Long Content
29-
</TabItem>
30-
<TabItem
31-
style={tabStyle}
32-
onClick={() => props.onChangeTab('three')}
33-
active={props.tab === 'three'}
34-
>
35-
Empty
36-
</TabItem>
37-
</Tabs>
38-
)
15+
export class SampleTabs extends React.PureComponent<SampleTabsProps, {}> {
16+
tab1 = () => this.props.onChangeTab('one')
17+
tab2 = () => this.props.onChangeTab('two')
18+
tab3 = () => this.props.onChangeTab('three')
19+
20+
componentDidMount() {
21+
bindKey('command+1', () => this.tab1())
22+
bindKey('command+2', () => this.tab2())
23+
bindKey('command+3', () => this.tab3())
24+
}
25+
26+
componentWillUnmount() {
27+
unbindKey('command+1')
28+
unbindKey('command+2')
29+
unbindKey('command+3')
30+
}
31+
32+
render() {
33+
const isTab1 = this.props.tab === 'one'
34+
const isTab2 = this.props.tab === 'two'
35+
const isTab3 = this.props.tab === 'three'
36+
return (
37+
<Tabs px={3} bg="gray1" style={style}>
38+
<TabItem style={tabStyle} onClick={() => this.tab1()} active={isTab1}>
39+
Random Dog
40+
</TabItem>
41+
<TabItem style={tabStyle} onClick={() => this.tab2()} active={isTab2}>
42+
Long Content
43+
</TabItem>
44+
<TabItem style={tabStyle} onClick={() => this.tab3()} active={isTab3}>
45+
Empty
46+
</TabItem>
47+
</Tabs>
48+
)
49+
}
3950
}

src/renderer/platform/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export * from './utils'
12
export * from './theme/theme'
23
export * from './components'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './keyboard/keyboard'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// A straight thru wrapper with the intention to add contexts
2+
// which will swap out groups of keybinds at a time.
3+
import * as Mousetrap from 'mousetrap'
4+
5+
export type KeyboardCallback = (e: ExtendedKeyboardEvent, combo: string) => any
6+
export type KeyboardAction = 'keypress' | 'keydown' | 'keyup'
7+
8+
// only needs to happen once
9+
Mousetrap.prototype.stopCallback = () => false
10+
11+
/**
12+
* Binds a keystroke to a function.
13+
*
14+
* @param keys The keystroke.
15+
* @param callback The function to fire.
16+
* @param action Optional keyboard event to further constraint.
17+
*/
18+
export function bindKey(keys: string | string[], callback: KeyboardCallback, action?: KeyboardAction) {
19+
Mousetrap.bind(keys, callback, action)
20+
}
21+
22+
/**
23+
* Removes a keybind.
24+
*
25+
* @param keys The keystroke.
26+
* @param action Optional keyboard event to further constraint.
27+
*/
28+
export function unbindKey(keys: string | string[], action?: KeyboardAction) {
29+
Mousetrap.unbind(keys, action)
30+
}

0 commit comments

Comments
 (0)