diff options
Diffstat (limited to 'packages/main/src/infrastructure/electron/impl/ElectronMainWindow.ts')
-rw-r--r-- | packages/main/src/infrastructure/electron/impl/ElectronMainWindow.ts | 62 |
1 files changed, 53 insertions, 9 deletions
diff --git a/packages/main/src/infrastructure/electron/impl/ElectronMainWindow.ts b/packages/main/src/infrastructure/electron/impl/ElectronMainWindow.ts index cff7957..f1e3c7e 100644 --- a/packages/main/src/infrastructure/electron/impl/ElectronMainWindow.ts +++ b/packages/main/src/infrastructure/electron/impl/ElectronMainWindow.ts | |||
@@ -22,22 +22,29 @@ import { | |||
22 | Action, | 22 | Action, |
23 | MainToRendererIpcMessage, | 23 | MainToRendererIpcMessage, |
24 | RendererToMainIpcMessage, | 24 | RendererToMainIpcMessage, |
25 | Translation, | ||
25 | } from '@sophie/shared'; | 26 | } from '@sophie/shared'; |
26 | import { BrowserWindow, ipcMain, IpcMainEvent } from 'electron'; | 27 | import { BrowserWindow, ipcMain, IpcMainEvent } from 'electron'; |
28 | import { reaction } from 'mobx'; | ||
27 | import type { IJsonPatch } from 'mobx-state-tree'; | 29 | import type { IJsonPatch } from 'mobx-state-tree'; |
28 | 30 | ||
29 | import type MainStore from '../../../stores/MainStore'; | 31 | import type MainStore from '../../../stores/MainStore.js'; |
30 | import { getLogger } from '../../../utils/log'; | 32 | import Disposer from '../../../utils/Disposer.js'; |
31 | import RendererBridge from '../RendererBridge'; | 33 | import getLogger from '../../../utils/getLogger.js'; |
32 | import type { MainWindow, ServiceView } from '../types'; | 34 | import RendererBridge from '../RendererBridge.js'; |
35 | import type { MainWindow, ServiceView } from '../types.js'; | ||
33 | 36 | ||
34 | import ElectronServiceView from './ElectronServiceView'; | 37 | import ElectronServiceView from './ElectronServiceView.js'; |
35 | import type ElectronViewFactory from './ElectronViewFactory'; | 38 | import type ElectronViewFactory from './ElectronViewFactory.js'; |
36 | import { openDevToolsWhenReady } from './devTools'; | 39 | import { openDevToolsWhenReady } from './devTools.js'; |
37 | import lockWebContentsToFile from './lockWebContentsToFile'; | 40 | import lockWebContentsToFile from './lockWebContentsToFile.js'; |
38 | 41 | ||
39 | const log = getLogger('ElectronMainWindow'); | 42 | const log = getLogger('ElectronMainWindow'); |
40 | 43 | ||
44 | function getBackroundColor(useDarkTheme: boolean): string { | ||
45 | return useDarkTheme ? '#121212' : '#FFF'; | ||
46 | } | ||
47 | |||
41 | export default class ElectronMainWindow implements MainWindow { | 48 | export default class ElectronMainWindow implements MainWindow { |
42 | private readonly browserWindow: BrowserWindow; | 49 | private readonly browserWindow: BrowserWindow; |
43 | 50 | ||
@@ -65,6 +72,8 @@ export default class ElectronMainWindow implements MainWindow { | |||
65 | } | 72 | } |
66 | }; | 73 | }; |
67 | 74 | ||
75 | private readonly disposeBackgroundColorReaction: Disposer; | ||
76 | |||
68 | constructor( | 77 | constructor( |
69 | private readonly store: MainStore, | 78 | private readonly store: MainStore, |
70 | private readonly parent: ElectronViewFactory, | 79 | private readonly parent: ElectronViewFactory, |
@@ -73,6 +82,7 @@ export default class ElectronMainWindow implements MainWindow { | |||
73 | show: false, | 82 | show: false, |
74 | autoHideMenuBar: true, | 83 | autoHideMenuBar: true, |
75 | darkTheme: store.shared.shouldUseDarkColors, | 84 | darkTheme: store.shared.shouldUseDarkColors, |
85 | backgroundColor: getBackroundColor(store.shared.shouldUseDarkColors), | ||
76 | webPreferences: { | 86 | webPreferences: { |
77 | sandbox: true, | 87 | sandbox: true, |
78 | devTools: parent.devMode, | 88 | devTools: parent.devMode, |
@@ -96,6 +106,24 @@ export default class ElectronMainWindow implements MainWindow { | |||
96 | return this.bridge.snapshot; | 106 | return this.bridge.snapshot; |
97 | }); | 107 | }); |
98 | 108 | ||
109 | ipcMain.handle( | ||
110 | RendererToMainIpcMessage.GetTranslation, | ||
111 | (event, translation) => { | ||
112 | const { id } = event.sender; | ||
113 | if (id !== webContents.id) { | ||
114 | log.warn( | ||
115 | 'Unexpected', | ||
116 | RendererToMainIpcMessage.GetTranslation, | ||
117 | 'from webContents', | ||
118 | id, | ||
119 | ); | ||
120 | throw new Error('Invalid IPC call'); | ||
121 | } | ||
122 | const { language, namespace } = Translation.parse(translation); | ||
123 | return store.getTranslation(language, namespace); | ||
124 | }, | ||
125 | ); | ||
126 | |||
99 | this.bridge = new RendererBridge(store, (patch) => { | 127 | this.bridge = new RendererBridge(store, (patch) => { |
100 | webContents.send(MainToRendererIpcMessage.SharedStorePatch, patch); | 128 | webContents.send(MainToRendererIpcMessage.SharedStorePatch, patch); |
101 | }); | 129 | }); |
@@ -107,6 +135,13 @@ export default class ElectronMainWindow implements MainWindow { | |||
107 | 135 | ||
108 | webContents.userAgent = parent.userAgents.mainWindowUserAgent; | 136 | webContents.userAgent = parent.userAgents.mainWindowUserAgent; |
109 | 137 | ||
138 | this.disposeBackgroundColorReaction = reaction( | ||
139 | () => store.shared.shouldUseDarkColors, | ||
140 | (useDarkTheme) => { | ||
141 | this.browserWindow.setBackgroundColor(getBackroundColor(useDarkTheme)); | ||
142 | }, | ||
143 | ); | ||
144 | |||
110 | this.browserWindow.on('ready-to-show', () => this.browserWindow.show()); | 145 | this.browserWindow.on('ready-to-show', () => this.browserWindow.show()); |
111 | 146 | ||
112 | this.browserWindow.on('close', () => this.dispose()); | 147 | this.browserWindow.on('close', () => this.dispose()); |
@@ -134,7 +169,9 @@ export default class ElectronMainWindow implements MainWindow { | |||
134 | } | 169 | } |
135 | if (serviceView instanceof ElectronServiceView) { | 170 | if (serviceView instanceof ElectronServiceView) { |
136 | this.browserWindow.setBrowserView(serviceView.browserView); | 171 | this.browserWindow.setBrowserView(serviceView.browserView); |
137 | serviceView.browserView.setBackgroundColor('#fff'); | 172 | // If this `BrowserView` hasn't been attached previously, |
173 | // we must update its bounds _after_ attaching for the resizing to take effect. | ||
174 | serviceView.updateBounds(); | ||
138 | return; | 175 | return; |
139 | } | 176 | } |
140 | throw new TypeError( | 177 | throw new TypeError( |
@@ -142,8 +179,15 @@ export default class ElectronMainWindow implements MainWindow { | |||
142 | ); | 179 | ); |
143 | } | 180 | } |
144 | 181 | ||
182 | reloadTranslations(): void { | ||
183 | this.browserWindow.webContents.send( | ||
184 | MainToRendererIpcMessage.ReloadTranslations, | ||
185 | ); | ||
186 | } | ||
187 | |||
145 | dispose() { | 188 | dispose() { |
146 | this.bridge.dispose(); | 189 | this.bridge.dispose(); |
190 | this.disposeBackgroundColorReaction(); | ||
147 | this.browserWindow.destroy(); | 191 | this.browserWindow.destroy(); |
148 | ipcMain.removeHandler(RendererToMainIpcMessage.GetSharedStoreSnapshot); | 192 | ipcMain.removeHandler(RendererToMainIpcMessage.GetSharedStoreSnapshot); |
149 | ipcMain.removeListener( | 193 | ipcMain.removeListener( |