Skip to content

Commit eaa20a2

Browse files
authored
Add mechanism for Electron to render toasts (#30765)
* Add mechanism for Electron to render toasts Signed-off-by: Michael Telatynski <[email protected]> * Tests Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 0747c9f commit eaa20a2

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/@types/global.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ type ElectronChannel =
6868
| "openDesktopCapturerSourcePicker"
6969
| "userAccessToken"
7070
| "homeserverUrl"
71-
| "serverSupportedVersions";
71+
| "serverSupportedVersions"
72+
| "showToast";
7273

7374
declare global {
7475
// use `number` as the return type in all cases for globalThis.set{Interval,Timeout},

src/vector/platform/ElectronPlatform.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from "matrix-js-sdk/src/matrix";
1919
import React from "react";
2020
import { logger } from "matrix-js-sdk/src/logger";
21+
import { uniqueId } from "lodash";
2122

2223
import BasePlatform, { UpdateCheckStatus, type UpdateStatus } from "../../BasePlatform";
2324
import type BaseEventIndexManager from "../../indexing/BaseEventIndexManager";
@@ -43,6 +44,7 @@ import { SeshatIndexManager } from "./SeshatIndexManager";
4344
import { IPCManager } from "./IPCManager";
4445
import { _t } from "../../languageHandler";
4546
import { BadgeOverlayRenderer } from "../../favicon";
47+
import GenericToast from "../../components/views/toasts/GenericToast.tsx";
4648

4749
interface SquirrelUpdate {
4850
releaseNotes: string;
@@ -182,6 +184,25 @@ export default class ElectronPlatform extends BasePlatform {
182184
await this.ipc.call("callDisplayMediaCallback", source ?? { id: "", name: "", thumbnailURL: "" });
183185
});
184186

187+
this.electron.on("showToast", (ev, { title, description, priority = 40 }) => {
188+
const key = uniqueId("electron_showToast_");
189+
const onPrimaryClick = (): void => {
190+
ToastStore.sharedInstance().dismissToast(key);
191+
};
192+
193+
ToastStore.sharedInstance().addOrReplaceToast({
194+
key,
195+
title,
196+
props: {
197+
description,
198+
primaryLabel: _t("action|dismiss"),
199+
onPrimaryClick,
200+
},
201+
component: GenericToast,
202+
priority,
203+
});
204+
});
205+
185206
BreadcrumbsStore.instance.on(UPDATE_EVENT, this.onBreadcrumbsUpdate);
186207

187208
this.initialised = this.initialise();

test/unit-tests/vector/platform/ElectronPlatform-test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import DesktopCapturerSourcePicker from "../../../../src/components/views/elemen
2121
import ElectronPlatform from "../../../../src/vector/platform/ElectronPlatform";
2222
import { setupLanguageMock } from "../../../setup/setupLanguage";
2323
import { stubClient } from "../../../test-utils";
24+
import ToastStore from "../../../../src/stores/ToastStore.ts";
2425

2526
jest.mock("../../../../src/rageshake/rageshake", () => ({
2627
flush: jest.fn(),
@@ -127,6 +128,19 @@ describe("ElectronPlatform", () => {
127128
expect(plat.ipc.call).toHaveBeenCalledWith("callDisplayMediaCallback", "source");
128129
});
129130

131+
it("should show a toast when showToast is fired", async () => {
132+
new ElectronPlatform();
133+
const spy = jest.spyOn(ToastStore.sharedInstance(), "addOrReplaceToast");
134+
135+
const [event, handler] = getElectronEventHandlerCall("showToast")!;
136+
handler({} as any, { title: "title", description: "description" });
137+
138+
expect(event).toBeTruthy();
139+
expect(spy).toHaveBeenCalledWith(
140+
expect.objectContaining({ title: "title", props: expect.objectContaining({ description: "description" }) }),
141+
);
142+
});
143+
130144
describe("updates", () => {
131145
it("dispatches on check updates action", () => {
132146
new ElectronPlatform();

0 commit comments

Comments
 (0)