From 3ae96ff98a0fbec35b1f88cd5a7a0cadf6b1503a Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 13 Nov 2021 09:18:59 +0100 Subject: [PATCH] Fix issues with rendering and freezing on scrolling Signed-off-by: trivernis --- .../file-gallery-entry.component.ts | 15 ++++++++--- .../file-gallery/file-gallery.component.ts | 25 +++++++++++-------- .../file-grid-entry.component.ts | 14 ++++++++--- .../file-grid/file-grid.component.html | 2 +- .../home/files-tab/files-tab.component.html | 2 +- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/mediarepo-ui/src/app/components/file-gallery/file-gallery-entry/file-gallery-entry.component.ts b/mediarepo-ui/src/app/components/file-gallery/file-gallery-entry/file-gallery-entry.component.ts index bed53c7..cf2c617 100644 --- a/mediarepo-ui/src/app/components/file-gallery/file-gallery-entry/file-gallery-entry.component.ts +++ b/mediarepo-ui/src/app/components/file-gallery/file-gallery-entry/file-gallery-entry.component.ts @@ -25,19 +25,26 @@ export class FileGalleryEntryComponent implements OnInit, OnChanges { contentUrl: SafeResourceUrl | undefined; private cachedFile: File | undefined; + private urlSetTimeout: number | undefined; constructor(@Inject(DomSanitizer) private sanitizer: DomSanitizer, private fileService: FileService, private errorBroker: ErrorBrokerService) { } - async ngOnChanges(changes: SimpleChanges): Promise { + ngOnChanges(changes: SimpleChanges) { if (changes["file"] && (!this.cachedFile || this.file.data.hash !== this.cachedFile!.hash)) { // handle changes to the file when the component is not destroyed this.cachedFile = this.file.data; - this.contentUrl = this.fileService.buildThumbnailUrl(this.file.data, 250, 250); + this.setImageDelayed(); } } - async ngOnInit() { + ngOnInit() { this.cachedFile = this.file.data; - this.contentUrl = this.fileService.buildThumbnailUrl(this.file.data, 250, 250); + this.setImageDelayed(); + } + + private setImageDelayed() { + this.contentUrl = undefined; + clearTimeout(this.urlSetTimeout); + this.urlSetTimeout = setTimeout(() => this.contentUrl = this.fileService.buildThumbnailUrl(this.file.data, 250, 250), 500); } } diff --git a/mediarepo-ui/src/app/components/file-gallery/file-gallery.component.ts b/mediarepo-ui/src/app/components/file-gallery/file-gallery.component.ts index 37a6864..9d8c911 100644 --- a/mediarepo-ui/src/app/components/file-gallery/file-gallery.component.ts +++ b/mediarepo-ui/src/app/components/file-gallery/file-gallery.component.ts @@ -43,6 +43,8 @@ export class FileGalleryComponent implements OnChanges, OnInit { public imagePosition = {x: 0, y: 0}; public mouseInImageView = false; + private scrollTimeout: number | undefined; + constructor(private tabService: TabService, private fileService: FileService) { tabService.selectedTab.subscribe(() => this.adjustElementSizes()); } @@ -61,7 +63,8 @@ export class FileGalleryComponent implements OnChanges, OnInit { await this.loadSelectedFile(); if (this.virtualScroll) { - this.scrollToSelection(); + clearTimeout(this.scrollTimeout); + this.scrollTimeout = setTimeout(() => this.scrollToSelection(), 0); // we need to make sure the viewport has rendered } this.fileSelectEvent.emit(this.selectedFile.data); @@ -100,15 +103,17 @@ export class FileGalleryComponent implements OnChanges, OnInit { } public async ngOnChanges(changes: SimpleChanges): Promise { - this.entries = this.files.map( - f => new Selectable(f, f.hash == this.selectedFile?.data.hash)); - const selectedIndex = this.files.findIndex( - f => f.hash === this.selectedFile?.data.hash); - - if (!this.selectedFile || selectedIndex < 0) { - await this.onEntrySelect(this.getPreselectedEntry() ?? this.entries[0]) - } else { - await this.onEntrySelect(this.entries[selectedIndex]) + if (changes["files"]) { + this.entries = this.files.map( + f => new Selectable(f, f.hash == this.selectedFile?.data.hash)); + const selectedIndex = this.files.findIndex( + f => f.hash === this.selectedFile?.data.hash); + + if (!this.selectedFile || selectedIndex < 0) { + await this.onEntrySelect(this.getPreselectedEntry() ?? this.entries[0]) + } else { + await this.onEntrySelect(this.entries[selectedIndex]) + } } } diff --git a/mediarepo-ui/src/app/components/file-grid/file-grid-entry/file-grid-entry.component.ts b/mediarepo-ui/src/app/components/file-grid/file-grid-entry/file-grid-entry.component.ts index e249ee7..a634e03 100644 --- a/mediarepo-ui/src/app/components/file-grid/file-grid-entry/file-grid-entry.component.ts +++ b/mediarepo-ui/src/app/components/file-grid/file-grid-entry/file-grid-entry.component.ts @@ -29,21 +29,27 @@ export class FileGridEntryComponent implements OnInit, OnChanges { contentUrl: SafeResourceUrl | undefined; private cachedFile: File | undefined; + private urlSetTimeout: number | undefined; constructor(private fileService: FileService, private errorBroker: ErrorBrokerService) { } async ngOnInit() { this.cachedFile = this.gridEntry.file; - this.contentUrl = this.fileService.buildThumbnailUrl(this.gridEntry.file, - 250, 250); + this.setImageDelayed(); } async ngOnChanges(changes: SimpleChanges) { if (changes["file"] && (!this.cachedFile || this.gridEntry.file.hash !== this.cachedFile.hash)) { this.cachedFile = this.gridEntry.file; - this.contentUrl = this.fileService.buildThumbnailUrl(this.gridEntry.file, - 250, 250); + this.setImageDelayed(); } } + + private setImageDelayed() { + this.contentUrl = undefined; + clearTimeout(this.urlSetTimeout); + this.urlSetTimeout = setTimeout(() => this.contentUrl = this.fileService.buildThumbnailUrl(this.gridEntry.file, + 250, 250), 500); + } } diff --git a/mediarepo-ui/src/app/components/file-grid/file-grid.component.html b/mediarepo-ui/src/app/components/file-grid/file-grid.component.html index 5a12fe0..48af744 100644 --- a/mediarepo-ui/src/app/components/file-grid/file-grid.component.html +++ b/mediarepo-ui/src/app/components/file-grid/file-grid.component.html @@ -1,5 +1,5 @@