From 51b8cf03bd2939c252574c7b79b0e9a3633eddb1 Mon Sep 17 00:00:00 2001 From: Meowtec Date: Sun, 18 Sep 2016 16:02:36 +0800 Subject: [PATCH] fix bounds parse --- assets/components/index.jsx | 6 ++--- assets/components/screen.jsx | 11 ++------ assets/libs/bounds.js | 51 +++++++----------------------------- lib/android.js | 19 ++++++++------ lib/ios.js | 26 +++++++++--------- 5 files changed, 39 insertions(+), 74 deletions(-) diff --git a/assets/components/index.jsx b/assets/components/index.jsx index a69eaa0..cfc3773 100644 --- a/assets/components/index.jsx +++ b/assets/components/index.jsx @@ -3,7 +3,7 @@ import Screen from './screen'; import Tree from './tree'; import Info from './info'; import AppInfo from './app-info'; -import { parseBounds, getNodePathByXY } from '../libs/bounds'; +import { getNodePathByXY } from '../libs/bounds'; import getXPath from '../libs/xpath'; const { appData } = window; @@ -43,7 +43,7 @@ class App extends Component { this.setState({ node, - focusBounds: parseBounds(node.bounds, isIOS), + focusBounds: node.bounds, XPath: getXPath(tree, nodePath, isIOS), }); this.resizeTreeViewport(); @@ -51,7 +51,7 @@ class App extends Component { handleMouseEnter(node) { this.setState({ - focusBounds: parseBounds(node.bounds, this.state.isIOS) + focusBounds: node.bounds }); } diff --git a/assets/components/screen.jsx b/assets/components/screen.jsx index 5b3bea4..8525d89 100644 --- a/assets/components/screen.jsx +++ b/assets/components/screen.jsx @@ -1,5 +1,4 @@ import React, { PureComponent } from 'react'; -import { formatFrameBounds } from '../libs/bounds'; import './screen.less'; // TODO: should by device name. @@ -44,7 +43,7 @@ export default class Screen extends PureComponent { } paintFrame(frameBounds, style) { - console.info('frameBounds', formatFrameBounds(frameBounds)); + console.info('frameBounds', frameBounds); const rate = this.rate; const cxt = this.cxt; cxt.clearRect(0, 0, this.state.width, this.state.height); @@ -52,14 +51,8 @@ export default class Screen extends PureComponent { cxt.fillStyle = 'red'; cxt.globalAlpha = 0.5; - const { leftTop, rightBottom } = frameBounds; - cxt.fillRect( - leftTop.x * rate, - leftTop.y * rate, - (rightBottom.x * rate - leftTop.x * rate), - (rightBottom.y * rate - leftTop.y * rate) - ); + cxt.fillRect.apply(cxt, frameBounds.map(x => x * rate)); } handleClick(e) { diff --git a/assets/libs/bounds.js b/assets/libs/bounds.js index 495cb6b..16b0fa4 100644 --- a/assets/libs/bounds.js +++ b/assets/libs/bounds.js @@ -1,50 +1,19 @@ - -/** - * @param {string} bounds [0,0][640,1136] - */ -export function parseBounds(boundsStr, isIOS) { - const matched = boundsStr.match(/[\d\.]+/g); - const i = x => parseInt(x, 10); - - const leftTop = { - x: i(matched[0]), - y: i(matched[1]), - }; - - var rightBottom = { - x: isIOS ? i(matched[0]) + i(matched[2]) : i(matched[2]), - y: isIOS ? i(matched[1]) + i(matched[3]) : i(matched[3]) - }; - - return { - leftTop, - rightBottom - }; -}; - -export function formatFrameBounds(frameBounds) { - return frameBounds && [ - frameBounds.leftTop.x, - frameBounds.leftTop.y, - frameBounds.rightBottom.x, - frameBounds.rightBottom.y - ].join(','); -}; - export function boundsSize(bounds) { - const { leftTop, rightBottom } = bounds; - return (rightBottom.x - leftTop.x) * (rightBottom.y - leftTop.y); + const [ x, y, width, height ] = bounds; + return width * height; }; export function compareBoundsSize(rectA, rectB) { return boundsSize(rectA) > boundsSize(rectB); }; -export function isInRect(x, y, { leftTop, rightBottom }) { - return x >= leftTop.x - && x <= rightBottom.x - && y >= leftTop.y - && y <= rightBottom.y; +export function isInRect(x, y, bounds) { + const [ _x, _y, width, height ] = bounds; + + return x >= _x + && x <= _x + width + && y >= _y + && y <= _y + height; }; export function getNodePathByXY(tree, isIOS, x, y) { @@ -52,7 +21,7 @@ export function getNodePathByXY(tree, isIOS, x, y) { let bestPath = null; function walk(node, path) { - let bounds = parseBounds(node.bounds, isIOS); + let bounds = node.bounds; let inRect = isInRect(x, y, bounds); if (inRect) { if (!bestBounds || compareBoundsSize(bestBounds, bounds)) { diff --git a/lib/android.js b/lib/android.js index 9434cfd..fc4dba7 100644 --- a/lib/android.js +++ b/lib/android.js @@ -30,21 +30,24 @@ exports.dumpXMLAndScreenShot = function *() { const hierarchy = xml2map.tojson(xml).hierarchy; logger.info(`Dump Android XML success, save to ${xmlFilePath}`); + const adaptor = function(node) { const bounds = node.bounds.match(/[\d\.]+/g); node.rawtext = node.text; - node.text = `${node.class} x:${bounds[0]},y:${bounds[1]} x:${bounds[2]},y:${bounds[3]}`; + + // [ x, y, width, height] + node.bounds = [ + ~~bounds[0], + ~~bounds[1], + bounds[2] - bounds[0], + bounds[3] - bounds[1], + ]; if (node.node) { node.nodes = node.node.length ? node.node : [node.node]; - node.state = { - expanded: true - }; - delete node.node; + node.nodes.forEach(adaptor); - for (var i = 0; i < node.nodes.length; i++) { - adaptor(node.nodes[i]); - } + delete node.node; } return node; }; diff --git a/lib/ios.js b/lib/ios.js index b10f45f..85c7ba7 100644 --- a/lib/ios.js +++ b/lib/ios.js @@ -9,28 +9,28 @@ const Simulator = require('ios-simulator'); const _ = require('./common/helper'); const adaptor = function(node) { - const bounds = node.bounds.match(/[\d\.]+/g); - const pathName = 'XCUIElementType' + node.type; - node.text = `${pathName} x:${bounds[0]},y:${bounds[1]} x:${parseInt(bounds[2], 10) + parseInt(bounds[0], 10)},y:${parseInt(bounds[3], 10) + parseInt(bounds[1], 10)}`; node.class = node.type; + const rect = node.rect; + node.bounds = [ + rect.origin.x, + rect.origin.y, + rect.size.width, + rect.size.height, + ]; + if (node.children) { - node.bounds = `[${bounds[0]},${bounds[1]}][${parseInt(bounds[2], 10) + parseInt(bounds[0], 10)},${parseInt(bounds[3], 10) + parseInt(bounds[1], 10)}]`; - node.nodes = node.children.length ? node.children : [node.children]; - node.state = { - expanded: true - }; - delete node.children; + const children = node.children.length ? node.children : [node.children]; var nodes = []; - - for (var i = 0; i < node.nodes.length; i++) { - var child = node.nodes[i]; + children.forEach(child => { if (parseInt(child.isVisible, 10) || child.type !== 'Window') { nodes.push(adaptor(child)); } - } + }); + node.nodes = nodes; + delete node.children; } return node; };