Implement virtual scrolling and video thumbnails
This commit is contained in:
1 parent
74dd0a9cf5
commit
7e3a27cf6d
3 files changed
+129
-74
No files matched your search
@@ -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">
|
||||||
|
|||||||
@@ -9,89 +9,59 @@ 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;
|
||||||
|
|
||||||
async function generateVideoThumbnail(url: string): Promise<string> {
|
let allFiles: FileInfo[] = [];
|
||||||
return new Promise((resolve, reject) => {
|
const ITEM_HEIGHT = 160; // Approximate height in pixels
|
||||||
if (!videoThumbElement || !canvasElement) {
|
const BUFFER_COUNT = 5;
|
||||||
reject(new Error("Elements not initialized"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
videoThumbElement.src = url;
|
|
||||||
videoThumbElement.currentTime = 1; // Seek to 1s to avoid black frame
|
|
||||||
videoThumbElement.load();
|
|
||||||
videoThumbElement.muted = true;
|
|
||||||
videoThumbElement.playsInline = true;
|
|
||||||
|
|
||||||
videoThumbElement.onloadeddata = () => {
|
|
||||||
videoThumbElement.currentTime = 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
videoThumbElement.onseeked = () => {
|
|
||||||
canvasElement.width = videoThumbElement.videoWidth > 128 ? 128 : videoThumbElement.videoWidth;
|
|
||||||
canvasElement.height = (videoThumbElement.videoHeight / videoThumbElement.videoWidth) * canvasElement.width;
|
|
||||||
const ctx = canvasElement.getContext("2d");
|
|
||||||
if (ctx) {
|
|
||||||
ctx.drawImage(videoThumbElement, 0, 0, canvasElement.width, canvasElement.height);
|
|
||||||
resolve(canvasElement.toDataURL());
|
|
||||||
} else {
|
|
||||||
reject(new Error("Could not get canvas context"));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
videoThumbElement.onerror = (e) => reject(e);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
async function listFiles(path: string) {
|
||||||
const files = await invoke<FileInfo[]>("list_files_command", {
|
const files = await invoke<FileInfo[]>("list_files_command", {
|
||||||
dirPath: path,
|
dirPath: path,
|
||||||
});
|
});
|
||||||
|
|
||||||
fileListEl = document.getElementById("file-list") as HTMLElement;
|
allFiles = files;
|
||||||
|
fileListContainer = document.getElementById("file-list") as HTMLElement;
|
||||||
titleEl = document.getElementById("app-title") as HTMLElement;
|
titleEl = document.getElementById("app-title") as HTMLElement;
|
||||||
|
spacerElement = document.getElementById("spacer") as HTMLElement;
|
||||||
|
contentElement = document.getElementById("content") as HTMLElement;
|
||||||
|
|
||||||
if (fileListEl) {
|
if (fileListContainer && spacerElement && contentElement) {
|
||||||
fileListEl.innerHTML = "";
|
|
||||||
titleEl!.textContent = `Files in: ${path}`;
|
titleEl!.textContent = `Files in: ${path}`;
|
||||||
|
renderVirtualList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (const file of files) {
|
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");
|
const fileDiv = document.createElement("div");
|
||||||
fileDiv.className = `file-item ${file.is_dir ? "is-dir" : ""}`;
|
fileDiv.className = `file-item ${file.is_dir ? "is-dir" : ""}`;
|
||||||
|
|
||||||
@@ -120,9 +90,58 @@ async function listFiles(path: string) {
|
|||||||
showViewer(file.path, file.extension);
|
showViewer(file.path, file.extension);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
fileListEl?.appendChild(fileDiv);
|
|
||||||
|
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> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!videoThumbElement || !canvasElement) {
|
||||||
|
reject(new Error("Elements not initialized"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
videoThumbElement.src = url;
|
||||||
|
videoThumbElement.currentTime = 1;
|
||||||
|
videoThumbElement.load();
|
||||||
|
videoThumbElement.muted = true;
|
||||||
|
videoThumbElement.playsInline = true;
|
||||||
|
|
||||||
|
videoThumbElement.onloadeddata = () => {
|
||||||
|
videoThumbElement.currentTime = 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
videoThumbElement.onseeked = () => {
|
||||||
|
canvasElement.width = videoThumbElement.videoWidth > 128 ? 128 : videoThumbElement.videoWidth;
|
||||||
|
canvasElement.height = (videoThumbElement.videoHeight / videoThumbElement.videoWidth) * canvasElement.width;
|
||||||
|
const ctx = canvasElement.getContext("2d");
|
||||||
|
if (ctx) {
|
||||||
|
ctx.drawImage(videoThumbElement, 0, 0, canvasElement.width, canvasElement.height);
|
||||||
|
resolve(canvasElement.toDataURL());
|
||||||
|
} else {
|
||||||
|
reject(new Error("Could not get canvas context"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
videoThumbElement.onerror = (e) => reject(e);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function showViewer(filePath: string, extension: string | null) {
|
async function showViewer(filePath: string, extension: string | null) {
|
||||||
@@ -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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -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;
|
||||||
|
|||||||
Reference in new issue
Block a user