Implement virtual scrolling and video thumbnails

This commit is contained in:
Alvin-Zilverstand committed 2026-11-19 20:38:02 +01:00
1 parent 74dd0a9cf5
commit 7e3a27cf6d
3 files changed
+139 -84

No files matched your search

+3 -1
View File
@@ -16,7 +16,9 @@
</header> </header>
<main> <main>
<div id="file-list" class="file-list"> <div id="file-list" class="file-list">
<!-- Files will be listed here --> <div id="spacer" style="position: relative; width: 100%;">
<div id="content" style="position: absolute; top: 0; left: 0; width: 100%;"></div>
</div>
</div> </div>
<div id="viewer" class="viewer" style="display: none;"> <div id="viewer" class="viewer" style="display: none;">
<div class="viewer-controls"> <div class="viewer-controls">
+108 -79
View File
@@ -9,13 +9,108 @@ interface FileInfo {
extension: string | null; extension: string | null;
} }
let fileListEl: HTMLElement | null; let fileListContainer: HTMLElement | null;
let spacerElement: HTMLElement | null;
let contentElement: HTMLElement | null;
let titleEl: HTMLElement | null; let titleEl: HTMLElement | null;
let viewerEl: HTMLElement | null; let viewerEl: HTMLElement | null;
let viewerContentEl: HTMLElement | null; let viewerContentEl: HTMLElement | null;
let videoThumbElement: HTMLVideoElement | null; let videoThumbElement: HTMLVideoElement | null;
let canvasElement: HTMLCanvasElement | null; let canvasElement: HTMLCanvasElement | null;
let allFiles: FileInfo[] = [];
const ITEM_HEIGHT = 160; // Approximate height in pixels
const BUFFER_COUNT = 5;
async function listFiles(path: string) {
const files = await invoke<FileInfo[]>("list_files_command", {
dirPath: path,
});
allFiles = files;
fileListContainer = document.getElementById("file-list") as HTMLElement;
titleEl = document.getElementById("app-title") as HTMLElement;
spacerElement = document.getElementById("spacer") as HTMLElement;
contentElement = document.getElementById("content") as HTMLElement;
if (fileListContainer && spacerElement && contentElement) {
titleEl!.textContent = `Files in: ${path}`;
renderVirtualList();
}
}
function renderVirtualList() {
if (!fileListContainer || !spacerElement || !contentElement || !allFiles) return;
const totalHeight = allFiles.length * ITEM_HEIGHT;
spacerElement.style.height = `${totalHeight}px`;
const scrollTop = fileListContainer.scrollTop;
const containerHeight = fileListContainer.clientHeight;
const startIndex = Math.max(0, Math.floor(scrollTop / ITEM_HEIGHT) - BUFFER_COUNT);
const endIndex = Math.min(allFiles.length, Math.ceil((scrollTop + containerHeight) / ITEM_HEIGHT) + BUFFER_COUNT);
contentElement.style.transform = `translateY(${startIndex * ITEM_HEIGHT}px)`;
contentElement.innerHTML = "";
for (let i = startIndex; i < endIndex; i++) {
const file = allFiles[i];
const fileDiv = createFileElement(file);
contentElement.appendChild(fileDiv);
}
}
function createFileElement(file: FileInfo) {
const fileDiv = document.createElement("div");
fileDiv.className = `file-item ${file.is_dir ? "is-dir" : ""}`;
const iconSpan = document.createElement("span");
iconSpan.className = "icon";
if (file.is_dir) {
iconSpan.textContent = "📁";
} else {
iconSpan.className = "icon thumbnail";
iconSpan.textContent = "⏳"; // Loading indicator
loadThumbnail(file, iconSpan);
}
const nameSpan = document.createElement("span");
nameSpan.className = "name";
nameSpan.textContent = file.name;
fileDiv.appendChild(iconSpan);
fileDiv.appendChild(nameSpan);
fileDiv.onclick = () => {
if (file.is_dir) {
// We could open the folder if we wanted
} else {
showViewer(file.path, file.extension);
}
};
return fileDiv;
}
async function loadThumbnail(file: FileInfo, iconSpan: HTMLElement) {
try {
const url = await convertFileUrl(file.path);
if (file.extension && ["mp4", "webm", "ogg", "mov"].includes(file.extension.toLowerCase())) {
const thumbDataUrl = await generateVideoThumbnail(url);
iconSpan.innerHTML = `<img src="${thumbDataUrl}" />`;
} else {
const base64 = await invoke<string>("get_thumbnail_command", { path: file.path });
iconSpan.innerHTML = `<img src="${base64}" />`;
}
} catch (e) {
iconSpan.textContent = "🖼️";
console.error(e);
}
}
async function generateVideoThumbnail(url: string): Promise<string> { async function generateVideoThumbnail(url: string): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!videoThumbElement || !canvasElement) { if (!videoThumbElement || !canvasElement) {
@@ -24,7 +119,7 @@ async function generateVideoThumbnail(url: string): Promise<string> {
} }
videoThumbElement.src = url; videoThumbElement.src = url;
videoThumbElement.currentTime = 1; // Seek to 1s to avoid black frame videoThumbElement.currentTime = 1;
videoThumbElement.load(); videoThumbElement.load();
videoThumbElement.muted = true; videoThumbElement.muted = true;
videoThumbElement.playsInline = true; videoThumbElement.playsInline = true;
@@ -49,82 +144,6 @@ async function generateVideoThumbnail(url: string): Promise<string> {
}); });
} }
async function loadThumbnail(file: FileInfo, iconSpan: HTMLElement) {
try {
const url = await convertFileUrl(file.path);
if (file.extension) {
const ext = file.extension.toLowerCase();
// Video formats for thumbnail generation
if (["mp4", "webm", "ogg", "mov"].includes(ext)) {
try {
const thumbDataUrl = await generateVideoThumbnail(url);
iconSpan.innerHTML = `<img src="${thumbDataUrl}" />`;
} catch (e) {
iconSpan.textContent = "🎬";
}
} else {
// For images, we use the backend thumbnailer (faster/better for large images)
// Or just use the asset URL directly if it's small enough
// But to keep it consistent, let's use the backend for images.
const base64 = await invoke<string>("get_thumbnail_command", { path: file.path });
iconSpan.innerHTML = `<img src="${base64}" />`;
}
} else {
iconSpan.textContent = "📄";
}
} catch (e) {
iconSpan.textContent = "🖼️";
console.error("Thumbnail error:", e);
}
}
async function listFiles(path: string) {
const files = await invoke<FileInfo[]>("list_files_command", {
dirPath: path,
});
fileListEl = document.getElementById("file-list") as HTMLElement;
titleEl = document.getElementById("app-title") as HTMLElement;
if (fileListEl) {
fileListEl.innerHTML = "";
titleEl!.textContent = `Files in: ${path}`;
for (const file of files) {
const fileDiv = document.createElement("div");
fileDiv.className = `file-item ${file.is_dir ? "is-dir" : ""}`;
const iconSpan = document.createElement("span");
iconSpan.className = "icon";
if (file.is_dir) {
iconSpan.textContent = "📁";
} else {
iconSpan.className = "icon thumbnail";
iconSpan.textContent = "⏳"; // Loading indicator
loadThumbnail(file, iconSpan);
}
const nameSpan = document.createElement("span");
nameSpan.className = "name";
nameSpan.textContent = file.name;
fileDiv.appendChild(iconSpan);
fileDiv.appendChild(nameSpan);
fileDiv.onclick = () => {
if (file.is_dir) {
// We could open the folder if we wanted
} else {
showViewer(file.path, file.extension);
}
};
fileListEl?.appendChild(fileDiv);
}
}
}
async function showViewer(filePath: string, extension: string | null) { async function showViewer(filePath: string, extension: string | null) {
viewerEl = document.getElementById("viewer") as HTMLElement; viewerEl = document.getElementById("viewer") as HTMLElement;
viewerContentEl = document.getElementById("viewer-content") as HTMLElement; viewerContentEl = document.getElementById("viewer-content") as HTMLElement;
@@ -169,7 +188,12 @@ async function handleOpenDir() {
} }
} }
// Initialize elements document.getElementById("close-viewer-btn")?.addEventListener("click", () => {
viewerEl!.style.display = "none";
});
document.getElementById("open-dir-btn")?.addEventListener("click", handleOpenDir);
window.addEventListener("DOMContentLoaded", () => { window.addEventListener("DOMContentLoaded", () => {
videoThumbElement = document.getElementById("thumbnail-video") as HTMLVideoElement; videoThumbElement = document.getElementById("thumbnail-video") as HTMLVideoElement;
canvasElement = document.getElementById("thumbnail-canvas") as HTMLCanvasElement; canvasElement = document.getElementById("thumbnail-canvas") as HTMLCanvasElement;
@@ -177,4 +201,9 @@ window.addEventListener("DOMContentLoaded", () => {
document.getElementById("close-viewer-btn")?.addEventListener("click", () => { document.getElementById("close-viewer-btn")?.addEventListener("click", () => {
viewerEl!.style.display = "none"; viewerEl!.style.display = "none";
}); });
const scrollContainer = document.getElementById("file-list");
scrollContainer?.addEventListener("scroll", () => {
renderVirtualList();
});
}); });
+28 -4
View File
@@ -1,5 +1,5 @@
body { body {
font-family: 'Segoe UI Variable', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f3f3f3; background-color: #f3f3f3;
margin: 0; margin: 0;
color: #333; color: #333;
@@ -21,6 +21,8 @@ header {
background-color: rgba(255, 255, 255, 0.7); background-color: rgba(255, 255, 255, 0.7);
backdrop-filter: blur(20px); backdrop-filter: blur(20px);
border-bottom: 1px solid rgba(0, 0, 0, 0.05); border-bottom: 1px solid rgba(0, 0, 0, 0.05);
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
position: relative;
z-index: 10; z-index: 10;
} }
@@ -40,17 +42,18 @@ header h1 {
font-size: 0.85rem; font-size: 0.85rem;
font-weight: 500; font-weight: 500;
transition: all 0.2s ease; transition: all 0.2s ease;
box-shadow: 0 1px 2px rgba(0,0,0,0.05);
} }
.controls button:hover { .controls button:hover {
background-color: #f5f5f5; background-color: #f5f5f5;
border-color: rgba(0, 0, 0, 0.2); border-color: rgba(0, 0, 0, 0.2);
box-shadow: 0 2px 4px rgba(0,0,0,0.05); transform: translateY(-1px);
} }
.controls button:active { .controls button:active {
background-color: #e5e5e5; background-color: #e5e5e5;
transform: scale(0.98); transform: translateY(0);
} }
main { main {
@@ -58,12 +61,32 @@ main {
overflow-y: auto; overflow-y: auto;
padding: 24px; padding: 24px;
background-color: #f3f3f3; background-color: #f3f3f3;
position: relative;
} }
.file-list { .file-list {
position: relative;
display: block; /* Important for virtual scrolling */
width: 100%;
}
#spacer {
position: absolute;
top: 0;
left: 0;
width: 100%;
pointer-events: none;
}
#content {
position: absolute;
top: 0;
left: 0;
width: 100%;
display: grid; display: grid;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
gap: 24px; gap: 24px;
pointer-events: auto;
} }
.file-item { .file-item {
@@ -76,6 +99,7 @@ main {
transition: all 0.2s ease; transition: all 0.2s ease;
text-align: center; text-align: center;
border: 1px solid transparent; border: 1px solid transparent;
box-sizing: border-box;
} }
.file-item:hover { .file-item:hover {
@@ -122,7 +146,7 @@ main {
left: 0; left: 0;
width: 100%; width: 100%;
height: 100%; height: 100%;
background-color: rgba(0, 0, 0, 0.9); background-color: rgba(0, 0, 0, 0.85);
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content: center; justify-content: center;