diff --git a/CHANGELOG.md b/CHANGELOG.md index d64f78db5..bf6dd4465 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ - updated *models -> list models* tab - updated *models -> metadata* tab - more css optimizations and styling + - gallery bypass browser cache for thumbnails + - gallery safer delete operation - **Offloading** - changed **default** values for offloading based on detected gpu memory see [offloading docs](https://vladmandic.github.io/sdnext-docs/Offload/) for details diff --git a/javascript/gallery.js b/javascript/gallery.js index 2de1d70a3..9d94235b3 100644 --- a/javascript/gallery.js +++ b/javascript/gallery.js @@ -192,7 +192,8 @@ async function addSeparators() { async function delayFetchThumb(fn) { while (outstanding > 16) await new Promise((resolve) => setTimeout(resolve, 50)); // eslint-disable-line no-promise-executor-return outstanding++; - const res = await fetch(`${window.api}/browser/thumb?file=${encodeURI(fn)}`, { priority: 'low' }); + const ts = Date.now().toString(); + const res = await fetch(`${window.api}/browser/thumb?file=${encodeURI(fn)}&ts=${ts}`, { priority: 'low' }); if (!res.ok) { error(`fetchThumb: ${res.statusText}`); outstanding--; diff --git a/javascript/ui.js b/javascript/ui.js index 4df527ceb..a499dc32d 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -32,6 +32,12 @@ function clip_gallery_urls(gallery) { ); } +function isVisible(el) { + const rect = el.getBoundingClientRect(); + if (rect.width === 0 && rect.height === 0) return false; + return rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth); +} + function all_gallery_buttons() { let allGalleryButtons = gradioApp().querySelectorAll('[style="display: block;"].tabitem div[id$=_gallery].gradio-gallery .thumbnails > .thumbnail-item.thumbnail-small'); if (allGalleryButtons.length === 0) allGalleryButtons = gradioApp().querySelectorAll('.gradio-gallery .thumbnails > .thumbnail-item.thumbnail-small'); @@ -66,6 +72,7 @@ function selected_gallery_files() { let allCurrentButtons = gradioApp().querySelectorAll('[style="display: block;"].tabitem div[id$=_gallery].gradio-gallery .thumbnail-item.thumbnail-small'); if (allCurrentButtons.length === 0) allCurrentButtons = gradioApp().querySelectorAll('.gradio-gallery .thumbnails > .thumbnail-item.thumbnail-small'); allImages = Array.from(allCurrentButtons).map((v) => v.querySelector('img')?.src); + allImages = allImages.filter((el) => isVisible(el)); } catch { /**/ } const selectedIndex = selected_gallery_index(); return [allImages, selectedIndex]; @@ -178,7 +185,7 @@ function switch_to_caption(...args) { function get_tab_index(tabId) { let res = 0; - gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button') + gradioApp().getElementById(tabId)?.querySelector('div').querySelectorAll('button') .forEach((button, i) => { if (button.className.indexOf('selected') !== -1) res = i; }); diff --git a/modules/ui_common.py b/modules/ui_common.py index 33a76e850..9df1a9ccb 100644 --- a/modules/ui_common.py +++ b/modules/ui_common.py @@ -67,7 +67,7 @@ def delete_files(js_data, files, all_files, index): for _image_index, filedata in enumerate(files, start_index): try: fn = filedata['name'] - if os.path.isfile(fn): + if os.path.exists(fn) and os.path.isfile(fn): deleted.append(fn) os.remove(fn) if fn in all_files: @@ -75,11 +75,11 @@ def delete_files(js_data, files, all_files, index): shared.log.info(f'Delete: image="{fn}"') base, _ext = os.path.splitext(fn) desc = f'{base}.txt' - if os.path.exists(desc): + if os.path.exists(desc) and os.path.isfile(desc): os.remove(desc) shared.log.info(f'Delete: text="{fn}"') except Exception as e: - shared.log.error(f'Delete: image="{fn}" {e}') + shared.log.error(f'Delete: file="{fn}" {e}') deleted = ', '.join(deleted) if len(deleted) > 0 else 'none' return all_files, plaintext_to_html(f"Deleted: {deleted}", ['performance']) diff --git a/modules/ui_gallery.py b/modules/ui_gallery.py index 3b0363359..e6dd3757e 100644 --- a/modules/ui_gallery.py +++ b/modules/ui_gallery.py @@ -6,6 +6,7 @@ from PIL import Image from modules import shared, ui_symbols, ui_common, images, video from modules.ui_components import ToolButton + def read_media(fn): fn = unquote(fn).replace('%3A', ':') if not os.path.isfile(fn):