Skip to content

Commit 5a255d7

Browse files
committed
feat: add font size setting
1 parent 3185484 commit 5a255d7

35 files changed

+376
-66
lines changed

public/app/config/channels.js

+2
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,6 @@ module.exports = {
7777
GET_APP_UPGRADE_CHANNEL: 'app:upgrade:get',
7878
INSTALL_APP_UPGRADE_CHANNEL: 'app:upgrade:install',
7979
GET_TOURS_ENABLED_CHANNEL: 'tour:enabled:get',
80+
SET_FONT_SIZE_CHANNEL: 'user:font-size:set',
81+
GET_FONT_SIZE_CHANNEL: 'user:font-size:get',
8082
};

public/app/config/config.js

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const AUTHENTICATED = 'authenticated';
6161
const DEFAULT_AUTHENTICATION = false;
6262
const DEFAULT_ACTION_ACCESSIBILITY = false;
6363
const DEFAULT_ACTIONS_AS_ENABLED = true;
64+
const DEFAULT_FONT_SIZE = 14;
6465

6566
const buildDefaultUser = (lang = DEFAULT_LANG) => ({
6667
geolocation: null,
@@ -79,6 +80,7 @@ const buildDefaultUser = (lang = DEFAULT_LANG) => ({
7980
[tours.VISIT_SPACE_TOUR]: false,
8081
[tours.SETTINGS_TOUR]: false,
8182
},
83+
fontSize: DEFAULT_FONT_SIZE,
8284
});
8385

8486
const ANONYMOUS_USERNAME = 'Anonymous';

public/app/config/messages.js

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ const ERROR_SETTING_ACTION_ACCESSIBILITY =
104104
const ERROR_SETTING_ACTIONS_AS_ENABLED = 'There was an error enabling actions';
105105
const ERROR_POSTING_FILE_MESSAGE = 'There was an error uploading the file';
106106
const ERROR_DELETING_FILE_MESSAGE = 'There was an error deleting the file';
107+
const ERROR_SETTING_FONT_SIZE = 'There was an error setting the font size';
108+
const ERROR_GETTING_FONT_SIZE = 'There was an error getting the font size';
107109

108110
module.exports = {
109111
ERROR_GETTING_DEVELOPER_MODE,
@@ -176,4 +178,6 @@ module.exports = {
176178
ERROR_SETTING_ACTIONS_AS_ENABLED,
177179
ERROR_POSTING_FILE_MESSAGE,
178180
ERROR_DELETING_FILE_MESSAGE,
181+
ERROR_GETTING_FONT_SIZE,
182+
ERROR_SETTING_FONT_SIZE,
179183
};

public/app/listeners/getFontSize.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { GET_FONT_SIZE_CHANNEL } = require('../config/channels');
2+
const { ERROR_GENERAL } = require('../config/errors');
3+
const logger = require('../logger');
4+
5+
const getFontSize = (mainWindow, db) => async () => {
6+
try {
7+
const fontSize = db.get('user.settings.fontSize').value();
8+
mainWindow.webContents.send(GET_FONT_SIZE_CHANNEL, fontSize);
9+
} catch (e) {
10+
logger.error(e);
11+
mainWindow.webContents.send(GET_FONT_SIZE_CHANNEL, ERROR_GENERAL);
12+
}
13+
};
14+
15+
module.exports = getFontSize;

public/app/listeners/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ const deleteFile = require('./deleteFile');
5757
const installAppUpgrade = require('./installAppUpgrade');
5858
const getAppUpgrade = require('./getAppUpgrade');
5959
const getToursEnabled = require('./getToursEnabled');
60+
const setFontSize = require('./setFontSize');
61+
const getFontSize = require('./getFontSize');
6062

6163
module.exports = {
6264
loadSpace,
@@ -117,4 +119,6 @@ module.exports = {
117119
installAppUpgrade,
118120
getAppUpgrade,
119121
getToursEnabled,
122+
setFontSize,
123+
getFontSize,
120124
};

public/app/listeners/setFontSize.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { SET_FONT_SIZE_CHANNEL } = require('../config/channels');
2+
const { ERROR_GENERAL } = require('../config/errors');
3+
const logger = require('../logger');
4+
5+
const setFontSize = (mainWindow, db) => async (event, fontSize) => {
6+
try {
7+
db.set('user.settings.fontSize', fontSize).write();
8+
mainWindow.webContents.send(SET_FONT_SIZE_CHANNEL, fontSize);
9+
} catch (e) {
10+
logger.error(e);
11+
mainWindow.webContents.send(SET_FONT_SIZE_CHANNEL, ERROR_GENERAL);
12+
}
13+
};
14+
15+
module.exports = setFontSize;

public/electron.js

+10
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ const {
8585
GET_APP_UPGRADE_CHANNEL,
8686
INSTALL_APP_UPGRADE_CHANNEL,
8787
GET_TOURS_ENABLED_CHANNEL,
88+
SET_FONT_SIZE_CHANNEL,
89+
GET_FONT_SIZE_CHANNEL,
8890
} = require('./app/config/channels');
8991
const env = require('./env.json');
9092
const {
@@ -145,6 +147,8 @@ const {
145147
installAppUpgrade,
146148
getAppUpgrade,
147149
getToursEnabled,
150+
setFontSize,
151+
getFontSize,
148152
} = require('./app/listeners');
149153
const isMac = require('./app/utils/isMac');
150154

@@ -493,6 +497,12 @@ app.on('ready', async () => {
493497
setActionsAsEnabled(mainWindow, db)
494498
);
495499

500+
// called when getting the font size
501+
ipcMain.on(GET_FONT_SIZE_CHANNEL, getFontSize(mainWindow, db));
502+
503+
// called when setting the font size
504+
ipcMain.on(SET_FONT_SIZE_CHANNEL, setFontSize(mainWindow, db));
505+
496506
// called when getting student mode
497507
ipcMain.on(GET_USER_MODE_CHANNEL, getUserMode(mainWindow, db));
498508

src/actions/user.js

+60
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ import {
3333
SET_ACTION_ACCESSIBILITY_SUCCEEDED,
3434
FLAG_SETTING_ACTIONS_AS_ENABLED,
3535
SET_ACTIONS_AS_ENABLED_SUCCEEDED,
36+
FLAG_SETTING_FONT_SIZE,
37+
SET_FONT_SIZE_SUCCEEDED,
38+
GET_FONT_SIZE_SUCCEEDED,
39+
FLAG_GETTING_FONT_SIZE,
3640
} from '../types';
3741
import {
3842
ERROR_GETTING_GEOLOCATION,
@@ -52,6 +56,8 @@ import {
5256
ERROR_SETTING_SPACE_AS_RECENT,
5357
ERROR_SETTING_ACTION_ACCESSIBILITY,
5458
ERROR_SETTING_ACTIONS_AS_ENABLED,
59+
ERROR_SETTING_FONT_SIZE,
60+
ERROR_GETTING_FONT_SIZE,
5561
} from '../config/messages';
5662
import {
5763
GET_USER_FOLDER_CHANNEL,
@@ -69,6 +75,8 @@ import {
6975
SET_SPACE_AS_RECENT_CHANNEL,
7076
SET_ACTION_ACCESSIBILITY_CHANNEL,
7177
SET_ACTIONS_AS_ENABLED_CHANNEL,
78+
SET_FONT_SIZE_CHANNEL,
79+
GET_FONT_SIZE_CHANNEL,
7280
} from '../config/channels';
7381
import { createFlag } from './common';
7482
import { ERROR_GENERAL } from '../config/errors';
@@ -94,6 +102,8 @@ const flagSettingActionAccessibility = createFlag(
94102
FLAG_SETTING_ACTION_ACCESSIBILITY
95103
);
96104
const flagSettingActionsAsEnabled = createFlag(FLAG_SETTING_ACTIONS_AS_ENABLED);
105+
const flagSettingFontSize = createFlag(FLAG_SETTING_FONT_SIZE);
106+
const flagGettingFontSize = createFlag(FLAG_GETTING_FONT_SIZE);
97107

98108
const getGeolocation = async () => async (dispatch) => {
99109
// only fetch location if online
@@ -535,6 +545,54 @@ const setActionsAsEnabled = (payload) => (dispatch) => {
535545
}
536546
};
537547

548+
const setFontSize = (payload) => (dispatch) => {
549+
try {
550+
dispatch(flagSettingFontSize(true));
551+
window.ipcRenderer.send(SET_FONT_SIZE_CHANNEL, payload);
552+
window.ipcRenderer.once(SET_FONT_SIZE_CHANNEL, (event, response) => {
553+
if (response === ERROR_GENERAL) {
554+
toastr.error(
555+
i18n.t(ERROR_MESSAGE_HEADER),
556+
i18n.t(ERROR_SETTING_FONT_SIZE)
557+
);
558+
} else {
559+
dispatch({
560+
type: SET_FONT_SIZE_SUCCEEDED,
561+
payload,
562+
});
563+
}
564+
dispatch(flagSettingFontSize(false));
565+
});
566+
} catch (e) {
567+
console.error(e);
568+
toastr.error(i18n.t(ERROR_MESSAGE_HEADER), i18n.t(ERROR_SETTING_FONT_SIZE));
569+
}
570+
};
571+
572+
const getFontSize = () => (dispatch) => {
573+
try {
574+
dispatch(flagGettingFontSize(true));
575+
window.ipcRenderer.send(GET_FONT_SIZE_CHANNEL);
576+
window.ipcRenderer.once(GET_FONT_SIZE_CHANNEL, (event, response) => {
577+
if (response === ERROR_GENERAL) {
578+
toastr.error(
579+
i18n.t(ERROR_MESSAGE_HEADER),
580+
i18n.t(ERROR_GETTING_FONT_SIZE)
581+
);
582+
} else {
583+
dispatch({
584+
type: GET_FONT_SIZE_SUCCEEDED,
585+
payload: response,
586+
});
587+
}
588+
dispatch(flagGettingFontSize(false));
589+
});
590+
} catch (e) {
591+
console.error(e);
592+
toastr.error(i18n.t(ERROR_MESSAGE_HEADER), i18n.t(ERROR_GETTING_FONT_SIZE));
593+
}
594+
};
595+
538596
export {
539597
getUserFolder,
540598
getGeolocation,
@@ -552,4 +610,6 @@ export {
552610
setSpaceAsRecent,
553611
setActionAccessibility,
554612
setActionsAsEnabled,
613+
setFontSize,
614+
getFontSize,
555615
};

src/components/Settings.js

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
DEFAULT_USER_MODE,
2727
AUTHENTICATED,
2828
} from '../config/constants';
29+
import FontSizeSlider from './common/FontSizeSlider';
2930

3031
const styles = (theme) => ({
3132
...Styles(theme),
@@ -74,6 +75,18 @@ export class Settings extends Component {
7475
);
7576
};
7677

78+
renderSpaceSettings = () => {
79+
const { t } = this.props;
80+
return (
81+
<>
82+
<Typography variant="h5" color="inherit" className="mt-2">
83+
{t('Spaces')}
84+
</Typography>
85+
<FontSizeSlider />
86+
</>
87+
);
88+
};
89+
7790
render() {
7891
const { classes, t, authenticated } = this.props;
7992

@@ -96,6 +109,10 @@ export class Settings extends Component {
96109
</FormGroup>
97110
<Divider variant="middle" classes={{ root: classes.divider }} />
98111

112+
{this.renderSpaceSettings()}
113+
114+
<Divider variant="middle" classes={{ root: classes.divider }} />
115+
99116
{authenticated && this.renderActionSettings()}
100117
</div>
101118
</Main>

src/components/__snapshots__/Settings.test.js.snap

+16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ exports[`<Settings /> renders correctly 1`] = `
2929
}
3030
variant="middle"
3131
/>
32+
<WithStyles(ForwardRef(Typography))
33+
className="mt-2"
34+
color="inherit"
35+
variant="h5"
36+
>
37+
Spaces
38+
</WithStyles(ForwardRef(Typography))>
39+
<Connect(FontSizeSlider) />
40+
<WithStyles(ForwardRef(Divider))
41+
classes={
42+
Object {
43+
"root": "",
44+
}
45+
}
46+
variant="middle"
47+
/>
3248
<WithStyles(ForwardRef(Typography))
3349
className="mt-2"
3450
color="inherit"
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { makeStyles } from '@material-ui/core/styles';
4+
import Grid from '@material-ui/core/Grid';
5+
import { useTranslation } from 'react-i18next';
6+
import Typography from '@material-ui/core/Typography';
7+
import Slider from '@material-ui/core/Slider';
8+
import { connect } from 'react-redux';
9+
import Input from '@material-ui/core/Input';
10+
import FormatSizeIcon from '@material-ui/icons/FormatSize';
11+
import { setFontSize } from '../../actions';
12+
import {
13+
FONT_SIZE_MAX_VALUE,
14+
DEFAULT_FONT_SIZE,
15+
FONT_SIZE_MIN_VALUE,
16+
} from '../../config/constants';
17+
18+
const useStyles = makeStyles({
19+
root: {
20+
width: 250,
21+
},
22+
input: {
23+
width: 42,
24+
},
25+
});
26+
27+
const FontSizeSlider = ({ fontSize, dispatchSetFontSize }) => {
28+
const classes = useStyles();
29+
const { t } = useTranslation();
30+
31+
const handleSliderChange = (event, newValue) => {
32+
dispatchSetFontSize(newValue);
33+
};
34+
35+
const handleInputChange = (event) => {
36+
dispatchSetFontSize(
37+
event.target.value === '' ? '' : Number(event.target.value)
38+
);
39+
};
40+
41+
const handleBlur = () => {
42+
if (fontSize < FONT_SIZE_MIN_VALUE) {
43+
dispatchSetFontSize(FONT_SIZE_MIN_VALUE);
44+
} else if (fontSize > FONT_SIZE_MAX_VALUE) {
45+
dispatchSetFontSize(FONT_SIZE_MAX_VALUE);
46+
}
47+
};
48+
49+
return (
50+
<div className={classes.root}>
51+
<Typography id="font-size" gutterBottom>
52+
{t('Font Size')}
53+
</Typography>
54+
<Grid container spacing={2} alignItems="center">
55+
<Grid item>
56+
<FormatSizeIcon />
57+
</Grid>
58+
<Grid item xs>
59+
<Slider
60+
value={typeof fontSize === 'number' ? fontSize : DEFAULT_FONT_SIZE}
61+
onChange={handleSliderChange}
62+
aria-labelledby="font-size"
63+
min={FONT_SIZE_MIN_VALUE}
64+
max={FONT_SIZE_MAX_VALUE}
65+
/>
66+
</Grid>
67+
<Grid item>
68+
<Input
69+
className={classes.input}
70+
value={fontSize}
71+
margin="dense"
72+
onChange={handleInputChange}
73+
onBlur={handleBlur}
74+
inputProps={{
75+
step: 1,
76+
min: FONT_SIZE_MIN_VALUE,
77+
max: FONT_SIZE_MAX_VALUE,
78+
type: 'number',
79+
'aria-labelledby': 'font-size',
80+
}}
81+
/>
82+
</Grid>
83+
</Grid>
84+
</div>
85+
);
86+
};
87+
88+
FontSizeSlider.propTypes = {
89+
dispatchSetFontSize: PropTypes.func.isRequired,
90+
fontSize: PropTypes.string.isRequired,
91+
};
92+
93+
const mapStateToProps = ({ authentication }) => ({
94+
fontSize:
95+
authentication.getIn(['user', 'settings', 'fontSize']) || DEFAULT_FONT_SIZE,
96+
});
97+
98+
const mapDispatchToProps = {
99+
dispatchSetFontSize: setFontSize,
100+
};
101+
102+
const ConnectedComponent = connect(
103+
mapStateToProps,
104+
mapDispatchToProps
105+
)(FontSizeSlider);
106+
107+
export default ConnectedComponent;

0 commit comments

Comments
 (0)