-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up and optimize codebase with more readable code (#36)
* Fix Go startup in README (#35) * Replace mouse button state logic with useReducer and useContext * Optimized tap interval * Clean up type declarations * Disable other Touchpad gestures in Android
- Loading branch information
Showing
8 changed files
with
119 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React, { useReducer } from "react"; | ||
|
||
import { mouseHandler } from "../utils"; | ||
import { MouseButtons, MouseClicks } from "../utils"; | ||
|
||
type LongButtonActions = { | ||
button: MouseButtons, | ||
turn?: boolean, | ||
} | ||
|
||
const initialLongButtonState = { | ||
toggleMouseLeft: false, | ||
toggleMouseMiddle: false, | ||
toggleMouseRight: false, | ||
}; | ||
|
||
export const LongButtonStateContext = React.createContext<typeof initialLongButtonState>(initialLongButtonState); | ||
export const LongButtonDispatchContext = React.createContext<React.Dispatch<LongButtonActions>>(() => null); | ||
|
||
export const LongButtonContextProvider = ({ children }: { children: React.ReactNode }) => { | ||
const [longButtonStates, longButtonDispatch] = useReducer(LongButtonReducer, initialLongButtonState); | ||
|
||
return ( | ||
<LongButtonStateContext.Provider value={longButtonStates}> | ||
<LongButtonDispatchContext.Provider value={longButtonDispatch}> | ||
{ children } | ||
</LongButtonDispatchContext.Provider> | ||
</LongButtonStateContext.Provider> | ||
); | ||
} | ||
|
||
const LongButtonReducer = (states: typeof initialLongButtonState, | ||
action: LongButtonActions) => { | ||
const { button, turn } = action; | ||
const { toggleMouseLeft, toggleMouseMiddle, toggleMouseRight } = states; | ||
switch (button) { | ||
case MouseButtons.LEFT: | ||
toggleMouseLeft | ||
? mouseHandler(MouseClicks.RELEASE, MouseButtons.LEFT) | ||
: mouseHandler(MouseClicks.PRESS, MouseButtons.LEFT); | ||
return { | ||
...states, | ||
toggleMouseLeft: (turn != undefined) ? turn : toggleMouseLeft ? false : true, | ||
}; | ||
case MouseButtons.MIDDLE: | ||
toggleMouseMiddle | ||
? mouseHandler(MouseClicks.RELEASE, MouseButtons.MIDDLE) | ||
: mouseHandler(MouseClicks.PRESS, MouseButtons.MIDDLE); | ||
return { | ||
...states, | ||
toggleMouseMiddle: (turn != undefined) ? turn : toggleMouseMiddle ? false : true, | ||
}; | ||
case MouseButtons.RIGHT: | ||
toggleMouseRight | ||
? mouseHandler(MouseClicks.RELEASE, MouseButtons.RIGHT) | ||
: mouseHandler(MouseClicks.PRESS, MouseButtons.RIGHT); | ||
return { | ||
...states, | ||
toggleMouseRight: (turn != undefined) ? turn : toggleMouseRight ? false : true, | ||
}; | ||
default: | ||
return states; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters