Skip to content

Optimize GUI rendering and add shift+tab navigation#22

Merged
mesolimbo merged 4 commits intomainfrom
claude/optimize-mac-gui-animation-1KjXI
Jan 21, 2026
Merged

Optimize GUI rendering and add shift+tab navigation#22
mesolimbo merged 4 commits intomainfrom
claude/optimize-mac-gui-animation-1KjXI

Conversation

@mesolimbo
Copy link
Owner

Summary

This PR improves GUI rendering performance on both macOS (iTerm2) and Windows, and adds shift+tab navigation support.

Performance Optimizations

iTerm2/macOS (<100ms latency target achieved):

  • Optimize fill_rect using slice assignment instead of nested loops
  • Optimize pixels_to_iterm2 using PIL palette mode ('P') and fast bytes() conversion
  • Add _pixels_to_iterm2_slow fallback for >256 colors edge case
  • Buffer operations reduced from ~200ms+ to ~3.5ms

Windows/Sixel:

  • Add palette caching to avoid regenerating palette string every frame
  • Use set.union(*band_rows) for fast color detection in each band
  • Cache list.append methods and precompute row/mask pairs for tight loops
  • Use tuple for bit_masks (faster indexing than lists)

New Feature: Shift+Tab Navigation

  • Add shift-tab detection in unix.py (ESC[Z sequence)
  • Add shift-tab detection in windows.py (ESC[Z for modern Windows Terminal, plus legacy \x00\x0f for older terminals)
  • Add shift-tab handling in app_loop.py to call focus_previous()
  • Users can now navigate backwards through focusable components

Test Fixes

  • Tests now properly handle platform differences (sixel vs iTerm2 output formats)
  • Config tests account for PLATFORM_SCALE (2x on iTerm2/macOS)
  • All 186 tests pass on both Windows and macOS/iTerm2

Test plan

  • All 186 unit tests pass on Windows
  • All 186 unit tests pass on macOS/iTerm2
  • Manual testing: Tab navigation works
  • Manual testing: Shift+Tab navigation works
  • Manual testing: UI is responsive on both platforms

🤖 Generated with Claude Code

claude and others added 4 commits January 21, 2026 03:11
- Optimize fill_rect using slice assignment instead of nested loops
- Optimize pixels_to_iterm2 using PIL palette mode ('P') and fast bytes()
- Add _pixels_to_iterm2_slow fallback for >256 colors edge case

The previous implementation iterated pixel-by-pixel in Python which caused
~200ms+ latency at demo resolution. The new implementation:
1. Uses slice assignment for fill_rect (much faster in CPython)
2. Uses bytes() + b"".join() for fast palette index conversion
3. Uses PIL 'P' mode to avoid manual RGB conversion loops

Performance impact: Buffer operations now take ~3.5ms vs much higher before.
- Add shift-tab detection in unix.py (ESC[Z sequence)
- Add shift-tab detection in windows.py (ESC[Z for modern Windows Terminal,
  plus legacy \x00\x0f for older terminals)
- Add shift-tab handling in app_loop.py to call focus_previous()
- Add test for shift-tab navigation

Users can now use Shift+Tab to navigate backwards through focusable
components, complementing the existing Tab forward navigation.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Optimizations ported from iTerm2 improvements:

1. Palette caching - generate_palette() now caches the result and only
   regenerates when image colors change, avoiding string building every frame

2. Fast color detection - replaced nested loops with set.union(*band_rows)
   which is significantly faster for finding unique colors in each band

3. Local variable caching - cache list.append methods and precompute
   row/mask pairs to reduce attribute lookups in tight inner loops

4. Tuple for bit_masks - tuples have faster indexing than lists

These optimizations reduce per-frame encoding time, improving UI
responsiveness on Windows terminals using Sixel graphics.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Tests now properly handle platform differences:
- Renderer tests accept both sixel and iTerm2 image formats
- Config tests account for PLATFORM_SCALE (2x on iTerm2/macOS)
- All 186 tests pass on both Windows and macOS/iTerm2

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@mesolimbo mesolimbo force-pushed the claude/optimize-mac-gui-animation-1KjXI branch from 6815547 to d64678f Compare January 21, 2026 04:24
@mesolimbo mesolimbo merged commit d5ce516 into main Jan 21, 2026
20 checks passed
@mesolimbo mesolimbo deleted the claude/optimize-mac-gui-animation-1KjXI branch January 21, 2026 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments