Skip to content

Commit

Permalink
feat: 🚀 新增支持添加设备备注
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Oct 19, 2023
1 parent 326a133 commit 43f15be
Show file tree
Hide file tree
Showing 17 changed files with 300 additions and 153 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ module.exports = {
'no-unused-vars': 'off',
'eqeqeq': 'off',
'prefer-promise-reject-errors': 'off',

'antfu/top-level-function': 'off',

'import/default': 'off',

'vue/no-mutating-props': 'off',
},
}
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

- Adb 路径
- Scrcpy 路径
- 文件存储路径(音视频及截图保存在这里
- 文件存储路径(音视频录制及设备截图都保存在这里

### 视频控制

Expand Down Expand Up @@ -98,7 +98,6 @@

### 音视频录制

- 文件保存路径
- 录制视频格式

### 音频控制
Expand Down
76 changes: 31 additions & 45 deletions electron/events/handles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,58 @@ export default () => {
'show-open-dialog',
async (event, { preset = '', ...options } = {}) => {
// console.log('options', options)
try {
const res = await dialog.showOpenDialog(options)
// console.log('showOpenDialog.res', res)
if (res.canceled) {
return false
}
const filePaths = res.filePaths
const res = await dialog
.showOpenDialog(options)
.catch(e => console.warn(e))

switch (preset) {
case 'replaceFile':
await fs.copy(filePaths[0], options.filePath, { overwrite: true })
break
}
if (res.canceled) {
throw new Error('用户取消操作')
}

return filePaths
if (!res.filePaths.length) {
throw new Error('获取目录或文件路径失败')
}
catch (error) {
console.warn(error?.message || error)
return false

const filePaths = res.filePaths

switch (preset) {
case 'replaceFile':
await fs.copy(filePaths[0], options.filePath, { overwrite: true })
break
}

return filePaths
},
)

ipcMain.handle('open-path', async (event, pathValue) => {
try {
await shell.openPath(pathValue)
return true
}
catch (error) {
console.warn(error?.message || error)
return false
}
return shell.openPath(pathValue)
})

ipcMain.handle('show-item-in-folder', async (event, filePath) => {
try {
await shell.showItemInFolder(filePath)
return true
}
catch (error) {
console.warn(error?.message || error)
return false
}
return shell.showItemInFolder(filePath)
})

ipcMain.handle(
'show-save-dialog',
async (event, { filePath = '', ...options } = {}) => {
try {
const result = await dialog.showSaveDialog({
const res = await dialog
.showSaveDialog({
...options,
})
if (!result || result.canceled || !result.filePath) {
return false
}

const destinationPath = result.filePath
.catch(e => console.warn(e))

await fs.copy(filePath, destinationPath)

return true
if (res.canceled) {
throw new Error('用户取消操作')
}
catch (error) {
console.error(error?.message || error)
return false

if (!res.filePath) {
throw new Error('获取文件路径失败')
}

const destinationPath = res.filePath

await fs.copy(filePath, destinationPath)
},
)
}
16 changes: 12 additions & 4 deletions electron/exposes/adbkit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import path from 'node:path'
import fs from 'node:fs'
import dayjs from 'dayjs'
import { Adb } from '@devicefarmer/adbkit'
import appStore from '@electron/helpers/store.js'
import { adbPath } from '@electron/configs/index.js'

// console.log('adbPath', adbPath)

const exec = util.promisify(child_process.exec)

let client = null
Expand All @@ -18,6 +17,15 @@ window.addEventListener('beforeunload', () => {
}
})

appStore.onDidChange('scrcpy.adbPath', async (value) => {
console.log('onDidChange.scrcpy.adbPath.value', value)
if (client) {
await client.kill().catch(e => console.warn(e))
client = null
}
client = Adb.createClient({ bin: value })
})

const shell = async command => exec(`${adbPath} ${command}`)
const getDevices = async () => client.listDevicesWithPaths()
const deviceShell = async (id, command) => client.getDevice(id).shell(command)
Expand Down Expand Up @@ -100,8 +108,8 @@ const watch = async (callback) => {
}

export default () => {
client = Adb.createClient({ bin: adbPath })
// console.log('client', client)
client = Adb.createClient({ bin: appStore.get('scrcpy.adbPath') || adbPath })
console.log('client', client)

return {
shell,
Expand Down
4 changes: 2 additions & 2 deletions electron/exposes/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'

import store from '@electron/helpers/store.js'
import * as configs from '@electron/configs/index.js'
import store from './store/index.js'
import electron from './electron/index.js'
import adbkit from './adbkit/index.js'
import scrcpy from './scrcpy/index.js'
Expand All @@ -10,7 +10,7 @@ export default {
init(expose) {
expose('nodePath', path)

expose('appStore', store())
expose('appStore', store)

expose('electron', {
...electron(),
Expand Down
13 changes: 9 additions & 4 deletions electron/exposes/scrcpy/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { spawn } from 'node:child_process'
import appStore from '@electron/helpers/store.js'
import { adbPath, scrcpyPath } from '@electron/configs/index.js'

const shell = async (command, { stdout, stderr } = {}) => {
const args = command.split(' ')
const scrcpyProcess = spawn(scrcpyPath, args, {
env: { ...process.env, ADB: adbPath },
shell: true,
})
const scrcpyProcess = spawn(
appStore.get('scrcpy.scrcpyPath') || scrcpyPath,
args,
{
env: { ...process.env, ADB: adbPath },
shell: true,
},
)

scrcpyProcess.stdout.on('data', (data) => {
const stringData = data.toString()
Expand Down
27 changes: 0 additions & 27 deletions electron/exposes/store/index.js

This file was deleted.

25 changes: 25 additions & 0 deletions electron/helpers/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Store from 'electron-store'
import { createProxy } from './index.js'

const appStore = new Store()

appStore.onDidAnyChange(() => {
console.log('appStore.onDidAnyChange', appStore.store)
})

export default {
...createProxy(appStore, [
'set',
'get',
'delete',
'clear',
'reset',
'has',
'onDidChange',
'onDidAnyChange',
'openInEditor',
]),
...appStore,
getAll: () => appStore.store,
setAll: value => (appStore.store = value),
}
4 changes: 1 addition & 3 deletions electron/main.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import path from 'node:path'
import { BrowserWindow, app, shell } from 'electron'
import Store from 'electron-store'
import { electronApp, optimizer } from '@electron-toolkit/utils'

// packaged.js 必须位于非依赖项的顶部
import './helpers/packaged.js'
import './helpers/store.js'

import { icnsLogoPath, icoLogoPath, logoPath } from './configs/index.js'

import events from './events/index.js'

const appStore = new Store()

// The built directory structure
//
// ├─┬─┬ dist
Expand Down
22 changes: 20 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:name="item.prop"
lazy
>
<component :is="item.prop" :ref="item.prop" />
<component :is="item.prop" v-if="isRender(item)" :ref="item.prop" />
</el-tab-pane>
</el-tabs>
</div>
Expand Down Expand Up @@ -42,13 +42,31 @@ export default {
},
],
activeTab: 'Device',
rendered: true,
}
},
created() {
this.$store.scrcpy.init()
},
methods: {
onTabChange(prop) {},
isRender(item) {
if (this.activeTab === item.prop) {
return this.rendered
}
return true
},
async reRender() {
this.rendered = false
await this.$nextTick()
this.rendered = true
},
async onTabChange(prop) {
switch (prop) {
case 'Device':
this.reRender()
break
}
},
},
}
</script>
Expand Down
21 changes: 15 additions & 6 deletions src/components/Device/ControlBar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,19 @@ export default {
},
methods: {
async handleInstall(device) {
const files = await this.$electron.ipcRenderer.invoke(
'show-open-dialog',
{
let files = null
try {
files = await this.$electron.ipcRenderer.invoke('show-open-dialog', {
properties: ['openFile', 'multiSelections'],
filters: [{ name: '请选择要安装的应用', extensions: ['apk'] }],
},
)
})
}
catch (error) {
if (error.message) {
this.$message.warning(error.message)
}
}
if (!files) {
return false
Expand Down Expand Up @@ -174,7 +180,10 @@ export default {
closeOnClickModal: false,
type: 'success',
})
this.$electron.ipcRenderer.invoke('show-item-in-folder', savePath)
await this.$electron.ipcRenderer.invoke(
'show-item-in-folder',
savePath,
)
}
catch (error) {
if (error.message) {
Expand Down
Loading

0 comments on commit 43f15be

Please sign in to comment.