# ntsd/cross-clipboard architecture

> Architecture diagram of the GitHub repository ntsd/cross-clipboard, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ntsd/cross-clipboard
- Repository: https://github.com/ntsd/cross-clipboard

## Overview

##### Main Components
- Main Application Launch: Represent the entry point (main.go) which initializes and orchestrates the overall operation.
- UI Layer:
- Desktop UI: The “ui” directory contains modules for terminal GUI pages (home, settings, confirm dialogs, etc.) built likely using the tview library.
- Mobile UI: The “mobile” folder holds Android-specific files and sources for mobile support (e.g., AndroidManifest.xml, mobile/main.go).
- Core Business Logic (in the “pkg” directory):
- Clipboard Management: In “pkg/clipboard”, include components that handle clipboard data operations.
- Configuration: “pkg/config” holds configuration handling for the application.
- P2P and Device Communication:
▪ “pkg/crossclipboard” ties together the multi-device clipboard sharing logic.
▪ “pkg/device” and “pkg/devicemanager” manage device information and interactions.
▪ “pkg/discovery” handles multicast DNS for discovering devices in the local network.
- Security and Encryption: “pkg/crypto” contains OpenPGP encryption/decryption functions to secure clipboard data.
- Data Streaming: “pkg/stream” encapsulates logic for encoding/decoding data streams exchanged over the network.
- Protocol Definition: “pkg/protobuf” includes the protocol definitions (data.proto) and generated Go files for standardized communication between peers.
- Error Handling: “pkg/xerror” can be used to capture and distinguish fatal vs. runtime errors.

##### Out Relationships and Data Flows
- The Main Application starts up and invokes the UI layer, which then interacts with the core packages for reading the clipboard, encrypting data, and sending/receiving via the network.
- The UI components trigger operations in the Clipboard Manager, which then delegates to:
- The encryption module (pgp functions) for data security.
- The device management and network discovery modules to establish P2P connections.
- The streaming module to package and unpack data.
- The protocol buffer definitions in “pkg/protobuf” ensure that the data exchanged between devices is formatted consistently.

##### Architectural Patterns and Design Principles
- Modularity: The structure clearly segments UI, network, encryption, and core business logic into distinct packages. Represent each as separate modules/components.
- Separation of Concerns: The design separates user interface concerns (UI layers) from the business logic and network communication processing.
- Cross-Platform Design: Different modules (e.g., desktop UI vs. mobile folder) reflect the cross-platform ambitions. Illustrate how similar services are reused or adapted for different platforms.
- Integration of External Libraries: Emphasize the role of external packages (libp2p, protobuf, etc.) that play critical roles in networking, serialization, and interface design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Main Application (main.go)"]:::main

    subgraph "UI Layer"
        B["Desktop UI (Terminal)"]:::ui
        C["Mobile UI (Android)"]:::ui
    end

    subgraph "Core Business Logic"
        E["Configuration Module"]:::core
        D["Clipboard Manager"]:::core
        F["P2P / CrossClipboard"]:::core
        G["Device Info"]:::core
        H["Device Manager"]:::core
        I["Network Discovery"]:::core
        J["Security & Crypto"]:::core
        K["Data Streaming"]:::core
        L["Protocol Definition"]:::core
        M["Error Handling"]:::core
    end

    N["External Libraries"]:::external

    A -->|"init_config"| E
    A -->|"launch_UI"| B
    A -->|"launch_UI"| C

    B -->|"trigger_clipboard"| D
    C -->|"trigger_clipboard"| D

    D -->|"encrypt_data"| J
    D -->|"share_clipboard"| F
    D -->|"stream_data"| K
    E -.->|"provide_settings"| D

    F -->|"manage_devices"| G
    F -->|"device_interact"| H
    F -->|"discover_peers"| I
    F -->|"protocol_format"| L

    K -->|"use_protocol"| L

    D -->|"error_handle"| M
    F -->|"error_handle"| M
    J -->|"error_handle"| M
    K -->|"error_handle"| M
    I -->|"error_handle"| M

    B -->|"uses_uiLib"| N
    F -->|"uses_p2pLib"| N
    L -->|"uses_protoc"| N

    click A "https://github.com/ntsd/cross-clipboard/blob/master/main.go"
    click B "https://github.com/ntsd/cross-clipboard/tree/master/ui/"
    click C "https://github.com/ntsd/cross-clipboard/tree/master/mobile/"
    click D "https://github.com/ntsd/cross-clipboard/tree/master/pkg/clipboard/"
    click E "https://github.com/ntsd/cross-clipboard/blob/master/pkg/config/config.go"
    click F "https://github.com/ntsd/cross-clipboard/blob/master/pkg/crossclipboard/crossclipboard.go"
    click G "https://github.com/ntsd/cross-clipboard/blob/master/pkg/device/device.go"
    click H "https://github.com/ntsd/cross-clipboard/blob/master/pkg/devicemanager/devicemanager.go"
    click I "https://github.com/ntsd/cross-clipboard/blob/master/pkg/discovery/mdns.go"
    click J "https://github.com/ntsd/cross-clipboard/tree/master/pkg/crypto/"
    click K "https://github.com/ntsd/cross-clipboard/tree/master/pkg/stream/"
    click L "https://github.com/ntsd/cross-clipboard/tree/master/pkg/protobuf/"
    click M "https://github.com/ntsd/cross-clipboard/tree/master/pkg/xerror/"

    classDef main fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef ui fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef core fill:#ccccff,stroke:#333,stroke-width:2px;
    classDef external fill:#ffcc99,stroke:#333,stroke-width:2px;
```
