Merge pull request #181 from lnrdnl/master
Remove SendSync (see Electron docs)
This commit is contained in:
7 files changed
+66
-23
No files matched your search
@@ -16,7 +16,6 @@ When sending events from renderer to main, you access the `window.mainApi` objec
|
|||||||
Here are the supported functions for mainApi:
|
Here are the supported functions for mainApi:
|
||||||
|
|
||||||
- `send`: Send an event to main.
|
- `send`: Send an event to main.
|
||||||
- `sendSync`: Send an event to main. It can be used when receiving a return value. The UI may be frozen until the operation is complete.
|
|
||||||
- `on`: A listener to receive events sent by main.
|
- `on`: A listener to receive events sent by main.
|
||||||
- `once`: A listener to receive events sent by main. (Handle only one call)
|
- `once`: A listener to receive events sent by main. (Handle only one call)
|
||||||
- `off`: Remove an event listener
|
- `off`: Remove an event listener
|
||||||
|
|||||||
+19
-3
@@ -1,4 +1,4 @@
|
|||||||
import { ipcMain, shell, IpcMainEvent } from 'electron'
|
import { ipcMain, shell, IpcMainEvent, dialog } from 'electron'
|
||||||
import Constants from './utils/Constants'
|
import Constants from './utils/Constants'
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -7,13 +7,29 @@ import Constants from './utils/Constants'
|
|||||||
export default class IPCs {
|
export default class IPCs {
|
||||||
static initialize(): void {
|
static initialize(): void {
|
||||||
// Get application version
|
// Get application version
|
||||||
ipcMain.on('msgRequestGetVersion', (event: IpcMainEvent) => {
|
ipcMain.handle('msgRequestGetVersion', () => {
|
||||||
event.returnValue = Constants.APP_VERSION
|
return Constants.APP_VERSION
|
||||||
})
|
})
|
||||||
|
|
||||||
// Open url via web browser
|
// Open url via web browser
|
||||||
ipcMain.on('msgOpenExternalLink', async (event: IpcMainEvent, url: string) => {
|
ipcMain.on('msgOpenExternalLink', async (event: IpcMainEvent, url: string) => {
|
||||||
await shell.openExternal(url)
|
await shell.openExternal(url)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Open file
|
||||||
|
ipcMain.handle('msgOpenFile', async (event, filter: string) => {
|
||||||
|
const filters = []
|
||||||
|
if (filter === 'text') {
|
||||||
|
filters.push({ name: 'Text', extensions: ['txt', 'json'] })
|
||||||
|
}
|
||||||
|
if (filter === 'zip') {
|
||||||
|
filters.push({ name: 'Zip', extensions: ['zip'] })
|
||||||
|
}
|
||||||
|
const dialogResult = await dialog.showOpenDialog({
|
||||||
|
properties: ['openFile'],
|
||||||
|
filters
|
||||||
|
})
|
||||||
|
return dialogResult
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,20 @@
|
|||||||
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron'
|
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron'
|
||||||
|
|
||||||
// Whitelist of valid channels used for IPC communication (Send message from Renderer to Main)
|
// Whitelist of valid channels used for IPC communication (Send message from Renderer to Main)
|
||||||
const mainAvailChannels: string[] = ['msgRequestGetVersion', 'msgOpenExternalLink']
|
const mainAvailChannels: string[] = ['msgRequestGetVersion', 'msgOpenExternalLink', 'msgOpenFile']
|
||||||
const rendererAvailChannels: string[] = []
|
const rendererAvailChannels: string[] = []
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld('mainApi', {
|
contextBridge.exposeInMainWorld('mainApi', {
|
||||||
send: (channel: string, ...data: any[]): void => {
|
send: (channel: string, ...data: any[]): void => {
|
||||||
if (mainAvailChannels.includes(channel)) {
|
if (mainAvailChannels.includes(channel)) {
|
||||||
ipcRenderer.send.apply(null, [channel, ...data])
|
ipcRenderer.send.apply(null, [channel, ...data])
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
console.log({ type: 'send', channel, request: data })
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Unknown ipc channel name: ${channel}`)
|
throw new Error(`Unknown ipc channel name: ${channel}`)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
sendSync: (channel: string, ...data: any[]): any => {
|
|
||||||
if (mainAvailChannels.includes(channel)) {
|
|
||||||
return ipcRenderer.sendSync.apply(null, [channel, ...data])
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(`Unknown ipc channel name: ${channel}`)
|
|
||||||
},
|
|
||||||
on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => {
|
on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => {
|
||||||
if (rendererAvailChannels.includes(channel)) {
|
if (rendererAvailChannels.includes(channel)) {
|
||||||
ipcRenderer.on(channel, listener)
|
ipcRenderer.on(channel, listener)
|
||||||
@@ -43,6 +39,9 @@ contextBridge.exposeInMainWorld('mainApi', {
|
|||||||
invoke: async (channel: string, ...data: any[]): Promise<any> => {
|
invoke: async (channel: string, ...data: any[]): Promise<any> => {
|
||||||
if (mainAvailChannels.includes(channel)) {
|
if (mainAvailChannels.includes(channel)) {
|
||||||
const result = await ipcRenderer.invoke.apply(null, [channel, ...data])
|
const result = await ipcRenderer.invoke.apply(null, [channel, ...data])
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
console.log({ type: 'invoke', channel, request: data, result })
|
||||||
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
"desc": {
|
"desc": {
|
||||||
"welcome-title": "Hello Vutron! Everything is ready.",
|
"welcome-title": "Hello Vutron! Everything is ready.",
|
||||||
"welcome-desc": "You can now write cross-platform web applications. See changes in real time and build to multiple platforms with one command. If you're not already familiar with it, you can get help from the documentation page below, or run some pre-written example code.",
|
"welcome-desc": "You can now write cross-platform web applications. See changes in real time and build to multiple platforms with one command. If you're not already familiar with it, you can get help from the documentation page below, or run some pre-written example code.",
|
||||||
"second-desc": "You have moved to the second screen! There is nothing here!"
|
"second-desc": "You have moved to the second screen! There is nothing here!",
|
||||||
|
"selected-file": "Selected file: {filePath}."
|
||||||
},
|
},
|
||||||
"title": {
|
"title": {
|
||||||
"main": "Main Screen",
|
"main": "Main Screen",
|
||||||
@@ -14,6 +15,7 @@
|
|||||||
"change-language": "Change Language",
|
"change-language": "Change Language",
|
||||||
"increase-count": "Count 1 increment",
|
"increase-count": "Count 1 increment",
|
||||||
"documentation": "Documentation",
|
"documentation": "Documentation",
|
||||||
"github": "Source Code"
|
"github": "Source Code",
|
||||||
|
"open-file": "Open a text file"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,8 @@
|
|||||||
"desc": {
|
"desc": {
|
||||||
"welcome-title": "Hallo Vutron! Alles is klaar.",
|
"welcome-title": "Hallo Vutron! Alles is klaar.",
|
||||||
"welcome-desc": "U kunt nu platformonafhankelijke webapplicaties schrijven. Bekijk wijzigingen in realtime en bouw met één opdracht naar meerdere platforms. Als u hiermee nog niet bekend bent, kunt u hulp krijgen via de onderstaande documentatiepagina, of een vooraf geschreven voorbeeldcode uitvoeren.",
|
"welcome-desc": "U kunt nu platformonafhankelijke webapplicaties schrijven. Bekijk wijzigingen in realtime en bouw met één opdracht naar meerdere platforms. Als u hiermee nog niet bekend bent, kunt u hulp krijgen via de onderstaande documentatiepagina, of een vooraf geschreven voorbeeldcode uitvoeren.",
|
||||||
"second-desc": "U bent naar het tweede scherm gegaan! Hier is niks!"
|
"second-desc": "U bent naar het tweede scherm gegaan! Hier is niks!",
|
||||||
|
"selected-file": "Geselecteerde bestand: {filePath}."
|
||||||
},
|
},
|
||||||
"title": {
|
"title": {
|
||||||
"main": "Hoofdscherm",
|
"main": "Hoofdscherm",
|
||||||
@@ -14,6 +15,7 @@
|
|||||||
"change-language": "Taal wijzigen",
|
"change-language": "Taal wijzigen",
|
||||||
"increase-count": "Één stap optellen",
|
"increase-count": "Één stap optellen",
|
||||||
"documentation": "Documentatie",
|
"documentation": "Documentatie",
|
||||||
"github": "Broncode"
|
"github": "Broncode",
|
||||||
|
"open-file": "Open een bestand"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<script setup lang="tsx">
|
<script setup lang="tsx">
|
||||||
import { useI18n } from 'vue-i18n'
|
import { useI18n } from 'vue-i18n'
|
||||||
import { useTheme } from 'vuetify'
|
import { useTheme } from 'vuetify'
|
||||||
import { openExternal } from '@/renderer/utils'
|
import { openExternal, openFile } from '@/renderer/utils'
|
||||||
import { useCounterStore } from '@/renderer/store/counter'
|
import { useCounterStore } from '@/renderer/store/counter'
|
||||||
import { storeToRefs } from 'pinia'
|
import { storeToRefs } from 'pinia'
|
||||||
import { onMounted, ref } from 'vue'
|
import { onMounted, ref } from 'vue'
|
||||||
@@ -12,6 +12,7 @@ const { counter } = storeToRefs(useCounterStore())
|
|||||||
const theme = useTheme()
|
const theme = useTheme()
|
||||||
const languages = ref(['en'])
|
const languages = ref(['en'])
|
||||||
const appVersion = ref('Unknown')
|
const appVersion = ref('Unknown')
|
||||||
|
const selectedFile = ref('')
|
||||||
|
|
||||||
onMounted((): void => {
|
onMounted((): void => {
|
||||||
languages.value = availableLocales
|
languages.value = availableLocales
|
||||||
@@ -21,7 +22,7 @@ onMounted((): void => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const getApplicationVersionFromMainProcess = (): void => {
|
const getApplicationVersionFromMainProcess = (): void => {
|
||||||
appVersion.value = window.mainApi.sendSync('msgRequestGetVersion')
|
window.mainApi.invoke('msgRequestGetVersion').then((result: string) => { appVersion.value = result })
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleChangeTheme = (): void => {
|
const handleChangeTheme = (): void => {
|
||||||
@@ -43,6 +44,13 @@ const handleOpenGitHub = async (): Promise<void> => {
|
|||||||
const handleCountIncrease = (): void => {
|
const handleCountIncrease = (): void => {
|
||||||
counterIncrease(1)
|
counterIncrease(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleOpenFile = async () => {
|
||||||
|
const dialogResult = await openFile('text')
|
||||||
|
if (!dialogResult.canceled) {
|
||||||
|
selectedFile.value = dialogResult.filePaths[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -63,8 +71,13 @@ const handleCountIncrease = (): void => {
|
|||||||
<p class="my-4">
|
<p class="my-4">
|
||||||
App Version: <strong>{{ appVersion }}</strong>
|
App Version: <strong>{{ appVersion }}</strong>
|
||||||
</p>
|
</p>
|
||||||
|
<p v-if="selectedFile !== ''">{{
|
||||||
|
$t('desc.selected-file', {
|
||||||
|
filePath: selectedFile
|
||||||
|
})
|
||||||
|
}}</p>
|
||||||
<v-row class="my-4">
|
<v-row class="my-4">
|
||||||
<v-col cols="3">
|
<v-col>
|
||||||
<v-btn icon color="primary" @click="handleChangeTheme">
|
<v-btn icon color="primary" @click="handleChangeTheme">
|
||||||
<v-icon icon="mdi-brightness-6" />
|
<v-icon icon="mdi-brightness-6" />
|
||||||
<v-tooltip activator="parent" location="bottom">
|
<v-tooltip activator="parent" location="bottom">
|
||||||
@@ -72,7 +85,7 @@ const handleCountIncrease = (): void => {
|
|||||||
</v-tooltip>
|
</v-tooltip>
|
||||||
</v-btn>
|
</v-btn>
|
||||||
</v-col>
|
</v-col>
|
||||||
<v-col cols="3">
|
<v-col>
|
||||||
<v-badge data-testid="counter-badge" color="blue" :content="counter">
|
<v-badge data-testid="counter-badge" color="blue" :content="counter">
|
||||||
<v-btn data-testid="btn-counter" icon color="primary" @click="handleCountIncrease">
|
<v-btn data-testid="btn-counter" icon color="primary" @click="handleCountIncrease">
|
||||||
<v-icon icon="mdi-plus-circle" />
|
<v-icon icon="mdi-plus-circle" />
|
||||||
@@ -82,7 +95,7 @@ const handleCountIncrease = (): void => {
|
|||||||
</v-btn>
|
</v-btn>
|
||||||
</v-badge>
|
</v-badge>
|
||||||
</v-col>
|
</v-col>
|
||||||
<v-col cols="3">
|
<v-col>
|
||||||
<v-btn icon color="primary" @click="handleOpenDocument">
|
<v-btn icon color="primary" @click="handleOpenDocument">
|
||||||
<v-icon icon="mdi-file-document" />
|
<v-icon icon="mdi-file-document" />
|
||||||
<v-tooltip activator="parent" location="bottom">
|
<v-tooltip activator="parent" location="bottom">
|
||||||
@@ -90,7 +103,7 @@ const handleCountIncrease = (): void => {
|
|||||||
</v-tooltip>
|
</v-tooltip>
|
||||||
</v-btn>
|
</v-btn>
|
||||||
</v-col>
|
</v-col>
|
||||||
<v-col cols="3">
|
<v-col>
|
||||||
<v-btn icon color="primary" @click="handleOpenGitHub">
|
<v-btn icon color="primary" @click="handleOpenGitHub">
|
||||||
<v-icon icon="mdi-github" />
|
<v-icon icon="mdi-github" />
|
||||||
<v-tooltip activator="parent" location="bottom">
|
<v-tooltip activator="parent" location="bottom">
|
||||||
@@ -98,6 +111,14 @@ const handleCountIncrease = (): void => {
|
|||||||
</v-tooltip>
|
</v-tooltip>
|
||||||
</v-btn>
|
</v-btn>
|
||||||
</v-col>
|
</v-col>
|
||||||
|
<v-col>
|
||||||
|
<v-btn icon color="primary" @click="handleOpenFile">
|
||||||
|
<v-icon icon="mdi-folder-open" />
|
||||||
|
<v-tooltip activator="parent" location="bottom">
|
||||||
|
{{ $t('menu.open-file') }}
|
||||||
|
</v-tooltip>
|
||||||
|
</v-btn>
|
||||||
|
</v-col>
|
||||||
<v-col cols="12">
|
<v-col cols="12">
|
||||||
<v-select
|
<v-select
|
||||||
data-testid="select-language"
|
data-testid="select-language"
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ export default class Utils {
|
|||||||
static async openExternal(url: string): Promise<void> {
|
static async openExternal(url: string): Promise<void> {
|
||||||
await window.mainApi.send('msgOpenExternalLink', url)
|
await window.mainApi.send('msgOpenExternalLink', url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async openFile(type: string): Promise<any> {
|
||||||
|
return window.mainApi.invoke('msgOpenFile', type)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const { getCurrentLocale, openExternal } = Utils
|
export const { getCurrentLocale, openExternal, openFile } = Utils
|
||||||
Reference in new issue
Block a user