Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: chrome/browser/resources/file_manager/foreground/js/photo/slide_mode.js

Issue 109973002: Migrate from URLs to Entries in the Files App's gallery. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleaned up. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * Slide mode displays a single image and has a set of controls to navigate 8 * Slide mode displays a single image and has a set of controls to navigate
9 * between the images and to edit an image. 9 * between the images and to edit an image.
10 * 10 *
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 this.savedSelection_ = null; 281 this.savedSelection_ = null;
282 else 282 else
283 this.savedSelection_ = currentSelection; 283 this.savedSelection_ = currentSelection;
284 284
285 // Ensure valid single selection. 285 // Ensure valid single selection.
286 // Note that the SlideMode object is not listening to selection change yet. 286 // Note that the SlideMode object is not listening to selection change yet.
287 this.select(Math.max(0, this.getSelectedIndex())); 287 this.select(Math.max(0, this.getSelectedIndex()));
288 this.displayedIndex_ = this.getSelectedIndex(); 288 this.displayedIndex_ = this.getSelectedIndex();
289 289
290 var selectedItem = this.getSelectedItem(); 290 var selectedItem = this.getSelectedItem();
291 var selectedUrl = selectedItem.getUrl();
292 // Show the selected item ASAP, then complete the initialization 291 // Show the selected item ASAP, then complete the initialization
293 // (loading the ribbon thumbnails can take some time). 292 // (loading the ribbon thumbnails can take some time).
294 this.metadataCache_.get(selectedUrl, Gallery.METADATA_TYPE, 293 this.metadataCache_.get(selectedItem.getEntry(), Gallery.METADATA_TYPE,
295 function(metadata) { 294 function(metadata) {
296 this.loadItem_(selectedUrl, metadata, 295 this.loadItem_(selectedItem.getEntry(), metadata,
297 zoomFromRect && this.imageView_.createZoomEffect(zoomFromRect), 296 zoomFromRect && this.imageView_.createZoomEffect(zoomFromRect),
298 displayCallback, loadDone); 297 displayCallback, loadDone);
299 }.bind(this)); 298 }.bind(this));
300 299
301 } 300 }
302 }; 301 };
303 302
304 /** 303 /**
305 * Leave the mode. 304 * Leave the mode.
306 * @param {Rect} zoomToRect Rectangle for zoom effect. 305 * @param {Rect} zoomToRect Rectangle for zoom effect.
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 this.sequenceLength_++; 469 this.sequenceLength_++;
471 } else { 470 } else {
472 // Reversed the direction. Reset the counter. 471 // Reversed the direction. Reset the counter.
473 this.sequenceDirection_ = step; 472 this.sequenceDirection_ = step;
474 this.sequenceLength_ = 1; 473 this.sequenceLength_ = 1;
475 } 474 }
476 475
477 if (this.sequenceLength_ <= 1) { 476 if (this.sequenceLength_ <= 1) {
478 // We have just broke the sequence. Touch the current image so that it stays 477 // We have just broke the sequence. Touch the current image so that it stays
479 // in the cache longer. 478 // in the cache longer.
480 this.imageView_.prefetch(this.imageView_.contentID_); 479 this.imageView_.prefetch(this.imageView_.contentEntry_);
481 } 480 }
482 481
483 this.displayedIndex_ = index; 482 this.displayedIndex_ = index;
484 483
485 function shouldPrefetch(loadType, step, sequenceLength) { 484 function shouldPrefetch(loadType, step, sequenceLength) {
486 // Never prefetch when selecting out of sequence. 485 // Never prefetch when selecting out of sequence.
487 if (Math.abs(step) != 1) 486 if (Math.abs(step) != 1)
488 return false; 487 return false;
489 488
490 // Never prefetch after a video load (decoding the next image can freeze 489 // Never prefetch after a video load (decoding the next image can freeze
491 // the UI for a second or two). 490 // the UI for a second or two).
492 if (loadType == ImageView.LOAD_TYPE_VIDEO_FILE) 491 if (loadType == ImageView.LOAD_TYPE_VIDEO_FILE)
493 return false; 492 return false;
494 493
495 // Always prefetch if the previous load was from cache. 494 // Always prefetch if the previous load was from cache.
496 if (loadType == ImageView.LOAD_TYPE_CACHED_FULL) 495 if (loadType == ImageView.LOAD_TYPE_CACHED_FULL)
497 return true; 496 return true;
498 497
499 // Prefetch if we have been going in the same direction for long enough. 498 // Prefetch if we have been going in the same direction for long enough.
500 return sequenceLength >= 3; 499 return sequenceLength >= 3;
501 } 500 }
502 501
503 var selectedItem = this.getSelectedItem(); 502 var selectedItem = this.getSelectedItem();
504 this.currentUniqueKey_++; 503 this.currentUniqueKey_++;
505 var selectedUniqueKey = this.currentUniqueKey_; 504 var selectedUniqueKey = this.currentUniqueKey_;
506 var onMetadata = function(metadata) { 505 var onMetadata = function(metadata) {
507 // Discard, since another load has been invoked after this one. 506 // Discard, since another load has been invoked after this one.
508 if (selectedUniqueKey != this.currentUniqueKey_) return; 507 if (selectedUniqueKey != this.currentUniqueKey_) return;
509 this.loadItem_(selectedItem.getUrl(), metadata, 508 this.loadItem_(selectedItem.getEntry(), metadata,
510 new ImageView.Effect.Slide(step, this.isSlideshowPlaying_()), 509 new ImageView.Effect.Slide(step, this.isSlideshowPlaying_()),
511 function() {} /* no displayCallback */, 510 function() {} /* no displayCallback */,
512 function(loadType, delay) { 511 function(loadType, delay) {
513 // Discard, since another load has been invoked after this one. 512 // Discard, since another load has been invoked after this one.
514 if (selectedUniqueKey != this.currentUniqueKey_) return; 513 if (selectedUniqueKey != this.currentUniqueKey_) return;
515 if (shouldPrefetch(loadType, step, this.sequenceLength_)) { 514 if (shouldPrefetch(loadType, step, this.sequenceLength_)) {
516 this.requestPrefetch(step, delay); 515 this.requestPrefetch(step, delay);
517 } 516 }
518 if (this.isSlideshowPlaying_()) 517 if (this.isSlideshowPlaying_())
519 this.scheduleNextSlide_(); 518 this.scheduleNextSlide_();
520 }.bind(this)); 519 }.bind(this));
521 }.bind(this); 520 }.bind(this);
522 this.metadataCache_.get( 521 this.metadataCache_.get(
523 selectedItem.getUrl(), Gallery.METADATA_TYPE, onMetadata); 522 selectedItem.getEntry(), Gallery.METADATA_TYPE, onMetadata);
524 }; 523 };
525 524
526 /** 525 /**
527 * Unload the current image. 526 * Unload the current image.
528 * 527 *
529 * @param {Rect} zoomToRect Rectangle for zoom effect. 528 * @param {Rect} zoomToRect Rectangle for zoom effect.
530 * @private 529 * @private
531 */ 530 */
532 SlideMode.prototype.unloadImage_ = function(zoomToRect) { 531 SlideMode.prototype.unloadImage_ = function(zoomToRect) {
533 this.imageView_.unload(zoomToRect); 532 this.imageView_.unload(zoomToRect);
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 */ 638 */
640 SlideMode.prototype.selectLast = function() { 639 SlideMode.prototype.selectLast = function() {
641 this.select(this.getItemCount_() - 1); 640 this.select(this.getItemCount_() - 1);
642 }; 641 };
643 642
644 // Loading/unloading 643 // Loading/unloading
645 644
646 /** 645 /**
647 * Load and display an item. 646 * Load and display an item.
648 * 647 *
649 * @param {string} url Item url. 648 * @param {FileEntry} entry Item entry to be loaded.
650 * @param {Object} metadata Item metadata. 649 * @param {Object} metadata Item metadata.
651 * @param {Object} effect Transition effect object. 650 * @param {Object} effect Transition effect object.
652 * @param {function} displayCallback Called when the image is displayed 651 * @param {function} displayCallback Called when the image is displayed
653 * (which can happen before the image load due to caching). 652 * (which can happen before the image load due to caching).
654 * @param {function} loadCallback Called when the image is fully loaded. 653 * @param {function} loadCallback Called when the image is fully loaded.
655 * @private 654 * @private
656 */ 655 */
657 SlideMode.prototype.loadItem_ = function( 656 SlideMode.prototype.loadItem_ = function(
658 url, metadata, effect, displayCallback, loadCallback) { 657 entry, metadata, effect, displayCallback, loadCallback) {
659 this.selectedImageMetadata_ = MetadataCache.cloneMetadata(metadata); 658 this.selectedImageMetadata_ = MetadataCache.cloneMetadata(metadata);
660 659
661 this.showSpinner_(true); 660 this.showSpinner_(true);
662 661
663 var loadDone = function(loadType, delay, error) { 662 var loadDone = function(loadType, delay, error) {
664 var video = this.isShowingVideo_(); 663 var video = this.isShowingVideo_();
665 ImageUtil.setAttribute(this.container_, 'video', video); 664 ImageUtil.setAttribute(this.container_, 'video', video);
666 665
667 this.showSpinner_(false); 666 this.showSpinner_(false);
668 if (loadType == ImageView.LOAD_TYPE_ERROR) { 667 if (loadType == ImageView.LOAD_TYPE_ERROR) {
(...skipping 23 matching lines...) Expand all
692 return Math.round(number / (1000 * 1000)); 691 return Math.round(number / (1000 * 1000));
693 }; 692 };
694 693
695 ImageUtil.metrics.recordSmallCount(ImageUtil.getMetricName('Size.MB'), 694 ImageUtil.metrics.recordSmallCount(ImageUtil.getMetricName('Size.MB'),
696 toMillions(metadata.filesystem.size)); 695 toMillions(metadata.filesystem.size));
697 696
698 var canvas = this.imageView_.getCanvas(); 697 var canvas = this.imageView_.getCanvas();
699 ImageUtil.metrics.recordSmallCount(ImageUtil.getMetricName('Size.MPix'), 698 ImageUtil.metrics.recordSmallCount(ImageUtil.getMetricName('Size.MPix'),
700 toMillions(canvas.width * canvas.height)); 699 toMillions(canvas.width * canvas.height));
701 700
702 var extIndex = url.lastIndexOf('.'); 701 var extIndex = entry.name.lastIndexOf('.');
703 var ext = extIndex < 0 ? '' : url.substr(extIndex + 1).toLowerCase(); 702 var ext = extIndex < 0 ? '' :
703 entry.name.substr(extIndex + 1).toLowerCase();
704 if (ext == 'jpeg') ext = 'jpg'; 704 if (ext == 'jpeg') ext = 'jpg';
705 ImageUtil.metrics.recordEnum( 705 ImageUtil.metrics.recordEnum(
706 ImageUtil.getMetricName('FileType'), ext, ImageUtil.FILE_TYPES); 706 ImageUtil.getMetricName('FileType'), ext, ImageUtil.FILE_TYPES);
707 } 707 }
708 708
709 // Enable or disable buttons for editing and printing. 709 // Enable or disable buttons for editing and printing.
710 if (video || error) { 710 if (video || error) {
711 this.editButton_.setAttribute('disabled', ''); 711 this.editButton_.setAttribute('disabled', '');
712 this.printButton_.setAttribute('disabled', ''); 712 this.printButton_.setAttribute('disabled', '');
713 } else { 713 } else {
(...skipping 18 matching lines...) Expand all
732 }.bind(this)); 732 }.bind(this));
733 733
734 loadCallback(loadType, delay); 734 loadCallback(loadType, delay);
735 }.bind(this); 735 }.bind(this);
736 736
737 var displayDone = function() { 737 var displayDone = function() {
738 cr.dispatchSimpleEvent(this, 'image-displayed'); 738 cr.dispatchSimpleEvent(this, 'image-displayed');
739 displayCallback(); 739 displayCallback();
740 }.bind(this); 740 }.bind(this);
741 741
742 this.editor_.openSession(url, metadata, effect, 742 this.editor_.openSession(entry, metadata, effect,
743 this.saveCurrentImage_.bind(this), displayDone, loadDone); 743 this.saveCurrentImage_.bind(this), displayDone, loadDone);
744 }; 744 };
745 745
746 /** 746 /**
747 * Commit changes to the current item and reset all messages/indicators. 747 * Commit changes to the current item and reset all messages/indicators.
748 * 748 *
749 * @param {function} callback Callback. 749 * @param {function} callback Callback.
750 * @private 750 * @private
751 */ 751 */
752 SlideMode.prototype.commitItem_ = function(callback) { 752 SlideMode.prototype.commitItem_ = function(callback) {
(...skipping 21 matching lines...) Expand all
774 * Request a prefetch for the next image. 774 * Request a prefetch for the next image.
775 * 775 *
776 * @param {number} direction -1 or 1. 776 * @param {number} direction -1 or 1.
777 * @param {number} delay Delay in ms. Used to prevent the CPU-heavy image 777 * @param {number} delay Delay in ms. Used to prevent the CPU-heavy image
778 * loading from disrupting the animation that might be still in progress. 778 * loading from disrupting the animation that might be still in progress.
779 */ 779 */
780 SlideMode.prototype.requestPrefetch = function(direction, delay) { 780 SlideMode.prototype.requestPrefetch = function(direction, delay) {
781 if (this.getItemCount_() <= 1) return; 781 if (this.getItemCount_() <= 1) return;
782 782
783 var index = this.getNextSelectedIndex_(direction); 783 var index = this.getNextSelectedIndex_(direction);
784 var nextItemUrl = this.getItem(index).getUrl(); 784 var nextItemEntry = this.getItem(index).getEntry();
785 this.imageView_.prefetch(nextItemUrl, delay); 785 this.imageView_.prefetch(nextItemEntry, delay);
786 }; 786 };
787 787
788 // Event handlers. 788 // Event handlers.
789 789
790 /** 790 /**
791 * Unload handler, to be called from the top frame. 791 * Unload handler, to be called from the top frame.
792 * @param {boolean} exiting True if the app is exiting. 792 * @param {boolean} exiting True if the app is exiting.
793 */ 793 */
794 SlideMode.prototype.onUnload = function(exiting) { 794 SlideMode.prototype.onUnload = function(exiting) {
795 if (this.isShowingVideo_() && this.mediaControls_.isPlaying()) { 795 if (this.isShowingVideo_() && this.mediaControls_.isPlaying()) {
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
935 // Saving 935 // Saving
936 936
937 /** 937 /**
938 * Save the current image to a file. 938 * Save the current image to a file.
939 * 939 *
940 * @param {function} callback Callback. 940 * @param {function} callback Callback.
941 * @private 941 * @private
942 */ 942 */
943 SlideMode.prototype.saveCurrentImage_ = function(callback) { 943 SlideMode.prototype.saveCurrentImage_ = function(callback) {
944 var item = this.getSelectedItem(); 944 var item = this.getSelectedItem();
945 var oldUrl = item.getUrl(); 945 var oldEntry = item.getEntry();
946 var canvas = this.imageView_.getCanvas(); 946 var canvas = this.imageView_.getCanvas();
947 947
948 this.showSpinner_(true); 948 this.showSpinner_(true);
949 var metadataEncoder = ImageEncoder.encodeMetadata( 949 var metadataEncoder = ImageEncoder.encodeMetadata(
950 this.selectedImageMetadata_.media, canvas, 1 /* quality */); 950 this.selectedImageMetadata_.media, canvas, 1 /* quality */);
951 951
952 this.selectedImageMetadata_ = ContentProvider.ConvertContentMetadata( 952 this.selectedImageMetadata_ = ContentProvider.ConvertContentMetadata(
953 metadataEncoder.getMetadata(), this.selectedImageMetadata_); 953 metadataEncoder.getMetadata(), this.selectedImageMetadata_);
954 954
955 item.saveToFile( 955 item.saveToFile(
956 this.context_.saveDirEntry, 956 this.context_.saveDirEntry,
957 this.shouldOverwriteOriginal_(), 957 this.shouldOverwriteOriginal_(),
958 canvas, 958 canvas,
959 metadataEncoder, 959 metadataEncoder,
960 function(success) { 960 function(success) {
961 // TODO(kaznacheev): Implement write error handling. 961 // TODO(kaznacheev): Implement write error handling.
962 // Until then pretend that the save succeeded. 962 // Until then pretend that the save succeeded.
963 this.showSpinner_(false); 963 this.showSpinner_(false);
964 this.flashSavedLabel_(); 964 this.flashSavedLabel_();
965 965
966 var e = new Event('content'); 966 var e = new Event('content');
hirono 2013/12/09 06:53:50 Please replace e with event.
mtomasz 2013/12/10 02:37:34 Done.
967 e.item = item; 967 e.item = item;
968 e.oldUrl = oldUrl; 968 e.oldEntry = oldEntry;
969 e.metadata = this.selectedImageMetadata_; 969 e.metadata = this.selectedImageMetadata_;
970 this.dataModel_.dispatchEvent(e); 970 this.dataModel_.dispatchEvent(e);
971 971
972 // Allow changing the 'Overwrite original' setting only if the user 972 // Allow changing the 'Overwrite original' setting only if the user
973 // used Undo to restore the original image AND it is not a copy. 973 // used Undo to restore the original image AND it is not a copy.
974 // Otherwise lock the setting in its current state. 974 // Otherwise lock the setting in its current state.
975 var mayChangeOverwrite = !this.editor_.canUndo() && item.isOriginal(); 975 var mayChangeOverwrite = !this.editor_.canUndo() && item.isOriginal();
976 ImageUtil.setAttribute(this.options_, 'saved', !mayChangeOverwrite); 976 ImageUtil.setAttribute(this.options_, 'saved', !mayChangeOverwrite);
977 977
978 if (this.imageView_.getContentRevision() == 1) { // First edit. 978 if (this.imageView_.getContentRevision() == 1) { // First edit.
979 ImageUtil.metrics.recordUserAction(ImageUtil.getMetricName('Edit')); 979 ImageUtil.metrics.recordUserAction(ImageUtil.getMetricName('Edit'));
980 } 980 }
981 981
982 if (oldUrl != item.getUrl()) { 982 if (oldEntry.toURL() != item.getEntry().toURL()) {
hirono 2013/12/09 06:53:50 Use util.isSameEntry.
mtomasz 2013/12/10 02:37:34 Done.
983 this.dataModel_.splice( 983 this.dataModel_.splice(
984 this.getSelectedIndex(), 0, new Gallery.Item(oldUrl)); 984 this.getSelectedIndex(), 0, new Gallery.Item(oldEntry));
985 // The ribbon will ignore the splice above and redraw after the 985 // The ribbon will ignore the splice above and redraw after the
986 // select call below (while being obscured by the Editor toolbar, 986 // select call below (while being obscured by the Editor toolbar,
987 // so there is no need for nice animation here). 987 // so there is no need for nice animation here).
988 // SlideMode will ignore the selection change as the displayed item 988 // SlideMode will ignore the selection change as the displayed item
989 // index has not changed. 989 // index has not changed.
990 this.select(++this.displayedIndex_); 990 this.select(++this.displayedIndex_);
991 } 991 }
992 callback(); 992 callback();
993 cr.dispatchSimpleEvent(this, 'image-saved'); 993 cr.dispatchSimpleEvent(this, 'image-saved');
994 }.bind(this)); 994 }.bind(this));
995 }; 995 };
996 996
997 /** 997 /**
998 * Update caches when the selected item url has changed. 998 * Update caches when the selected item has been renamed.
999 *
1000 * @param {Event} event Event. 999 * @param {Event} event Event.
1001 * @private 1000 * @private
1002 */ 1001 */
1003 SlideMode.prototype.onContentChange_ = function(event) { 1002 SlideMode.prototype.onContentChange_ = function(event) {
1004 var newUrl = event.item.getUrl(); 1003 var newEntry = event.item.getEntry();
1005 if (newUrl != event.oldUrl) { 1004 if (newEntry.toURL() != event.oldEntry.toURL()) {
hirono 2013/12/09 06:53:50 ditto.
mtomasz 2013/12/10 02:37:34 Done.
1006 this.imageView_.changeUrl(newUrl); 1005 this.imageView_.changeEntry(newEntry);
1007 } 1006 }
1008 this.metadataCache_.clear(event.oldUrl, Gallery.METADATA_TYPE); 1007 this.metadataCache_.clear(event.oldEntry, Gallery.METADATA_TYPE);
1009 }; 1008 };
1010 1009
1011 /** 1010 /**
1012 * Flash 'Saved' label briefly to indicate that the image has been saved. 1011 * Flash 'Saved' label briefly to indicate that the image has been saved.
1013 * @private 1012 * @private
1014 */ 1013 */
1015 SlideMode.prototype.flashSavedLabel_ = function() { 1014 SlideMode.prototype.flashSavedLabel_ = function() {
1016 var setLabelHighlighted = 1015 var setLabelHighlighted =
1017 ImageUtil.setAttribute.bind(null, this.savedLabel_, 'highlighted'); 1016 ImageUtil.setAttribute.bind(null, this.savedLabel_, 'highlighted');
1018 setTimeout(setLabelHighlighted.bind(null, true), 0); 1017 setTimeout(setLabelHighlighted.bind(null, true), 0);
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1347 done = true; 1346 done = true;
1348 } 1347 }
1349 }.bind(this); 1348 }.bind(this);
1350 }; 1349 };
1351 1350
1352 /** 1351 /**
1353 * If the user touched the image and moved the finger more than SWIPE_THRESHOLD 1352 * If the user touched the image and moved the finger more than SWIPE_THRESHOLD
1354 * horizontally it's considered as a swipe gesture (change the current image). 1353 * horizontally it's considered as a swipe gesture (change the current image).
1355 */ 1354 */
1356 SwipeOverlay.SWIPE_THRESHOLD = 100; 1355 SwipeOverlay.SWIPE_THRESHOLD = 100;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698