Skip to content

Conversation

@cyole
Copy link
Owner

@cyole cyole commented Dec 10, 2025

Description

Linked Issues

Additional context

Summary by CodeRabbit

  • New Features

    • Added HTML content support to clipboard synchronization between devices.
  • Improvements

    • Enhanced image handling with size optimization and compression for better performance.
    • Improved error handling and logging for clipboard operations on all platforms.

✏️ Tip: You can customize this high-level summary in your review settings.

改进:
- 添加图片大小限制(10MB)和尺寸限制(4096x4096)
- 自动缩放过大的图片
- 优化 PNG 编码性能
- 添加富文本(HTML)支持
- 支持 text/html MIME 类型(Linux wl-clipboard)
- 改进错误处理和日志输出

这解决了图片复制粘贴不稳定的问题,并为富文本内容提供了支持
修复和改进:
- 将图片大小限制从 10MB 降低到 5MB
- 添加多次尝试的降级压缩机制(最多3次)
- 改进错误处理,不会因为单个图片失败而中断同步
- 添加详细的日志输出,便于调试
- 在图片处理失败时自动回退到文本模式
- Linux wl-clipboard 也会检查和重新压缩过大的图片
- 每次压缩失败后会缩小到 70% 重试

这大大提高了图片同步的成功率和稳定性
- 改进 map_err 调用的多行格式
- 移除行尾多余空格
- 提高代码可读性
问题:
- 服务器收到客户端图片后会重复广播
- 因为 receive_handle 和 clipboard_handle 使用了独立的 ClipboardMonitor 实例
- 它们的 last_hash 不同步,导致检测到"重复"变化

修复:
- 统一服务器端的剪贴板管理,使用单一 ClipboardMonitor 实例
- 使用 tokio::select! 同时处理本地变化和客户端消息
- 确保 hash 在同一实例中更新,避免重复检测
- 移除未使用的 monitor 方法和导入

现在服务器端和客户端都使用统一的剪贴板管理模式
@coderabbitai
Copy link

coderabbitai bot commented Dec 10, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

The changes add HTML content support to a clipboard synchronization system. The implementation includes new enum variants for HTML content, restructured clipboard monitoring with a unified task-based architecture, enhanced image handling with compression and size constraints, new Linux Wayland clipboard utilities, and centralized error handling with graceful fallbacks.

Changes

Cohort / File(s) Summary
HTML Content Type Support
src/modules/sync.rs, src/modules/clipboard.rs
Added Html variant to ClipboardContent enum with html and text fields; includes serde support for serialization/deserialization and hashing integration.
Clipboard Synchronization Architecture
src/main.rs
Refactored clipboard monitoring from callback-based to unified event loop driven by local monitor signals; server now broadcasts ClipboardMessage with HTML content support; client extended to log and apply received HTML content; replaced separate handlers with consolidated task management.
Image Processing & Linux Clipboard Integration
src/modules/clipboard.rs
Added image size constraints (MAX_IMAGE_SIZE 5 MB, MAX_IMAGE_DIMENSION 4096); implemented iterative PNG compression with aspect-ratio-preserving resize; enhanced image fallback handling with logging; added Linux Wayland utilities (wl_copy_html, wl_paste, wl_paste_image); centralized error logging in clipboard read/set operations.

Sequence Diagram

sequenceDiagram
    participant LM as Local Monitor
    participant Server
    participant Client
    participant CB as Local Clipboard

    Note over LM,Client: New Unified Clipboard Management Flow
    
    loop Periodic Monitoring
        LM->>LM: Signal via internal channel
        LM->>LM: Fetch current clipboard (Text/Image/Html)
    end
    
    alt Local Content Changed
        LM->>Server: Broadcast ClipboardMessage<br/>(Text | Image | Html)
        Server->>Server: Log content type & length
        Server->>Client: Forward ClipboardMessage
    end
    
    alt Client Receives Message
        Client->>Client: Detect content type
        Client->>Client: Log received content<br/>(Text/Image/Html)
        Client->>CB: Apply to local clipboard
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

  • Unified clipboard task architecture in src/main.rs: Review the new event loop structure, channel signaling, and task coordination with shutdown handling
  • Image compression and dimension handling in src/modules/clipboard.rs: Validate iterative compression logic, aspect-ratio preservation, and size-based downscaling decisions
  • Linux Wayland integration (wl_paste, wl_paste_image, wl_copy_html): Verify fallback paths and error handling for platform-specific clipboard operations
  • Cross-file HTML variant consistency: Ensure HTML content flows correctly through enum definitions, serialization, hashing, and message broadcasting

Poem

🐰 HTML hops into the clipboard, no more just text!
From server to client, through channels of jest—
Wayland whispers, images shrink with care,
One unified task keeps the sync so fair! ✨

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/stable-image-and-richtext

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 28cf2d8 and bdc89cf.

📒 Files selected for processing (3)
  • src/main.rs (5 hunks)
  • src/modules/clipboard.rs (9 hunks)
  • src/modules/sync.rs (1 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cyole cyole merged commit c84338f into main Dec 10, 2025
2 checks passed
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