summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/animation/components/PauseResumeButton.js
blob: bcbb597a3917fdfe80cbaf2af9e3a495bce1ac45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* 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 {
  createRef,
  PureComponent,
} = require("resource://devtools/client/shared/vendor/react.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");

const { KeyCodes } = require("resource://devtools/client/shared/keycodes.js");

const {
  getStr,
} = require("resource://devtools/client/inspector/animation/utils/l10n.js");
const {
  hasRunningAnimation,
} = require("resource://devtools/client/inspector/animation/utils/utils.js");

class PauseResumeButton extends PureComponent {
  static get propTypes() {
    return {
      animations: PropTypes.arrayOf(PropTypes.object).isRequired,
      setAnimationsPlayState: PropTypes.func.isRequired,
    };
  }

  constructor(props) {
    super(props);

    this.onKeyDown = this.onKeyDown.bind(this);
    this.pauseResumeButtonRef = createRef();

    this.state = {
      isRunning: false,
    };
  }

  // FIXME: https://bugzilla.mozilla.org/show_bug.cgi?id=1774507
  UNSAFE_componentWillMount() {
    this.updateState(this.props);
  }

  componentDidMount() {
    const targetEl = this.getKeyEventTarget();
    targetEl.addEventListener("keydown", this.onKeyDown);
  }

  componentDidUpdate() {
    this.updateState();
  }

  componentWillUnount() {
    const targetEl = this.getKeyEventTarget();
    targetEl.removeEventListener("keydown", this.onKeyDown);
  }

  getKeyEventTarget() {
    return this.pauseResumeButtonRef.current.closest("#animation-container");
  }

  onToggleAnimationsPlayState(event) {
    event.stopPropagation();
    const { setAnimationsPlayState } = this.props;
    const { isRunning } = this.state;

    setAnimationsPlayState(!isRunning);
  }

  onKeyDown(event) {
    // Prevent to the duplicated call from the key listener and click listener.
    if (
      event.keyCode === KeyCodes.DOM_VK_SPACE &&
      event.target !== this.pauseResumeButtonRef.current
    ) {
      this.onToggleAnimationsPlayState(event);
    }
  }

  updateState() {
    const { animations } = this.props;
    const isRunning = hasRunningAnimation(animations);
    this.setState({ isRunning });
  }

  render() {
    const { isRunning } = this.state;

    return dom.button({
      className:
        "pause-resume-button devtools-button" + (isRunning ? "" : " paused"),
      onClick: this.onToggleAnimationsPlayState.bind(this),
      title: isRunning
        ? getStr("timeline.resumedButtonTooltip")
        : getStr("timeline.pausedButtonTooltip"),
      ref: this.pauseResumeButtonRef,
    });
  }
}

module.exports = PauseResumeButton;