Source: ui/pip_button.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ui.PipButton');
  7. goog.require('shaka.ui.ContextMenu');
  8. goog.require('shaka.ui.Controls');
  9. goog.require('shaka.ui.Element');
  10. goog.require('shaka.ui.Enums');
  11. goog.require('shaka.ui.Locales');
  12. goog.require('shaka.ui.Localization');
  13. goog.require('shaka.ui.OverflowMenu');
  14. goog.require('shaka.ui.Utils');
  15. goog.require('shaka.util.Dom');
  16. goog.requireType('shaka.ui.Controls');
  17. /**
  18. * @extends {shaka.ui.Element}
  19. * @final
  20. * @export
  21. */
  22. shaka.ui.PipButton = class extends shaka.ui.Element {
  23. /**
  24. * @param {!HTMLElement} parent
  25. * @param {!shaka.ui.Controls} controls
  26. */
  27. constructor(parent, controls) {
  28. super(parent, controls);
  29. /** @private {HTMLMediaElement} */
  30. this.localVideo_ = this.controls.getLocalVideo();
  31. /** @private {HTMLElement } */
  32. this.videoContainer_ = this.controls.getVideoContainer();
  33. const LocIds = shaka.ui.Locales.Ids;
  34. /** @private {!HTMLButtonElement} */
  35. this.pipButton_ = shaka.util.Dom.createButton();
  36. this.pipButton_.classList.add('shaka-pip-button');
  37. this.pipButton_.classList.add('shaka-tooltip');
  38. /** @private {!HTMLElement} */
  39. this.pipIcon_ = shaka.util.Dom.createHTMLElement('i');
  40. this.pipIcon_.classList.add('material-icons-round');
  41. this.pipIcon_.textContent = shaka.ui.Enums.MaterialDesignIcons.PIP;
  42. this.pipButton_.appendChild(this.pipIcon_);
  43. const label = shaka.util.Dom.createHTMLElement('label');
  44. label.classList.add('shaka-overflow-button-label');
  45. label.classList.add('shaka-overflow-menu-only');
  46. this.pipNameSpan_ = shaka.util.Dom.createHTMLElement('span');
  47. this.pipNameSpan_.textContent =
  48. this.localization.resolve(LocIds.PICTURE_IN_PICTURE);
  49. label.appendChild(this.pipNameSpan_);
  50. /** @private {!HTMLElement} */
  51. this.currentPipState_ = shaka.util.Dom.createHTMLElement('span');
  52. this.currentPipState_.classList.add('shaka-current-selection-span');
  53. label.appendChild(this.currentPipState_);
  54. this.pipButton_.appendChild(label);
  55. this.updateLocalizedStrings_();
  56. this.parent.appendChild(this.pipButton_);
  57. // Don't display the button if PiP is not supported or not allowed.
  58. // TODO: Can this ever change? Is it worth creating the button if the below
  59. // condition is true?
  60. if (!this.controls.isPiPAllowed()) {
  61. shaka.ui.Utils.setDisplay(this.pipButton_, false);
  62. }
  63. this.eventManager.listen(
  64. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  65. this.updateLocalizedStrings_();
  66. });
  67. this.eventManager.listen(
  68. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  69. this.updateLocalizedStrings_();
  70. });
  71. this.eventManager.listen(this.pipButton_, 'click', () => {
  72. this.controls.togglePiP();
  73. });
  74. this.eventManager.listen(this.localVideo_, 'enterpictureinpicture', () => {
  75. this.onEnterPictureInPicture_();
  76. });
  77. this.eventManager.listen(this.localVideo_, 'leavepictureinpicture', () => {
  78. this.onLeavePictureInPicture_();
  79. });
  80. this.eventManager.listen(this.controls, 'caststatuschanged', () => {
  81. this.onTracksChanged_();
  82. });
  83. this.eventManager.listen(this.player, 'trackschanged', () => {
  84. this.onTracksChanged_();
  85. });
  86. if ('documentPictureInPicture' in window) {
  87. this.eventManager.listen(window.documentPictureInPicture, 'enter',
  88. (e) => {
  89. this.onEnterPictureInPicture_();
  90. const event = /** @type {DocumentPictureInPictureEvent} */(e);
  91. const pipWindow = event.window;
  92. this.eventManager.listenOnce(pipWindow, 'pagehide', () => {
  93. this.onLeavePictureInPicture_();
  94. });
  95. });
  96. }
  97. }
  98. /** @private */
  99. onEnterPictureInPicture_() {
  100. const LocIds = shaka.ui.Locales.Ids;
  101. this.pipIcon_.textContent = shaka.ui.Enums.MaterialDesignIcons.EXIT_PIP;
  102. this.pipButton_.ariaLabel =
  103. this.localization.resolve(LocIds.EXIT_PICTURE_IN_PICTURE);
  104. this.currentPipState_.textContent =
  105. this.localization.resolve(LocIds.ON);
  106. }
  107. /** @private */
  108. onLeavePictureInPicture_() {
  109. const LocIds = shaka.ui.Locales.Ids;
  110. this.pipIcon_.textContent = shaka.ui.Enums.MaterialDesignIcons.PIP;
  111. this.pipButton_.ariaLabel =
  112. this.localization.resolve(LocIds.ENTER_PICTURE_IN_PICTURE);
  113. this.currentPipState_.textContent =
  114. this.localization.resolve(LocIds.OFF);
  115. }
  116. /**
  117. * @private
  118. */
  119. updateLocalizedStrings_() {
  120. const LocIds = shaka.ui.Locales.Ids;
  121. this.pipNameSpan_.textContent =
  122. this.localization.resolve(LocIds.PICTURE_IN_PICTURE);
  123. const enabled = this.controls.isPiPEnabled();
  124. const ariaLabel = enabled ?
  125. LocIds.EXIT_PICTURE_IN_PICTURE :
  126. LocIds.ENTER_PICTURE_IN_PICTURE;
  127. this.pipButton_.ariaLabel = this.localization.resolve(ariaLabel);
  128. const currentPipState = enabled ? LocIds.ON : LocIds.OFF;
  129. this.currentPipState_.textContent =
  130. this.localization.resolve(currentPipState);
  131. }
  132. /**
  133. * Display the picture-in-picture button only when the content contains video.
  134. * If it's displaying in picture-in-picture mode, and an audio only content is
  135. * loaded, exit the picture-in-picture display.
  136. * @return {!Promise}
  137. * @private
  138. */
  139. async onTracksChanged_() {
  140. if (!this.controls.isPiPAllowed()) {
  141. shaka.ui.Utils.setDisplay(this.pipButton_, false);
  142. if (this.controls.isPiPEnabled()) {
  143. await this.controls.togglePiP();
  144. }
  145. } else if (this.player && this.player.isAudioOnly()) {
  146. shaka.ui.Utils.setDisplay(this.pipButton_, false);
  147. if (this.controls.isPiPEnabled()) {
  148. await this.controls.togglePiP();
  149. }
  150. } else {
  151. shaka.ui.Utils.setDisplay(this.pipButton_, true);
  152. }
  153. }
  154. };
  155. /**
  156. * @implements {shaka.extern.IUIElement.Factory}
  157. * @final
  158. */
  159. shaka.ui.PipButton.Factory = class {
  160. /** @override */
  161. create(rootElement, controls) {
  162. return new shaka.ui.PipButton(rootElement, controls);
  163. }
  164. };
  165. shaka.ui.OverflowMenu.registerElement(
  166. 'picture_in_picture', new shaka.ui.PipButton.Factory());
  167. shaka.ui.Controls.registerElement(
  168. 'picture_in_picture', new shaka.ui.PipButton.Factory());
  169. shaka.ui.ContextMenu.registerElement(
  170. 'picture_in_picture', new shaka.ui.PipButton.Factory());