refactor: preference calls

This commit is contained in:
tiddlygit-test 2021-01-31 23:09:45 +08:00
parent 52e45e4239
commit be1ed47b13
11 changed files with 127 additions and 129 deletions

View file

@ -366,7 +366,7 @@ const Preferences = ({
});
const debouncedRequestShowRequireRestartDialog = useCallback(
debounce(() => requestShowRequireRestartDialog(), 2500),
debounce(async () => await window.service.window.requestShowRequireRestartDialog(), 2500),
[],
);
@ -420,8 +420,8 @@ const Preferences = ({
<TextField
helperText={t('Preference.UserNameDetail')}
fullWidth
onChange={(event) => {
void window.service.preference.set('userName', event.target.value);
onChange={async (event) => {
await window.service.preference.set('userName', event.target.value);
}}
label={t('Preference.UserName')}
value={userName}
@ -455,14 +455,15 @@ const Preferences = ({
inputFormat="HH:mm:ss"
renderInput={(timeProps) => <TextField {...timeProps} />}
value={fromUnixTime(syncDebounceInterval / 1000 + new Date().getTimezoneOffset() * 60)}
onChange={(date) => {
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type 'Date | null' is not assignable t... Remove this comment to see the full error message
onChange={async (date) => {
if (date === null) throw new Error(`date is null`);
const timeWithoutDate = setDate(setMonth(setYear(date, 1970), 0), 1);
const utcTime = (timeWithoutDate.getTime() / 1000 - new Date().getTimezoneOffset() * 60) * 1000;
void window.service.preference.set('syncDebounceInterval', utcTime);
debouncedRequestShowRequireRestartDialog();
await window.service.preference.set('syncDebounceInterval', utcTime);
await debouncedRequestShowRequireRestartDialog();
}}
onClose={() => {
// FIXME: no global assign
window.preventClosingWindow = false;
}}
onOpen={() => {
@ -488,13 +489,13 @@ const Preferences = ({
<ChevronRightIcon color="action" />
</ListItem>
}>
<MenuItem dense onClick={() => void window.service.preference.set('themeSource', 'system')}>
<MenuItem dense onClick={async () => await window.service.preference.set('themeSource', 'system')}>
{t('Preference.SystemDefalutTheme')}
</MenuItem>
<MenuItem dense onClick={() => void window.service.preference.set('themeSource', 'light')}>
<MenuItem dense onClick={async () => await window.service.preference.set('themeSource', 'light')}>
{t('Preference.LightTheme')}
</MenuItem>
<MenuItem dense onClick={() => void window.service.preference.set('themeSource', 'dark')}>
<MenuItem dense onClick={async () => await window.service.preference.set('themeSource', 'dark')}>
{t('Preference.DarkTheme')}
</MenuItem>
</StatedMenu>
@ -506,8 +507,8 @@ const Preferences = ({
edge="end"
color="primary"
checked={sidebar}
onChange={(event) => {
void window.service.preference.set('sidebar', event.target.checked);
onChange={async (event) => {
await window.service.preference.set('sidebar', event.target.checked);
requestRealignActiveWorkspace();
}}
/>
@ -521,8 +522,8 @@ const Preferences = ({
edge="end"
color="primary"
checked={sidebarShortcutHints}
onChange={(event) => {
void window.service.preference.set('sidebarShortcutHints', event.target.checked);
onChange={async (event) => {
await window.service.preference.set('sidebarShortcutHints', event.target.checked);
}}
/>
</ListItemSecondaryAction>
@ -539,8 +540,8 @@ const Preferences = ({
// they can't access preferences or notifications
checked={(window.remote.getPlatform() === 'linux' && attachToMenubar && !sidebar) || navigationBar}
disabled={window.remote.getPlatform() === 'linux' && attachToMenubar && !sidebar}
onChange={(event) => {
void window.service.preference.set('navigationBar', event.target.checked);
onChange={async (event) => {
await window.service.preference.set('navigationBar', event.target.checked);
requestRealignActiveWorkspace();
}}
/>
@ -556,8 +557,8 @@ const Preferences = ({
edge="end"
color="primary"
checked={titleBar}
onChange={(event) => {
void window.service.preference.set('titleBar', event.target.checked);
onChange={async (event) => {
await window.service.preference.set('titleBar', event.target.checked);
requestRealignActiveWorkspace();
}}
/>
@ -575,9 +576,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={hideMenuBar}
onChange={(event) => {
void window.service.preference.set('hideMenuBar', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('hideMenuBar', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -595,9 +596,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={attachToMenubar}
onChange={(event) => {
void window.service.preference.set('attachToMenubar', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('attachToMenubar', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -639,9 +640,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={blockAds}
onChange={(event) => {
void window.service.preference.set('blockAds', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('blockAds', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -678,8 +679,8 @@ const Preferences = ({
color="primary"
checked={themeSource !== 'light' && darkReader}
disabled={themeSource === 'light'}
onChange={(event) => {
void window.service.preference.set('darkReader', event.target.checked);
onChange={async (event) => {
await window.service.preference.set('darkReader', event.target.checked);
}}
/>
</ListItemSecondaryAction>
@ -714,7 +715,7 @@ const Preferences = ({
max={50}
onChange={(_, value) => {
// @ts-expect-error ts-migrate(2365) FIXME: Operator '+' cannot be applied to types 'number | ... Remove this comment to see the full error message
void window.service.preference.set('darkReaderBrightness', value + 100);
await window.service.preference.set('darkReaderBrightness', value + 100);
}}
/>
</Grid>
@ -747,7 +748,7 @@ const Preferences = ({
max={50}
onChange={(_, value) => {
// @ts-expect-error ts-migrate(2365) FIXME: Operator '+' cannot be applied to types 'number | ... Remove this comment to see the full error message
void window.service.preference.set('darkReaderContrast', value + 100);
await window.service.preference.set('darkReaderContrast', value + 100);
}}
/>
</Grid>
@ -774,8 +775,8 @@ const Preferences = ({
]}
min={0}
max={100}
onChange={(_, value) => {
void window.service.preference.set('darkReaderSepia', value);
onChange={async (_, value) => {
await window.service.preference.set('darkReaderSepia', value);
}}
/>
</Grid>
@ -802,8 +803,8 @@ const Preferences = ({
]}
min={0}
max={100}
onChange={(_, value) => {
void window.service.preference.set('darkReaderGrayscale', value);
onChange={async (_, value) => {
await window.service.preference.set('darkReaderGrayscale', value);
}}
/>
</Grid>
@ -835,7 +836,7 @@ const Preferences = ({
renderInput={(timeProps) => <TextField {...timeProps} />}
value={new Date(pauseNotificationsByScheduleFrom)}
// @ts-expect-error ts-migrate(2531) FIXME: Object is possibly 'null'.
onChange={(d) => void window.service.preference.set('pauseNotificationsByScheduleFrom', d.toString())}
onChange={(d) => await window.service.preference.set('pauseNotificationsByScheduleFrom', d.toString())}
onClose={() => {
window.preventClosingWindow = false;
}}
@ -851,7 +852,7 @@ const Preferences = ({
renderInput={(timeProps) => <TextField {...timeProps} />}
value={new Date(pauseNotificationsByScheduleTo)}
// @ts-expect-error ts-migrate(2531) FIXME: Object is possibly 'null'.
onChange={(d) => void window.service.preference.set('pauseNotificationsByScheduleTo', d.toString())}
onChange={(d) => await window.service.preference.set('pauseNotificationsByScheduleTo', d.toString())}
onClose={() => {
window.preventClosingWindow = false;
}}
@ -868,8 +869,8 @@ const Preferences = ({
edge="end"
color="primary"
checked={pauseNotificationsBySchedule}
onChange={(event) => {
void window.service.preference.set('pauseNotificationsBySchedule', event.target.checked);
onChange={async (event) => {
await window.service.preference.set('pauseNotificationsBySchedule', event.target.checked);
}}
/>
</ListItemSecondaryAction>
@ -882,8 +883,8 @@ const Preferences = ({
edge="end"
color="primary"
checked={pauseNotificationsMuteAudio}
onChange={(event) => {
void window.service.preference.set('pauseNotificationsMuteAudio', event.target.checked);
onChange={async (event) => {
await window.service.preference.set('pauseNotificationsMuteAudio', event.target.checked);
}}
/>
</ListItemSecondaryAction>
@ -896,9 +897,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={unreadCountBadge}
onChange={(event) => {
void window.service.preference.set('unreadCountBadge', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('unreadCountBadge', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -975,9 +976,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={spellcheck}
onChange={(event) => {
void window.service.preference.set('spellcheck', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('spellcheck', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -1011,10 +1012,10 @@ const Preferences = ({
.showOpenDialog({
properties: ['openDirectory'],
})
.then((result: any) => {
.then(async (result: any) => {
// eslint-disable-next-line promise/always-return
if (!result.canceled && result.filePaths) {
void window.service.preference.set('downloadPath', result.filePaths[0]);
await window.service.preference.set('downloadPath', result.filePaths[0]);
}
})
.catch((error: any) => {
@ -1032,8 +1033,8 @@ const Preferences = ({
edge="end"
color="primary"
checked={askForDownloadPath}
onChange={(event) => {
void window.service.preference.set('askForDownloadPath', event.target.checked);
onChange={async (event) => {
await window.service.preference.set('askForDownloadPath', event.target.checked);
}}
/>
</ListItemSecondaryAction>
@ -1067,9 +1068,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={blockAds}
onChange={(event) => {
void window.service.preference.set('blockAds', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('blockAds', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -1082,9 +1083,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={rememberLastPageVisited}
onChange={(event) => {
void window.service.preference.set('rememberLastPageVisited', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('rememberLastPageVisited', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -1097,9 +1098,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={shareWorkspaceBrowsingData}
onChange={(event) => {
void window.service.preference.set('shareWorkspaceBrowsingData', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('shareWorkspaceBrowsingData', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -1133,9 +1134,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={ignoreCertificateErrors}
onChange={(event) => {
void window.service.preference.set('ignoreCertificateErrors', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('ignoreCertificateErrors', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -1231,8 +1232,8 @@ const Preferences = ({
edge="end"
color="primary"
checked={hibernateUnusedWorkspacesAtLaunch}
onChange={(event) => {
void window.service.preference.set('hibernateUnusedWorkspacesAtLaunch', event.target.checked);
onChange={async (event) => {
await window.service.preference.set('hibernateUnusedWorkspacesAtLaunch', event.target.checked);
}}
/>
</ListItemSecondaryAction>
@ -1262,9 +1263,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={swipeToNavigate}
onChange={(event) => {
void window.service.preference.set('swipeToNavigate', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('swipeToNavigate', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -1279,9 +1280,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={useHardwareAcceleration}
onChange={(event) => {
void window.service.preference.set('useHardwareAcceleration', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('useHardwareAcceleration', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -1318,9 +1319,9 @@ const Preferences = ({
edge="end"
color="primary"
checked={allowPrerelease}
onChange={(event) => {
void window.service.preference.set('allowPrerelease', event.target.checked);
requestShowRequireRestartDialog();
onChange={async (event) => {
await window.service.preference.set('allowPrerelease', event.target.checked);
await debouncedRequestShowRequireRestartDialog();
}}
/>
</ListItemSecondaryAction>
@ -1334,7 +1335,7 @@ const Preferences = ({
</Typography>
<Paper elevation={0} className={classes.paper}>
<List dense disablePadding>
<ListItem button onClick={requestResetPreferences}>
<ListItem button onClick={window.service.preference.resetWithConfirm}>
<ListItemText primary="Restore preferences to their original defaults" />
<ChevronRightIcon color="action" />
</ListItem>

View file

@ -49,8 +49,6 @@ export enum WorkspaceViewChannel {
export enum PreferenceChannel {
name = 'PreferenceChannel',
update = 'update',
requestResetPreferences = 'request-reset-preferences',
requestShowRequireRestartDialog = 'request-show-require-restart-dialog',
getPreference = 'get-preference',
getPreferences = 'get-preferences',
requestClearBrowsingData = 'request-clear-browsing-data',
@ -58,7 +56,6 @@ export enum PreferenceChannel {
export enum WindowChannel {
name = 'WindowChannel',
requestShowRequireRestartDialog = 'request-show-require-restart-dialog',
}
export enum ThemeChannel {

View file

@ -12,10 +12,6 @@ export const getShouldUseDarkColors = async (): Promise<void> => {
await ipcRenderer.invoke('get-should-use-dark-colors');
};
// Preferences
export const requestResetPreferences = async () => await ipcRenderer.invoke('request-reset-preferences');
export const requestShowRequireRestartDialog = async () => await ipcRenderer.invoke('request-show-require-restart-dialog');
// System Preferences
export const getSystemPreference = (name: string): JsonValue => ipcRenderer.invokeSync('get-system-preference', name);
export const getSystemPreferences = (): JsonObject => ipcRenderer.invokeSync('get-system-preferences');

View file

@ -97,25 +97,6 @@ export class Preference implements IPreferenceService {
}
init(): void {
ipcMain.handle(PreferenceChannel.requestResetPreferences, () => {
const preferenceWindow = this.windowService.get(WindowNames.preferences);
if (preferenceWindow !== undefined) {
dialog
.showMessageBox(preferenceWindow, {
type: 'question',
buttons: [i18n.t('Preference.ResetNow'), i18n.t('Cancel')],
message: i18n.t('Preference.Reset'),
cancelId: 1,
})
.then(async ({ response }) => {
if (response === 0) {
await this.reset();
ipcMain.emit(PreferenceChannel.requestShowRequireRestartDialog);
}
})
.catch(console.error);
}
});
ipcMain.handle(PreferenceChannel.requestClearBrowsingData, () => {
const availableWindowToShowDialog = this.windowService.get(WindowNames.preferences) ?? this.windowService.get(WindowNames.main);
if (availableWindowToShowDialog !== undefined) {
@ -143,6 +124,26 @@ export class Preference implements IPreferenceService {
});
}
public async resetWithConfirm(): Promise<void> {
const preferenceWindow = this.windowService.get(WindowNames.preferences);
if (preferenceWindow !== undefined) {
await dialog
.showMessageBox(preferenceWindow, {
type: 'question',
buttons: [i18n.t('Preference.ResetNow'), i18n.t('Cancel')],
message: i18n.t('Preference.Reset'),
cancelId: 1,
})
.then(async ({ response }) => {
if (response === 0) {
await this.reset();
await this.windowService.requestShowRequireRestartDialog();
}
})
.catch(console.error);
}
}
/**
* load preferences in sync, and ensure it is an Object
*/

View file

@ -53,6 +53,7 @@ export interface IPreferenceService {
getPreferences: () => IPreferences;
get<K extends keyof IPreferences>(key: K): IPreferences[K];
reset(): Promise<void>;
resetWithConfirm(): Promise<void>;
}
export const PreferenceServiceIPCDescriptor = {
channel: PreferenceChannel.name,
@ -61,5 +62,6 @@ export const PreferenceServiceIPCDescriptor = {
getPreferences: ProxyPropertyType.Function,
get: ProxyPropertyType.Function,
reset: ProxyPropertyType.Function,
resetWithConfirm: ProxyPropertyType.Function,
},
};

View file

@ -39,27 +39,6 @@ export class Window implements IWindowService {
}
initIPCHandlers(): void {
ipcMain.handle(WindowChannel.requestShowRequireRestartDialog, () => {
const availableWindowToShowDialog = this.get(WindowNames.preferences) ?? this.get(WindowNames.main);
if (availableWindowToShowDialog !== undefined) {
dialog
.showMessageBox(availableWindowToShowDialog, {
type: 'question',
buttons: [i18n.t('Dialog.RestartNow'), i18n.t('Dialog.Later')],
message: i18n.t('Dialog.RestartMessage'),
cancelId: 1,
})
.then(({ response }) => {
if (response === 0) {
const availableApp = (app as App | undefined) === undefined ? remote.app : app;
availableApp.relaunch();
availableApp.quit();
}
})
.catch(console.error);
}
});
ipcMain.handle('request-find-in-page', (_event, text: string, forward?: boolean, windowName: WindowNames = WindowNames.main) => {
const mainWindow = this.get(windowName);
const contents = mainWindow?.getBrowserView()?.webContents;
@ -97,6 +76,26 @@ export class Window implements IWindowService {
});
}
public async requestShowRequireRestartDialog(): Promise<void> {
const availableWindowToShowDialog = this.get(WindowNames.preferences) ?? this.get(WindowNames.main);
if (availableWindowToShowDialog !== undefined) {
await dialog
.showMessageBox(availableWindowToShowDialog, {
type: 'question',
buttons: [i18n.t('Dialog.RestartNow'), i18n.t('Dialog.Later')],
message: i18n.t('Dialog.RestartMessage'),
cancelId: 1,
})
.then(({ response }) => {
if (response === 0) {
app.relaunch();
app.quit();
}
})
.catch(console.error);
}
}
public get(windowName: WindowNames = WindowNames.main): BrowserWindow | undefined {
return this.windows[windowName];
}

View file

@ -14,6 +14,7 @@ export interface IWindowService {
updateWindowMeta<N extends WindowNames>(windowName: N, meta?: WindowMeta[N]): void;
getWindowMeta<N extends WindowNames>(windowName: N): WindowMeta[N] | undefined;
sendToAllWindows: (channel: Channels, ...arguments_: unknown[]) => void;
requestShowRequireRestartDialog(): Promise<void>;
goHome(windowName: WindowNames): Promise<void>;
goBack(windowName: WindowNames): void;
goForward(windowName: WindowNames): void;
@ -29,6 +30,7 @@ export const WindowServiceIPCDescriptor = {
setWindowMeta: ProxyPropertyType.Function,
updateWindowMeta: ProxyPropertyType.Function,
getWindowMeta: ProxyPropertyType.Function,
requestShowRequireRestartDialog: ProxyPropertyType.Function,
sendToAllWindows: ProxyPropertyType.Function,
goHome: ProxyPropertyType.Function,
goBack: ProxyPropertyType.Function,

View file

@ -12,7 +12,7 @@ export const updateForm = (changes: any) => (dispatch: any) =>
changes,
});
export const save = () => (dispatch: any, getState: any) => {
export const save = async () => (dispatch: any, getState: any) => {
const { form } = getState().dialogCodeInjection;
const { codeInjectionType } = window.meta as WindowMeta[WindowNames.codeInjection];
@ -22,7 +22,7 @@ export const save = () => (dispatch: any, getState: any) => {
void window.service.preference.set('allowNodeInJsCodeInjection', form.allowNodeInJsCodeInjection);
}
requestShowRequireRestartDialog();
await window.service.window.requestShowRequireRestartDialog();
window.remote.closeCurrentWindow();
};

View file

@ -15,7 +15,7 @@ export const save = () => async (dispatch: any, getState: any) => {
if ((await window.service.preference.get('customUserAgent')) !== form.code) {
await window.service.preference.set('customUserAgent', form.code);
requestShowRequireRestartDialog();
await window.service.window.requestShowRequireRestartDialog();
}
window.remote.closeCurrentWindow();

View file

@ -47,7 +47,7 @@ export const updateForm = (changes: any) => (dispatch: any, getState: any) => {
}
};
export const save = () => (dispatch: any, getState: any) => {
export const save = async () => (dispatch: any, getState: any) => {
const state = getState();
const { form } = state.dialogProxy;
@ -61,7 +61,7 @@ export const save = () => (dispatch: any, getState: any) => {
void window.service.preference.set('proxyBypassRules', form.proxyBypassRules);
void window.service.preference.set('proxyPacScript', form.proxyPacScript);
void window.service.preference.set('proxyType', form.proxyType);
requestShowRequireRestartDialog();
await window.service.window.requestShowRequireRestartDialog()
window.remote.closeCurrentWindow();
return null;

View file

@ -31,12 +31,12 @@ export const removeLanguage = (code: any) => (dispatch: any, getState: any) => {
);
};
export const save = () => (dispatch: any, getState: any) => {
export const save = async () => (dispatch: any, getState: any) => {
const { form } = getState().dialogSpellcheckLanguages;
void window.service.preference.set('spellcheckLanguages', form.spellcheckLanguages);
requestShowRequireRestartDialog();
await window.service.window.requestShowRequireRestartDialog()
window.remote.closeCurrentWindow();
};