Commit 46355144 authored by Esmael El-Moslimany's avatar Esmael El-Moslimany Committed by Commit Bot

Video Player WebUI: replace paper-slider with cr-slider

The video player progress needs to be updated from the slider value
only when the change is due to a user interaction. For this
reason cr-slider-value-changed-from-ui was introduced.

cr-slider getRatio() was made public since it is meaningful
representation of the slider value.

Swapped the skip direction of ArrowLeft/ArrowRight when the text
direction is RTL.

When the volume value is changed with the keyboard and the media
controls are hidden, the media controls are shown.

Bug: 902873
Change-Id: Ide0a23783274d4c11ea83b367aba8b41a5dd8ad1
Reviewed-on: https://chromium-review.googlesource.com/c/1357216Reviewed-by: default avatarLuciano Pacheco <lucmult@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Esmael El-Moslimany <aee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#613384}
parent 9b34c2c9
...@@ -364,4 +364,29 @@ suite('cr-slider', function() { ...@@ -364,4 +364,29 @@ suite('cr-slider', function() {
assertNoTransition(); assertNoTransition();
}); });
}); });
test('getRatio()', () => {
crSlider.min = 1;
crSlider.max = 11;
crSlider.value = 1;
assertEquals(0, crSlider.getRatio());
crSlider.value = 11;
assertEquals(1, crSlider.getRatio());
crSlider.value = 6;
assertEquals(.5, crSlider.getRatio());
});
test('cr-slider-value-changed-from-ui event when mouse clicked', () => {
const wait =
test_util.eventToPromise('cr-slider-value-changed-from-ui', crSlider);
pointerDown(0);
return wait;
});
test('cr-slider-value-changed-from-ui event when key pressed', () => {
const wait =
test_util.eventToPromise('cr-slider-value-changed-from-ui', crSlider);
pressArrowRight();
return wait;
});
}); });
...@@ -19,8 +19,7 @@ ...@@ -19,8 +19,7 @@
} }
.media-button.disabled, .media-button.disabled,
paper-slider[disabled], cr-slider.readonly {
paper-slider.readonly {
pointer-events: none; pointer-events: none;
} }
...@@ -112,7 +111,7 @@ paper-slider.readonly { ...@@ -112,7 +111,7 @@ paper-slider.readonly {
/* Volume slider. */ /* Volume slider. */
paper-slider.volume { cr-slider.volume {
height: 100%; height: 100%;
position: relative; position: relative;
width: 110px; width: 110px;
......
...@@ -41,7 +41,7 @@ js_library("error_util") { ...@@ -41,7 +41,7 @@ js_library("error_util") {
js_library("media_controls") { js_library("media_controls") {
deps = [ deps = [
"../../file_manager/common/js:util", "../../file_manager/common/js:util",
"//third_party/polymer/v1_0/components-chromium/paper-slider:paper-slider-extracted", "//ui/webui/resources/cr_elements/cr_slider:cr_slider",
"//ui/webui/resources/js/cr/ui:menu_button", "//ui/webui/resources/js/cr/ui:menu_button",
] ]
} }
......
...@@ -101,13 +101,13 @@ function MediaControls(containerElement, onMediaError) { ...@@ -101,13 +101,13 @@ function MediaControls(containerElement, onMediaError) {
this.playButton_ = null; this.playButton_ = null;
/** /**
* @type {PaperSliderElement} * @type {CrSliderElement}
* @private * @private
*/ */
this.progressSlider_ = null; this.progressSlider_ = null;
/** /**
* @type {PaperSliderElement} * @type {CrSliderElement}
* @private * @private
*/ */
this.volume_ = null; this.volume_ = null;
...@@ -372,20 +372,21 @@ MediaControls.prototype.initTimeControls = function(opt_parent) { ...@@ -372,20 +372,21 @@ MediaControls.prototype.initTimeControls = function(opt_parent) {
// Set the initial width to the minimum to reduce the flicker. // Set the initial width to the minimum to reduce the flicker.
this.updateTimeLabel_(0, 0); this.updateTimeLabel_(0, 0);
this.progressSlider_ = /** @type {!PaperSliderElement} */ ( this.progressSlider_ =
document.createElement('paper-slider')); /** @type {!CrSliderElement} */ (document.createElement('cr-slider'));
this.progressSlider_.classList.add('progress', 'media-control'); this.progressSlider_.classList.add('progress', 'media-control');
this.progressSlider_.max = MediaControls.PROGRESS_RANGE; this.progressSlider_.max = MediaControls.PROGRESS_RANGE;
this.progressSlider_.noKeybindings = true;
this.progressSlider_.setAttribute('aria-label', this.progressSlider_.setAttribute('aria-label',
str('MEDIA_PLAYER_SEEK_SLIDER_LABEL')); str('MEDIA_PLAYER_SEEK_SLIDER_LABEL'));
this.progressSlider_.addEventListener('change', function(event) { this.progressSlider_.addEventListener('dragging-changed', event => {
this.onProgressChange_(this.progressSlider_.ratio / 100); this.setSeeking_(event.detail.value);
}.bind(this)); });
this.progressSlider_.addEventListener( this.progressSlider_.addEventListener(
'immediate-value-change', 'cr-slider-value-changed-from-ui', () => {
function(event) { this.onProgressChange_();
this.onProgressDrag_(); this.updateTimeFromSlider_();
}.bind(this)); });
timeControls.appendChild(this.progressSlider_); timeControls.appendChild(this.progressSlider_);
}; };
...@@ -400,11 +401,19 @@ MediaControls.prototype.displayProgress_ = function(current, duration) { ...@@ -400,11 +401,19 @@ MediaControls.prototype.displayProgress_ = function(current, duration) {
this.updateTimeLabel_(current); this.updateTimeLabel_(current);
}; };
/** /** @private */
* @param {number} value Progress [0..1]. MediaControls.prototype.updateTimeFromSlider_ = function() {
* @private if (!this.media_)
*/ return; // Media is detached.
MediaControls.prototype.onProgressChange_ = function(value) {
if (this.media_.duration && this.progressSlider_.max > 0) {
this.media_.currentTime =
this.media_.duration * this.progressSlider_.getRatio();
}
};
/** @private */
MediaControls.prototype.onProgressChange_ = function() {
if (!this.media_) if (!this.media_)
return; // Media is detached. return; // Media is detached.
...@@ -413,32 +422,13 @@ MediaControls.prototype.onProgressChange_ = function(value) { ...@@ -413,32 +422,13 @@ MediaControls.prototype.onProgressChange_ = function(value) {
return; return;
} }
this.setSeeking_(false);
// Re-start playing the video when the seek bar is moved from ending point. // Re-start playing the video when the seek bar is moved from ending point.
if (this.media_.ended) if (this.media_.ended)
this.play(); this.play();
var current = this.media_.duration * value;
this.media_.currentTime = current;
this.updateTimeLabel_(current);
};
/**
* @private
*/
MediaControls.prototype.onProgressDrag_ = function() {
if (!this.media_)
return; // Media is detached.
this.setSeeking_(true);
// Show seeking position instead of playing position while dragging.
if (this.media_.duration && this.progressSlider_.max > 0) { if (this.media_.duration && this.progressSlider_.max > 0) {
var immediateRatio = this.updateTimeLabel_(
this.progressSlider_.immediateValue / this.progressSlider_.max; this.media_.duration * this.progressSlider_.getRatio());
var current = this.media_.duration * immediateRatio;
this.updateTimeLabel_(current);
} }
}; };
...@@ -454,7 +444,8 @@ MediaControls.prototype.skip_ = function(sec) { ...@@ -454,7 +444,8 @@ MediaControls.prototype.skip_ = function(sec) {
this.progressSlider_.value = Math.max(Math.min( this.progressSlider_.value = Math.max(Math.min(
this.progressSlider_.value + stepsToSkip, this.progressSlider_.value + stepsToSkip,
this.progressSlider_.max), 0); this.progressSlider_.max), 0);
this.onProgressChange_(this.progressSlider_.ratio / 100); this.onProgressChange_();
this.updateTimeFromSlider_();
} }
}; };
...@@ -578,17 +569,13 @@ MediaControls.prototype.initVolumeControls = function(opt_parent) { ...@@ -578,17 +569,13 @@ MediaControls.prototype.initVolumeControls = function(opt_parent) {
this.soundButton_.setAttribute('aria-label', this.soundButton_.setAttribute('aria-label',
str('MEDIA_PLAYER_MUTE_BUTTON_LABEL')); str('MEDIA_PLAYER_MUTE_BUTTON_LABEL'));
this.volume_ = /** @type {!PaperSliderElement} */ ( this.volume_ =
document.createElement('paper-slider')); /** @type {!CrSliderElement} */ (document.createElement('cr-slider'));
this.volume_.classList.add('volume', 'media-control'); this.volume_.classList.add('volume', 'media-control');
this.volume_.setAttribute('aria-label', this.volume_.setAttribute('aria-label',
str('MEDIA_PLAYER_VOLUME_SLIDER_LABEL')); str('MEDIA_PLAYER_VOLUME_SLIDER_LABEL'));
this.volume_.addEventListener('change', function(event) { this.volume_.addEventListener(
this.onVolumeChange_(this.volume_.ratio / 100); 'cr-slider-value-changed-from-ui', this.onVolumeChange_.bind(this));
}.bind(this));
this.volume_.addEventListener('immediate-value-change', function(event) {
this.onVolumeDrag_();
}.bind(this));
this.loadVolumeControlState(); this.loadVolumeControlState();
volumeControls.appendChild(this.volume_); volumeControls.appendChild(this.volume_);
}; };
...@@ -655,27 +642,17 @@ MediaControls.prototype.reflectVolumeToUi_ = function() { ...@@ -655,27 +642,17 @@ MediaControls.prototype.reflectVolumeToUi_ = function() {
/** /**
* Handles change event of the volume slider. * Handles change event of the volume slider.
* @param {number} value Volume [0..1].
* @private * @private
*/ */
MediaControls.prototype.onVolumeChange_ = function(value) { MediaControls.prototype.onVolumeChange_ = function() {
if (!this.media_) if (!this.media_)
return; // Media is detached. return; // Media is detached.
this.volumeModel_.onVolumeChanged(value); this.volumeModel_.onVolumeChanged(this.volume_.getRatio());
this.saveVolumeControlState(); this.saveVolumeControlState();
this.reflectVolumeToUi_(); this.reflectVolumeToUi_();
}; };
/**
* @private
*/
MediaControls.prototype.onVolumeDrag_ = function() {
if (this.media_.volume !== 0) {
this.volumeModel_.onVolumeChanged(this.media_.volume);
}
};
/** /**
* Initializes subtitles button. * Initializes subtitles button.
*/ */
......
...@@ -22,6 +22,8 @@ function FullWindowVideoControls( ...@@ -22,6 +22,8 @@ function FullWindowVideoControls(
this.decodeErrorOccured = false; this.decodeErrorOccured = false;
this.casting = false; this.casting = false;
this.isRtl_ =
window.getComputedStyle(this.playerContainer_)['direction'] === 'rtl';
var currentWindow = chrome.app.window.current(); var currentWindow = chrome.app.window.current();
currentWindow.onFullscreened.addListener(this.onFullScreenChanged.bind(this)); currentWindow.onFullscreened.addListener(this.onFullScreenChanged.bind(this));
...@@ -62,17 +64,17 @@ function FullWindowVideoControls( ...@@ -62,17 +64,17 @@ function FullWindowVideoControls(
break; break;
case 'ArrowRight': case 'ArrowRight':
if (!e.target.classList.contains('volume')) if (!e.target.classList.contains('volume'))
this.smallSkip(true); this.smallSkip(!this.isRtl_ /* forward */);
break; break;
case 'ArrowLeft': case 'ArrowLeft':
if (!e.target.classList.contains('volume')) if (!e.target.classList.contains('volume'))
this.smallSkip(false); this.smallSkip(this.isRtl_ /* forward */);
break; break;
case 'l': case 'l':
this.bigSkip(true); this.bigSkip(true /* forward */);
break; break;
case 'j': case 'j':
this.bigSkip(false); this.bigSkip(false /* forward */);
break; break;
case 'BrowserBack': case 'BrowserBack':
chrome.app.window.current().close(); chrome.app.window.current().close();
...@@ -85,6 +87,9 @@ function FullWindowVideoControls( ...@@ -85,6 +87,9 @@ function FullWindowVideoControls(
document.addEventListener('keypress', function(e) { document.addEventListener('keypress', function(e) {
this.inactivityWatcher_.kick(); this.inactivityWatcher_.kick();
}.wrap(this)); }.wrap(this));
controlsContainer.addEventListener('cr-slider-value-changed-from-ui', () => {
this.inactivityWatcher_.kick();
});
// TODO(mtomasz): Simplify. crbug.com/254318. // TODO(mtomasz): Simplify. crbug.com/254318.
var clickInProgress = false; var clickInProgress = false;
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
<link rel="stylesheet" type="text/css" href="css/cast_menu.css"> <link rel="stylesheet" type="text/css" href="css/cast_menu.css">
<link rel="import" href="chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/foreground/elements/files_icon_button.html"> <link rel="import" href="chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/foreground/elements/files_icon_button.html">
<link rel="import" href="chrome://resources/polymer/v1_0/paper-slider/paper-slider.html"> <link rel="import" href="chrome://resources/cr_elements/cr_slider/cr_slider.html">
<script src="js/video_player_scripts.js"></script> <script src="js/video_player_scripts.js"></script>
</head> </head>
...@@ -35,11 +35,11 @@ ...@@ -35,11 +35,11 @@
}; };
} }
paper-slider { cr-slider {
cursor: pointer; cursor: pointer;
--paper-slider-knob-color: rgb(64, 138, 241); --cr-slider-knob-color: rgb(64, 138, 241);
--paper-slider-active-color: rgb(66, 133, 244); --cr-slider-active-color: rgb(66, 133, 244);
} }
</style> </style>
</custom-style> </custom-style>
......
...@@ -34,6 +34,13 @@ cr_slider.SliderTick; ...@@ -34,6 +34,13 @@ cr_slider.SliderTick;
return Math.min(max, Math.max(min, value)); return Math.min(max, Math.max(min, value));
} }
/**
* The following are the events emitted from cr-slider.
*
* cr-slider-value-changed-from-ui: fired when updating slider via the UI.
* dragging-changed: fired on pointer down and on pointer up.
* value-changed: fired anytime |value| is changed, manually or via the UI.
*/
Polymer({ Polymer({
is: 'cr-slider', is: 'cr-slider',
...@@ -231,7 +238,7 @@ cr_slider.SliderTick; ...@@ -231,7 +238,7 @@ cr_slider.SliderTick;
* @private * @private
*/ */
getMarkerClass_: function(index) { getMarkerClass_: function(index) {
const currentStep = (this.markerCount - 1) * this.getRatio_(); const currentStep = (this.markerCount - 1) * this.getRatio();
return index < currentStep ? 'active-marker' : 'inactive-marker'; return index < currentStep ? 'active-marker' : 'inactive-marker';
}, },
...@@ -241,9 +248,8 @@ cr_slider.SliderTick; ...@@ -241,9 +248,8 @@ cr_slider.SliderTick;
* This is a helper function used to calculate the bar width, knob location * This is a helper function used to calculate the bar width, knob location
* and label location. * and label location.
* @return {number} * @return {number}
* @private
*/ */
getRatio_: function() { getRatio: function() {
return (this.immediateValue_ - this.min) / (this.max - this.min); return (this.immediateValue_ - this.min) / (this.max - this.min);
}, },
...@@ -315,14 +321,15 @@ cr_slider.SliderTick; ...@@ -315,14 +321,15 @@ cr_slider.SliderTick;
} else if (event.key == 'End') { } else if (event.key == 'End') {
this.immediateValue_ = this.max; this.immediateValue_ = this.max;
} else if (this.deltaKeyMap_.has(event.key)) { } else if (this.deltaKeyMap_.has(event.key)) {
const newValue = this.value + this.deltaKeyMap_.get(event.key); const value = this.value + this.deltaKeyMap_.get(event.key);
this.immediateValue_ = clamp(this.min, this.max, newValue); this.immediateValue_ = clamp(this.min, this.max, value);
} else { } else {
handled = false; handled = false;
} }
if (handled) { if (handled) {
this.value = this.immediateValue_; this.value = this.immediateValue_;
this.fire('cr-slider-value-changed-from-ui');
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
setTimeout(() => { setTimeout(() => {
...@@ -407,7 +414,7 @@ cr_slider.SliderTick; ...@@ -407,7 +414,7 @@ cr_slider.SliderTick;
/** @private */ /** @private */
updateKnobAndBar_: function() { updateKnobAndBar_: function() {
const percent = `${this.getRatio_() * 100}%`; const percent = `${this.getRatio() * 100}%`;
this.$.bar.style.width = percent; this.$.bar.style.width = percent;
this.$.knob.style.marginInlineStart = percent; this.$.knob.style.marginInlineStart = percent;
}, },
...@@ -434,7 +441,7 @@ cr_slider.SliderTick; ...@@ -434,7 +441,7 @@ cr_slider.SliderTick;
const labelWidth = label.offsetWidth; const labelWidth = label.offsetWidth;
// The left and right margin are 16px. // The left and right margin are 16px.
const margin = 16; const margin = 16;
const knobLocation = parentWidth * this.getRatio_() + margin; const knobLocation = parentWidth * this.getRatio() + margin;
const offsetStart = knobLocation - (labelWidth / 2); const offsetStart = knobLocation - (labelWidth / 2);
// The label should be centered over the knob. Clamping the offset to a // The label should be centered over the knob. Clamping the offset to a
// min and max value prevents the label from being cutoff. // min and max value prevents the label from being cutoff.
...@@ -467,6 +474,7 @@ cr_slider.SliderTick; ...@@ -467,6 +474,7 @@ cr_slider.SliderTick;
ratio = 1 - ratio; ratio = 1 - ratio;
this.immediateValue_ = ratio * (this.max - this.min) + this.min; this.immediateValue_ = ratio * (this.max - this.min) + this.min;
this.ensureValidValue_(); this.ensureValidValue_();
this.fire('cr-slider-value-changed-from-ui');
}, },
_createRipple: function() { _createRipple: function() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment