OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 'use strict'; |
| 6 |
| 7 /** |
| 8 * Displays error message. |
| 9 * @param {string} message Message id. |
| 10 */ |
| 11 function showErrorMessage(message) { |
| 12 var errorBanner = document.querySelector('#error'); |
| 13 errorBanner.textContent = |
| 14 loadTimeData.getString(message); |
| 15 errorBanner.setAttribute('visible', 'true'); |
| 16 |
| 17 // The window is hidden if the video has not loaded yet. |
| 18 chrome.app.window.current().show(); |
| 19 } |
| 20 |
| 21 /** |
| 22 * Handles playback (decoder) errors. |
| 23 */ |
| 24 function onPlaybackError() { |
| 25 showErrorMessage('GALLERY_VIDEO_DECODING_ERROR'); |
| 26 decodeErrorOccured = true; |
| 27 |
| 28 // Disable inactivity watcher, and disable the ui, by hiding tools manually. |
| 29 controls.inactivityWatcher.disabled = true; |
| 30 document.querySelector('#video-player').setAttribute('disabled', 'true'); |
| 31 |
| 32 // Detach the video element, since it may be unreliable and reset stored |
| 33 // current playback time. |
| 34 controls.cleanup(); |
| 35 controls.clearState(); |
| 36 |
| 37 // Avoid reusing a video element. |
| 38 video.parentNode.removeChild(video); |
| 39 video = null; |
| 40 } |
| 41 |
| 42 /** |
| 43 * @param {Element} playerContainer Main container. |
| 44 * @param {Element} videoContainer Container for the video element. |
| 45 * @param {Element} controlsContainer Container for video controls. |
| 46 * @constructor |
| 47 */ |
| 48 function FullWindowVideoControls( |
| 49 playerContainer, videoContainer, controlsContainer) { |
| 50 VideoControls.call(this, |
| 51 controlsContainer, |
| 52 onPlaybackError, |
| 53 loadTimeData.getString.bind(loadTimeData), |
| 54 this.toggleFullScreen_.bind(this), |
| 55 videoContainer); |
| 56 |
| 57 this.playerContainer_ = playerContainer; |
| 58 |
| 59 this.updateStyle(); |
| 60 window.addEventListener('resize', this.updateStyle.bind(this)); |
| 61 |
| 62 document.addEventListener('keydown', function(e) { |
| 63 if (e.keyIdentifier == 'U+0020') { // Space |
| 64 this.togglePlayStateWithFeedback(); |
| 65 e.preventDefault(); |
| 66 } |
| 67 if (e.keyIdentifier == 'U+001B') { // Escape |
| 68 util.toggleFullScreen( |
| 69 chrome.app.window.current(), |
| 70 false); // Leave the full screen mode. |
| 71 e.preventDefault(); |
| 72 } |
| 73 }.bind(this)); |
| 74 |
| 75 // TODO(mtomasz): Simplify. crbug.com/254318. |
| 76 videoContainer.addEventListener('click', function(e) { |
| 77 if (e.ctrlKey) { |
| 78 this.toggleLoopedModeWithFeedback(true); |
| 79 if (!this.isPlaying()) |
| 80 this.togglePlayStateWithFeedback(); |
| 81 } else { |
| 82 this.togglePlayStateWithFeedback(); |
| 83 } |
| 84 }.bind(this)); |
| 85 |
| 86 this.inactivityWatcher_ = new MouseInactivityWatcher(playerContainer); |
| 87 this.__defineGetter__('inactivityWatcher', function() { |
| 88 return this.inactivityWatcher_; |
| 89 }); |
| 90 |
| 91 this.inactivityWatcher_.check(); |
| 92 |
| 93 Object.seal(this); |
| 94 } |
| 95 |
| 96 FullWindowVideoControls.prototype = { __proto__: VideoControls.prototype }; |
| 97 |
| 98 /** |
| 99 * Toggles the full screen mode. |
| 100 * @private |
| 101 */ |
| 102 FullWindowVideoControls.prototype.toggleFullScreen_ = function() { |
| 103 var appWindow = chrome.app.window.current(); |
| 104 util.toggleFullScreen(appWindow, !util.isFullScreen(appWindow)); |
| 105 }; |
| 106 |
| 107 // TODO(mtomasz): Convert it to class members: crbug.com/171191. |
| 108 var decodeErrorOccured; |
| 109 var video; |
| 110 var controls; |
| 111 |
| 112 /** |
| 113 * Initializes the video player window. |
| 114 */ |
| 115 function loadVideoPlayer() { |
| 116 document.ondragstart = function(e) { e.preventDefault() }; |
| 117 |
| 118 chrome.fileBrowserPrivate.getStrings(function(strings) { |
| 119 loadTimeData.data = strings; |
| 120 |
| 121 var url = window.videoUrl; |
| 122 document.title = window.videoTitle; |
| 123 |
| 124 controls = new FullWindowVideoControls( |
| 125 document.querySelector('#video-player'), |
| 126 document.querySelector('#video-container'), |
| 127 document.querySelector('#controls')); |
| 128 |
| 129 var reloadVideo = function(e) { |
| 130 if (decodeErrorOccured && |
| 131 // Ignore shortcut keys |
| 132 !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) { |
| 133 loadVideo(url); |
| 134 e.preventDefault(); |
| 135 } |
| 136 }; |
| 137 |
| 138 loadVideo(url); |
| 139 document.addEventListener('keydown', reloadVideo, true); |
| 140 document.addEventListener('click', reloadVideo, true); |
| 141 }); |
| 142 } |
| 143 |
| 144 /** |
| 145 * Unloads the player. |
| 146 */ |
| 147 function unload() { |
| 148 if (!controls.getMedia()) |
| 149 return; |
| 150 |
| 151 controls.savePosition(true /* exiting */); |
| 152 controls.cleanup(); |
| 153 } |
| 154 |
| 155 /** |
| 156 * Reloads the player. |
| 157 * @param {string} url URL of the video file. |
| 158 */ |
| 159 function loadVideo(url) { |
| 160 // Re-enable ui and hide error message if already displayed. |
| 161 document.querySelector('#video-player').removeAttribute('disabled'); |
| 162 document.querySelector('#error').removeAttribute('visible'); |
| 163 controls.inactivityWatcher.disabled = false; |
| 164 decodeErrorOccured = false; |
| 165 |
| 166 // Detach the previous video element, if exists. |
| 167 if (video) |
| 168 video.parentNode.removeChild(video); |
| 169 |
| 170 video = document.createElement('video'); |
| 171 document.querySelector('#video-container').appendChild(video); |
| 172 controls.attachMedia(video); |
| 173 |
| 174 video.src = url; |
| 175 video.load(); |
| 176 video.addEventListener('loadedmetadata', function() { |
| 177 // TODO: chrome.app.window soon will be able to resize the content area. |
| 178 // Until then use approximate title bar height. |
| 179 var TITLE_HEIGHT = 33; |
| 180 |
| 181 var aspect = video.videoWidth / video.videoHeight; |
| 182 var newWidth = video.videoWidth; |
| 183 var newHeight = video.videoHeight + TITLE_HEIGHT; |
| 184 |
| 185 var shrinkX = newWidth / window.screen.availWidth; |
| 186 var shrinkY = newHeight / window.screen.availHeight; |
| 187 if (shrinkX > 1 || shrinkY > 1) { |
| 188 if (shrinkY > shrinkX) { |
| 189 newHeight = newHeight / shrinkY; |
| 190 newWidth = (newHeight - TITLE_HEIGHT) * aspect; |
| 191 } else { |
| 192 newWidth = newWidth / shrinkX; |
| 193 newHeight = newWidth / aspect + TITLE_HEIGHT; |
| 194 } |
| 195 } |
| 196 |
| 197 var oldLeft = window.screenX; |
| 198 var oldTop = window.screenY; |
| 199 var oldWidth = window.outerWidth; |
| 200 var oldHeight = window.outerHeight; |
| 201 |
| 202 if (!oldWidth && !oldHeight) { |
| 203 oldLeft = window.screen.availWidth / 2; |
| 204 oldTop = window.screen.availHeight / 2; |
| 205 } |
| 206 |
| 207 var appWindow = chrome.app.window.current(); |
| 208 appWindow.resizeTo(newWidth, newHeight); |
| 209 appWindow.moveTo(oldLeft - (newWidth - oldWidth) / 2, |
| 210 oldTop - (newHeight - oldHeight) / 2); |
| 211 appWindow.show(); |
| 212 |
| 213 video.play(); |
| 214 }); |
| 215 } |
| 216 |
| 217 util.addPageLoadHandler(loadVideoPlayer); |
OLD | NEW |