Keyboard management and remove ugly errors

This commit is contained in:
J Francisco Rader committed 2020-08-23 01:47:38 -03:00
1 parent eb570f21b3
commit dd9b3bddff
13 files changed
+156 -34

No files matched your search

+7 -2
View File
@@ -11,12 +11,15 @@ import MessageIcon from "@material-ui/icons/Email";
import { HomeApp } from "./apps/home/components/Home"; import { HomeApp } from "./apps/home/components/Home";
import { ContactsApp } from "./apps/contacts/components/ContactsApp"; import { ContactsApp } from "./apps/contacts/components/ContactsApp";
import { SettingsApp } from "./apps/settings/components/SettingsApp"; 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 { ThemeProvider } from "@material-ui/core";
import { CalculatorApp } from "./apps/calculator/components/CalculatorApp"; import { CalculatorApp } from "./apps/calculator/components/CalculatorApp";
import { useInitKeyboard } from "./os/hooks/useKeyboard";
function Phone() { function Phone() {
useInitKeyboard();
const { settings, currentTheme } = useSettings(); const { settings, currentTheme } = useSettings();
return ( return (
<ThemeProvider theme={currentTheme()}> <ThemeProvider theme={currentTheme()}>
<div className="PhoneWrapper"> <div className="PhoneWrapper">
@@ -36,7 +39,9 @@ function Phone() {
}} }}
> >
<NotificationBar <NotificationBar
notifications={[{ icon: <NotificationIcon Icon={MessageIcon} /> }, { icon: <NotificationIcon Icon={MessageIcon} /> }]} notifications={[
{ key: 'newMessage', icon: <NotificationIcon Icon={MessageIcon} /> },
]}
/> />
<div className="PhoneAppContainer"> <div className="PhoneAppContainer">
<Route exact path="/" component={HomeApp} /> <Route exact path="/" component={HomeApp} />
+1 -1
View File
@@ -38,7 +38,7 @@ export const useApps = () => {
}, },
{ {
id: "calculator", id: "calculator",
name: t("APPS_SETTINGS"), name: t("APPS_CALCULATOR"),
icon: <CalculatorIcon />, icon: <CalculatorIcon />,
backgroundColor: currentTheme().palette.primary.light, backgroundColor: currentTheme().palette.primary.light,
color: currentTheme().palette.primary.contrastText, color: currentTheme().palette.primary.contrastText,
+3 -1
View File
@@ -1,6 +1,6 @@
import React from "react"; import React from "react";
import { AppWrapper } from "../../../ui/components"; 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 { AppTitle } from "../../../ui/components/AppTitle";
import { AppContent } from "../../../ui/components/AppContent"; import { AppContent } from "../../../ui/components/AppContent";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
@@ -11,6 +11,8 @@ import {
import { useConfig } from "../../../config/hooks/useConfig"; import { useConfig } from "../../../config/hooks/useConfig";
import { useSettings } from "../hooks/useSettings"; import { useSettings } from "../hooks/useSettings";
import { useApps } from "../../appmarket/hooks/useApps"; 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 }) => { export const SettingItem = ({ options, label, value, onClick }) => {
return ( return (
-2
View File
@@ -6,8 +6,6 @@ const configState = atom({
default: Default default: Default
}) })
console.log(Default)
export const useConfig = (state = configState) => { export const useConfig = (state = configState) => {
const [config, _setConfig] = useRecoilState(state) const [config, _setConfig] = useRecoilState(state)
+5 -7
View File
@@ -7,13 +7,11 @@ import { BrowserRouter } from "react-router-dom";
import { RecoilRoot } from "recoil"; import { RecoilRoot } from "recoil";
ReactDOM.render( ReactDOM.render(
<React.StrictMode> <BrowserRouter>
<BrowserRouter> <RecoilRoot>
<RecoilRoot> <Phone />
<Phone /> </RecoilRoot>
</RecoilRoot> </BrowserRouter>,
</BrowserRouter>
</React.StrictMode>,
document.getElementById("root") document.getElementById("root")
); );
+11 -11
View File
@@ -2,7 +2,7 @@ import React from "react";
import { import {
makeStyles, makeStyles,
BottomNavigation, BottomNavigation,
BottomNavigationAction BottomNavigationAction,
} from "@material-ui/core"; } from "@material-ui/core";
import KeyboardArrowLeftIcon from "@material-ui/icons/KeyboardArrowLeft"; import KeyboardArrowLeftIcon from "@material-ui/icons/KeyboardArrowLeft";
import AppsIcon from "@material-ui/icons/Apps"; import AppsIcon from "@material-ui/icons/Apps";
@@ -24,21 +24,21 @@ export const Navigation = () => {
className={classes.root} className={classes.root}
onChange={(_e, value) => value()} onChange={(_e, value) => value()}
> >
<BottomNavigationAction
label="Phone"
value={() => history.push("/phone")}
icon={<PhoneIcon className={classes.icon} />}
/>
<BottomNavigationAction
label="Home"
value={() => history.push("/")}
icon={<AppsIcon className={classes.icon} />}
/>
<BottomNavigationAction <BottomNavigationAction
label="Back" label="Back"
value={() => history.goBack()} value={() => history.goBack()}
icon={<KeyboardArrowLeftIcon />} icon={<KeyboardArrowLeftIcon />}
/> />
<BottomNavigationAction
label="Home"
value={() => history.push('/')}
icon={<AppsIcon className={classes.icon} />}
/>
<BottomNavigationAction
label="Phone"
value={() => history.push('/phone')}
icon={<PhoneIcon className={classes.icon} />}
/>
</BottomNavigation> </BottomNavigation>
); );
}; };
+1 -1
View File
@@ -36,7 +36,7 @@ export const NotificationBar = ({ notifications = [] }) => {
> >
<Grid container item wrap="nowrap"> <Grid container item wrap="nowrap">
{notifications.map((notification) => ( {notifications.map((notification) => (
<Grid item component={IconButton} className={classes.icon}> <Grid item key={notification.key} component={IconButton} className={classes.icon}>
{notification.icon} {notification.icon}
</Grid> </Grid>
))} ))}
+99
View File
@@ -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 });
};
};
+10
View File
@@ -0,0 +1,10 @@
import React from "react";
import MaterialButton from "@material-ui/core/Button";
export const Button = ({ ...props }) => {
return (
<MaterialButton aria-label="button" {...props}>
{props.children}
</MaterialButton>
);
};
+1 -1
View File
@@ -23,7 +23,7 @@ export const ContextMenu = ({ open, onClose, options }) => {
return ( return (
<Slide direction="up" in={open} mountOnEnter unmountOnExit> <Slide direction="up" in={open} mountOnEnter unmountOnExit>
<Paper square className={classes.root}> <Paper square className={classes.root}>
<List aria-label="context menu"> <List>
{options.map((option) => ( {options.map((option) => (
<ListItem <ListItem
selected={option.selected} selected={option.selected}
+16 -6
View File
@@ -1,20 +1,30 @@
import React from "react"; import React, { useEffect } from "react";
import { AppIcon } from "./AppIcon"; import { AppIcon } from "./AppIcon";
import { Grid, makeStyles } from "@material-ui/core"; import { Grid, makeStyles } from "@material-ui/core";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import { useKeyboard } from "../../os/hooks/useKeyboard";
const useStyles = makeStyles(theme => ({ const useStyles = makeStyles((theme) => ({
root: { root: {
marginTop: theme.spacing(3) marginTop: theme.spacing(3),
}, },
item: { item: {
marginTop: theme.spacing(3), marginTop: theme.spacing(3),
width: '20%' width: "20%",
} },
})); }));
export const GridMenu = ({ items, Component = AppIcon }) => { export const GridMenu = ({ items, Component = AppIcon }) => {
const setKey = useKeyboard();
const classes = useStyles(); const classes = useStyles();
useEffect(function registerKeyHandlers() {
setKey("ArrowLeft", () => {
console.log(":v");
});
// eslint-disable-next-line
}, []);
return ( return (
<Grid container justify="center"> <Grid container justify="center">
<Grid container item> <Grid container item>
@@ -25,7 +35,7 @@ export const GridMenu = ({ items, Component = AppIcon }) => {
item item
className={classes.item} className={classes.item}
key={item.name} key={item.name}
tabindex="-1" tabIndex="-1"
component={Link} component={Link}
to={item.path} to={item.path}
> >
+1 -1
View File
@@ -3,7 +3,7 @@ import MaterialList from "@material-ui/core/List";
export const List = ({ ...props }) => { export const List = ({ ...props }) => {
return ( return (
<MaterialList {...props} aria-label="list"> <MaterialList aria-label="list" {...props}>
{props.children} {props.children}
</MaterialList> </MaterialList>
); );
+1 -1
View File
@@ -3,7 +3,7 @@ import MaterialListItem from "@material-ui/core/ListItem";
export const ListItem = ({ ...props }) => { export const ListItem = ({ ...props }) => {
return ( return (
<MaterialListItem {...props} aria-label="list item"> <MaterialListItem aria-label="list item" {...props}>
{props.children} {props.children}
</MaterialListItem> </MaterialListItem>
); );