aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Vijay A <vraravam@users.noreply.github.com>2022-12-26 13:26:12 +0530
committerLibravatar Vijay A <vraravam@users.noreply.github.com>2022-12-26 13:26:12 +0530
commit5c350b370cbe8430582d25b062c8de19fdec033b (patch)
tree55db1e27efd7159ea7c4dc42c0e115b531bc3b83
parentUpgrade 'nodejs' to '16.19.0' and 'pnpm' to '7.19.0' (diff)
downloadferdium-app-5c350b370cbe8430582d25b062c8de19fdec033b.tar.gz
ferdium-app-5c350b370cbe8430582d25b062c8de19fdec033b.tar.zst
ferdium-app-5c350b370cbe8430582d25b062c8de19fdec033b.zip
Minor refactoring
-rw-r--r--src/index.ts21
-rw-r--r--src/jsUtils.ts15
-rw-r--r--src/webview/badge.ts19
-rw-r--r--src/webview/lib/RecipeWebview.ts3
-rw-r--r--src/webview/recipe.ts15
-rw-r--r--test/jsUtils.test.ts22
6 files changed, 67 insertions, 28 deletions
diff --git a/src/index.ts b/src/index.ts
index 08e81a2a8..bb6d64d1e 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -101,7 +101,7 @@ const retrieveSettingValue = (key: string, defaultValue: boolean | string) =>
101 101
102const liftSingleInstanceLock = retrieveSettingValue( 102const liftSingleInstanceLock = retrieveSettingValue(
103 'liftSingleInstanceLock', 103 'liftSingleInstanceLock',
104 false, 104 DEFAULT_APP_SETTINGS.liftSingleInstanceLock,
105); 105);
106 106
107// Force single window 107// Force single window
@@ -165,7 +165,12 @@ if (
165} 165}
166 166
167// Disable GPU acceleration 167// Disable GPU acceleration
168if (!retrieveSettingValue('enableGPUAcceleration', false)) { 168if (
169 !retrieveSettingValue(
170 'enableGPUAcceleration',
171 DEFAULT_APP_SETTINGS.enableGPUAcceleration,
172 )
173) {
169 debug('Disable GPU Acceleration'); 174 debug('Disable GPU Acceleration');
170 app.disableHardwareAcceleration(); 175 app.disableHardwareAcceleration();
171} 176}
@@ -198,9 +203,15 @@ const createWindow = () => {
198 } 203 }
199 204
200 // Create the browser window. 205 // Create the browser window.
201 const backgroundColor = retrieveSettingValue('darkMode', false) 206 const backgroundColor = retrieveSettingValue(
207 'darkMode',
208 DEFAULT_APP_SETTINGS.darkMode,
209 )
202 ? '#1E1E1E' 210 ? '#1E1E1E'
203 : settings.get('accentColor'); 211 : (retrieveSettingValue(
212 'accentColor',
213 DEFAULT_APP_SETTINGS.accentColor,
214 ) as string);
204 215
205 mainWindow = new BrowserWindow({ 216 mainWindow = new BrowserWindow({
206 x: posX, 217 x: posX,
@@ -217,7 +228,7 @@ const createWindow = () => {
217 spellcheck: retrieveSettingValue( 228 spellcheck: retrieveSettingValue(
218 'enableSpellchecking', 229 'enableSpellchecking',
219 DEFAULT_APP_SETTINGS.enableSpellchecking, 230 DEFAULT_APP_SETTINGS.enableSpellchecking,
220 ) as boolean | undefined, 231 ) as boolean,
221 nodeIntegration: true, 232 nodeIntegration: true,
222 contextIsolation: false, 233 contextIsolation: false,
223 webviewTag: true, 234 webviewTag: true,
diff --git a/src/jsUtils.ts b/src/jsUtils.ts
index b93d6db5b..0cc83bb91 100644
--- a/src/jsUtils.ts
+++ b/src/jsUtils.ts
@@ -9,4 +9,17 @@ export const convertToJSON = (data: string | any | undefined | null) =>
9export const cleanseJSObject = (data: any | undefined | null) => 9export const cleanseJSObject = (data: any | undefined | null) =>
10 JSON.parse(JSON.stringify(data)); 10 JSON.parse(JSON.stringify(data));
11 11
12export const isEscKeyPress = (keyCode: Number) => keyCode === 27; 12export const isEscKeyPress = (keyCode: number) => keyCode === 27;
13
14export const safeParseInt = (text: string | number | undefined | null) => {
15 if (text === undefined || text === null) {
16 return 0;
17 }
18
19 // Parse number to integer
20 // This will correct errors that recipes may introduce, e.g.
21 // by sending a String instead of an integer
22 const parsedNumber = Number.parseInt(text.toString(), 10);
23 const adjustedNumber = Number.isNaN(parsedNumber) ? 0 : parsedNumber;
24 return Math.max(adjustedNumber, 0);
25};
diff --git a/src/webview/badge.ts b/src/webview/badge.ts
index afecd22d4..b33d05255 100644
--- a/src/webview/badge.ts
+++ b/src/webview/badge.ts
@@ -1,29 +1,16 @@
1import { ipcRenderer } from 'electron'; 1import { ipcRenderer } from 'electron';
2import { safeParseInt } from '../jsUtils';
2 3
3const debug = require('../preload-safe-debug')('Ferdium:Plugin:BadgeHandler'); 4const debug = require('../preload-safe-debug')('Ferdium:Plugin:BadgeHandler');
4 5
5export default class BadgeHandler { 6export default class BadgeHandler {
6 // TODO: Need to extract this into a utility class and reuse outside of the recipes
7 safeParseInt(text: string | number | undefined | null) {
8 if (text === undefined || text === null) {
9 return 0;
10 }
11
12 // Parse number to integer
13 // This will correct errors that recipes may introduce, e.g.
14 // by sending a String instead of an integer
15 const parsedNumber = Number.parseInt(text.toString(), 10);
16 const adjustedNumber = Number.isNaN(parsedNumber) ? 0 : parsedNumber;
17 return Math.max(adjustedNumber, 0);
18 }
19
20 setBadge( 7 setBadge(
21 direct: string | number | undefined | null, 8 direct: string | number | undefined | null,
22 indirect: string | number | undefined | null, 9 indirect: string | number | undefined | null,
23 ) { 10 ) {
24 const count = { 11 const count = {
25 direct: this.safeParseInt(direct), 12 direct: safeParseInt(direct),
26 indirect: this.safeParseInt(indirect), 13 indirect: safeParseInt(indirect),
27 }; 14 };
28 15
29 debug('Sending badge count to host: %j', count); 16 debug('Sending badge count to host: %j', count);
diff --git a/src/webview/lib/RecipeWebview.ts b/src/webview/lib/RecipeWebview.ts
index 20be3f866..436525b9e 100644
--- a/src/webview/lib/RecipeWebview.ts
+++ b/src/webview/lib/RecipeWebview.ts
@@ -1,6 +1,7 @@
1import { ipcRenderer } from 'electron'; 1import { ipcRenderer } from 'electron';
2import { BrowserWindow } from '@electron/remote'; 2import { BrowserWindow } from '@electron/remote';
3import { pathExistsSync, readFileSync, existsSync } from 'fs-extra'; 3import { pathExistsSync, readFileSync, existsSync } from 'fs-extra';
4import { safeParseInt } from '../../jsUtils';
4 5
5const debug = require('../../preload-safe-debug')( 6const debug = require('../../preload-safe-debug')(
6 'Ferdium:Plugin:RecipeWebview', 7 'Ferdium:Plugin:RecipeWebview',
@@ -91,7 +92,7 @@ class RecipeWebview {
91 * @param {string | number | undefined | null} text to be parsed 92 * @param {string | number | undefined | null} text to be parsed
92 */ 93 */
93 safeParseInt(text) { 94 safeParseInt(text) {
94 return this.badgeHandler.safeParseInt(text); 95 return safeParseInt(text);
95 } 96 }
96 97
97 /** 98 /**
diff --git a/src/webview/recipe.ts b/src/webview/recipe.ts
index dc3f39401..ed45192d3 100644
--- a/src/webview/recipe.ts
+++ b/src/webview/recipe.ts
@@ -44,7 +44,7 @@ import {
44} from './spellchecker'; 44} from './spellchecker';
45 45
46import { DEFAULT_APP_SETTINGS } from '../config'; 46import { DEFAULT_APP_SETTINGS } from '../config';
47import { ifUndefined } from '../jsUtils'; 47import { ifUndefined, safeParseInt } from '../jsUtils';
48import { AppStore } from '../@types/stores.types'; 48import { AppStore } from '../@types/stores.types';
49import Service from '../models/Service'; 49import Service from '../models/Service';
50 50
@@ -116,10 +116,15 @@ window.open = (url, frameName, features): WindowProxy | null => {
116// then overwrite the corresponding field of the window object by injected JS. 116// then overwrite the corresponding field of the window object by injected JS.
117contextBridge.exposeInMainWorld('ferdium', { 117contextBridge.exposeInMainWorld('ferdium', {
118 open: window.open, 118 open: window.open,
119 setBadge: (direct, indirect) => badgeHandler.setBadge(direct, indirect), 119 setBadge: (
120 safeParseInt: text => badgeHandler.safeParseInt(text), 120 direct: string | number | null | undefined,
121 setDialogTitle: title => dialogTitleHandler.setDialogTitle(title), 121 indirect: string | number | null | undefined,
122 displayNotification: (title, options) => 122 ) => badgeHandler.setBadge(direct, indirect),
123 safeParseInt: (text: string | number | null | undefined) =>
124 safeParseInt(text),
125 setDialogTitle: (title: string | null | undefined) =>
126 dialogTitleHandler.setDialogTitle(title),
127 displayNotification: (title: string, options: any) =>
123 notificationsHandler.displayNotification(title, options), 128 notificationsHandler.displayNotification(title, options),
124 getDisplayMediaSelector, 129 getDisplayMediaSelector,
125}); 130});
diff --git a/test/jsUtils.test.ts b/test/jsUtils.test.ts
index a8de4475c..7245d9a68 100644
--- a/test/jsUtils.test.ts
+++ b/test/jsUtils.test.ts
@@ -105,4 +105,26 @@ describe('jsUtils', () => {
105 expect(result).toEqual(false); 105 expect(result).toEqual(false);
106 }); 106 });
107 }); 107 });
108
109 describe('safeParseInt', () => {
110 it('returns zero for undefined', () => {
111 expect(jsUtils.safeParseInt(undefined)).toEqual(0);
112 });
113
114 it('returns zero for null', () => {
115 expect(jsUtils.safeParseInt(null)).toEqual(0);
116 });
117
118 it('parses integer number correctly in beginning of string input', () => {
119 expect(jsUtils.safeParseInt('47.45G')).toEqual(47);
120 });
121
122 it('returns 0 for string input whose starting characters are non-numeric', () => {
123 expect(jsUtils.safeParseInt('G47.45')).toEqual(0);
124 });
125
126 it('parses integer number correctly', () => {
127 expect(jsUtils.safeParseInt(47.45)).toEqual(47);
128 });
129 });
108}); 130});