From dd9b3bddff8cee67a226f9801c59a47b5bbdea64 Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Sun, 23 Aug 2020 01:47:38 -0300 Subject: [PATCH] Keyboard management and remove ugly errors --- src/Phone.js | 9 +- src/apps/appmarket/hooks/useApps.js | 2 +- src/apps/settings/components/SettingsApp.js | 4 +- src/config/hooks/useConfig.js | 2 - src/index.js | 12 ++- src/os/components/Navigation.js | 22 ++--- src/os/components/NotificationBar.js | 2 +- src/os/hooks/useKeyboard.js | 99 +++++++++++++++++++++ src/ui/components/Button.js | 10 +++ src/ui/components/ContextMenu.js | 2 +- src/ui/components/GridMenu.js | 22 +++-- src/ui/components/List.js | 2 +- src/ui/components/ListItem.js | 2 +- 13 files changed, 156 insertions(+), 34 deletions(-) create mode 100644 src/os/hooks/useKeyboard.js diff --git a/src/Phone.js b/src/Phone.js index 6db6172..254fb14 100644 --- a/src/Phone.js +++ b/src/Phone.js @@ -11,12 +11,15 @@ import MessageIcon from "@material-ui/icons/Email"; import { HomeApp } from "./apps/home/components/Home"; import { ContactsApp } from "./apps/contacts/components/ContactsApp"; import { SettingsApp } from "./apps/settings/components/SettingsApp"; -import { PhoneApp } from './apps/phone/components/PhoneApp'; +import { PhoneApp } from "./apps/phone/components/PhoneApp"; import { ThemeProvider } from "@material-ui/core"; import { CalculatorApp } from "./apps/calculator/components/CalculatorApp"; +import { useInitKeyboard } from "./os/hooks/useKeyboard"; function Phone() { + useInitKeyboard(); const { settings, currentTheme } = useSettings(); + return (
@@ -36,7 +39,9 @@ function Phone() { }} > }, { icon: }]} + notifications={[ + { key: 'newMessage', icon: }, + ]} />
diff --git a/src/apps/appmarket/hooks/useApps.js b/src/apps/appmarket/hooks/useApps.js index 02e2321..f5bdf10 100644 --- a/src/apps/appmarket/hooks/useApps.js +++ b/src/apps/appmarket/hooks/useApps.js @@ -38,7 +38,7 @@ export const useApps = () => { }, { id: "calculator", - name: t("APPS_SETTINGS"), + name: t("APPS_CALCULATOR"), icon: , backgroundColor: currentTheme().palette.primary.light, color: currentTheme().palette.primary.contrastText, diff --git a/src/apps/settings/components/SettingsApp.js b/src/apps/settings/components/SettingsApp.js index 5adbd4b..ce5e5a4 100644 --- a/src/apps/settings/components/SettingsApp.js +++ b/src/apps/settings/components/SettingsApp.js @@ -1,6 +1,6 @@ import React from "react"; import { AppWrapper } from "../../../ui/components"; -import { List, ListItem, ListItemText, Divider } from "@material-ui/core"; +import { ListItemText, Divider } from "@material-ui/core"; import { AppTitle } from "../../../ui/components/AppTitle"; import { AppContent } from "../../../ui/components/AppContent"; import { useTranslation } from "react-i18next"; @@ -11,6 +11,8 @@ import { import { useConfig } from "../../../config/hooks/useConfig"; import { useSettings } from "../hooks/useSettings"; import { useApps } from "../../appmarket/hooks/useApps"; +import { List } from "../../../ui/components/List"; +import { ListItem } from "../../../ui/components/ListItem"; export const SettingItem = ({ options, label, value, onClick }) => { return ( diff --git a/src/config/hooks/useConfig.js b/src/config/hooks/useConfig.js index 68f41c4..70ed6b1 100644 --- a/src/config/hooks/useConfig.js +++ b/src/config/hooks/useConfig.js @@ -6,8 +6,6 @@ const configState = atom({ default: Default }) -console.log(Default) - export const useConfig = (state = configState) => { const [config, _setConfig] = useRecoilState(state) diff --git a/src/index.js b/src/index.js index f84a406..c8b4f48 100644 --- a/src/index.js +++ b/src/index.js @@ -7,13 +7,11 @@ import { BrowserRouter } from "react-router-dom"; import { RecoilRoot } from "recoil"; ReactDOM.render( - - - - - - - , + + + + + , document.getElementById("root") ); diff --git a/src/os/components/Navigation.js b/src/os/components/Navigation.js index 1eb61ad..77c7dcc 100644 --- a/src/os/components/Navigation.js +++ b/src/os/components/Navigation.js @@ -2,7 +2,7 @@ import React from "react"; import { makeStyles, BottomNavigation, - BottomNavigationAction + BottomNavigationAction, } from "@material-ui/core"; import KeyboardArrowLeftIcon from "@material-ui/icons/KeyboardArrowLeft"; import AppsIcon from "@material-ui/icons/Apps"; @@ -24,21 +24,21 @@ export const Navigation = () => { className={classes.root} onChange={(_e, value) => value()} > + history.push("/phone")} + icon={} + /> + history.push("/")} + icon={} + /> history.goBack()} icon={} /> - history.push('/')} - icon={} - /> - history.push('/phone')} - icon={} - /> ); }; diff --git a/src/os/components/NotificationBar.js b/src/os/components/NotificationBar.js index 88bb2df..8bd028d 100644 --- a/src/os/components/NotificationBar.js +++ b/src/os/components/NotificationBar.js @@ -36,7 +36,7 @@ export const NotificationBar = ({ notifications = [] }) => { > {notifications.map((notification) => ( - + {notification.icon} ))} diff --git a/src/os/hooks/useKeyboard.js b/src/os/hooks/useKeyboard.js new file mode 100644 index 0000000..1fe5c85 --- /dev/null +++ b/src/os/hooks/useKeyboard.js @@ -0,0 +1,99 @@ +import { useEffect } from "react"; +import { atom, useSetRecoilState, useRecoilValue } from "recoil"; + +const keyboardState = { + ArrowRight: atom({ + key: "ArrowRight", + default: null, + }), + ArrowLeft: atom({ + key: "ArrowLeft", + default: null, + }), + ArrowUp: atom({ + key: "ArrowUp", + default: null, + }), + ArrowDown: atom({ + key: "ArrowDown", + default: null, + }), + Backspace: atom({ + key: "Backspace", + default: null, + }), + Enter: atom({ + key: "Enter", + default: null, + }), + Escape: atom({ + key: "Escape", + default: null, + }), +}; + +const validKeys = [ + "ArrowRight", + "ArrowLeft", + "ArrowUp", + "ArrowDown", + "Backspace", + "Enter", + "Escape", +]; + +const isKeyValid = (key) => validKeys.indexOf(key) !== -1; + +export const useInitKeyboard = () => { + const getters = { + ArrowRight: useRecoilValue(keyboardState.ArrowRight), + ArrowLeft: useRecoilValue(keyboardState.ArrowLeft), + ArrowUp: useRecoilValue(keyboardState.ArrowUp), + ArrowDown: useRecoilValue(keyboardState.ArrowDown), + Backspace: useRecoilValue(keyboardState.Backspace), + Enter: useRecoilValue(keyboardState.Enter), + Escape: useRecoilValue(keyboardState.Escape), + }; + + const setEscape = useSetRecoilState(keyboardState.Escape); + + useEffect( + function handleNUIKeyboardMessage() { + function onKeyUp(event) { + const { key } = event; + const callback = getters[key]; + if ( + isKeyValid(key) && + callback && + callback.handler && + callback.handler.call + ) { + return callback.handler(); + } + } + + window.addEventListener("keyup", onKeyUp); + return () => window.removeEventListener("keyup", onKeyUp); + }, + [getters] + ); + + useEffect(function registerEscapeHandler() { + setEscape({ handler: () => console.log("close phone") }); + }, [setEscape]); +}; + +export const useKeyboard = () => { + const setters = { + ArrowRight: useSetRecoilState(keyboardState.ArrowRight), + ArrowLeft: useSetRecoilState(keyboardState.ArrowLeft), + ArrowUp: useSetRecoilState(keyboardState.ArrowUp), + ArrowDown: useSetRecoilState(keyboardState.ArrowDown), + Backspace: useSetRecoilState(keyboardState.Backspace), + Enter: useSetRecoilState(keyboardState.Enter), + }; + + return (key, handler) => { + setters[key]({ handler }); + }; +}; diff --git a/src/ui/components/Button.js b/src/ui/components/Button.js index e69de29..707ecbe 100644 --- a/src/ui/components/Button.js +++ b/src/ui/components/Button.js @@ -0,0 +1,10 @@ +import React from "react"; +import MaterialButton from "@material-ui/core/Button"; + +export const Button = ({ ...props }) => { + return ( + + {props.children} + + ); +}; diff --git a/src/ui/components/ContextMenu.js b/src/ui/components/ContextMenu.js index a131371..5b08fdc 100644 --- a/src/ui/components/ContextMenu.js +++ b/src/ui/components/ContextMenu.js @@ -23,7 +23,7 @@ export const ContextMenu = ({ open, onClose, options }) => { return ( - + {options.map((option) => ( ({ +const useStyles = makeStyles((theme) => ({ root: { - marginTop: theme.spacing(3) + marginTop: theme.spacing(3), }, item: { marginTop: theme.spacing(3), - width: '20%' - } + width: "20%", + }, })); export const GridMenu = ({ items, Component = AppIcon }) => { + const setKey = useKeyboard(); const classes = useStyles(); + + useEffect(function registerKeyHandlers() { + setKey("ArrowLeft", () => { + console.log(":v"); + }); + // eslint-disable-next-line + }, []); + return ( @@ -25,7 +35,7 @@ export const GridMenu = ({ items, Component = AppIcon }) => { item className={classes.item} key={item.name} - tabindex="-1" + tabIndex="-1" component={Link} to={item.path} > diff --git a/src/ui/components/List.js b/src/ui/components/List.js index 27d2f92..3d43122 100644 --- a/src/ui/components/List.js +++ b/src/ui/components/List.js @@ -3,7 +3,7 @@ import MaterialList from "@material-ui/core/List"; export const List = ({ ...props }) => { return ( - + {props.children} ); diff --git a/src/ui/components/ListItem.js b/src/ui/components/ListItem.js index 2f9d5be..95dfbdc 100644 --- a/src/ui/components/ListItem.js +++ b/src/ui/components/ListItem.js @@ -3,7 +3,7 @@ import MaterialListItem from "@material-ui/core/ListItem"; export const ListItem = ({ ...props }) => { return ( - + {props.children} );