diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-03-15 00:15:31 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-05-16 00:54:56 +0200 |
commit | d425435c290a7e63c4e30a469da51f43be4db5cc (patch) | |
tree | 73b1cfed3ad81bc987312a9093b654ca4bc37c1f /packages | |
parent | feat(renderer): Show service error on service icon (diff) | |
download | sophie-d425435c290a7e63c4e30a469da51f43be4db5cc.tar.gz sophie-d425435c290a7e63c4e30a469da51f43be4db5cc.tar.zst sophie-d425435c290a7e63c4e30a469da51f43be4db5cc.zip |
feat: Open in external browser
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages')
-rw-r--r-- | packages/main/src/index.ts | 5 | ||||
-rw-r--r-- | packages/main/src/infrastructure/electron/impl/electronShell.ts | 36 | ||||
-rw-r--r-- | packages/main/src/stores/MainEnv.ts (renamed from packages/renderer/src/env/RendererEnv.ts) | 12 | ||||
-rw-r--r-- | packages/main/src/stores/MainStore.ts | 4 | ||||
-rw-r--r-- | packages/main/src/stores/Service.ts | 29 | ||||
-rw-r--r-- | packages/renderer/src/components/locationBar/ExtraButtons.tsx | 52 | ||||
-rw-r--r-- | packages/renderer/src/components/locationBar/LocationBar.tsx | 2 | ||||
-rw-r--r-- | packages/renderer/src/components/locationBar/NavigationButtons.tsx | 2 | ||||
-rw-r--r-- | packages/renderer/src/stores/GlobalSettings.ts | 3 | ||||
-rw-r--r-- | packages/renderer/src/stores/RendererEnv.ts (renamed from packages/renderer/src/env/getEnv.ts) | 11 | ||||
-rw-r--r-- | packages/renderer/src/stores/RendererStore.ts | 3 | ||||
-rw-r--r-- | packages/renderer/src/stores/Service.ts | 8 | ||||
-rw-r--r-- | packages/shared/src/schemas/ServiceAction.ts | 3 |
13 files changed, 140 insertions, 30 deletions
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index 40b15f0..869c555 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts | |||
@@ -26,8 +26,9 @@ import { ensureDirSync } from 'fs-extra'; | |||
26 | import osName from 'os-name'; | 26 | import osName from 'os-name'; |
27 | 27 | ||
28 | import { enableStacktraceSourceMaps } from './infrastructure/electron/impl/devTools'; | 28 | import { enableStacktraceSourceMaps } from './infrastructure/electron/impl/devTools'; |
29 | import electronShell from './infrastructure/electron/impl/electronShell'; | ||
29 | import initReactions from './initReactions'; | 30 | import initReactions from './initReactions'; |
30 | import { createMainStore } from './stores/MainStore'; | 31 | import MainStore from './stores/MainStore'; |
31 | import { getLogger } from './utils/log'; | 32 | import { getLogger } from './utils/log'; |
32 | 33 | ||
33 | const isDevelopment = import.meta.env.MODE === 'development'; | 34 | const isDevelopment = import.meta.env.MODE === 'development'; |
@@ -77,7 +78,7 @@ app.setAboutPanelOptions({ | |||
77 | version: '', | 78 | version: '', |
78 | }); | 79 | }); |
79 | 80 | ||
80 | const store = createMainStore(); | 81 | const store = MainStore.create({}, electronShell); |
81 | 82 | ||
82 | app.on('second-instance', () => { | 83 | app.on('second-instance', () => { |
83 | store.mainWindow?.bringToForeground(); | 84 | store.mainWindow?.bringToForeground(); |
diff --git a/packages/main/src/infrastructure/electron/impl/electronShell.ts b/packages/main/src/infrastructure/electron/impl/electronShell.ts new file mode 100644 index 0000000..0e8c0c1 --- /dev/null +++ b/packages/main/src/infrastructure/electron/impl/electronShell.ts | |||
@@ -0,0 +1,36 @@ | |||
1 | /* | ||
2 | * Copyright (C) 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 | |||
21 | import { shell } from 'electron'; | ||
22 | import { getLogger } from 'loglevel'; | ||
23 | |||
24 | import type MainEnv from '../../../stores/MainEnv'; | ||
25 | |||
26 | const log = getLogger('ElectronShell'); | ||
27 | |||
28 | const electronShell: MainEnv = { | ||
29 | openURLInExternalBrowser(url: string): void { | ||
30 | shell.openExternal(url).catch((error) => { | ||
31 | log.error('Failed to open', url, 'in external program', error); | ||
32 | }); | ||
33 | }, | ||
34 | }; | ||
35 | |||
36 | export default electronShell; | ||
diff --git a/packages/renderer/src/env/RendererEnv.ts b/packages/main/src/stores/MainEnv.ts index ba4c43e..94c34f3 100644 --- a/packages/renderer/src/env/RendererEnv.ts +++ b/packages/main/src/stores/MainEnv.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> | 2 | * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com> |
3 | * | 3 | * |
4 | * This file is part of Sophie. | 4 | * This file is part of Sophie. |
5 | * | 5 | * |
@@ -18,8 +18,12 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import type { Action } from '@sophie/shared'; | 21 | import { getEnv as getAnyEnv, IAnyStateTreeNode } from 'mobx-state-tree'; |
22 | 22 | ||
23 | export default interface RendererEnv { | 23 | export function getEnv(model: IAnyStateTreeNode): MainEnv { |
24 | dispatchMainAction(action: Action): void; | 24 | return getAnyEnv<MainEnv>(model); |
25 | } | ||
26 | |||
27 | export default interface MainEnv { | ||
28 | openURLInExternalBrowser(url: string): void; | ||
25 | } | 29 | } |
diff --git a/packages/main/src/stores/MainStore.ts b/packages/main/src/stores/MainStore.ts index dedc740..ed0f391 100644 --- a/packages/main/src/stores/MainStore.ts +++ b/packages/main/src/stores/MainStore.ts | |||
@@ -124,7 +124,3 @@ const MainStore = types | |||
124 | interface MainStore extends Instance<typeof MainStore> {} | 124 | interface MainStore extends Instance<typeof MainStore> {} |
125 | 125 | ||
126 | export default MainStore; | 126 | export default MainStore; |
127 | |||
128 | export function createMainStore(): MainStore { | ||
129 | return MainStore.create(); | ||
130 | } | ||
diff --git a/packages/main/src/stores/Service.ts b/packages/main/src/stores/Service.ts index 9b2bf1e..26e3517 100644 --- a/packages/main/src/stores/Service.ts +++ b/packages/main/src/stores/Service.ts | |||
@@ -30,6 +30,7 @@ import { Instance, getSnapshot, cast } from 'mobx-state-tree'; | |||
30 | import type { ServiceView } from '../infrastructure/electron/types'; | 30 | import type { ServiceView } from '../infrastructure/electron/types'; |
31 | import { getLogger } from '../utils/log'; | 31 | import { getLogger } from '../utils/log'; |
32 | 32 | ||
33 | import { getEnv } from './MainEnv'; | ||
33 | import ServiceSettings from './ServiceSettings'; | 34 | import ServiceSettings from './ServiceSettings'; |
34 | import type ServiceConfig from './config/ServiceConfig'; | 35 | import type ServiceConfig from './config/ServiceConfig'; |
35 | 36 | ||
@@ -88,6 +89,22 @@ const Service = defineServiceModel(ServiceSettings) | |||
88 | self.indirectMessageCount = indirect; | 89 | self.indirectMessageCount = indirect; |
89 | } | 90 | } |
90 | }, | 91 | }, |
92 | goBack(): void { | ||
93 | self.serviceView?.goBack(); | ||
94 | }, | ||
95 | goForward(): void { | ||
96 | self.serviceView?.goForward(); | ||
97 | }, | ||
98 | stop(): void { | ||
99 | self.serviceView?.stop(); | ||
100 | }, | ||
101 | openCurrentURLInExternalBrowser(): void { | ||
102 | if (self.currentUrl === undefined) { | ||
103 | log.error('Cannot open empty URL in external browser'); | ||
104 | return; | ||
105 | } | ||
106 | getEnv(self).openURLInExternalBrowser(self.currentUrl); | ||
107 | }, | ||
91 | })) | 108 | })) |
92 | .actions((self) => { | 109 | .actions((self) => { |
93 | function setState(state: ServiceStateSnapshotIn): void { | 110 | function setState(state: ServiceStateSnapshotIn): void { |
@@ -134,12 +151,6 @@ const Service = defineServiceModel(ServiceSettings) | |||
134 | }); | 151 | }); |
135 | } | 152 | } |
136 | }, | 153 | }, |
137 | goBack(): void { | ||
138 | self.serviceView?.goBack(); | ||
139 | }, | ||
140 | goForward(): void { | ||
141 | self.serviceView?.goForward(); | ||
142 | }, | ||
143 | reload(ignoreCache = false): void { | 154 | reload(ignoreCache = false): void { |
144 | if (self.serviceView === undefined) { | 155 | if (self.serviceView === undefined) { |
145 | setState({ type: 'initializing' }); | 156 | setState({ type: 'initializing' }); |
@@ -147,9 +158,6 @@ const Service = defineServiceModel(ServiceSettings) | |||
147 | self.serviceView?.reload(ignoreCache); | 158 | self.serviceView?.reload(ignoreCache); |
148 | } | 159 | } |
149 | }, | 160 | }, |
150 | stop(): void { | ||
151 | self.serviceView?.stop(); | ||
152 | }, | ||
153 | go(url: string): void { | 161 | go(url: string): void { |
154 | if (self.serviceView === undefined) { | 162 | if (self.serviceView === undefined) { |
155 | self.currentUrl = url; | 163 | self.currentUrl = url; |
@@ -207,6 +215,9 @@ const Service = defineServiceModel(ServiceSettings) | |||
207 | case 'temporarily-trust-current-certificate': | 215 | case 'temporarily-trust-current-certificate': |
208 | self.temporarilyTrustCurrentCertificate(action.fingerprint); | 216 | self.temporarilyTrustCurrentCertificate(action.fingerprint); |
209 | break; | 217 | break; |
218 | case 'open-current-url-in-external-browser': | ||
219 | self.openCurrentURLInExternalBrowser(); | ||
220 | break; | ||
210 | default: | 221 | default: |
211 | log.error('Unknown action to dispatch', action); | 222 | log.error('Unknown action to dispatch', action); |
212 | break; | 223 | break; |
diff --git a/packages/renderer/src/components/locationBar/ExtraButtons.tsx b/packages/renderer/src/components/locationBar/ExtraButtons.tsx new file mode 100644 index 0000000..4eaee29 --- /dev/null +++ b/packages/renderer/src/components/locationBar/ExtraButtons.tsx | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * Copyright (C) 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 | |||
21 | import IconOpenInBrowser from '@mui/icons-material/OpenInBrowser'; | ||
22 | import Box from '@mui/material/Box'; | ||
23 | import IconButton from '@mui/material/IconButton'; | ||
24 | import { observer } from 'mobx-react-lite'; | ||
25 | import React from 'react'; | ||
26 | |||
27 | import type Service from '../../stores/Service'; | ||
28 | |||
29 | function ExtraButtons({ | ||
30 | service, | ||
31 | }: { | ||
32 | service: Service | undefined; | ||
33 | }): JSX.Element { | ||
34 | return ( | ||
35 | <Box | ||
36 | sx={{ | ||
37 | display: 'flex', | ||
38 | flexDirection: 'row', | ||
39 | }} | ||
40 | > | ||
41 | <IconButton | ||
42 | aria-label="Open in browser" | ||
43 | disabled={service?.currentUrl === undefined} | ||
44 | onClick={() => service?.openCurrentURLInExternalBrowser()} | ||
45 | > | ||
46 | <IconOpenInBrowser /> | ||
47 | </IconButton> | ||
48 | </Box> | ||
49 | ); | ||
50 | } | ||
51 | |||
52 | export default observer(ExtraButtons); | ||
diff --git a/packages/renderer/src/components/locationBar/LocationBar.tsx b/packages/renderer/src/components/locationBar/LocationBar.tsx index 0debaab..fc9c147 100644 --- a/packages/renderer/src/components/locationBar/LocationBar.tsx +++ b/packages/renderer/src/components/locationBar/LocationBar.tsx | |||
@@ -24,6 +24,7 @@ import React from 'react'; | |||
24 | 24 | ||
25 | import { useStore } from '../StoreProvider'; | 25 | import { useStore } from '../StoreProvider'; |
26 | 26 | ||
27 | import ExtraButtons from './ExtraButtons'; | ||
27 | import LocationTextField from './LocationTextField'; | 28 | import LocationTextField from './LocationTextField'; |
28 | import NavigationButtons from './NavigationButtons'; | 29 | import NavigationButtons from './NavigationButtons'; |
29 | 30 | ||
@@ -49,6 +50,7 @@ function LocationBar(): JSX.Element { | |||
49 | <LocationBarRoot id={LOCATION_BAR_ID} hidden={!showLocationBar}> | 50 | <LocationBarRoot id={LOCATION_BAR_ID} hidden={!showLocationBar}> |
50 | <NavigationButtons service={selectedService} /> | 51 | <NavigationButtons service={selectedService} /> |
51 | <LocationTextField service={selectedService} /> | 52 | <LocationTextField service={selectedService} /> |
53 | <ExtraButtons service={selectedService} /> | ||
52 | </LocationBarRoot> | 54 | </LocationBarRoot> |
53 | ); | 55 | ); |
54 | } | 56 | } |
diff --git a/packages/renderer/src/components/locationBar/NavigationButtons.tsx b/packages/renderer/src/components/locationBar/NavigationButtons.tsx index 5c5c959..9995a21 100644 --- a/packages/renderer/src/components/locationBar/NavigationButtons.tsx +++ b/packages/renderer/src/components/locationBar/NavigationButtons.tsx | |||
@@ -29,7 +29,7 @@ import IconButton from '@mui/material/IconButton'; | |||
29 | import { observer } from 'mobx-react-lite'; | 29 | import { observer } from 'mobx-react-lite'; |
30 | import React from 'react'; | 30 | import React from 'react'; |
31 | 31 | ||
32 | import Service from '../../stores/Service'; | 32 | import type Service from '../../stores/Service'; |
33 | 33 | ||
34 | function NavigationButtons({ | 34 | function NavigationButtons({ |
35 | service, | 35 | service, |
diff --git a/packages/renderer/src/stores/GlobalSettings.ts b/packages/renderer/src/stores/GlobalSettings.ts index 79815ba..52971d4 100644 --- a/packages/renderer/src/stores/GlobalSettings.ts +++ b/packages/renderer/src/stores/GlobalSettings.ts | |||
@@ -21,8 +21,7 @@ | |||
21 | import { defineGlobalSettingsModel, ThemeSource } from '@sophie/shared'; | 21 | import { defineGlobalSettingsModel, ThemeSource } from '@sophie/shared'; |
22 | import { Instance } from 'mobx-state-tree'; | 22 | import { Instance } from 'mobx-state-tree'; |
23 | 23 | ||
24 | import getEnv from '../env/getEnv'; | 24 | import { getEnv } from './RendererEnv'; |
25 | |||
26 | import Service from './Service'; | 25 | import Service from './Service'; |
27 | 26 | ||
28 | const GlobalSettings = defineGlobalSettingsModel(Service).actions((self) => ({ | 27 | const GlobalSettings = defineGlobalSettingsModel(Service).actions((self) => ({ |
diff --git a/packages/renderer/src/env/getEnv.ts b/packages/renderer/src/stores/RendererEnv.ts index 4f7357a..496ede6 100644 --- a/packages/renderer/src/env/getEnv.ts +++ b/packages/renderer/src/stores/RendererEnv.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com> | 2 | * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> |
3 | * | 3 | * |
4 | * This file is part of Sophie. | 4 | * This file is part of Sophie. |
5 | * | 5 | * |
@@ -18,10 +18,9 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import type { Action } from '@sophie/shared'; | ||
21 | import { getEnv as getAnyEnv, IAnyStateTreeNode } from 'mobx-state-tree'; | 22 | import { getEnv as getAnyEnv, IAnyStateTreeNode } from 'mobx-state-tree'; |
22 | 23 | ||
23 | import type RendererEnv from './RendererEnv'; | ||
24 | |||
25 | /** | 24 | /** |
26 | * Gets a well-typed environment from `model`. | 25 | * Gets a well-typed environment from `model`. |
27 | * | 26 | * |
@@ -29,6 +28,10 @@ import type RendererEnv from './RendererEnv'; | |||
29 | * | 28 | * |
30 | * @param model The state tree node. | 29 | * @param model The state tree node. |
31 | */ | 30 | */ |
32 | export default function getEnv(model: IAnyStateTreeNode): RendererEnv { | 31 | export function getEnv(model: IAnyStateTreeNode): RendererEnv { |
33 | return getAnyEnv<RendererEnv>(model); | 32 | return getAnyEnv<RendererEnv>(model); |
34 | } | 33 | } |
34 | |||
35 | export default interface RendererEnv { | ||
36 | dispatchMainAction(action: Action): void; | ||
37 | } | ||
diff --git a/packages/renderer/src/stores/RendererStore.ts b/packages/renderer/src/stores/RendererStore.ts index 8f424f6..f48d484 100644 --- a/packages/renderer/src/stores/RendererStore.ts +++ b/packages/renderer/src/stores/RendererStore.ts | |||
@@ -21,11 +21,10 @@ | |||
21 | import { BrowserViewBounds, SophieRenderer } from '@sophie/shared'; | 21 | import { BrowserViewBounds, SophieRenderer } from '@sophie/shared'; |
22 | import { applySnapshot, applyPatch, Instance, types } from 'mobx-state-tree'; | 22 | import { applySnapshot, applyPatch, Instance, types } from 'mobx-state-tree'; |
23 | 23 | ||
24 | import RendererEnv from '../env/RendererEnv'; | ||
25 | import getEnv from '../env/getEnv'; | ||
26 | import { getLogger } from '../utils/log'; | 24 | import { getLogger } from '../utils/log'; |
27 | 25 | ||
28 | import GlobalSettings from './GlobalSettings'; | 26 | import GlobalSettings from './GlobalSettings'; |
27 | import RendererEnv, { getEnv } from './RendererEnv'; | ||
29 | import Service from './Service'; | 28 | import Service from './Service'; |
30 | import SharedStore from './SharedStore'; | 29 | import SharedStore from './SharedStore'; |
31 | 30 | ||
diff --git a/packages/renderer/src/stores/Service.ts b/packages/renderer/src/stores/Service.ts index 695cff4..c50e5bd 100644 --- a/packages/renderer/src/stores/Service.ts +++ b/packages/renderer/src/stores/Service.ts | |||
@@ -21,8 +21,7 @@ | |||
21 | import { defineServiceModel, ServiceAction } from '@sophie/shared'; | 21 | import { defineServiceModel, ServiceAction } from '@sophie/shared'; |
22 | import { Instance } from 'mobx-state-tree'; | 22 | import { Instance } from 'mobx-state-tree'; |
23 | 23 | ||
24 | import getEnv from '../env/getEnv'; | 24 | import { getEnv } from './RendererEnv'; |
25 | |||
26 | import ServiceSettings from './ServiceSettings'; | 25 | import ServiceSettings from './ServiceSettings'; |
27 | 26 | ||
28 | const Service = defineServiceModel(ServiceSettings).actions((self) => { | 27 | const Service = defineServiceModel(ServiceSettings).actions((self) => { |
@@ -76,6 +75,11 @@ const Service = defineServiceModel(ServiceSettings).actions((self) => { | |||
76 | fingerprint: self.state.certificate.fingerprint, | 75 | fingerprint: self.state.certificate.fingerprint, |
77 | }); | 76 | }); |
78 | }, | 77 | }, |
78 | openCurrentURLInExternalBrowser(): void { | ||
79 | dispatch({ | ||
80 | action: 'open-current-url-in-external-browser', | ||
81 | }); | ||
82 | }, | ||
79 | }; | 83 | }; |
80 | }); | 84 | }); |
81 | 85 | ||
diff --git a/packages/shared/src/schemas/ServiceAction.ts b/packages/shared/src/schemas/ServiceAction.ts index 8961bfe..da0b96b 100644 --- a/packages/shared/src/schemas/ServiceAction.ts +++ b/packages/shared/src/schemas/ServiceAction.ts | |||
@@ -46,6 +46,9 @@ export const ServiceAction = /* @__PURE__ */ (() => | |||
46 | action: z.literal('temporarily-trust-current-certificate'), | 46 | action: z.literal('temporarily-trust-current-certificate'), |
47 | fingerprint: z.string(), | 47 | fingerprint: z.string(), |
48 | }), | 48 | }), |
49 | z.object({ | ||
50 | action: z.literal('open-current-url-in-external-browser'), | ||
51 | }), | ||
49 | ]))(); | 52 | ]))(); |
50 | 53 | ||
51 | /* | 54 | /* |