@@ -1,10 +1,11 @@ |
| 1 | 1 | //! Grid/icon view for displaying directory contents. |
| 2 | 2 | |
| 3 | | -use crate::core::{EntryType, FileEntry, SortDirection, SortOrder}; |
| 3 | +use crate::core::{is_supported_image, EntryType, FileEntry, SortDirection, SortOrder, ThumbnailLoader}; |
| 4 | 4 | use crate::ui::tab::RenameState; |
| 5 | 5 | use gartk_core::{Color, Modifiers, Point, Rect}; |
| 6 | | -use gartk_render::{Renderer, TextAlign, TextStyle}; |
| 7 | | -use std::collections::HashSet; |
| 6 | +use gartk_render::{Renderer, Surface, TextAlign, TextStyle}; |
| 7 | +use std::collections::{HashMap, HashSet}; |
| 8 | +use std::path::PathBuf; |
| 8 | 9 | use std::time::{Duration, Instant}; |
| 9 | 10 | |
| 10 | 11 | /// Padding around cells. |
@@ -98,6 +99,10 @@ pub struct GridView { |
| 98 | 99 | last_selection_update: Option<Instant>, |
| 99 | 100 | /// Last time we requested a redraw during drag (for frame rate limiting). |
| 100 | 101 | last_drag_render: Option<Instant>, |
| 102 | + /// Thumbnail loader for image files. |
| 103 | + thumbnail_loader: ThumbnailLoader, |
| 104 | + /// Cached thumbnail surfaces. |
| 105 | + thumbnail_cache: HashMap<PathBuf, Surface>, |
| 101 | 106 | } |
| 102 | 107 | |
| 103 | 108 | impl GridView { |
@@ -121,6 +126,8 @@ impl GridView { |
| 121 | 126 | icon_size, |
| 122 | 127 | last_selection_update: None, |
| 123 | 128 | last_drag_render: None, |
| 129 | + thumbnail_loader: ThumbnailLoader::new(), |
| 130 | + thumbnail_cache: HashMap::new(), |
| 124 | 131 | } |
| 125 | 132 | } |
| 126 | 133 | |
@@ -170,6 +177,60 @@ impl GridView { |
| 170 | 177 | self.selected.insert(0); |
| 171 | 178 | self.selection_anchor = Some(0); |
| 172 | 179 | self.scroll_offset = 0; |
| 180 | + // Clear thumbnail cache when directory changes |
| 181 | + self.thumbnail_cache.clear(); |
| 182 | + self.thumbnail_loader.clear_cache(); |
| 183 | + } |
| 184 | + |
| 185 | + /// Poll for completed thumbnail loads. Returns true if any thumbnails were loaded. |
| 186 | + pub fn poll_thumbnails(&mut self) -> bool { |
| 187 | + let loaded = self.thumbnail_loader.poll(); |
| 188 | + let mut any_loaded = false; |
| 189 | + |
| 190 | + for path in loaded { |
| 191 | + // Create surface from cached thumbnail (borrow ends after and_then) |
| 192 | + let surface_opt = self.thumbnail_loader |
| 193 | + .get_cached(&path) |
| 194 | + .and_then(|thumbnail| { |
| 195 | + Surface::from_rgba(&thumbnail.data, thumbnail.width, thumbnail.height).ok() |
| 196 | + }); |
| 197 | + |
| 198 | + // Now insert (no borrow of thumbnail_loader active) |
| 199 | + if let Some(surface) = surface_opt { |
| 200 | + self.thumbnail_cache.insert(path, surface); |
| 201 | + any_loaded = true; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + any_loaded |
| 206 | + } |
| 207 | + |
| 208 | + /// Request thumbnails for visible image files. |
| 209 | + pub fn request_visible_thumbnails(&mut self) { |
| 210 | + let visible_count = self.visible_count(); |
| 211 | + let visible_rows = self.visible_rows(); |
| 212 | + let start_index = self.scroll_offset * self.columns; |
| 213 | + let end_index = (start_index + visible_rows * self.columns).min(visible_count); |
| 214 | + |
| 215 | + // Collect paths to load (immutable borrow of entries) |
| 216 | + let paths_to_load: Vec<PathBuf> = (start_index..end_index) |
| 217 | + .filter_map(|i| { |
| 218 | + self.visible_entry(i).and_then(|entry| { |
| 219 | + if is_supported_image(entry.extension().as_deref()) |
| 220 | + && !self.thumbnail_cache.contains_key(&entry.path) |
| 221 | + { |
| 222 | + Some(entry.path.clone()) |
| 223 | + } else { |
| 224 | + None |
| 225 | + } |
| 226 | + }) |
| 227 | + }) |
| 228 | + .collect(); |
| 229 | + |
| 230 | + // Now request loading (mutable borrow of thumbnail_loader) |
| 231 | + for path in paths_to_load { |
| 232 | + self.thumbnail_loader.get_or_load(&path); |
| 233 | + } |
| 173 | 234 | } |
| 174 | 235 | |
| 175 | 236 | /// Get visible entries (respecting hidden filter). |
@@ -427,6 +488,33 @@ impl GridView { |
| 427 | 488 | self.hovered != old_hovered |
| 428 | 489 | } |
| 429 | 490 | |
| 491 | + /// Handle mouse scroll. Returns true if scrolled. |
| 492 | + pub fn on_scroll(&mut self, delta_y: i32) -> bool { |
| 493 | + let visible_count = self.visible_count(); |
| 494 | + let total_rows = (visible_count + self.columns - 1) / self.columns; |
| 495 | + let visible_rows = self.visible_rows(); |
| 496 | + |
| 497 | + if total_rows <= visible_rows { |
| 498 | + return false; // No scrolling needed |
| 499 | + } |
| 500 | + |
| 501 | + let max_scroll = total_rows.saturating_sub(visible_rows); |
| 502 | + let old_offset = self.scroll_offset; |
| 503 | + |
| 504 | + // Scroll by number of rows (negative delta = scroll up) |
| 505 | + if delta_y < 0 { |
| 506 | + // Scroll up |
| 507 | + let rows = ((-delta_y) as usize / 3).max(1); |
| 508 | + self.scroll_offset = self.scroll_offset.saturating_sub(rows); |
| 509 | + } else if delta_y > 0 { |
| 510 | + // Scroll down |
| 511 | + let rows = (delta_y as usize / 3).max(1); |
| 512 | + self.scroll_offset = (self.scroll_offset + rows).min(max_scroll); |
| 513 | + } |
| 514 | + |
| 515 | + self.scroll_offset != old_offset |
| 516 | + } |
| 517 | + |
| 430 | 518 | /// Start rubber band selection. |
| 431 | 519 | pub fn start_drag(&mut self, pos: Point) { |
| 432 | 520 | self.drag_start = Some(pos); |
@@ -613,32 +701,53 @@ impl GridView { |
| 613 | 701 | renderer.stroke_rect(cell, theme.selection_background.with_alpha(0.5), 1.0)?; |
| 614 | 702 | } |
| 615 | 703 | |
| 616 | | - // Icon (placeholder using text) |
| 617 | | - let icon = match entry.entry_type { |
| 618 | | - EntryType::Directory => "\u{1F4C1}", // folder emoji |
| 619 | | - EntryType::Symlink => "\u{1F517}", // link emoji |
| 620 | | - _ => Self::file_icon_for_extension(entry.extension().as_deref()), |
| 621 | | - }; |
| 622 | | - |
| 623 | | - let icon_color = if is_selected { |
| 624 | | - theme.selection_foreground |
| 625 | | - } else { |
| 626 | | - match entry.entry_type { |
| 627 | | - EntryType::Directory => dir_color, |
| 628 | | - EntryType::Symlink => symlink_color, |
| 629 | | - _ => theme.item_foreground, |
| 704 | + // Check for cached thumbnail first (for image files) |
| 705 | + let mut rendered_thumbnail = false; |
| 706 | + if is_supported_image(entry.extension().as_deref()) { |
| 707 | + if let Some(surface) = self.thumbnail_cache.get(&entry.path) { |
| 708 | + // Render the thumbnail centered in the icon area |
| 709 | + let thumb_w = surface.width(); |
| 710 | + let thumb_h = surface.height(); |
| 711 | + let icon_area_size = icon_size_px; |
| 712 | + let thumb_x = cell.x + (cell.width as i32 - thumb_w as i32) / 2; |
| 713 | + let thumb_y = cell.y + 8 + (icon_area_size as i32 - thumb_h as i32) / 2; |
| 714 | + |
| 715 | + let ctx = renderer.context()?; |
| 716 | + ctx.set_source_surface(surface.cairo_surface(), thumb_x as f64, thumb_y as f64)?; |
| 717 | + ctx.paint()?; |
| 718 | + rendered_thumbnail = true; |
| 630 | 719 | } |
| 631 | | - }; |
| 720 | + // Thumbnails are requested via request_visible_thumbnails() called before render |
| 721 | + } |
| 632 | 722 | |
| 633 | | - let icon_style = TextStyle::new() |
| 634 | | - .font_family(&theme.font_family) |
| 635 | | - .font_size(icon_font_size) |
| 636 | | - .color(icon_color); |
| 723 | + // Fall back to emoji icon if no thumbnail |
| 724 | + if !rendered_thumbnail { |
| 725 | + let icon = match entry.entry_type { |
| 726 | + EntryType::Directory => "\u{1F4C1}", // folder emoji |
| 727 | + EntryType::Symlink => "\u{1F517}", // link emoji |
| 728 | + _ => Self::file_icon_for_extension(entry.extension().as_deref()), |
| 729 | + }; |
| 637 | 730 | |
| 638 | | - // Center icon horizontally in cell |
| 639 | | - let icon_center_x = cell.x + cell.width as i32 / 2; |
| 640 | | - let icon_center_y = cell.y + 10 + (icon_font_size / 2.0) as i32; |
| 641 | | - renderer.text_centered(icon, Point::new(icon_center_x, icon_center_y), &icon_style)?; |
| 731 | + let icon_color = if is_selected { |
| 732 | + theme.selection_foreground |
| 733 | + } else { |
| 734 | + match entry.entry_type { |
| 735 | + EntryType::Directory => dir_color, |
| 736 | + EntryType::Symlink => symlink_color, |
| 737 | + _ => theme.item_foreground, |
| 738 | + } |
| 739 | + }; |
| 740 | + |
| 741 | + let icon_style = TextStyle::new() |
| 742 | + .font_family(&theme.font_family) |
| 743 | + .font_size(icon_font_size) |
| 744 | + .color(icon_color); |
| 745 | + |
| 746 | + // Center icon horizontally in cell |
| 747 | + let icon_center_x = cell.x + cell.width as i32 / 2; |
| 748 | + let icon_center_y = cell.y + 10 + (icon_font_size / 2.0) as i32; |
| 749 | + renderer.text_centered(icon, Point::new(icon_center_x, icon_center_y), &icon_style)?; |
| 750 | + } |
| 642 | 751 | |
| 643 | 752 | // Rectangle for the text area below the icon |
| 644 | 753 | let text_rect = Rect::new( |