Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit d6c162a

Browse files
committed
Abstract electron settings properly to avoid boilerplate-hell (#8798)
* Remove unused method `BasePlatform::screenCaptureErrorString` * Improve platform typescripting * Remove redundant awaits * Abstract electron settings properly to avoid boilerplate-hell * i18n * Fix stray semi-colons * Fix setting level order for Platform settings (cherry picked from commit ba2ce5e)
1 parent ec9117d commit d6c162a

File tree

9 files changed

+122
-220
lines changed

9 files changed

+122
-220
lines changed

src/BasePlatform.ts

Lines changed: 15 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ export enum UpdateCheckStatus {
4646
Ready = "READY",
4747
}
4848

49+
export interface UpdateStatus {
50+
/**
51+
* The current phase of the manual update check.
52+
*/
53+
status: UpdateCheckStatus;
54+
/**
55+
* Detail string relating to the current status, typically for error details.
56+
*/
57+
detail?: string;
58+
}
59+
4960
const UPDATE_DEFER_KEY = "mx_defer_update";
5061

5162
/**
@@ -225,79 +236,21 @@ export default abstract class BasePlatform {
225236
*/
226237
public abstract getAppVersion(): Promise<string>;
227238

228-
/*
229-
* If it's not expected that capturing the screen will work
230-
* with getUserMedia, return a string explaining why not.
231-
* Otherwise, return null.
232-
*/
233-
public screenCaptureErrorString(): string {
234-
return "Not implemented";
235-
}
236-
237239
/**
238240
* Restarts the application, without necessarily reloading
239241
* any application code
240242
*/
241243
public abstract reload(): void;
242244

243-
public supportsAutoLaunch(): boolean {
244-
return false;
245-
}
246-
247-
// XXX: Surely this should be a setting like any other?
248-
public async getAutoLaunchEnabled(): Promise<boolean> {
249-
return false;
250-
}
251-
252-
public async setAutoLaunchEnabled(enabled: boolean): Promise<void> {
253-
throw new Error("Unimplemented");
254-
}
255-
256-
public supportsWarnBeforeExit(): boolean {
257-
return false;
258-
}
259-
260-
public async shouldWarnBeforeExit(): Promise<boolean> {
261-
return false;
262-
}
263-
264-
public async setWarnBeforeExit(enabled: boolean): Promise<void> {
265-
throw new Error("Unimplemented");
266-
}
267-
268-
public supportsAutoHideMenuBar(): boolean {
269-
return false;
270-
}
271-
272-
public async getAutoHideMenuBarEnabled(): Promise<boolean> {
273-
return false;
274-
}
275-
276-
public async setAutoHideMenuBarEnabled(enabled: boolean): Promise<void> {
277-
throw new Error("Unimplemented");
278-
}
279-
280-
public supportsMinimizeToTray(): boolean {
281-
return false;
282-
}
283-
284-
public async getMinimizeToTrayEnabled(): Promise<boolean> {
285-
return false;
286-
}
287-
288-
public async setMinimizeToTrayEnabled(enabled: boolean): Promise<void> {
289-
throw new Error("Unimplemented");
290-
}
291-
292-
public supportsTogglingHardwareAcceleration(): boolean {
245+
public supportsSetting(settingName?: string): boolean {
293246
return false;
294247
}
295248

296-
public async getHardwareAccelerationEnabled(): Promise<boolean> {
297-
return true;
249+
public getSettingValue(settingName: string): Promise<any> {
250+
return undefined;
298251
}
299252

300-
public async setHardwareAccelerationEnabled(enabled: boolean): Promise<void> {
253+
public setSettingValue(settingName: string, value: any): Promise<void> {
301254
throw new Error("Unimplemented");
302255
}
303256

src/components/views/elements/SettingsFlag.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface IProps {
3333
// XXX: once design replaces all toggles make this the default
3434
useCheckbox?: boolean;
3535
disabled?: boolean;
36+
hideIfCannotSet?: boolean;
3637
onChange?(checked: boolean): void;
3738
}
3839

@@ -76,6 +77,8 @@ export default class SettingsFlag extends React.Component<IProps, IState> {
7677
public render() {
7778
const canChange = SettingsStore.canSetValue(this.props.name, this.props.roomId, this.props.level);
7879

80+
if (!canChange && this.props.hideIfCannotSet) return null;
81+
7982
const label = this.props.label
8083
? _t(this.props.label)
8184
: SettingsStore.getDisplayName(this.props.name, this.props.level);

src/components/views/settings/tabs/user/PreferencesUserSettingsTab.tsx

Lines changed: 14 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ limitations under the License.
1818
import React from 'react';
1919

2020
import { _t } from "../../../../../languageHandler";
21-
import LabelledToggleSwitch from "../../../elements/LabelledToggleSwitch";
2221
import SettingsStore from "../../../../../settings/SettingsStore";
2322
import Field from "../../../elements/Field";
24-
import PlatformPeg from "../../../../../PlatformPeg";
2523
import { SettingLevel } from "../../../../../settings/SettingLevel";
2624
import SettingsFlag from '../../../elements/SettingsFlag';
2725
import AccessibleButton from "../../../elements/AccessibleButton";
@@ -36,16 +34,6 @@ interface IProps {
3634
}
3735

3836
interface IState {
39-
autoLaunch: boolean;
40-
autoLaunchSupported: boolean;
41-
warnBeforeExit: boolean;
42-
warnBeforeExitSupported: boolean;
43-
alwaysShowMenuBarSupported: boolean;
44-
alwaysShowMenuBar: boolean;
45-
minimizeToTraySupported: boolean;
46-
minimizeToTray: boolean;
47-
togglingHardwareAccelerationSupported: boolean;
48-
enableHardwareAcceleration: boolean;
4937
autocompleteDelay: string;
5038
readMarkerInViewThresholdMs: string;
5139
readMarkerOutOfViewThresholdMs: string;
@@ -112,16 +100,6 @@ export default class PreferencesUserSettingsTab extends React.Component<IProps,
112100
super(props);
113101

114102
this.state = {
115-
autoLaunch: false,
116-
autoLaunchSupported: false,
117-
warnBeforeExit: true,
118-
warnBeforeExitSupported: false,
119-
alwaysShowMenuBar: true,
120-
alwaysShowMenuBarSupported: false,
121-
minimizeToTray: true,
122-
minimizeToTraySupported: false,
123-
enableHardwareAcceleration: true,
124-
togglingHardwareAccelerationSupported: false,
125103
autocompleteDelay:
126104
SettingsStore.getValueAt(SettingLevel.DEVICE, 'autocompleteDelay').toString(10),
127105
readMarkerInViewThresholdMs:
@@ -131,74 +109,6 @@ export default class PreferencesUserSettingsTab extends React.Component<IProps,
131109
};
132110
}
133111

134-
async componentDidMount() {
135-
const platform = PlatformPeg.get();
136-
137-
const autoLaunchSupported = await platform.supportsAutoLaunch();
138-
let autoLaunch = false;
139-
if (autoLaunchSupported) {
140-
autoLaunch = await platform.getAutoLaunchEnabled();
141-
}
142-
143-
const warnBeforeExitSupported = await platform.supportsWarnBeforeExit();
144-
let warnBeforeExit = false;
145-
if (warnBeforeExitSupported) {
146-
warnBeforeExit = await platform.shouldWarnBeforeExit();
147-
}
148-
149-
const alwaysShowMenuBarSupported = await platform.supportsAutoHideMenuBar();
150-
let alwaysShowMenuBar = true;
151-
if (alwaysShowMenuBarSupported) {
152-
alwaysShowMenuBar = !(await platform.getAutoHideMenuBarEnabled());
153-
}
154-
155-
const minimizeToTraySupported = await platform.supportsMinimizeToTray();
156-
let minimizeToTray = true;
157-
if (minimizeToTraySupported) {
158-
minimizeToTray = await platform.getMinimizeToTrayEnabled();
159-
}
160-
161-
const togglingHardwareAccelerationSupported = platform.supportsTogglingHardwareAcceleration();
162-
let enableHardwareAcceleration = true;
163-
if (togglingHardwareAccelerationSupported) {
164-
enableHardwareAcceleration = await platform.getHardwareAccelerationEnabled();
165-
}
166-
167-
this.setState({
168-
autoLaunch,
169-
autoLaunchSupported,
170-
warnBeforeExit,
171-
warnBeforeExitSupported,
172-
alwaysShowMenuBarSupported,
173-
alwaysShowMenuBar,
174-
minimizeToTraySupported,
175-
minimizeToTray,
176-
togglingHardwareAccelerationSupported,
177-
enableHardwareAcceleration,
178-
});
179-
}
180-
181-
private onAutoLaunchChange = (checked: boolean) => {
182-
PlatformPeg.get().setAutoLaunchEnabled(checked).then(() => this.setState({ autoLaunch: checked }));
183-
};
184-
185-
private onWarnBeforeExitChange = (checked: boolean) => {
186-
PlatformPeg.get().setWarnBeforeExit(checked).then(() => this.setState({ warnBeforeExit: checked }));
187-
};
188-
189-
private onAlwaysShowMenuBarChange = (checked: boolean) => {
190-
PlatformPeg.get().setAutoHideMenuBarEnabled(!checked).then(() => this.setState({ alwaysShowMenuBar: checked }));
191-
};
192-
193-
private onMinimizeToTrayChange = (checked: boolean) => {
194-
PlatformPeg.get().setMinimizeToTrayEnabled(checked).then(() => this.setState({ minimizeToTray: checked }));
195-
};
196-
197-
private onHardwareAccelerationChange = (checked: boolean) => {
198-
PlatformPeg.get().setHardwareAccelerationEnabled(checked).then(
199-
() => this.setState({ enableHardwareAcceleration: checked }));
200-
};
201-
202112
private onAutocompleteDelayChange = (e: React.ChangeEvent<HTMLInputElement>) => {
203113
this.setState({ autocompleteDelay: e.target.value });
204114
SettingsStore.setValue("autocompleteDelay", null, SettingLevel.DEVICE, e.target.value);
@@ -232,49 +142,6 @@ export default class PreferencesUserSettingsTab extends React.Component<IProps,
232142
};
233143

234144
render() {
235-
let autoLaunchOption = null;
236-
if (this.state.autoLaunchSupported) {
237-
autoLaunchOption = <LabelledToggleSwitch
238-
value={this.state.autoLaunch}
239-
onChange={this.onAutoLaunchChange}
240-
label={_t('Start automatically after system login')} />;
241-
}
242-
243-
let warnBeforeExitOption = null;
244-
if (this.state.warnBeforeExitSupported) {
245-
warnBeforeExitOption = <LabelledToggleSwitch
246-
value={this.state.warnBeforeExit}
247-
onChange={this.onWarnBeforeExitChange}
248-
label={_t('Warn before quitting')} />;
249-
}
250-
251-
let autoHideMenuOption = null;
252-
if (this.state.alwaysShowMenuBarSupported) {
253-
autoHideMenuOption = <LabelledToggleSwitch
254-
value={this.state.alwaysShowMenuBar}
255-
onChange={this.onAlwaysShowMenuBarChange}
256-
label={_t('Always show the window menu bar')} />;
257-
}
258-
259-
let minimizeToTrayOption = null;
260-
if (this.state.minimizeToTraySupported) {
261-
minimizeToTrayOption = <LabelledToggleSwitch
262-
value={this.state.minimizeToTray}
263-
onChange={this.onMinimizeToTrayChange}
264-
label={_t('Show tray icon and minimise window to it on close')} />;
265-
}
266-
267-
let hardwareAccelerationOption = null;
268-
if (this.state.togglingHardwareAccelerationSupported) {
269-
const appName = SdkConfig.get().brand;
270-
hardwareAccelerationOption = <LabelledToggleSwitch
271-
value={this.state.enableHardwareAcceleration}
272-
onChange={this.onHardwareAccelerationChange}
273-
label={_t('Enable hardware acceleration (restart %(appName)s to take effect)', {
274-
appName,
275-
})} />;
276-
}
277-
278145
return (
279146
<div className="mx_SettingsTab mx_PreferencesUserSettingsTab">
280147
<div className="mx_SettingsTab_heading">{ _t("Preferences") }</div>
@@ -331,11 +198,20 @@ export default class PreferencesUserSettingsTab extends React.Component<IProps,
331198
<div className="mx_SettingsTab_section">
332199
<span className="mx_SettingsTab_subheading">{ _t("General") }</span>
333200
{ this.renderGroup(PreferencesUserSettingsTab.GENERAL_SETTINGS) }
334-
{ minimizeToTrayOption }
335-
{ hardwareAccelerationOption }
336-
{ autoHideMenuOption }
337-
{ autoLaunchOption }
338-
{ warnBeforeExitOption }
201+
202+
<SettingsFlag name="Electron.showTrayIcon" level={SettingLevel.PLATFORM} hideIfCannotSet />
203+
<SettingsFlag
204+
name="Electron.enableHardwareAcceleration"
205+
level={SettingLevel.PLATFORM}
206+
hideIfCannotSet
207+
label={_t('Enable hardware acceleration (restart %(appName)s to take effect)', {
208+
appName: SdkConfig.get().brand,
209+
})}
210+
/>
211+
<SettingsFlag name="Electron.alwaysShowMenuBar" level={SettingLevel.PLATFORM} hideIfCannotSet />
212+
<SettingsFlag name="Electron.autoLaunch" level={SettingLevel.PLATFORM} hideIfCannotSet />
213+
<SettingsFlag name="Electron.warnBeforeExit" level={SettingLevel.PLATFORM} hideIfCannotSet />
214+
339215
<Field
340216
label={_t('Autocomplete delay (ms)')}
341217
type='number'

src/dispatcher/payloads/CheckUpdatesPayload.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,8 @@ limitations under the License.
1616

1717
import { ActionPayload } from "../payloads";
1818
import { Action } from "../actions";
19-
import { UpdateCheckStatus } from "../../BasePlatform";
19+
import { UpdateStatus } from "../../BasePlatform";
2020

21-
export interface CheckUpdatesPayload extends ActionPayload {
21+
export interface CheckUpdatesPayload extends ActionPayload, UpdateStatus {
2222
action: Action.CheckUpdates;
23-
24-
/**
25-
* The current phase of the manual update check.
26-
*/
27-
status: UpdateCheckStatus;
28-
29-
/**
30-
* Detail string relating to the current status, typically for error details.
31-
*/
32-
detail?: string;
3323
}

src/i18n/strings/en_EN.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,11 @@
970970
"Automatically send debug logs on any error": "Automatically send debug logs on any error",
971971
"Automatically send debug logs on decryption errors": "Automatically send debug logs on decryption errors",
972972
"Automatically send debug logs when key backup is not functioning": "Automatically send debug logs when key backup is not functioning",
973+
"Start automatically after system login": "Start automatically after system login",
974+
"Warn before quitting": "Warn before quitting",
975+
"Always show the window menu bar": "Always show the window menu bar",
976+
"Show tray icon and minimise window to it on close": "Show tray icon and minimise window to it on close",
977+
"Enable hardware acceleration": "Enable hardware acceleration",
973978
"Partial Support for Threads": "Partial Support for Threads",
974979
"Your homeserver does not currently support threads, so this feature may be unreliable. Some threaded messages may not be reliably available. <a>Learn more</a>.": "Your homeserver does not currently support threads, so this feature may be unreliable. Some threaded messages may not be reliably available. <a>Learn more</a>.",
975980
"Do you want to enable threads anyway?": "Do you want to enable threads anyway?",
@@ -1498,11 +1503,6 @@
14981503
"If this isn't what you want, please use a different tool to ignore users.": "If this isn't what you want, please use a different tool to ignore users.",
14991504
"Room ID or address of ban list": "Room ID or address of ban list",
15001505
"Subscribe": "Subscribe",
1501-
"Start automatically after system login": "Start automatically after system login",
1502-
"Warn before quitting": "Warn before quitting",
1503-
"Always show the window menu bar": "Always show the window menu bar",
1504-
"Show tray icon and minimise window to it on close": "Show tray icon and minimise window to it on close",
1505-
"Enable hardware acceleration (restart %(appName)s to take effect)": "Enable hardware acceleration (restart %(appName)s to take effect)",
15061506
"Preferences": "Preferences",
15071507
"Room list": "Room list",
15081508
"Keyboard shortcuts": "Keyboard shortcuts",
@@ -1512,6 +1512,7 @@
15121512
"Code blocks": "Code blocks",
15131513
"Images, GIFs and videos": "Images, GIFs and videos",
15141514
"Timeline": "Timeline",
1515+
"Enable hardware acceleration (restart %(appName)s to take effect)": "Enable hardware acceleration (restart %(appName)s to take effect)",
15151516
"Autocomplete delay (ms)": "Autocomplete delay (ms)",
15161517
"Read Marker lifetime (ms)": "Read Marker lifetime (ms)",
15171518
"Read Marker off-screen lifetime (ms)": "Read Marker off-screen lifetime (ms)",

src/settings/SettingLevel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export enum SettingLevel {
2424
ROOM_ACCOUNT = "room-account",
2525
ACCOUNT = "account",
2626
ROOM = "room",
27+
PLATFORM = "platform",
2728
CONFIG = "config",
2829
DEFAULT = "default",
2930
}

0 commit comments

Comments
 (0)