# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "User Interface Layer"
        UI_macOS["macOS App<br/>(Swift/SwiftUI)"]
        UI_GTK["Linux App<br/>(GTK4)"]
        UI_Browser["Browser<br/>(WASM)"]
    end

    subgraph "Application Runtime Layer (apprt)"
        APPRT_Embedded["embedded.zig<br/>(libghostty)"]
        APPRT_GTK["gtk/<br/>(GTK Runtime)"]
        APPRT_Browser["browser.zig<br/>(WASM Runtime)"]
    end

    subgraph "Core Application Layer"
        APP["App.zig<br/>(Application Manager)"]
        SURFACE["Surface.zig<br/>(Terminal Surface)"]
        CONFIG["config/<br/>(Configuration)"]
    end

    subgraph "Rendering Layer"
        RENDERER["renderer/<br/>(Renderer)"]
        RENDERER_METAL["Metal.zig"]
        RENDERER_OPENGL["OpenGL.zig"]
        RENDERER_WEBGL["WebGL.zig"]
        FONT["font/<br/>(Font System)"]
    end

    subgraph "Terminal Emulation Layer"
        TERMINAL["terminal/<br/>(Terminal Emulator)"]
        TERMIO["termio/<br/>(Terminal IO)"]
        PTY["pty.zig<br/>(PTY Management)"]
    end

    subgraph "Platform Abstraction Layer"
        OS["os/<br/>(OS Abstraction)"]
        GLOBAL["global.zig<br/>(Global State)"]
        INPUT["input/<br/>(Input Handling)"]
    end

    subgraph "External Systems"
        SHELL["Shell Process"]
        GPU["GPU<br/>(Metal/OpenGL)"]
        FONT_SYS["Font System<br/>(CoreText/Fontconfig)"]
        SENTRY["Sentry<br/>(Crash Reports)"]
    end

    UI_macOS --> APPRT_Embedded
    UI_GTK --> APPRT_GTK
    UI_Browser --> APPRT_Browser

    APPRT_Embedded --> APP
    APPRT_GTK --> APP
    APPRT_Browser --> APP

    APP --> SURFACE
    APP --> CONFIG

    SURFACE --> RENDERER
    SURFACE --> TERMIO
    SURFACE --> INPUT

    RENDERER --> RENDERER_METAL
    RENDERER --> RENDERER_OPENGL
    RENDERER --> RENDERER_WEBGL
    RENDERER --> FONT

    TERMIO --> TERMINAL
    TERMIO --> PTY

    TERMINAL --> OS

    PTY --> SHELL
    RENDERER_METAL --> GPU
    RENDERER_OPENGL --> GPU
    FONT --> FONT_SYS
    GLOBAL --> SENTRY
```

## レイヤー構成図

```mermaid
graph LR
    subgraph "Presentation"
        A1["macOS SwiftUI"]
        A2["GTK4"]
        A3["WASM/Browser"]
    end

    subgraph "Application"
        B1["App.zig"]
        B2["Surface.zig"]
        B3["apprt/*"]
    end

    subgraph "Domain"
        C1["terminal/*"]
        C2["renderer/*"]
        C3["font/*"]
        C4["input/*"]
    end

    subgraph "Infrastructure"
        D1["pty.zig"]
        D2["termio/*"]
        D3["os/*"]
        D4["config/*"]
    end

    A1 --> B1
    A2 --> B1
    A3 --> B1

    B1 --> B2
    B2 --> B3

    B2 --> C1
    B2 --> C2
    B2 --> C3
    B2 --> C4

    C1 --> D2
    C2 --> D3
    D2 --> D1
    D4 --> D3
```

## データフロー図

```mermaid
sequenceDiagram
    participant User
    participant AppRT as Application Runtime
    participant Surface
    participant Input
    participant Termio
    participant Terminal
    participant PTY
    participant Shell
    participant Renderer

    Note over User,Renderer: Input Flow
    User->>AppRT: Key Press Event
    AppRT->>Surface: keyEvent()
    Surface->>Input: Process Binding
    alt Has Binding
        Input->>Surface: Execute Action
    else No Binding
        Input->>Termio: queueMessage()
        Termio->>PTY: Write
        PTY->>Shell: Send Data
    end

    Note over User,Renderer: Output Flow
    Shell->>PTY: Output Data
    PTY->>Termio: Read
    Termio->>Terminal: process()
    Terminal->>Terminal: Update Screen
    Terminal->>Renderer: Request Render
    Renderer->>AppRT: Draw Frame
    AppRT->>User: Display
