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

Commit d75e2f1

Browse files
authored
Fix font not resetting when logging out (#8670)
* Fix font not resetting when logging out * Adopt on_logged_in and on_logged_out into DispatcherAction * Add tests * Add copyright
1 parent f3b762c commit d75e2f1

File tree

11 files changed

+97
-39
lines changed

11 files changed

+97
-39
lines changed

src/BasePlatform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default abstract class BasePlatform {
7070
protected onAction = (payload: ActionPayload) => {
7171
switch (payload.action) {
7272
case 'on_client_not_viable':
73-
case 'on_logged_out':
73+
case Action.OnLoggedOut:
7474
this.setNotificationCount(0);
7575
break;
7676
}

src/DeviceListener.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export default class DeviceListener {
192192
};
193193

194194
private onAction = ({ action }: ActionPayload) => {
195-
if (action !== "on_logged_in") return;
195+
if (action !== Action.OnLoggedIn) return;
196196
this.recheck();
197197
};
198198

src/Lifecycle.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ async function doSetLoggedIn(
632632
logger.warn("No local storage available: can't persist session!");
633633
}
634634

635-
dis.dispatch({ action: 'on_logged_in' });
635+
dis.fire(Action.OnLoggedIn);
636636

637637
await startMatrixClient(/*startSyncing=*/!softLogout);
638638
return client;
@@ -857,15 +857,15 @@ async function startMatrixClient(startSyncing = true): Promise<void> {
857857
*/
858858
export async function onLoggedOut(): Promise<void> {
859859
_isLoggingOut = false;
860-
// Ensure that we dispatch a view change **before** stopping the client so
860+
// Ensure that we dispatch a view change **before** stopping the client,
861861
// so that React components unmount first. This avoids React soft crashes
862862
// that can occur when components try to use a null client.
863-
dis.dispatch({ action: 'on_logged_out' }, true);
863+
dis.fire(Action.OnLoggedOut, true);
864864
stopMatrixClient();
865865
await clearStorage({ deleteEverything: true });
866866
LifecycleCustomisations.onLoggedOutAndStorageCleared?.();
867867

868-
// Do this last so we can make sure all storage has been cleared and all
868+
// Do this last, so we can make sure all storage has been cleared and all
869869
// customisations got the memo.
870870
if (SdkConfig.get().logout_redirect_url) {
871871
logger.log("Redirecting to external provider to finish logout");

src/components/structures/MatrixChat.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
743743
case Action.OpenDialPad:
744744
Modal.createTrackedDialog('Dial pad', '', DialPadModal, {}, "mx_Dialog_dialPadWrapper");
745745
break;
746-
case 'on_logged_in':
746+
case Action.OnLoggedIn:
747747
if (
748748
// Skip this handling for token login as that always calls onLoggedIn itself
749749
!this.tokenLogin &&
@@ -759,7 +759,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
759759
case 'on_client_not_viable':
760760
this.onSoftLogout();
761761
break;
762-
case 'on_logged_out':
762+
case Action.OnLoggedOut:
763763
this.onLoggedOut();
764764
break;
765765
case 'will_start_client':

src/dispatcher/actions.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,15 @@ export enum Action {
317317
/**
318318
* Show current room topic
319319
*/
320-
ShowRoomTopic = "show_room_topic"
320+
ShowRoomTopic = "show_room_topic",
321+
322+
/**
323+
* Fired when the client was logged out. No additional payload information required.
324+
*/
325+
OnLoggedOut = "on_logged_out",
326+
327+
/**
328+
* Fired when the client was logged in. No additional payload information required.
329+
*/
330+
OnLoggedIn = "on_logged_in",
321331
}

src/settings/Settings.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { ImageSize } from "./enums/ImageSize";
4242
import { MetaSpace } from "../stores/spaces";
4343
import SdkConfig from "../SdkConfig";
4444
import ThreadBetaController from './controllers/ThreadBetaController';
45+
import { FontWatcher } from "./watchers/FontWatcher";
4546

4647
// These are just a bunch of helper arrays to avoid copy/pasting a bunch of times
4748
const LEVELS_ROOM_SETTINGS = [
@@ -420,7 +421,7 @@ export const SETTINGS: {[setting: string]: ISetting} = {
420421
"baseFontSize": {
421422
displayName: _td("Font size"),
422423
supportedLevels: LEVELS_ACCOUNT_SETTINGS,
423-
default: 10,
424+
default: FontWatcher.DEFAULT_SIZE,
424425
controller: new FontSizeController(),
425426
},
426427
"useCustomFontSize": {

src/settings/watchers/FontWatcher.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ import IWatcher from "./Watcher";
2020
import { toPx } from '../../utils/units';
2121
import { Action } from '../../dispatcher/actions';
2222
import { SettingLevel } from "../SettingLevel";
23+
import { UpdateSystemFontPayload } from "../../dispatcher/payloads/UpdateSystemFontPayload";
24+
import { ActionPayload } from "../../dispatcher/payloads";
2325

2426
export class FontWatcher implements IWatcher {
2527
public static readonly MIN_SIZE = 8;
28+
public static readonly DEFAULT_SIZE = 10;
2629
public static readonly MAX_SIZE = 15;
2730
// Externally we tell the user the font is size 15. Internally we use 10.
2831
public static readonly SIZE_DIFF = 5;
@@ -34,27 +37,41 @@ export class FontWatcher implements IWatcher {
3437
}
3538

3639
public start() {
37-
this.setRootFontSize(SettingsStore.getValue("baseFontSize"));
38-
this.setSystemFont({
39-
useSystemFont: SettingsStore.getValue("useSystemFont"),
40-
font: SettingsStore.getValue("systemFont"),
41-
});
40+
this.updateFont();
4241
this.dispatcherRef = dis.register(this.onAction);
4342
}
4443

4544
public stop() {
4645
dis.unregister(this.dispatcherRef);
4746
}
4847

49-
private onAction = (payload) => {
48+
private updateFont() {
49+
this.setRootFontSize(SettingsStore.getValue("baseFontSize"));
50+
this.setSystemFont({
51+
useSystemFont: SettingsStore.getValue("useSystemFont"),
52+
font: SettingsStore.getValue("systemFont"),
53+
});
54+
}
55+
56+
private onAction = (payload: ActionPayload) => {
5057
if (payload.action === Action.UpdateFontSize) {
5158
this.setRootFontSize(payload.size);
5259
} else if (payload.action === Action.UpdateSystemFont) {
53-
this.setSystemFont(payload);
60+
this.setSystemFont(payload as UpdateSystemFontPayload);
61+
} else if (payload.action === Action.OnLoggedOut) {
62+
// Clear font overrides when logging out
63+
this.setRootFontSize(FontWatcher.DEFAULT_SIZE);
64+
this.setSystemFont({
65+
useSystemFont: false,
66+
font: "",
67+
});
68+
} else if (payload.action === Action.OnLoggedIn) {
69+
// Font size can be saved on the account, so grab value when logging in
70+
this.updateFont();
5471
}
5572
};
5673

57-
private setRootFontSize = (size) => {
74+
private setRootFontSize = (size: number) => {
5875
const fontSize = Math.max(Math.min(FontWatcher.MAX_SIZE, size), FontWatcher.MIN_SIZE);
5976

6077
if (fontSize !== size) {
@@ -63,7 +80,7 @@ export class FontWatcher implements IWatcher {
6380
document.querySelector<HTMLElement>(":root").style.fontSize = toPx(fontSize);
6481
};
6582

66-
private setSystemFont = ({ useSystemFont, font }) => {
83+
private setSystemFont = ({ useSystemFont, font }: Pick<UpdateSystemFontPayload, "useSystemFont" | "font">) => {
6784
if (useSystemFont) {
6885
// Make sure that fonts with spaces in their names get interpreted properly
6986
document.body.style.fontFamily = font

src/stores/LifecycleStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class LifecycleStore extends Store<ActionPayload> {
7171
break;
7272
}
7373
case 'on_client_not_viable':
74-
case 'on_logged_out':
74+
case Action.OnLoggedOut:
7575
this.reset();
7676
break;
7777
}

src/stores/ReadyWatchingStore.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { EventEmitter } from "events";
2222
import { MatrixClientPeg } from "../MatrixClientPeg";
2323
import { ActionPayload } from "../dispatcher/payloads";
2424
import { IDestroyable } from "../utils/IDestroyable";
25+
import { Action } from "../dispatcher/actions";
2526

2627
export abstract class ReadyWatchingStore extends EventEmitter implements IDestroyable {
2728
protected matrixClient: MatrixClient;
@@ -83,7 +84,7 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
8384
this.matrixClient = payload.matrixClient;
8485
await this.onReady();
8586
}
86-
} else if (payload.action === 'on_client_not_viable' || payload.action === 'on_logged_out') {
87+
} else if (payload.action === 'on_client_not_viable' || payload.action === Action.OnLoggedOut) {
8788
if (this.matrixClient) {
8889
await this.onNotReady();
8990
this.matrixClient = null;

src/stores/RoomViewStore.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export class RoomViewStore extends Store<ActionPayload> {
241241
break;
242242
}
243243
case 'on_client_not_viable':
244-
case 'on_logged_out':
244+
case Action.OnLoggedOut:
245245
this.reset();
246246
break;
247247
case 'reply_to_event':
Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2022 r00ster91 <[email protected]>
3+
Copyright 2022 The Matrix.org Foundation C.I.C.
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -21,34 +22,62 @@ import { SettingLevel } from '../../../src/settings/SettingLevel';
2122
import { FontWatcher } from "../../../src/settings/watchers/FontWatcher";
2223
import { Action } from "../../../src/dispatcher/actions";
2324
import { untilDispatch } from "../../test-utils";
25+
import defaultDispatcher from "../../../src/dispatcher/dispatcher";
2426

2527
async function setSystemFont(font: string): Promise<void> {
28+
await SettingsStore.setValue("useSystemFont", null, SettingLevel.DEVICE, !!font);
2629
await SettingsStore.setValue("systemFont", null, SettingLevel.DEVICE, font);
2730
await untilDispatch(Action.UpdateSystemFont);
2831
await sleep(1); // await the FontWatcher doing its action
2932
}
3033

3134
describe('FontWatcher', function() {
32-
let fontWatcher: FontWatcher;
33-
beforeEach(() => {
34-
fontWatcher = new FontWatcher();
35-
fontWatcher.start();
36-
return SettingsStore.setValue("useSystemFont", null, SettingLevel.DEVICE, true);
37-
});
38-
afterEach(() => {
39-
fontWatcher.stop();
35+
it("should load font on start()", async () => {
36+
const watcher = new FontWatcher();
37+
await setSystemFont("Font Name");
38+
expect(document.body.style.fontFamily).toBe("");
39+
watcher.start();
40+
expect(document.body.style.fontFamily).toBe('"Font Name"');
4041
});
4142

42-
it('encloses the fonts by double quotes and sets them as the system font', async () => {
43-
await setSystemFont("Fira Sans Thin, Commodore 64");
44-
expect(document.body.style.fontFamily).toBe(`"Fira Sans Thin","Commodore 64"`);
43+
it("should load font on Action.OnLoggedIn", async () => {
44+
await setSystemFont("Font Name");
45+
new FontWatcher().start();
46+
document.body.style.fontFamily = ""; // clear the fontFamily which was set by start which we tested already
47+
defaultDispatcher.fire(Action.OnLoggedIn, true);
48+
expect(document.body.style.fontFamily).toBe('"Font Name"');
4549
});
46-
it('does not add double quotes if already present and sets the font as the system font', async () => {
47-
await setSystemFont(`"Commodore 64"`);
48-
expect(document.body.style.fontFamily).toBe(`"Commodore 64"`);
50+
51+
it("should reset font on Action.OnLoggedOut", async () => {
52+
await setSystemFont("Font Name");
53+
const watcher = new FontWatcher();
54+
watcher.start();
55+
expect(document.body.style.fontFamily).toBe('"Font Name"');
56+
defaultDispatcher.fire(Action.OnLoggedOut, true);
57+
expect(document.body.style.fontFamily).toBe("");
4958
});
50-
it('trims whitespace, encloses the fonts by double quotes, and sets them as the system font', async () => {
51-
await setSystemFont(` Fira Code , "Commodore 64" `);
52-
expect(document.body.style.fontFamily).toBe(`"Fira Code","Commodore 64"`);
59+
60+
describe("Sets font as expected", () => {
61+
let fontWatcher: FontWatcher;
62+
beforeEach(() => {
63+
fontWatcher = new FontWatcher();
64+
fontWatcher.start();
65+
});
66+
afterEach(() => {
67+
fontWatcher.stop();
68+
});
69+
70+
it('encloses the fonts by double quotes and sets them as the system font', async () => {
71+
await setSystemFont("Fira Sans Thin, Commodore 64");
72+
expect(document.body.style.fontFamily).toBe(`"Fira Sans Thin","Commodore 64"`);
73+
});
74+
it('does not add double quotes if already present and sets the font as the system font', async () => {
75+
await setSystemFont(`"Commodore 64"`);
76+
expect(document.body.style.fontFamily).toBe(`"Commodore 64"`);
77+
});
78+
it('trims whitespace, encloses the fonts by double quotes, and sets them as the system font', async () => {
79+
await setSystemFont(` Fira Code , "Commodore 64" `);
80+
expect(document.body.style.fontFamily).toBe(`"Fira Code","Commodore 64"`);
81+
});
5382
});
5483
});

0 commit comments

Comments
 (0)