tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PauseResumeButton.js (2520B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const {
      8  createRef,
      9  PureComponent,
     10 } = require("resource://devtools/client/shared/vendor/react.mjs");
     11 const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
     12 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs");
     13 
     14 const { KeyCodes } = require("resource://devtools/client/shared/keycodes.js");
     15 
     16 const {
     17  getStr,
     18 } = require("resource://devtools/client/inspector/animation/utils/l10n.js");
     19 const {
     20  hasRunningAnimation,
     21 } = require("resource://devtools/client/inspector/animation/utils/utils.js");
     22 
     23 class PauseResumeButton extends PureComponent {
     24  static get propTypes() {
     25    return {
     26      animations: PropTypes.arrayOf(PropTypes.object).isRequired,
     27      setAnimationsPlayState: PropTypes.func.isRequired,
     28    };
     29  }
     30 
     31  constructor(props) {
     32    super(props);
     33 
     34    this.onKeyDown = this.onKeyDown.bind(this);
     35    this.pauseResumeButtonRef = createRef();
     36  }
     37 
     38  componentDidMount() {
     39    const targetEl = this.getKeyEventTarget();
     40    targetEl.addEventListener("keydown", this.onKeyDown);
     41  }
     42 
     43  componentWillUnount() {
     44    const targetEl = this.getKeyEventTarget();
     45    targetEl.removeEventListener("keydown", this.onKeyDown);
     46  }
     47 
     48  getKeyEventTarget() {
     49    return this.pauseResumeButtonRef.current.closest("#animation-container");
     50  }
     51 
     52  onToggleAnimationsPlayState(event) {
     53    event.stopPropagation();
     54    const { setAnimationsPlayState, animations } = this.props;
     55    const isRunning = hasRunningAnimation(animations);
     56 
     57    setAnimationsPlayState(!isRunning);
     58  }
     59 
     60  onKeyDown(event) {
     61    // Prevent to the duplicated call from the key listener and click listener.
     62    if (
     63      event.keyCode === KeyCodes.DOM_VK_SPACE &&
     64      event.target !== this.pauseResumeButtonRef.current
     65    ) {
     66      this.onToggleAnimationsPlayState(event);
     67    }
     68  }
     69 
     70  render() {
     71    const isRunning = hasRunningAnimation(this.props.animations);
     72 
     73    return dom.button({
     74      className:
     75        "pause-resume-button devtools-button" + (isRunning ? "" : " paused"),
     76      onClick: this.onToggleAnimationsPlayState.bind(this),
     77      title: isRunning
     78        ? getStr("timeline.resumedButtonTooltip")
     79        : getStr("timeline.pausedButtonTooltip"),
     80      ref: this.pauseResumeButtonRef,
     81    });
     82  }
     83 }
     84 
     85 module.exports = PauseResumeButton;