diff options
author | 2021-12-24 01:36:20 +0100 | |
---|---|---|
committer | 2021-12-24 01:36:20 +0100 | |
commit | cf3ec7fc396125ed452c553b34ae2737329fc61d (patch) | |
tree | 6d2fdbb40b9c9b1d32168d1ccb2372b5602f1968 /packages/main/src/index.ts | |
parent | feat: Set up partition persistence (diff) | |
download | sophie-cf3ec7fc396125ed452c553b34ae2737329fc61d.tar.gz sophie-cf3ec7fc396125ed452c553b34ae2737329fc61d.tar.zst sophie-cf3ec7fc396125ed452c553b34ae2737329fc61d.zip |
feat: Service to main process communication
Diffstat (limited to 'packages/main/src/index.ts')
-rw-r--r-- | packages/main/src/index.ts | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index f56d629..857c15d 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts | |||
@@ -23,6 +23,10 @@ import { autorun } from 'mobx'; | |||
23 | import { getSnapshot, onPatch } from 'mobx-state-tree'; | 23 | import { getSnapshot, onPatch } from 'mobx-state-tree'; |
24 | import { join } from 'path'; | 24 | import { join } from 'path'; |
25 | import { | 25 | import { |
26 | ServiceToMainIpcMessage, | ||
27 | unreadCount, | ||
28 | } from '@sophie/service-shared'; | ||
29 | import { | ||
26 | browserViewBounds, | 30 | browserViewBounds, |
27 | MainToRendererIpcMessage, | 31 | MainToRendererIpcMessage, |
28 | paletteMode, | 32 | paletteMode, |
@@ -54,9 +58,9 @@ app.enableSandbox(); | |||
54 | 58 | ||
55 | // Remove sophie and electron from the user-agent string to avoid detection. | 59 | // Remove sophie and electron from the user-agent string to avoid detection. |
56 | const originalUserAgent = app.userAgentFallback; | 60 | const originalUserAgent = app.userAgentFallback; |
61 | const userAgent = originalUserAgent.replaceAll(/ ([Ss]ophie|Electron)\/[0-9.]+/g, ''); | ||
57 | // Removing the electron version breaks redux devtools, so we only do this in production. | 62 | // Removing the electron version breaks redux devtools, so we only do this in production. |
58 | if (!isDevelopment) { | 63 | if (!isDevelopment) { |
59 | const userAgent = originalUserAgent.replaceAll(/ ([Ss]ophie|Electron)\/[0-9.]+/g, ''); | ||
60 | app.userAgentFallback = userAgent; | 64 | app.userAgentFallback = userAgent; |
61 | } | 65 | } |
62 | 66 | ||
@@ -97,6 +101,17 @@ async function createWindow(): Promise<void> { | |||
97 | 101 | ||
98 | webContents.userAgent = originalUserAgent; | 102 | webContents.userAgent = originalUserAgent; |
99 | 103 | ||
104 | const browserView = new BrowserView({ | ||
105 | webPreferences: { | ||
106 | sandbox: true, | ||
107 | partition: 'persist:service', | ||
108 | preload: join(__dirname, '../../service-preload/dist/index.cjs'), | ||
109 | }, | ||
110 | }); | ||
111 | |||
112 | browserView.webContents.userAgent = userAgent; | ||
113 | browserView.setBackgroundColor('#fff'); | ||
114 | |||
100 | webContents.on('ipc-message', (_event, channel, ...args) => { | 115 | webContents.on('ipc-message', (_event, channel, ...args) => { |
101 | try { | 116 | try { |
102 | switch (channel) { | 117 | switch (channel) { |
@@ -109,8 +124,11 @@ async function createWindow(): Promise<void> { | |||
109 | case RendererToMainIpcMessage.SetPaletteMode: | 124 | case RendererToMainIpcMessage.SetPaletteMode: |
110 | store.setPaletteMode(paletteMode.parse(args[0])) | 125 | store.setPaletteMode(paletteMode.parse(args[0])) |
111 | break; | 126 | break; |
127 | case RendererToMainIpcMessage.ReloadAllServices: | ||
128 | browserView.webContents.reload(); | ||
129 | break; | ||
112 | default: | 130 | default: |
113 | console.warn('Unknown IPC message:', channel, args); | 131 | console.error('Unknown IPC message:', channel, args); |
114 | break; | 132 | break; |
115 | } | 133 | } |
116 | } catch (err) { | 134 | } catch (err) { |
@@ -122,26 +140,31 @@ async function createWindow(): Promise<void> { | |||
122 | webContents.send(MainToRendererIpcMessage.SharedStorePatch, patch); | 140 | webContents.send(MainToRendererIpcMessage.SharedStorePatch, patch); |
123 | }); | 141 | }); |
124 | 142 | ||
125 | const pageUrl = (isDevelopment && import.meta.env.VITE_DEV_SERVER_URL !== undefined) | 143 | browserView.webContents.on('ipc-message', (_event, channel, ...args) => { |
126 | ? import.meta.env.VITE_DEV_SERVER_URL | 144 | try { |
127 | : new URL('../renderer/dist/index.html', `file://${__dirname}`).toString(); | 145 | switch (channel) { |
128 | 146 | case ServiceToMainIpcMessage.SetUnreadCount: | |
129 | const browserView = new BrowserView({ | 147 | console.log('Unread count:', unreadCount.parse(args[0])); |
130 | webPreferences: { | 148 | break; |
131 | sandbox: true, | 149 | default: |
132 | partition: 'persist:service', | 150 | console.error('Unknown IPC message:', channel, args); |
133 | }, | 151 | break; |
152 | } | ||
153 | } catch (err) { | ||
154 | console.error('Error while processing IPC message:', channel, args, err); | ||
155 | } | ||
134 | }); | 156 | }); |
135 | 157 | ||
136 | browserView.setBackgroundColor('#fff'); | ||
137 | |||
138 | autorun(() => { | 158 | autorun(() => { |
139 | browserView.setBounds(store.shared.browserViewBounds); | 159 | browserView.setBounds(store.shared.browserViewBounds); |
140 | }); | 160 | }); |
141 | 161 | ||
162 | const pageUrl = (isDevelopment && import.meta.env.VITE_DEV_SERVER_URL !== undefined) | ||
163 | ? import.meta.env.VITE_DEV_SERVER_URL | ||
164 | : new URL('../renderer/dist/index.html', `file://${__dirname}`).toString(); | ||
142 | await mainWindow.loadURL(pageUrl); | 165 | await mainWindow.loadURL(pageUrl); |
143 | 166 | ||
144 | mainWindow.addBrowserView(browserView); | 167 | mainWindow.setBrowserView(browserView); |
145 | 168 | ||
146 | return browserView.webContents.loadURL('https://git.marussy.com/sophie/about'); | 169 | return browserView.webContents.loadURL('https://git.marussy.com/sophie/about'); |
147 | } | 170 | } |