Skip to content

Commit

Permalink
perf: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
lei1248276 committed Aug 13, 2024
1 parent 2600630 commit df91c61
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
37 changes: 2 additions & 35 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { app, shell, BrowserWindow, ipcMain } from 'electron'
import { app, shell, BrowserWindow } from 'electron'
import { optimizer, is } from '@electron-toolkit/utils'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import fixPath from 'fix-path'
import Scrcpy from './scrcpy'
import { createTray, replaceTray, updateTray } from './tray'
import { createTray } from './tray'
import { appStore } from './store/appStore'

function createWindow() {
Expand Down Expand Up @@ -69,39 +69,6 @@ app.whenReady().then(() => {
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})

ipcMain.handle('getStoreIps', () => appStore.get('ips'))
ipcMain.on('setStoreIps', (_, ips: string[]) => {
appStore.set('ips', ips)
replaceTray(...ips.map((ip, i) => ({
id: 'tcp' + (i + 1),
type: 'radio',
label: ip,
checked: false,
click: () => {
Scrcpy.start(['--tcpip=' + ip, ...appStore.get('scrcpyOptions')])
}
} as const)))
})
ipcMain.handle('getStoreScrcpyOptions', () => appStore.get('scrcpyOptions'))
ipcMain.on('setStoreScrcpyOptions', (_, options: string[]) => {
appStore.set('scrcpyOptions', options)
})

ipcMain.on('scrcpy', (_, ip: string, options: string[]) => {
if (ip) {
Scrcpy.start(['--tcpip=' + ip, ...options])
updateTray({ label: ip, checked: true })
} else {
Scrcpy.start(['--select-usb', ...options])
updateTray({ id: 'usb', label: 'USB', checked: true })
}
updateTray({ id: 'close', checked: false })
// !win?.isDestroyed() && win?.close()
})
ipcMain.on('scrcpy-kill', () => {
Scrcpy.stop()
})
})

// Quit when all windows are closed, except on macOS. There, it's common
Expand Down
16 changes: 16 additions & 0 deletions src/main/scrcpy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,20 @@ const Scrcpy = {
}
}

ipcMain.on('scrcpy', (_, ip: string, options: string[]) => {
if (ip) {
Scrcpy.start(['--tcpip=' + ip, ...options])
updateTray({ label: ip, checked: true })
} else {
Scrcpy.start(['--select-usb', ...options])
updateTray({ id: 'usb', label: 'USB', checked: true })
}
updateTray({ id: 'close', checked: false })
// !win?.isDestroyed() && win?.close()
})

ipcMain.on('scrcpy-kill', () => {
Scrcpy.stop()
})

export default Scrcpy
22 changes: 22 additions & 0 deletions src/main/store/appStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Store from 'electron-store'
import { ipcMain } from 'electron'
import Scrcpy from '../scrcpy'
import { replaceTray } from '../tray'

export const appStore = new Store({
defaults: {
Expand All @@ -8,3 +11,22 @@ export const appStore = new Store({
hideWindow: false
}
})

ipcMain.handle('getStoreIps', () => appStore.get('ips'))
ipcMain.on('setStoreIps', (_, ips: string[]) => {
appStore.set('ips', ips)
replaceTray(...ips.map((ip, i) => ({
id: 'tcp' + (i + 1),
type: 'radio',
label: ip,
checked: false,
click: () => {
Scrcpy.start(['--tcpip=' + ip, ...appStore.get('scrcpyOptions')])
}
} as const)))
})

ipcMain.handle('getStoreScrcpyOptions', () => appStore.get('scrcpyOptions'))
ipcMain.on('setStoreScrcpyOptions', (_, options: string[]) => {
appStore.set('scrcpyOptions', options)
})

0 comments on commit df91c61

Please sign in to comment.