Skip to content

Commit 35d3e51

Browse files
authored
Expand support for MSC2873 (changing theme/language) (#117)
MSC2873 specifies some actions for updating the widget's theme/language after it's loaded, and I'd like to make use of them.
1 parent bbff7c1 commit 35d3e51

File tree

6 files changed

+99
-1
lines changed

6 files changed

+99
-1
lines changed

src/ClientWidgetApi.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ import {
102102
IDownloadFileActionFromWidgetActionRequest,
103103
IDownloadFileActionFromWidgetResponseData,
104104
} from "./interfaces/DownloadFileAction";
105+
import { IThemeChangeActionRequestData } from "./interfaces/ThemeChangeAction";
105106

106107
/**
107108
* API handler for the client side of widgets. This raises events
@@ -896,6 +897,22 @@ export class ClientWidgetApi extends EventEmitter {
896897
}
897898
}
898899

900+
/**
901+
* Informs the widget that the client's theme has changed.
902+
* @param theme The theme data, as an object with arbitrary contents.
903+
*/
904+
public updateTheme(theme: IThemeChangeActionRequestData): Promise<IWidgetApiResponseData> {
905+
return this.transport.send(WidgetApiToWidgetAction.ThemeChange, theme);
906+
}
907+
908+
/**
909+
* Informs the widget that the client's language has changed.
910+
* @param lang The BCP 47 identifier representing the client's current language.
911+
*/
912+
public updateLanguage(lang: string): Promise<IWidgetApiResponseData> {
913+
return this.transport.send(WidgetApiToWidgetAction.LanguageChange, { lang });
914+
}
915+
899916
/**
900917
* Takes a screenshot of the widget.
901918
* @returns Resolves to the widget's screenshot.

src/interfaces/ApiVersion.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export enum MatrixApiVersion {
2323
export enum UnstableApiVersion {
2424
MSC2762 = "org.matrix.msc2762",
2525
MSC2871 = "org.matrix.msc2871",
26+
MSC2873 = "org.matrix.msc2873",
2627
MSC2931 = "org.matrix.msc2931",
2728
MSC2974 = "org.matrix.msc2974",
2829
MSC2876 = "org.matrix.msc2876",
@@ -41,6 +42,7 @@ export const CurrentApiVersions: ApiVersion[] = [
4142
//MatrixApiVersion.V010,
4243
UnstableApiVersion.MSC2762,
4344
UnstableApiVersion.MSC2871,
45+
UnstableApiVersion.MSC2873,
4446
UnstableApiVersion.MSC2931,
4547
UnstableApiVersion.MSC2974,
4648
UnstableApiVersion.MSC2876,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2024 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { IWidgetApiRequest, IWidgetApiRequestData } from "./IWidgetApiRequest";
18+
import { WidgetApiToWidgetAction } from "./WidgetApiAction";
19+
import { IWidgetApiAcknowledgeResponseData } from "./IWidgetApiResponse";
20+
21+
export interface ILanguageChangeActionRequestData extends IWidgetApiRequestData {
22+
/**
23+
* The BCP 47 identifier for the client's current language.
24+
*/
25+
lang: string;
26+
}
27+
28+
export interface ILanguageChangeActionRequest extends IWidgetApiRequest {
29+
action: WidgetApiToWidgetAction.LanguageChange;
30+
data: ILanguageChangeActionRequestData;
31+
}
32+
33+
export interface ILanguageChangeActionResponse extends ILanguageChangeActionRequest {
34+
response: IWidgetApiAcknowledgeResponseData;
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2024 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { IWidgetApiRequest, IWidgetApiRequestData } from "./IWidgetApiRequest";
18+
import { WidgetApiToWidgetAction } from "./WidgetApiAction";
19+
import { IWidgetApiAcknowledgeResponseData } from "./IWidgetApiResponse";
20+
21+
export interface IThemeChangeActionRequestData extends IWidgetApiRequestData {
22+
// The format of a theme is deliberately unstandardized
23+
}
24+
25+
export interface IThemeChangeActionRequest extends IWidgetApiRequest {
26+
action: WidgetApiToWidgetAction.ThemeChange;
27+
data: IThemeChangeActionRequestData;
28+
}
29+
30+
export interface IThemeChangeActionResponse extends IThemeChangeActionRequest {
31+
response: IWidgetApiAcknowledgeResponseData;
32+
}

src/interfaces/WidgetApiAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export enum WidgetApiToWidgetAction {
1818
SupportedApiVersions = "supported_api_versions",
1919
Capabilities = "capabilities",
2020
NotifyCapabilities = "notify_capabilities",
21+
ThemeChange = "theme_change",
22+
LanguageChange = "language_change",
2123
TakeScreenshot = "screenshot",
2224
UpdateVisibility = "visibility",
2325
OpenIDCredentials = "openid_credentials",

test/ClientWidgetApi-test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { IWidgetApiRequest } from '../src/interfaces/IWidgetApiRequest';
2525
import { IReadRelationsFromWidgetActionRequest } from '../src/interfaces/ReadRelationsAction';
2626
import { ISupportedVersionsActionRequest } from '../src/interfaces/SupportedVersionsAction';
2727
import { IUserDirectorySearchFromWidgetActionRequest } from '../src/interfaces/UserDirectorySearchAction';
28-
import { WidgetApiFromWidgetAction } from '../src/interfaces/WidgetApiAction';
28+
import { WidgetApiFromWidgetAction, WidgetApiToWidgetAction } from '../src/interfaces/WidgetApiAction';
2929
import { WidgetApiDirection } from '../src/interfaces/WidgetApiDirection';
3030
import { Widget } from '../src/models/Widget';
3131
import { PostmessageTransport } from '../src/transport/PostmessageTransport';
@@ -2194,4 +2194,14 @@ describe('ClientWidgetApi', () => {
21942194
});
21952195
});
21962196
});
2197+
2198+
it('updates theme', () => {
2199+
clientWidgetApi.updateTheme({ name: 'dark' });
2200+
expect(transport.send).toHaveBeenCalledWith(WidgetApiToWidgetAction.ThemeChange, { name: 'dark' });
2201+
});
2202+
2203+
it('updates language', () => {
2204+
clientWidgetApi.updateLanguage('tlh');
2205+
expect(transport.send).toHaveBeenCalledWith(WidgetApiToWidgetAction.LanguageChange, { lang: 'tlh' });
2206+
});
21972207
});

0 commit comments

Comments
 (0)