```

## マルチスレッド構成図

```mermaid
graph TB
    subgraph "Main Thread (UI)"
        MAIN_LOOP["Event Loop"]
        APP_TICK["App.tick()"]
        INPUT_HANDLER["Input Handler"]
    end

    subgraph "IO Thread"
        IO_LOOP["libxev Loop"]
        PTY_READ["PTY Read"]
        PTY_WRITE["PTY Write"]
        STREAM_PARSE["Stream Parse"]
    end

    subgraph "Render Thread"
        RENDER_LOOP["Render Loop"]
        GPU_DRAW["GPU Draw"]
        VSYNC["VSync Wait"]
    end

    subgraph "Search Thread (Optional)"
        SEARCH["Text Search"]
    end

    subgraph "Shared State"
        MAILBOX["Mailbox<br/>(BlockingQueue)"]
        RENDERER_STATE["Renderer State<br/>(Mutex)"]
        TERMINAL_STATE["Terminal State"]
    end

    MAIN_LOOP --> APP_TICK
    APP_TICK --> MAILBOX
    INPUT_HANDLER --> MAILBOX

    IO_LOOP --> PTY_READ
    IO_LOOP --> PTY_WRITE
    PTY_READ --> STREAM_PARSE
    STREAM_PARSE --> TERMINAL_STATE

    RENDER_LOOP --> RENDERER_STATE
    RENDER_LOOP --> GPU_DRAW
    GPU_DRAW --> VSYNC

    MAILBOX --> IO_LOOP
    TERMINAL_STATE --> RENDERER_STATE
    RENDERER_STATE --> RENDER_LOOP
```

## モジュール依存関係図

```mermaid
graph TD
    subgraph "Entry Points"
        MAIN["main.zig"]
        MAIN_GHOSTTY["main_ghostty.zig"]
        MAIN_C["main_c.zig"]
    end

    subgraph "Core"
        APP["App.zig"]
        SURFACE["Surface.zig"]
        COMMAND["Command.zig"]
    end

    subgraph "Application Runtime"
        APPRT["apprt.zig"]
        APPRT_ACTION["apprt/action.zig"]
        APPRT_GTK["apprt/gtk/"]
        APPRT_EMBEDDED["apprt/embedded.zig"]
    end

    subgraph "Terminal"
        TERMINAL["terminal/"]
        TERMINAL_MAIN["terminal/Terminal.zig"]
        PARSER["terminal/Parser.zig"]
        SCREEN["terminal/Screen.zig"]
        PAGELIST["terminal/PageList.zig"]
    end

    subgraph "IO"
        TERMIO["termio/"]
        TERMIO_MAIN["termio/Termio.zig"]
        TERMIO_EXEC["termio/Exec.zig"]
        PTY["pty.zig"]
    end

    subgraph "Rendering"
        RENDERER["renderer/"]
        RENDERER_GENERIC["renderer/generic.zig"]
        RENDERER_METAL["renderer/Metal.zig"]
        RENDERER_OPENGL["renderer/OpenGL.zig"]
        RENDERER_THREAD["renderer/Thread.zig"]
    end

    subgraph "Font"
        FONT["font/"]
        FONT_ATLAS["font/Atlas.zig"]
        FONT_COLLECTION["font/Collection.zig"]
        FONT_SHAPER["font/shaper/"]
    end

    subgraph "Input"
        INPUT["input/"]
        INPUT_BINDING["input/Binding.zig"]
        INPUT_KEY["input/key.zig"]
    end

    subgraph "Config"
        CONFIG["config/"]
        CONFIG_MAIN["config/Config.zig"]
    end

    subgraph "Infrastructure"
        OS["os/"]
        GLOBAL["global.zig"]
        CRASH["crash/"]
        UNICODE["unicode/"]
    end

    MAIN --> MAIN_GHOSTTY
    MAIN_GHOSTTY --> APP
    MAIN_C --> APP

    APP --> SURFACE
    APP --> APPRT
    APP --> CONFIG

    SURFACE --> RENDERER
    SURFACE --> TERMIO
    SURFACE --> INPUT
    SURFACE --> FONT

    APPRT --> APPRT_ACTION
    APPRT --> APPRT_GTK
    APPRT --> APPRT_EMBEDDED

    TERMINAL --> TERMINAL_MAIN
    TERMINAL_MAIN --> PARSER
    TERMINAL_MAIN --> SCREEN
    SCREEN --> PAGELIST

    TERMIO --> TERMIO_MAIN
    TERMIO_MAIN --> TERMIO_EXEC
    TERMIO_EXEC --> PTY
    TERMIO_MAIN --> TERMINAL

    RENDERER --> RENDERER_GENERIC
    RENDERER_GENERIC --> RENDERER_METAL
    RENDERER_GENERIC --> RENDERER_OPENGL
    RENDERER --> RENDERER_THREAD

    FONT --> FONT_ATLAS
    FONT --> FONT_COLLECTION
    FONT --> FONT_SHAPER

    INPUT --> INPUT_BINDING
    INPUT --> INPUT_KEY

    CONFIG --> CONFIG_MAIN

    GLOBAL --> CRASH
    GLOBAL --> OS
    TERMINAL --> UNICODE
