Skip to content

Commit

Permalink
fix(picture-in-picture-toggle): hide the component in non-compatible …
Browse files Browse the repository at this point in the history
…browsers, e.g. Firefox
  • Loading branch information
amtins committed May 21, 2023
1 parent 3329b2f commit e135cc7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
10 changes: 1 addition & 9 deletions src/js/control-bar/control-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* @file control-bar.js
*/
import Component from '../component.js';
import document from 'global/document';

// Required children
import './play-toggle.js';
Expand Down Expand Up @@ -73,17 +72,10 @@ ControlBar.prototype.options_ = {
'descriptionsButton',
'subsCapsButton',
'audioTrackButton',
'pictureInPictureToggle',
'fullscreenToggle'
]
};

if ('exitPictureInPicture' in document) {
ControlBar.prototype.options_.children.splice(
ControlBar.prototype.options_.children.length - 1,
0,
'pictureInPictureToggle'
);
}

Component.registerComponent('ControlBar', ControlBar);
export default ControlBar;
15 changes: 14 additions & 1 deletion src/js/control-bar/picture-in-picture-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PictureInPictureToggle extends Button {
*/
constructor(player, options) {
super(player, options);

this.on(player, ['enterpictureinpicture', 'leavepictureinpicture'], (e) => this.handlePictureInPictureChange(e));
this.on(player, ['disablepictureinpicturechanged', 'loadedmetadata'], (e) => this.handlePictureInPictureEnabledChange(e));
this.on(player, ['loadedmetadata', 'audioonlymodechange', 'audiopostermodechange'], () => this.handlePictureInPictureAudioModeChange());
Expand All @@ -42,7 +43,7 @@ class PictureInPictureToggle extends Button {
* The DOM `className` for this object.
*/
buildCSSClass() {
return `vjs-picture-in-picture-control ${super.buildCSSClass()}`;
return `vjs-picture-in-picture-control vjs-hidden ${super.buildCSSClass()}`;
}

/**
Expand Down Expand Up @@ -126,6 +127,18 @@ class PictureInPictureToggle extends Button {
}
}

/**
* Show the `Component`s element if it is hidden by removing the
* 'vjs-hidden' class name from it only in browsers that support the Picture-in-Picture API.
*/
show() {
// Does not allow to display the pictureInPictureToggle in browsers that do not support the Picture-in-Picture API, e.g. Firefox.
if (typeof document.exitPictureInPicture !== 'function') {
return;
}

super.show();
}
}

/**
Expand Down
30 changes: 28 additions & 2 deletions test/unit/controls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,11 @@ QUnit.test('Picture-in-Picture control is hidden when the source is audio', func
player.src({src: 'example.mp4', type: 'video/mp4'});
player.trigger('loadedmetadata');

assert.notOk(pictureInPictureToggle.hasClass('vjs-hidden'), 'pictureInPictureToggle button is not hidden initially');
if (document.exitPictureInPicture) {
assert.notOk(pictureInPictureToggle.hasClass('vjs-hidden'), 'pictureInPictureToggle button is not hidden initially');
} else {
assert.ok(pictureInPictureToggle.hasClass('vjs-hidden'), 'pictureInPictureToggle button is hidden if PiP is not supported');
}

player.src({src: 'example1.mp3', type: 'audio/mp3'});
player.trigger('loadedmetadata');
Expand All @@ -316,7 +320,11 @@ QUnit.test('Picture-in-Picture control is displayed if docPiP is enabled', funct
player.src({src: 'example.mp4', type: 'video/mp4'});
player.trigger('loadedmetadata');

assert.notOk(pictureInPictureToggle.hasClass('vjs-hidden'), 'pictureInPictureToggle button is not hidden');
if (document.exitPictureInPicture) {
assert.notOk(pictureInPictureToggle.hasClass('vjs-hidden'), 'pictureInPictureToggle button is not hidden');
} else {
assert.ok(pictureInPictureToggle.hasClass('vjs-hidden'), 'pictureInPictureToggle button is hidden if PiP is not supported');
}

player.dispose();
pictureInPictureToggle.dispose();
Expand All @@ -325,6 +333,24 @@ QUnit.test('Picture-in-Picture control is displayed if docPiP is enabled', funct
}
});

QUnit.test('Picture-in-Picture control should only be displayed if the browser supports it', function(assert) {
const player = TestHelpers.makePlayer();
const pictureInPictureToggle = new PictureInPictureToggle(player);

player.trigger('loadedmetadata');

if (document.exitPictureInPicture) {
// Browser that does support PiP
assert.false(pictureInPictureToggle.hasClass('vjs-hidden'), 'pictureInPictureToggle button is not hidden');
} else {
// Browser that does not support PiP
assert.true(pictureInPictureToggle.hasClass('vjs-hidden'), 'pictureInPictureToggle button is hidden');
}

player.dispose();
pictureInPictureToggle.dispose();
});

QUnit.test('Fullscreen control text should be correct when fullscreenchange is triggered', function(assert) {
const player = TestHelpers.makePlayer({controlBar: false});
const fullscreentoggle = new FullscreenToggle(player);
Expand Down
15 changes: 12 additions & 3 deletions test/unit/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3150,15 +3150,20 @@ QUnit.test('audioOnlyMode(true/false) hides/shows video-specific control bar com
controlBar.getChild('ChaptersButton').update();

player.trigger('ready');
player.trigger('loadedmetadata');
player.hasStarted(true);

// Show all control bar children
allChildren.forEach(child => {
const el = controlBar.getChild(child) && controlBar.getChild(child).el_;

if (el) {
// Sanity check that component is showing
assert.notEqual(TestHelpers.getComputedStyle(el, 'display'), 'none', `${child} is initially visible`);
if (!document.exitPictureInPicture && child === 'PictureInPictureToggle') {
assert.equal(TestHelpers.getComputedStyle(el, 'display'), 'none', `${child} is not visible if PiP is not supported`);
} else {
// Sanity check that component is showing
assert.notEqual(TestHelpers.getComputedStyle(el, 'display'), 'none', `${child} is initially visible`);
}
}
});

Expand Down Expand Up @@ -3187,7 +3192,11 @@ QUnit.test('audioOnlyMode(true/false) hides/shows video-specific control bar com
const el = controlBar.getChild(child) && controlBar.getChild(child).el_;

if (el) {
assert.notEqual(TestHelpers.getComputedStyle(el, 'display'), 'none', `${child} is shown`);
if (!document.exitPictureInPicture && child === 'PictureInPictureToggle') {
assert.equal(TestHelpers.getComputedStyle(el, 'display'), 'none', `${child} is not visible if PiP is not supported`);
} else {
assert.notEqual(TestHelpers.getComputedStyle(el, 'display'), 'none', `${child} is shown`);
}
}
});
})
Expand Down

0 comments on commit e135cc7

Please sign in to comment.