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 { 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 (
<ThemeProvider theme={currentTheme()}>
<div className="PhoneWrapper">
@@ -36,7 +39,9 @@ function Phone() {
}}
>
<NotificationBar
notifications={[{ icon: <NotificationIcon Icon={MessageIcon} /> }, { icon: <NotificationIcon Icon={MessageIcon} /> }]}
notifications={[
{ key: 'newMessage', icon: <NotificationIcon Icon={MessageIcon} /> },
]}
/>
<div className="PhoneAppContainer">
<Route exact path="/" component={HomeApp} />
+1 -1
View File
@@ -38,7 +38,7 @@ export const useApps = () => {
},
{
id: "calculator",
name: t("APPS_SETTINGS"),
name: t("APPS_CALCULATOR"),
icon: <CalculatorIcon />,
backgroundColor: currentTheme().palette.primary.light,
color: currentTheme().palette.primary.contrastText,
+3 -1
View File
@@ -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 (
-2
View File
@@ -6,8 +6,6 @@ const configState = atom({
default: Default
})
console.log(Default)
export const useConfig = (state = configState) => {
const [config, _setConfig] = useRecoilState(state)
+5 -7
View File
@@ -7,13 +7,11 @@ import { BrowserRouter } from "react-router-dom";
import { RecoilRoot } from "recoil";
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<RecoilRoot>
<Phone />
</RecoilRoot>
</BrowserRouter>
</React.StrictMode>,
<BrowserRouter>
<RecoilRoot>
<Phone />
</RecoilRoot>
</BrowserRouter>,
document.getElementById("root")
);
+11 -11
View File
@@ -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()}
>
<BottomNavigationAction
label="Phone"
value={() => history.push("/phone")}
icon={<PhoneIcon className={classes.icon} />}
/>
<BottomNavigationAction
label="Home"
value={() => history.push("/")}
icon={<AppsIcon className={classes.icon} />}
/>
<BottomNavigationAction
label="Back"
value={() => history.goBack()}
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>
);
};
+1 -1
View File
@@ -36,7 +36,7 @@ export const NotificationBar = ({ notifications = [] }) => {
>
<Grid container item wrap="nowrap">
{notifications.map((notification) => (
<Grid item component={IconButton} className={classes.icon}>
<Grid item key={notification.key} component={IconButton} className={classes.icon}>
{notification.icon}
</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 (
<Slide direction="up" in={open} mountOnEnter unmountOnExit>
<Paper square className={classes.root}>
<List aria-label="context menu">
<List>
{options.map((option) => (
<ListItem
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 { Grid, makeStyles } from "@material-ui/core";
import { Link } from "react-router-dom";
import { useKeyboard } from "../../os/hooks/useKeyboard";
const useStyles = makeStyles(theme => ({
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 (
<Grid container justify="center">
<Grid container item>
@@ -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}
>
+1 -1
View File
@@ -3,7 +3,7 @@ import MaterialList from "@material-ui/core/List";
export const List = ({ ...props }) => {
return (
<MaterialList {...props} aria-label="list">
<MaterialList aria-label="list" {...props}>
{props.children}
</MaterialList>
);
+1 -1
View File
@@ -3,7 +3,7 @@ import MaterialListItem from "@material-ui/core/ListItem";
export const ListItem = ({ ...props }) => {
return (
<MaterialListItem {...props} aria-label="list item">
<MaterialListItem aria-label="list item" {...props}>
{props.children}
</MaterialListItem>
);