Skip to content

Commit

Permalink
fix bounds parse
Browse files Browse the repository at this point in the history
  • Loading branch information
meowtec committed Sep 18, 2016
1 parent 7e46c4f commit 51b8cf0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 74 deletions.
6 changes: 3 additions & 3 deletions assets/components/index.jsx
Expand Up @@ -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;
Expand Down Expand Up @@ -43,15 +43,15 @@ class App extends Component {

this.setState({
node,
focusBounds: parseBounds(node.bounds, isIOS),
focusBounds: node.bounds,
XPath: getXPath(tree, nodePath, isIOS),
});
this.resizeTreeViewport();
}

handleMouseEnter(node) {
this.setState({
focusBounds: parseBounds(node.bounds, this.state.isIOS)
focusBounds: node.bounds
});
}

Expand Down
11 changes: 2 additions & 9 deletions 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.
Expand Down Expand Up @@ -44,22 +43,16 @@ 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);
if (!frameBounds) return;

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) {
Expand Down
51 changes: 10 additions & 41 deletions assets/libs/bounds.js
@@ -1,58 +1,27 @@

/**
* @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) {
let bestBounds = null;
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)) {
Expand Down
19 changes: 11 additions & 8 deletions lib/android.js
Expand Up @@ -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;
};
Expand Down
26 changes: 13 additions & 13 deletions lib/ios.js
Expand Up @@ -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;
};
Expand Down

0 comments on commit 51b8cf0

Please sign in to comment.