Files
ThreadNet-Web/src/components/views/settings/tabs/user/AppearanceUserSettingsTab.tsx
T

243 lines
8.5 KiB
TypeScript
Raw Normal View History

2020-04-14 16:19:50 +01:00
/*
Copyright 2019 New Vector Ltd
2021-08-06 10:40:26 +02:00
Copyright 2019 - 2021 The Matrix.org Foundation C.I.C.
2020-04-14 16:19:50 +01: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';
2021-06-29 13:11:58 +01:00
import { _t } from "../../../../../languageHandler";
2020-07-10 19:07:11 +01:00
import SdkConfig from "../../../../../SdkConfig";
2021-02-26 16:02:46 -05:00
import { MatrixClientPeg } from '../../../../../MatrixClientPeg';
import SettingsStore from "../../../../../settings/SettingsStore";
2020-06-18 13:58:35 +01:00
import StyledCheckbox from '../../../elements/StyledCheckbox';
import SettingsFlag from '../../../elements/SettingsFlag';
import Field from '../../../elements/Field';
import { SettingLevel } from "../../../../../settings/SettingLevel";
import { UIFeature } from "../../../../../settings/UIFeature";
import { Layout } from "../../../../../settings/Layout";
import { replaceableComponent } from "../../../../../utils/replaceableComponent";
import LayoutSwitcher from "../../LayoutSwitcher";
2020-04-14 16:19:50 +01:00
import FontScalingPanel from '../../FontScalingPanel';
import ThemeChoicePanel from '../../ThemeChoicePanel';
2021-10-15 16:30:53 +02:00
2020-05-28 13:55:07 +01:00
interface IProps {
}
2020-05-28 14:43:01 +01:00
interface IThemeState {
2020-06-18 14:32:43 +01:00
theme: string;
useSystemTheme: boolean;
2020-05-28 14:43:01 +01:00
}
export interface CustomThemeMessage {
2020-06-18 14:32:43 +01:00
isError: boolean;
text: string;
}
2020-05-28 14:43:01 +01:00
2020-05-28 13:55:07 +01:00
interface IState extends IThemeState {
2020-06-22 11:39:11 +01:00
customThemeUrl: string;
customThemeMessage: CustomThemeMessage;
useSystemFont: boolean;
systemFont: string;
showAdvanced: boolean;
2021-01-22 13:44:45 +01:00
layout: Layout;
2021-02-26 16:02:46 -05:00
// User profile data for the message preview
2021-08-27 19:32:38 +02:00
userId?: string;
2021-02-26 16:02:46 -05:00
displayName: string;
avatarUrl: string;
2020-05-28 13:55:07 +01:00
}
@replaceableComponent("views.settings.tabs.user.AppearanceUserSettingsTab")
2020-05-28 13:55:07 +01:00
export default class AppearanceUserSettingsTab extends React.Component<IProps, IState> {
2020-06-18 15:47:26 +01:00
private readonly MESSAGE_PREVIEW_TEXT = _t("Hey you. You're the best!");
2020-05-28 13:55:07 +01:00
private unmounted = false;
2020-05-28 13:55:07 +01:00
constructor(props: IProps) {
super(props);
2020-04-14 16:19:50 +01:00
this.state = {
2020-05-28 13:55:07 +01:00
...this.calculateThemeState(),
2020-04-14 16:19:50 +01:00
customThemeUrl: "",
2021-06-29 13:11:58 +01:00
customThemeMessage: { isError: false, text: "" },
2020-06-15 15:33:52 +01:00
useSystemFont: SettingsStore.getValue("useSystemFont"),
systemFont: SettingsStore.getValue("systemFont"),
showAdvanced: false,
2021-01-22 13:44:45 +01:00
layout: SettingsStore.getValue("layout"),
userId: null,
displayName: null,
2021-02-26 16:02:46 -05:00
avatarUrl: null,
2020-04-16 11:56:43 +01:00
};
2020-04-14 16:19:50 +01:00
}
2021-02-26 16:02:46 -05:00
async componentDidMount() {
// Fetch the current user profile for the message preview
const client = MatrixClientPeg.get();
const userId = client.getUserId();
const profileInfo = await client.getProfileInfo(userId);
if (this.unmounted) return;
2021-02-26 16:02:46 -05:00
this.setState({
userId,
displayName: profileInfo.displayname,
avatarUrl: profileInfo.avatar_url,
});
}
componentWillUnmount() {
this.unmounted = true;
}
2020-05-28 13:55:07 +01:00
private calculateThemeState(): IThemeState {
2020-04-14 16:19:50 +01:00
// We have to mirror the logic from ThemeWatcher.getEffectiveTheme so we
// show the right values for things.
const themeChoice: string = SettingsStore.getValue("theme");
2020-05-28 13:55:07 +01:00
const systemThemeExplicit: boolean = SettingsStore.getValueAt(
2020-04-14 16:19:50 +01:00
SettingLevel.DEVICE, "use_system_theme", null, false, true);
2020-05-28 13:55:07 +01:00
const themeExplicit: string = SettingsStore.getValueAt(
2020-04-14 16:19:50 +01: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"),
};
}
private onLayoutChanged = (layout: Layout): void => {
2021-06-17 14:11:44 +01:00
this.setState({ layout: layout });
};
2021-01-22 13:44:45 +01:00
private onIRCLayoutChange = (enabled: boolean) => {
if (enabled) {
2021-06-29 13:11:58 +01:00
this.setState({ layout: Layout.IRC });
2021-01-22 13:44:45 +01:00
SettingsStore.setValue("layout", null, SettingLevel.DEVICE, Layout.IRC);
} else {
2021-06-29 13:11:58 +01:00
this.setState({ layout: Layout.Group });
2021-01-22 13:44:45 +01:00
SettingsStore.setValue("layout", null, SettingLevel.DEVICE, Layout.Group);
}
2021-06-29 13:11:58 +01:00
};
2021-01-22 13:44:45 +01:00
private renderAdvancedSection() {
2020-09-18 11:34:35 +01:00
if (!SettingsStore.getValue(UIFeature.AdvancedSettings)) return null;
2020-07-14 18:04:29 +01:00
const brand = SdkConfig.get().brand;
2020-06-15 15:33:52 +01:00
const toggle = <div
className="mx_AppearanceUserSettingsTab_AdvancedToggle"
2021-06-29 13:11:58 +01:00
onClick={() => this.setState({ showAdvanced: !this.state.showAdvanced })}
2020-06-15 15:33:52 +01:00
>
{ this.state.showAdvanced ? _t("Hide advanced") : _t("Show advanced") }
2020-06-15 15:33:52 +01:00
</div>;
let advanced: React.ReactNode;
if (this.state.showAdvanced) {
2020-07-14 18:04:29 +01:00
const tooltipContent = _t(
"Set the name of a font installed on your system & %(brand)s will attempt to use it.",
{ brand },
);
2020-06-24 16:06:50 +01:00
advanced = <>
<SettingsFlag
name="useCompactLayout"
level={SettingLevel.DEVICE}
useCheckbox={true}
2021-06-17 14:11:44 +01:00
disabled={this.state.layout !== Layout.Group}
2020-07-09 15:54:44 +01:00
/>
2021-06-17 14:11:44 +01:00
{ !SettingsStore.getValue("feature_new_layout_switcher") ?
<StyledCheckbox
checked={this.state.layout == Layout.IRC}
onChange={(ev) => this.onIRCLayoutChange(ev.target.checked)}
>
2021-07-20 11:08:13 +01:00
{ _t("Enable experimental, compact IRC style layout") }
2021-06-17 14:11:44 +01:00
</StyledCheckbox> : null
}
2021-01-22 13:44:45 +01:00
2020-06-15 15:33:52 +01:00
<SettingsFlag
name="useSystemFont"
level={SettingLevel.DEVICE}
useCheckbox={true}
2021-06-29 13:11:58 +01:00
onChange={(checked) => this.setState({ useSystemFont: checked })}
2020-06-15 15:33:52 +01:00
/>
<Field
className="mx_AppearanceUserSettingsTab_systemFont"
label={SettingsStore.getDisplayName("systemFont")}
onChange={(value) => {
this.setState({
systemFont: value.target.value,
2020-06-22 11:39:11 +01:00
});
2020-06-15 15:33:52 +01:00
SettingsStore.setValue("systemFont", null, SettingLevel.DEVICE, value.target.value);
}}
2020-07-14 18:04:29 +01:00
tooltipContent={tooltipContent}
2020-06-15 17:42:30 +01:00
forceTooltipVisible={true}
2020-06-15 15:33:52 +01:00
disabled={!this.state.useSystemFont}
value={this.state.systemFont}
/>
2020-06-24 16:06:50 +01:00
</>;
2020-06-15 15:33:52 +01:00
}
return <div className="mx_SettingsTab_section mx_AppearanceUserSettingsTab_Advanced">
{ toggle }
{ advanced }
2020-06-22 11:39:11 +01:00
</div>;
2020-06-15 15:33:52 +01:00
}
2020-05-28 13:55:07 +01:00
render() {
2020-07-10 19:07:11 +01:00
const brand = SdkConfig.get().brand;
let layoutSection;
if (SettingsStore.getValue("feature_new_layout_switcher")) {
layoutSection = (
<LayoutSwitcher
userId={this.state.userId}
displayName={this.state.displayName}
avatarUrl={this.state.avatarUrl}
messagePreviewText={this.MESSAGE_PREVIEW_TEXT}
onLayoutChanged={this.onLayoutChanged}
/>
);
}
2020-05-28 13:55:07 +01:00
return (
<div className="mx_SettingsTab mx_AppearanceUserSettingsTab">
<div className="mx_SettingsTab_heading">{ _t("Customise your appearance") }</div>
<div className="mx_SettingsTab_SubHeading">
{ _t("Appearance Settings only affect this %(brand)s session.", { brand }) }
</div>
<ThemeChoicePanel />
{ layoutSection }
<FontScalingPanel />
{ this.renderAdvancedSection() }
2020-05-28 13:55:07 +01:00
</div>
);
}
2020-04-14 16:19:50 +01:00
}