2021-10-19 14:50:09 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
|
Copyright 2021-2023 The Matrix.org Foundation C.I.C.
|
2021-10-19 14:50:09 +01:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-10-19 14:50:09 +01:00
|
|
|
*/
|
|
|
|
|
|
2024-02-21 12:23:07 +01:00
|
|
|
import React from "react";
|
2025-02-04 13:41:34 +00:00
|
|
|
import { EmptyObject } from "matrix-js-sdk/src/matrix";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2021-10-19 14:50:09 +01:00
|
|
|
import EventTilePreview from "../elements/EventTilePreview";
|
|
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2021-11-17 08:19:30 -07:00
|
|
|
import { Layout } from "../../../settings/enums/Layout";
|
2021-10-19 14:50:09 +01:00
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
|
|
|
|
import { SettingLevel } from "../../../settings/SettingLevel";
|
|
|
|
|
import { _t } from "../../../languageHandler";
|
2024-11-05 18:36:26 +00:00
|
|
|
import { SettingsSubsection } from "./shared/SettingsSubsection";
|
2024-02-21 12:23:07 +01:00
|
|
|
import Field from "../elements/Field";
|
|
|
|
|
import { FontWatcher } from "../../../settings/watchers/FontWatcher";
|
2021-10-19 14:50:09 +01:00
|
|
|
|
|
|
|
|
interface IState {
|
2024-02-21 12:23:07 +01:00
|
|
|
browserFontSize: number;
|
2021-10-19 14:50:09 +01:00
|
|
|
// String displaying the current selected fontSize.
|
2024-02-21 12:23:07 +01:00
|
|
|
// Needs to be string for things like '1.' without
|
2021-10-19 14:50:09 +01:00
|
|
|
// trailing 0s.
|
2024-02-21 12:23:07 +01:00
|
|
|
fontSizeDelta: number;
|
2021-10-19 14:50:09 +01:00
|
|
|
useCustomFontSize: boolean;
|
|
|
|
|
layout: Layout;
|
|
|
|
|
// User profile data for the message preview
|
|
|
|
|
userId?: string;
|
2023-02-24 15:28:40 +00:00
|
|
|
displayName?: string;
|
|
|
|
|
avatarUrl?: string;
|
2021-10-19 14:50:09 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-04 13:41:34 +00:00
|
|
|
export default class FontScalingPanel extends React.Component<EmptyObject, IState> {
|
2023-09-19 17:16:38 +01:00
|
|
|
private readonly MESSAGE_PREVIEW_TEXT = _t("common|preview_message");
|
2024-02-21 12:23:07 +01:00
|
|
|
/**
|
|
|
|
|
* Font sizes available (in px)
|
|
|
|
|
*/
|
|
|
|
|
private readonly sizes = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36];
|
2023-09-22 21:42:48 +05:30
|
|
|
private layoutWatcherRef?: string;
|
2021-10-19 14:50:09 +01:00
|
|
|
private unmounted = false;
|
|
|
|
|
|
2025-02-04 13:41:34 +00:00
|
|
|
public constructor(props: EmptyObject) {
|
2021-10-19 14:50:09 +01:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2024-12-23 20:25:15 +00:00
|
|
|
fontSizeDelta: SettingsStore.getValue("fontSizeDelta", null),
|
2024-02-21 12:23:07 +01:00
|
|
|
browserFontSize: FontWatcher.getBrowserDefaultFontSize(),
|
2021-10-19 14:50:09 +01:00
|
|
|
useCustomFontSize: SettingsStore.getValue("useCustomFontSize"),
|
|
|
|
|
layout: SettingsStore.getValue("layout"),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public async componentDidMount(): Promise<void> {
|
2024-11-01 17:39:08 +00:00
|
|
|
this.unmounted = false;
|
2021-10-19 14:50:09 +01:00
|
|
|
// Fetch the current user profile for the message preview
|
2023-06-15 08:46:19 +01:00
|
|
|
const client = MatrixClientPeg.safeGet();
|
|
|
|
|
const userId = client.getSafeUserId();
|
2021-10-19 14:50:09 +01:00
|
|
|
const profileInfo = await client.getProfileInfo(userId);
|
2023-09-22 21:42:48 +05:30
|
|
|
this.layoutWatcherRef = SettingsStore.watchSetting("layout", null, () => {
|
|
|
|
|
// Update the layout for the preview window according to the user selection
|
|
|
|
|
const value = SettingsStore.getValue("layout");
|
|
|
|
|
if (this.state.layout !== value) {
|
|
|
|
|
this.setState({
|
|
|
|
|
layout: value,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-10-19 14:50:09 +01:00
|
|
|
if (this.unmounted) return;
|
|
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
|
userId,
|
|
|
|
|
displayName: profileInfo.displayname,
|
|
|
|
|
avatarUrl: profileInfo.avatar_url,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public componentWillUnmount(): void {
|
2021-10-19 14:50:09 +01:00
|
|
|
this.unmounted = true;
|
2024-11-01 15:15:04 +00:00
|
|
|
SettingsStore.unwatchSetting(this.layoutWatcherRef);
|
2021-10-19 14:50:09 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-21 12:23:07 +01:00
|
|
|
/**
|
|
|
|
|
* Save the new font size
|
|
|
|
|
* @param delta
|
|
|
|
|
*/
|
|
|
|
|
private onFontSizeChanged = async (delta: string): Promise<void> => {
|
|
|
|
|
const parsedDelta = parseInt(delta, 10) || 0;
|
|
|
|
|
this.setState({ fontSizeDelta: parsedDelta });
|
|
|
|
|
await SettingsStore.setValue("fontSizeDelta", null, SettingLevel.DEVICE, parsedDelta);
|
2021-10-19 14:50:09 +01:00
|
|
|
};
|
|
|
|
|
|
2024-02-21 12:23:07 +01:00
|
|
|
/**
|
|
|
|
|
* Compute the difference between the selected font size and the browser font size
|
|
|
|
|
* @param fontSize
|
|
|
|
|
*/
|
|
|
|
|
private computeDeltaFontSize = (fontSize: number): number => {
|
|
|
|
|
return fontSize - this.state.browserFontSize;
|
2021-10-19 14:50:09 +01:00
|
|
|
};
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2021-10-19 14:50:09 +01:00
|
|
|
return (
|
2023-09-08 09:58:31 +01:00
|
|
|
<SettingsSubsection
|
|
|
|
|
heading={_t("settings|appearance|font_size")}
|
|
|
|
|
stretchContent
|
|
|
|
|
data-testid="mx_FontScalingPanel"
|
|
|
|
|
>
|
2024-02-21 12:23:07 +01:00
|
|
|
<Field
|
|
|
|
|
element="select"
|
|
|
|
|
className="mx_FontScalingPanel_Dropdown"
|
|
|
|
|
label={_t("settings|appearance|font_size")}
|
|
|
|
|
value={this.state.fontSizeDelta.toString()}
|
|
|
|
|
onChange={(e) => this.onFontSizeChanged(e.target.value)}
|
|
|
|
|
>
|
|
|
|
|
{this.sizes.map((size) => (
|
|
|
|
|
<option key={size} value={this.computeDeltaFontSize(size)}>
|
|
|
|
|
{size === this.state.browserFontSize
|
|
|
|
|
? _t("settings|appearance|font_size_default", { fontSize: size })
|
|
|
|
|
: size}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</Field>
|
2021-10-19 14:50:09 +01:00
|
|
|
<EventTilePreview
|
2022-06-25 12:20:01 +00:00
|
|
|
className="mx_FontScalingPanel_preview"
|
2021-10-19 14:50:09 +01:00
|
|
|
message={this.MESSAGE_PREVIEW_TEXT}
|
|
|
|
|
layout={this.state.layout}
|
|
|
|
|
userId={this.state.userId}
|
|
|
|
|
displayName={this.state.displayName}
|
|
|
|
|
avatarUrl={this.state.avatarUrl}
|
2022-12-12 12:24:14 +01:00
|
|
|
/>
|
2023-05-22 09:12:11 +12:00
|
|
|
</SettingsSubsection>
|
2022-12-12 12:24:14 +01:00
|
|
|
);
|
2021-10-19 14:50:09 +01:00
|
|
|
}
|
|
|
|
|
}
|