aboutsummaryrefslogtreecommitdiffstats
path: root/packages/renderer/src/stores/RendererStore.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/renderer/src/stores/RendererStore.ts')
-rw-r--r--packages/renderer/src/stores/RendererStore.ts79
1 files changed, 79 insertions, 0 deletions
diff --git a/packages/renderer/src/stores/RendererStore.ts b/packages/renderer/src/stores/RendererStore.ts
new file mode 100644
index 0000000..001f23a
--- /dev/null
+++ b/packages/renderer/src/stores/RendererStore.ts
@@ -0,0 +1,79 @@
1/*
2 * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import {
22 applySnapshot,
23 applyPatch,
24 Instance,
25 types
26} from 'mobx-state-tree';
27import {
28 BrowserViewBounds,
29 sharedStore,
30 SophieRenderer,
31 ThemeSource,
32} from '@sophie/shared';
33
34import { getEnv } from './RendererEnv';
35
36export const rendererStore = types.model('RendererStore', {
37 shared: types.optional(sharedStore, {}),
38}).actions((self) => ({
39 setBrowserViewBounds(bounds: BrowserViewBounds) {
40 getEnv(self).ipc.setBrowserViewBounds(bounds);
41 },
42 setThemeSource(mode: ThemeSource) {
43 getEnv(self).ipc.setThemeSource(mode);
44 },
45 toggleDarkMode() {
46 if (self.shared.shouldUseDarkColors) {
47 this.setThemeSource('light');
48 } else {
49 this.setThemeSource('dark');
50 }
51 },
52}));
53
54export interface RendererStore extends Instance<typeof rendererStore> {}
55
56/**
57 * Creates a new `RootStore` with a new environment and connects it to `ipc`.
58 *
59 * Changes to the `shared` store in the main process will be propagated to
60 * the newly created store via `ipc`.
61 *
62 * @param ipc The `sophieRenderer` context bridge.
63 */
64export function createAndConnectRendererStore(ipc: SophieRenderer): RendererStore {
65 const store = rendererStore.create({}, {
66 ipc,
67 });
68
69 ipc.setSharedStoreListener({
70 onSnapshot(snapshot) {
71 applySnapshot(store.shared, snapshot);
72 },
73 onPatch(patch) {
74 applyPatch(store.shared, patch);
75 },
76 });
77
78 return store;
79}