2020-04-14 18:19:50 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
2020-04-21 13:34:15 +03:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2020-04-14 18:19:50 +03:00
|
|
|
|
2020-04-21 14:03:32 +03:00
|
|
|
|
2020-04-14 18:19:50 +03:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import {_t} from "../../../../../languageHandler";
|
|
|
|
import SettingsStore, {SettingLevel} from "../../../../../settings/SettingsStore";
|
|
|
|
import * as sdk from "../../../../../index";
|
2020-05-28 15:55:07 +03:00
|
|
|
import { enumerateThemes } from "../../../../../theme";
|
|
|
|
import ThemeWatcher from "../../../../../settings/watchers/ThemeWatcher";
|
2020-04-14 18:19:50 +03:00
|
|
|
import Field from "../../../elements/Field";
|
2020-04-21 14:03:32 +03:00
|
|
|
import Slider from "../../../elements/Slider";
|
2020-04-16 13:56:43 +03:00
|
|
|
import AccessibleButton from "../../../elements/AccessibleButton";
|
2020-05-20 16:44:56 +03:00
|
|
|
import dis from "../../../../../dispatcher/dispatcher";
|
2020-05-28 15:55:07 +03:00
|
|
|
import { FontWatcher } from "../../../../../settings/watchers/FontWatcher";
|
|
|
|
import { RecheckThemePayload } from '../../../../../dispatcher/payloads/RecheckThemePayload';
|
|
|
|
import { Action } from '../../../../../dispatcher/actions';
|
|
|
|
import { IValidationResult, IFieldState } from '../../../elements/Validation';
|
2020-04-14 18:19:50 +03:00
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
interface IProps {
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState extends IThemeState {
|
|
|
|
// String displaying the current selected fontSize.
|
|
|
|
// Needs to be string for things like '17.' without
|
|
|
|
// trailing 0s.
|
|
|
|
fontSize: string,
|
|
|
|
customThemeUrl: string,
|
|
|
|
customThemeMessage: CustomThemeMessage,
|
|
|
|
useCustomFontSize: boolean,
|
|
|
|
}
|
|
|
|
|
|
|
|
export type CustomThemeMessage = {isError: boolean, text: string};
|
|
|
|
|
|
|
|
interface IThemeState {
|
|
|
|
theme: string,
|
|
|
|
useSystemTheme: boolean,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class AppearanceUserSettingsTab extends React.Component<IProps, IState> {
|
|
|
|
|
|
|
|
private themeTimer: NodeJS.Timeout;
|
|
|
|
|
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
2020-04-14 18:19:50 +03:00
|
|
|
|
|
|
|
this.state = {
|
2020-05-28 15:55:07 +03:00
|
|
|
fontSize: SettingsStore.getValue("fontSize", null).toString(),
|
|
|
|
...this.calculateThemeState(),
|
2020-04-14 18:19:50 +03:00
|
|
|
customThemeUrl: "",
|
|
|
|
customThemeMessage: {isError: false, text: ""},
|
2020-04-23 14:09:08 +03:00
|
|
|
useCustomFontSize: SettingsStore.getValue("useCustomFontSize"),
|
2020-04-16 13:56:43 +03:00
|
|
|
};
|
2020-04-14 18:19:50 +03:00
|
|
|
}
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
private calculateThemeState(): IThemeState {
|
2020-04-14 18:19:50 +03:00
|
|
|
// We have to mirror the logic from ThemeWatcher.getEffectiveTheme so we
|
|
|
|
// show the right values for things.
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
const themeChoice: string = SettingsStore.getValueAt(SettingLevel.ACCOUNT, "theme");
|
|
|
|
const systemThemeExplicit: boolean = SettingsStore.getValueAt(
|
2020-04-14 18:19:50 +03:00
|
|
|
SettingLevel.DEVICE, "use_system_theme", null, false, true);
|
2020-05-28 15:55:07 +03:00
|
|
|
const themeExplicit: string = SettingsStore.getValueAt(
|
2020-04-14 18:19:50 +03:00
|
|
|
SettingLevel.DEVICE, "theme", null, false, true);
|
|
|
|
|
|
|
|
// If the user has enabled system theme matching, use that.
|
|
|
|
if (systemThemeExplicit) {
|
|
|
|
return {
|
|
|
|
theme: themeChoice,
|
|
|
|
useSystemTheme: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user has set a theme explicitly, use that (no system theme matching)
|
|
|
|
if (themeExplicit) {
|
|
|
|
return {
|
|
|
|
theme: themeChoice,
|
|
|
|
useSystemTheme: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise assume the defaults for the settings
|
|
|
|
return {
|
|
|
|
theme: themeChoice,
|
|
|
|
useSystemTheme: SettingsStore.getValueAt(SettingLevel.DEVICE, "use_system_theme"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
private onThemeChange(e: React.ChangeEvent<HTMLSelectElement | HTMLInputElement>): void {
|
2020-04-14 18:19:50 +03:00
|
|
|
const newTheme = e.target.value;
|
|
|
|
if (this.state.theme === newTheme) return;
|
|
|
|
|
|
|
|
// doing getValue in the .catch will still return the value we failed to set,
|
|
|
|
// so remember what the value was before we tried to set it so we can revert
|
2020-05-28 15:55:07 +03:00
|
|
|
const oldTheme: string = SettingsStore.getValue('theme');
|
2020-04-14 18:19:50 +03:00
|
|
|
SettingsStore.setValue("theme", null, SettingLevel.ACCOUNT, newTheme).catch(() => {
|
2020-05-28 15:55:07 +03:00
|
|
|
dis.dispatch<RecheckThemePayload>({action: Action.RecheckTheme});
|
2020-04-14 18:19:50 +03:00
|
|
|
this.setState({theme: oldTheme});
|
|
|
|
});
|
|
|
|
this.setState({theme: newTheme});
|
|
|
|
// The settings watcher doesn't fire until the echo comes back from the
|
|
|
|
// server, so to make the theme change immediately we need to manually
|
|
|
|
// do the dispatch now
|
|
|
|
// XXX: The local echoed value appears to be unreliable, in particular
|
|
|
|
// when settings custom themes(!) so adding forceTheme to override
|
|
|
|
// the value from settings.
|
2020-05-28 15:55:07 +03:00
|
|
|
dis.dispatch<RecheckThemePayload>({action: Action.RecheckTheme, forceTheme: newTheme});
|
2020-04-14 18:19:50 +03:00
|
|
|
};
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
private onUseSystemThemeChanged(checked: boolean) {
|
2020-04-14 18:19:50 +03:00
|
|
|
this.setState({useSystemTheme: checked});
|
|
|
|
SettingsStore.setValue("use_system_theme", null, SettingLevel.DEVICE, checked);
|
2020-05-28 15:55:07 +03:00
|
|
|
dis.dispatch<RecheckThemePayload>({action: Action.RecheckTheme});
|
2020-04-14 18:19:50 +03:00
|
|
|
};
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
private onFontSizeChanged(size: number) {
|
|
|
|
this.setState({fontSize: size.toString()});
|
2020-05-06 19:25:54 +03:00
|
|
|
SettingsStore.setValue("fontSize", null, SettingLevel.DEVICE, size);
|
2020-04-14 18:19:50 +03:00
|
|
|
};
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
private async onValidateFontSize({value}: Pick<IFieldState, "value">): Promise<IValidationResult> {
|
2020-04-23 15:52:08 +03:00
|
|
|
const parsedSize = parseFloat(value);
|
2020-05-20 17:17:47 +03:00
|
|
|
const min = FontWatcher.MIN_SIZE;
|
|
|
|
const max = FontWatcher.MAX_SIZE;
|
2020-04-23 15:52:08 +03:00
|
|
|
|
|
|
|
if (isNaN(parsedSize)) {
|
|
|
|
return {valid: false, feedback: _t("Size must be a number")};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(min <= parsedSize && parsedSize <= max)) {
|
2020-04-23 15:55:10 +03:00
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
feedback: _t('Custom font size can only be between %(min)s pt and %(max)s pt', {min, max}),
|
|
|
|
};
|
2020-04-23 15:52:08 +03:00
|
|
|
}
|
|
|
|
|
2020-05-06 19:25:54 +03:00
|
|
|
SettingsStore.setValue("fontSize", null, SettingLevel.DEVICE, value);
|
2020-04-23 15:52:08 +03:00
|
|
|
return {valid: true, feedback: _t('Use between %(min)s pt and %(max)s pt', {min, max})};
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
private async onAddCustomTheme() {
|
|
|
|
let currentThemes: string[] = SettingsStore.getValue("custom_themes");
|
2020-04-14 18:19:50 +03:00
|
|
|
if (!currentThemes) currentThemes = [];
|
|
|
|
currentThemes = currentThemes.map(c => c); // cheap clone
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
if (this.themeTimer) {
|
|
|
|
clearTimeout(this.themeTimer);
|
2020-04-14 18:19:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const r = await fetch(this.state.customThemeUrl);
|
2020-05-28 15:55:07 +03:00
|
|
|
// XXX: need some schema for this
|
2020-04-14 18:19:50 +03:00
|
|
|
const themeInfo = await r.json();
|
|
|
|
if (!themeInfo || typeof(themeInfo['name']) !== 'string' || typeof(themeInfo['colors']) !== 'object') {
|
|
|
|
this.setState({customThemeMessage: {text: _t("Invalid theme schema."), isError: true}});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
currentThemes.push(themeInfo);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
this.setState({customThemeMessage: {text: _t("Error downloading theme information."), isError: true}});
|
|
|
|
return; // Don't continue on error
|
|
|
|
}
|
|
|
|
|
|
|
|
await SettingsStore.setValue("custom_themes", null, SettingLevel.ACCOUNT, currentThemes);
|
|
|
|
this.setState({customThemeUrl: "", customThemeMessage: {text: _t("Theme added!"), isError: false}});
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
this.themeTimer = setTimeout(() => {
|
2020-04-14 18:19:50 +03:00
|
|
|
this.setState({customThemeMessage: {text: "", isError: false}});
|
|
|
|
}, 3000);
|
|
|
|
};
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
private onCustomThemeChange(e: React.ChangeEvent<HTMLSelectElement | HTMLInputElement>) {
|
2020-04-14 18:19:50 +03:00
|
|
|
this.setState({customThemeUrl: e.target.value});
|
|
|
|
};
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
private renderThemeSection() {
|
2020-04-14 18:19:50 +03:00
|
|
|
const SettingsFlag = sdk.getComponent("views.elements.SettingsFlag");
|
|
|
|
const LabelledToggleSwitch = sdk.getComponent("views.elements.LabelledToggleSwitch");
|
|
|
|
|
|
|
|
const themeWatcher = new ThemeWatcher();
|
2020-05-28 15:55:07 +03:00
|
|
|
let systemThemeSection: JSX.Element;
|
2020-04-14 18:19:50 +03:00
|
|
|
if (themeWatcher.isSystemThemeSupported()) {
|
|
|
|
systemThemeSection = <div>
|
|
|
|
<LabelledToggleSwitch
|
|
|
|
value={this.state.useSystemTheme}
|
|
|
|
label={SettingsStore.getDisplayName("use_system_theme")}
|
2020-05-28 15:55:07 +03:00
|
|
|
onChange={this.onUseSystemThemeChanged}
|
2020-04-14 18:19:50 +03:00
|
|
|
/>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
let customThemeForm: JSX.Element;
|
2020-04-14 18:19:50 +03:00
|
|
|
if (SettingsStore.isFeatureEnabled("feature_custom_themes")) {
|
|
|
|
let messageElement = null;
|
|
|
|
if (this.state.customThemeMessage.text) {
|
|
|
|
if (this.state.customThemeMessage.isError) {
|
|
|
|
messageElement = <div className='text-error'>{this.state.customThemeMessage.text}</div>;
|
|
|
|
} else {
|
|
|
|
messageElement = <div className='text-success'>{this.state.customThemeMessage.text}</div>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
customThemeForm = (
|
|
|
|
<div className='mx_SettingsTab_section'>
|
2020-05-28 15:55:07 +03:00
|
|
|
<form onSubmit={this.onAddCustomTheme}>
|
2020-04-14 18:19:50 +03:00
|
|
|
<Field
|
|
|
|
label={_t("Custom theme URL")}
|
|
|
|
type='text'
|
|
|
|
id='mx_GeneralUserSettingsTab_customThemeInput'
|
|
|
|
autoComplete="off"
|
2020-05-28 15:55:07 +03:00
|
|
|
onChange={this.onCustomThemeChange}
|
2020-04-14 18:19:50 +03:00
|
|
|
value={this.state.customThemeUrl}
|
|
|
|
/>
|
|
|
|
<AccessibleButton
|
2020-05-28 15:55:07 +03:00
|
|
|
onClick={this.onAddCustomTheme}
|
2020-04-14 18:19:50 +03:00
|
|
|
type="submit" kind="primary_sm"
|
|
|
|
disabled={!this.state.customThemeUrl.trim()}
|
|
|
|
>{_t("Add theme")}</AccessibleButton>
|
|
|
|
{messageElement}
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
// XXX: replace any type here
|
|
|
|
const themes = Object.entries<any>(enumerateThemes())
|
2020-04-14 18:19:50 +03:00
|
|
|
.map(p => ({id: p[0], name: p[1]})); // convert pairs to objects for code readability
|
|
|
|
const builtInThemes = themes.filter(p => !p.id.startsWith("custom-"));
|
|
|
|
const customThemes = themes.filter(p => !builtInThemes.includes(p))
|
|
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
|
|
const orderedThemes = [...builtInThemes, ...customThemes];
|
|
|
|
return (
|
2020-04-21 14:03:32 +03:00
|
|
|
<div className="mx_SettingsTab_section mx_AppearanceUserSettingsTab_themeSection">
|
2020-04-14 18:19:50 +03:00
|
|
|
<span className="mx_SettingsTab_subheading">{_t("Theme")}</span>
|
|
|
|
{systemThemeSection}
|
2020-04-28 16:24:44 +03:00
|
|
|
<Field
|
|
|
|
id="theme" label={_t("Theme")} element="select"
|
2020-05-28 15:55:07 +03:00
|
|
|
value={this.state.theme} onChange={this.onThemeChange}
|
2020-04-28 16:24:44 +03:00
|
|
|
disabled={this.state.useSystemTheme}
|
2020-04-14 18:19:50 +03:00
|
|
|
>
|
|
|
|
{orderedThemes.map(theme => {
|
|
|
|
return <option key={theme.id} value={theme.id}>{theme.name}</option>;
|
|
|
|
})}
|
|
|
|
</Field>
|
|
|
|
{customThemeForm}
|
|
|
|
<SettingsFlag name="useCompactLayout" level={SettingLevel.ACCOUNT} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:55:07 +03:00
|
|
|
private renderFontSection() {
|
2020-04-23 14:09:08 +03:00
|
|
|
const SettingsFlag = sdk.getComponent("views.elements.SettingsFlag");
|
2020-04-21 14:03:32 +03:00
|
|
|
return <div className="mx_SettingsTab_section mx_AppearanceUserSettingsTab_fontScaling">
|
2020-04-14 18:19:50 +03:00
|
|
|
<span className="mx_SettingsTab_subheading">{_t("Font size")}</span>
|
2020-04-22 19:31:49 +03:00
|
|
|
<div className="mx_AppearanceUserSettingsTab_fontSlider">
|
|
|
|
<div className="mx_AppearanceUserSettingsTab_fontSlider_smallText">Aa</div>
|
|
|
|
<Slider
|
2020-05-14 00:08:29 +03:00
|
|
|
values={[13, 15, 16, 18, 20]}
|
2020-05-28 15:55:07 +03:00
|
|
|
value={parseInt(this.state.fontSize, 10)}
|
|
|
|
onSelectionChange={this.onFontSizeChanged}
|
|
|
|
displayFunc={value => ""}
|
2020-04-23 14:09:08 +03:00
|
|
|
disabled={this.state.useCustomFontSize}
|
2020-04-22 19:31:49 +03:00
|
|
|
/>
|
|
|
|
<div className="mx_AppearanceUserSettingsTab_fontSlider_largeText">Aa</div>
|
|
|
|
</div>
|
2020-04-23 14:09:08 +03:00
|
|
|
<SettingsFlag
|
|
|
|
name="useCustomFontSize"
|
|
|
|
level={SettingLevel.ACCOUNT}
|
2020-05-28 15:55:07 +03:00
|
|
|
onChange={(checked) => this.setState({useCustomFontSize: checked})}
|
2020-04-23 14:09:08 +03:00
|
|
|
/>
|
2020-04-14 18:19:50 +03:00
|
|
|
<Field
|
|
|
|
type="text"
|
|
|
|
label={_t("Font size")}
|
|
|
|
autoComplete="off"
|
2020-04-29 12:32:05 +03:00
|
|
|
placeholder={this.state.fontSize.toString()}
|
|
|
|
value={this.state.fontSize.toString()}
|
2020-04-14 18:19:50 +03:00
|
|
|
id="font_size_field"
|
2020-05-28 15:55:07 +03:00
|
|
|
onValidate={this.onValidateFontSize}
|
2020-04-29 12:32:05 +03:00
|
|
|
onChange={(value) => this.setState({fontSize: value.target.value})}
|
2020-04-23 14:09:08 +03:00
|
|
|
disabled={!this.state.useCustomFontSize}
|
2020-04-14 18:19:50 +03:00
|
|
|
/>
|
2020-04-16 13:56:43 +03:00
|
|
|
</div>;
|
2020-04-14 18:19:50 +03:00
|
|
|
}
|
2020-05-28 15:55:07 +03:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="mx_SettingsTab">
|
|
|
|
<div className="mx_SettingsTab_heading">{_t("Appearance")}</div>
|
|
|
|
{this.renderThemeSection()}
|
|
|
|
{SettingsStore.isFeatureEnabled("feature_font_scaling") ? this.renderFontSection() : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2020-04-14 18:19:50 +03:00
|
|
|
}
|