swipe.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * adb method: swipe — 滑动(方向或起止坐标)
  3. */
  4. const { calculateSwipeCoordinates } = require('./utils.js')
  5. async function run(action, ctx) {
  6. const { device, resolution, variableContext, api, extractVarName, resolveValue } = ctx
  7. const inVars = action.inVars || []
  8. let direction = inVars.length > 0 ? (variableContext[extractVarName(inVars[0])] || inVars[0]) : resolveValue(action.value, variableContext)
  9. if (!direction) return { success: false, error: 'swipe 操作缺少方向参数' }
  10. let x1, y1, x2, y2
  11. if (inVars.length >= 3) {
  12. const start = variableContext[extractVarName(inVars[1])]
  13. const end = variableContext[extractVarName(inVars[2])]
  14. if (start && end) {
  15. x1 = start.x ?? start[0]
  16. y1 = start.y ?? start[1]
  17. x2 = end.x ?? end[0]
  18. y2 = end.y ?? end[1]
  19. }
  20. }
  21. if (x1 === undefined || y1 === undefined || x2 === undefined || y2 === undefined) {
  22. const coords = calculateSwipeCoordinates(direction, resolution.width, resolution.height)
  23. x1 = coords.x1
  24. y1 = coords.y1
  25. x2 = coords.x2
  26. y2 = coords.y2
  27. }
  28. if (!api?.sendSwipe) return { success: false, error: '滑动 API 不可用' }
  29. const swipeResult = await api.sendSwipe(device, x1, y1, x2, y2, 300)
  30. if (!swipeResult.success) return { success: false, error: `滑动失败: ${swipeResult.error != null ? swipeResult.error : 'unknown'}` }
  31. return { success: true }
  32. }
  33. module.exports = { run }