Bug 1348256 - Throttle the grid outline mouseover. r=pbro draft
authorGabriel Luong <gabriel.luong@gmail.com>
Mon, 20 Mar 2017 16:11:17 -0400
changeset 501683 3c40655eed62b9b6494069cdadfbac4402b08419
parent 501682 333b172694da88d07e96ab17e09dcfc5d082a297
child 549964 60f9e8ddc4e75908e8d8124167f32fb064360cb9
push id50075
push userbmo:gl@mozilla.com
push dateMon, 20 Mar 2017 20:12:04 +0000
reviewerspbro
bugs1348256
milestone55.0a1
Bug 1348256 - Throttle the grid outline mouseover. r=pbro MozReview-Commit-ID: CFm4rXR7w5l
devtools/client/inspector/grids/components/GridOutline.js
--- a/devtools/client/inspector/grids/components/GridOutline.js
+++ b/devtools/client/inspector/grids/components/GridOutline.js
@@ -1,19 +1,22 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
 const { addons, createClass, DOM: dom, PropTypes } =
   require("devtools/client/shared/vendor/react");
+const { throttle } = require("devtools/client/inspector/shared/utils");
 
 const Types = require("../types");
 
+const GRID_CELL_MOUSEOVER_TIMEOUT = 150;
+
 // Move SVG grid to the right 100 units, so that it is not flushed against the edge of
 // layout border
 const TRANSLATE_X = -100;
 const TRANSLATE_Y = 0;
 
 const VIEWPORT_HEIGHT = 100;
 const VIEWPORT_WIDTH = 450;
 
@@ -30,16 +33,20 @@ module.exports = createClass({
   mixins: [ addons.PureRenderMixin ],
 
   getInitialState() {
     return {
       selectedGrids: [],
     };
   },
 
+  componentWillMount() {
+    this.highlightCell = throttle(this.highlightCell, GRID_CELL_MOUSEOVER_TIMEOUT);
+  },
+
   componentWillReceiveProps({ grids }) {
     this.setState({
       selectedGrids: grids.filter(grid => grid.highlighted),
     });
   },
 
   /**
    * Returns the grid area name if the given grid cell is part of a grid area, otherwise
@@ -61,16 +68,41 @@ module.exports = createClass({
 
     if (!gridArea) {
       return null;
     }
 
     return gridArea.name;
   },
 
+  highlightCell({ target }) {
+    const {
+      grids,
+      onShowGridAreaHighlight,
+      onShowGridCellHighlight,
+    } = this.props;
+    const name = target.getAttribute("data-grid-area-name");
+    const id = target.getAttribute("data-grid-id");
+    const fragmentIndex = target.getAttribute("data-grid-fragment-index");
+    const color = target.getAttribute("stroke");
+    const rowNumber = target.getAttribute("data-grid-row");
+    const columnNumber = target.getAttribute("data-grid-column");
+
+    target.setAttribute("fill", color);
+
+    if (name) {
+      onShowGridAreaHighlight(grids[id].nodeFront, name, color);
+    }
+
+    if (fragmentIndex && rowNumber && columnNumber) {
+      onShowGridCellHighlight(grids[id].nodeFront, fragmentIndex,
+        rowNumber, columnNumber);
+    }
+  },
+
   /**
    * Renders the grid outline for the given grid container object.
    *
    * @param  {Object} grid
    *         A single grid container in the document.
    */
   renderGrid(grid) {
     const { id, color, gridFragments } = grid;
@@ -181,39 +213,19 @@ module.exports = createClass({
     const color = target.getAttribute("stroke");
 
     target.setAttribute("fill", "none");
 
     onShowGridAreaHighlight(grids[id].nodeFront, null, color);
     onShowGridCellHighlight(grids[id].nodeFront);
   },
 
-  onMouseOverCell({ target }) {
-    const {
-      grids,
-      onShowGridAreaHighlight,
-      onShowGridCellHighlight,
-    } = this.props;
-    const name = target.getAttribute("data-grid-area-name");
-    const id = target.getAttribute("data-grid-id");
-    const fragmentIndex = target.getAttribute("data-grid-fragment-index");
-    const color = target.getAttribute("stroke");
-    const rowNumber = target.getAttribute("data-grid-row");
-    const columnNumber = target.getAttribute("data-grid-column");
-
-    target.setAttribute("fill", color);
-
-    if (name) {
-      onShowGridAreaHighlight(grids[id].nodeFront, name, color);
-    }
-
-    if (fragmentIndex && rowNumber && columnNumber) {
-      onShowGridCellHighlight(grids[id].nodeFront, fragmentIndex,
-        rowNumber, columnNumber);
-    }
+  onMouseOverCell(event) {
+    event.persist();
+    this.highlightCell(event);
   },
 
   render() {
     return this.state.selectedGrids.length ?
       dom.svg(
         {
           className: "grid-outline",
           width: "100%",