-
+
+
+
diff --git a/rust_media_viewer_v2/src/main.ts b/rust_media_viewer_v2/src/main.ts
index 1c23c36..aae62ec 100644
--- a/rust_media_viewer_v2/src/main.ts
+++ b/rust_media_viewer_v2/src/main.ts
@@ -9,13 +9,108 @@ interface FileInfo {
extension: string | null;
}
-let fileListEl: HTMLElement | null;
+let fileListContainer: HTMLElement | null;
+let spacerElement: HTMLElement | null;
+let contentElement: HTMLElement | null;
let titleEl: HTMLElement | null;
let viewerEl: HTMLElement | null;
let viewerContentEl: HTMLElement | null;
let videoThumbElement: HTMLVideoElement | 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("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 = `
`;
+ } else {
+ const base64 = await invoke("get_thumbnail_command", { path: file.path });
+ iconSpan.innerHTML = `
`;
+ }
+ } catch (e) {
+ iconSpan.textContent = "🖼️";
+ console.error(e);
+ }
+}
+
async function generateVideoThumbnail(url: string): Promise {
return new Promise((resolve, reject) => {
if (!videoThumbElement || !canvasElement) {
@@ -24,7 +119,7 @@ async function generateVideoThumbnail(url: string): Promise {
}
videoThumbElement.src = url;
- videoThumbElement.currentTime = 1; // Seek to 1s to avoid black frame
+ videoThumbElement.currentTime = 1;
videoThumbElement.load();
videoThumbElement.muted = true;
videoThumbElement.playsInline = true;
@@ -49,82 +144,6 @@ async function generateVideoThumbnail(url: string): Promise {
});
}
-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 = `
`;
- } 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("get_thumbnail_command", { path: file.path });
- iconSpan.innerHTML = `
`;
- }
- } else {
- iconSpan.textContent = "📄";
- }
- } catch (e) {
- iconSpan.textContent = "🖼️";
- console.error("Thumbnail error:", e);
- }
-}
-
-async function listFiles(path: string) {
- const files = await invoke("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) {
viewerEl = document.getElementById("viewer") 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", () => {
videoThumbElement = document.getElementById("thumbnail-video") as HTMLVideoElement;
canvasElement = document.getElementById("thumbnail-canvas") as HTMLCanvasElement;
@@ -177,4 +201,9 @@ window.addEventListener("DOMContentLoaded", () => {
document.getElementById("close-viewer-btn")?.addEventListener("click", () => {
viewerEl!.style.display = "none";
});
+
+ const scrollContainer = document.getElementById("file-list");
+ scrollContainer?.addEventListener("scroll", () => {
+ renderVirtualList();
+ });
});
diff --git a/rust_media_viewer_v2/src/styles.css b/rust_media_viewer_v2/src/styles.css
index b583e26..20905ee 100644
--- a/rust_media_viewer_v2/src/styles.css
+++ b/rust_media_viewer_v2/src/styles.css
@@ -1,5 +1,5 @@
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;
margin: 0;
color: #333;
@@ -21,6 +21,8 @@ header {
background-color: rgba(255, 255, 255, 0.7);
backdrop-filter: blur(20px);
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;
}
@@ -40,17 +42,18 @@ header h1 {
font-size: 0.85rem;
font-weight: 500;
transition: all 0.2s ease;
+ box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.controls button:hover {
background-color: #f5f5f5;
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 {
background-color: #e5e5e5;
- transform: scale(0.98);
+ transform: translateY(0);
}
main {
@@ -58,12 +61,32 @@ main {
overflow-y: auto;
padding: 24px;
background-color: #f3f3f3;
+ position: relative;
}
.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;
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
gap: 24px;
+ pointer-events: auto;
}
.file-item {
@@ -76,6 +99,7 @@ main {
transition: all 0.2s ease;
text-align: center;
border: 1px solid transparent;
+ box-sizing: border-box;
}
.file-item:hover {
@@ -122,7 +146,7 @@ main {
left: 0;
width: 100%;
height: 100%;
- background-color: rgba(0, 0, 0, 0.9);
+ background-color: rgba(0, 0, 0, 0.85);
display: flex;
flex-direction: column;
justify-content: center;