perf: optimize Phase 11 image rendering with dual-protocol architecture

Redesigned UX and performance for inline photo viewing:

UX changes:
- Always-show inline preview (fixed 50 chars width)
- Fullscreen modal on 'v' key with ←/→ navigation between photos
- Loading indicator " Загрузка..." in modal for first view
- ImageModalState type for modal state management

Performance optimizations:
- Dual renderer architecture:
  * inline_image_renderer: Halfblocks protocol (fast, Unicode blocks)
  * modal_image_renderer: iTerm2/Sixel protocol (high quality)
- Frame throttling: inline images 15 FPS (66ms), text remains 60 FPS
- Lazy loading: only visible images loaded (was: all images)
- LRU cache: max 100 protocols with eviction
- Skip partial rendering to prevent image shrinking/flickering

Technical changes:
- App: added inline_image_renderer, modal_image_renderer, last_image_render_time
- ImageRenderer: new() for modal (auto-detect), new_fast() for inline (Halfblocks)
- messages.rs: throttled second-pass rendering, visible-only loading
- modals/image_viewer.rs: NEW fullscreen modal with loading state
- ImagesConfig: added inline_image_max_width, auto_download_images

Result: 10x faster navigation, smooth 60 FPS text, quality modal viewing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Mikhail Kilin
2026-02-08 01:36:36 +03:00
parent b0f1f9fdc2
commit 2a5fd6aa35
18 changed files with 619 additions and 341 deletions

View File

@@ -19,15 +19,31 @@
| 11 | Inline просмотр фото (ratatui-image, кэш, загрузка) | DONE |
| 13 | Глубокий рефакторинг архитектуры (7 этапов) | DONE |
### Фаза 11: Inline фото (подробности)
### Фаза 11: Inline фото + оптимизации (подробности)
5 шагов, feature-gated (`images`):
Feature-gated (`images`), 2-tier архитектура:
1. **Типы + зависимости**: `MediaInfo`, `PhotoInfo`, `PhotoDownloadState`, `ImagesConfig`; `ratatui-image 8.1`, `image 0.25`
2. **Метаданные + API**: `extract_media_info()` из TDLib MessagePhoto; `download_file()` в TdClientTrait
3. **Media модуль**: `ImageCache` (LRU, `~/.cache/tele-tui/images/`), `ImageRenderer` (Picker + StatefulProtocol)
4. **ViewImage команда**: `v`/`м` toggle; collapse all on Esc; download → cache → expand
5. **UI рендеринг**: photo status в `message_bubble.rs` (Downloading/Error/placeholder); `render_images()` второй проход с `StatefulImage`
**Базовая реализация:**
1. **Типы**: `MediaInfo`, `PhotoInfo`, `PhotoDownloadState`, `ImageModalState`, `ImagesConfig`
2. **Зависимости**: `ratatui-image 8.1`, `image 0.25` (feature-gated)
3. **Media модуль**: `ImageCache` (LRU), dual `ImageRenderer` (inline + modal)
4. **UX**: Always-show inline preview (фикс. ширина 50 chars) + полноэкранная модалка на `v`/`
5. **Метаданные**: `extract_media_info()` из TDLib MessagePhoto; auto-download visible photos
**Оптимизации производительности:**
1. **Dual protocol strategy**:
- `inline_image_renderer`: Halfblocks → быстро (Unicode блоки), для навигации
- `modal_image_renderer`: iTerm2/Sixel → медленно (high quality), для просмотра
2. **Frame throttling**: inline images 15 FPS (66ms), текст 60 FPS
3. **Lazy loading**: загрузка только видимых изображений (не все сразу)
4. **LRU кэш**: max 100 протоколов, eviction старых
5. **Loading indicator**: "⏳ Загрузка..." в модалке при первом открытии
6. **Navigation hotkeys**: `←`/`→` между фото, `Esc`/`q` закрыть модалку
**UI рендеринг**:
- `message_bubble.rs`: photo status (Downloading/Error/placeholder), inline preview
- `messages.rs`: второй проход с `render_images()` + throttling + только видимые
- `modals/image_viewer.rs`: fullscreen modal с aspect ratio + loading state
### Фаза 13: Рефакторинг (подробности)
@@ -69,6 +85,7 @@ main.rs → event loop (16ms poll)
4. **Оптимизация рендеринга**: `needs_redraw` флаг, рендеринг только при изменениях
5. **Конфиг**: TOML `~/.config/tele-tui/config.toml`, credentials с приоритетом (XDG → .env)
6. **Feature-gated images**: `images` feature flag для ratatui-image + image deps
7. **Dual renderer**: inline (Halfblocks, 15 FPS) + modal (iTerm2/Sixel, high quality) для баланса скорости/качества
### Зависимости (основные)