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. if (!this.controls.isOpaque()) {
  73. return;
  74. }
  75. this.controls.togglePiP();
  76. });
  77. this.eventManager.listen(this.localVideo_, 'enterpictureinpicture', () => {
  78. this.onEnterPictureInPicture_();
  79. });
  80. this.eventManager.listen(this.localVideo_, 'leavepictureinpicture', () => {
  81. this.onLeavePictureInPicture_();
  82. });
  83. this.eventManager.listen(this.controls, 'caststatuschanged', () => {
  84. this.onTracksChanged_();
  85. });
  86. this.eventManager.listen(this.player, 'trackschanged', () => {
  87. this.onTracksChanged_();
  88. });
  89. if ('documentPictureInPicture' in window) {
  90. this.eventManager.listen(window.documentPictureInPicture, 'enter',
  91. (e) => {
  92. this.onEnterPictureInPicture_();
  93. const event = /** @type {DocumentPictureInPictureEvent} */(e);
  94. const pipWindow = event.window;
  95. this.eventManager.listenOnce(pipWindow, 'pagehide', () => {
  96. this.onLeavePictureInPicture_();
  97. });
  98. });
  99. }
  100. }
  101. /** @private */
  102. onEnterPictureInPicture_() {
  103. const LocIds = shaka.ui.Locales.Ids;
  104. this.pipIcon_.textContent = shaka.ui.Enums.MaterialDesignIcons.EXIT_PIP;
  105. this.pipButton_.ariaLabel =
  106. this.localization.resolve(LocIds.EXIT_PICTURE_IN_PICTURE);
  107. this.currentPipState_.textContent =
  108. this.localization.resolve(LocIds.ON);
  109. }
  110. /** @private */
  111. onLeavePictureInPicture_() {
  112. const LocIds = shaka.ui.Locales.Ids;
  113. this.pipIcon_.textContent = shaka.ui.Enums.MaterialDesignIcons.PIP;
  114. this.pipButton_.ariaLabel =
  115. this.localization.resolve(LocIds.ENTER_PICTURE_IN_PICTURE);
  116. this.currentPipState_.textContent =
  117. this.localization.resolve(LocIds.OFF);
  118. }
  119. /**
  120. * @private
  121. */
  122. updateLocalizedStrings_() {
  123. const LocIds = shaka.ui.Locales.Ids;
  124. this.pipNameSpan_.textContent =
  125. this.localization.resolve(LocIds.PICTURE_IN_PICTURE);
  126. const enabled = this.controls.isPiPEnabled();
  127. const ariaLabel = enabled ?
  128. LocIds.EXIT_PICTURE_IN_PICTURE :
  129. LocIds.ENTER_PICTURE_IN_PICTURE;
  130. this.pipButton_.ariaLabel = this.localization.resolve(ariaLabel);
  131. const currentPipState = enabled ? LocIds.ON : LocIds.OFF;
  132. this.currentPipState_.textContent =
  133. this.localization.resolve(currentPipState);
  134. }
  135. /**
  136. * Display the picture-in-picture button only when the content contains video.
  137. * If it's displaying in picture-in-picture mode, and an audio only content is
  138. * loaded, exit the picture-in-picture display.
  139. * @return {!Promise}
  140. * @private
  141. */
  142. async onTracksChanged_() {
  143. if (!this.controls.isPiPAllowed()) {
  144. shaka.ui.Utils.setDisplay(this.pipButton_, false);
  145. if (this.controls.isPiPEnabled()) {
  146. await this.controls.togglePiP();
  147. }
  148. } else if (this.player && this.player.isAudioOnly()) {
  149. shaka.ui.Utils.setDisplay(this.pipButton_, false);
  150. if (this.controls.isPiPEnabled()) {
  151. await this.controls.togglePiP();
  152. }
  153. } else {
  154. shaka.ui.Utils.setDisplay(this.pipButton_, true);
  155. }
  156. }
  157. };
  158. /**
  159. * @implements {shaka.extern.IUIElement.Factory}
  160. * @final
  161. */
  162. shaka.ui.PipButton.Factory = class {
  163. /** @override */
  164. create(rootElement, controls) {
  165. return new shaka.ui.PipButton(rootElement, controls);
  166. }
  167. };
  168. shaka.ui.OverflowMenu.registerElement(
  169. 'picture_in_picture', new shaka.ui.PipButton.Factory());
  170. shaka.ui.Controls.registerElement(
  171. 'picture_in_picture', new shaka.ui.PipButton.Factory());
  172. shaka.ui.ContextMenu.registerElement(
  173. 'picture_in_picture', new shaka.ui.PipButton.Factory());