```

## コンパイル時構成選択図

```mermaid
graph TD
    BUILD_CONFIG["build_config.zig"]

    subgraph "Runtime Selection"
        RT_NONE["none<br/>(libghostty)"]
        RT_GTK["gtk<br/>(GTK4 App)"]
    end

    subgraph "Renderer Selection"
        RENDER_METAL["metal<br/>(macOS)"]
        RENDER_OPENGL["opengl<br/>(Linux)"]
        RENDER_WEBGL["webgl<br/>(WASM)"]
    end

    subgraph "Font Backend Selection"
        FONT_CORETEXT["coretext<br/>(macOS)"]
        FONT_FONTCONFIG["fontconfig_freetype<br/>(Linux)"]
    end

    subgraph "Artifact Type"
        ART_EXE["exe<br/>(Executable)"]
        ART_LIB["lib<br/>(Library)"]
        ART_WASM["wasm_module<br/>(WASM)"]
    end

    BUILD_CONFIG --> RT_NONE
    BUILD_CONFIG --> RT_GTK

    BUILD_CONFIG --> RENDER_METAL
    BUILD_CONFIG --> RENDER_OPENGL
    BUILD_CONFIG --> RENDER_WEBGL

    BUILD_CONFIG --> FONT_CORETEXT
    BUILD_CONFIG --> FONT_FONTCONFIG

    BUILD_CONFIG --> ART_EXE
    BUILD_CONFIG --> ART_LIB
    BUILD_CONFIG --> ART_WASM
```

## 外部依存関係図

```mermaid
graph LR
    GHOSTTY["Ghostty Core<br/>(Zig)"]

    subgraph "Font Libraries"
        FREETYPE["FreeType"]
        HARFBUZZ["HarfBuzz"]
        FONTCONFIG["Fontconfig"]
        CORETEXT["CoreText<br/>(macOS)"]
    end

    subgraph "Graphics"
        METAL["Metal<br/>(macOS)"]
        OPENGL["OpenGL"]
        GLSLANG["glslang"]
    end

    subgraph "System"
        PTY_SYS["PTY System<br/>(POSIX/Windows)"]
        XEV["libxev<br/>(Event Loop)"]
    end

    subgraph "Utilities"
        ONIGURUMA["Oniguruma<br/>(Regex)"]
        SIMDUTF["simdutf<br/>(Unicode)"]
        SENTRY_SDK["Sentry SDK<br/>(Crash)"]
    end

    subgraph "UI Frameworks"
        GTK4["GTK4"]
        SWIFTUI["SwiftUI"]
    end

    GHOSTTY --> FREETYPE
    GHOSTTY --> HARFBUZZ
    GHOSTTY --> FONTCONFIG
    GHOSTTY --> CORETEXT

    GHOSTTY --> METAL
    GHOSTTY --> OPENGL
    GHOSTTY --> GLSLANG

    GHOSTTY --> PTY_SYS
    GHOSTTY --> XEV

    GHOSTTY --> ONIGURUMA
    GHOSTTY --> SIMDUTF
    GHOSTTY --> SENTRY_SDK

    GHOSTTY --> GTK4
    GHOSTTY --> SWIFTUI
```
