# fl0p/lg architecture

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

- Interactive diagram: https://gitdiagram.com/fl0p/lg
- Repository: https://github.com/fl0p/lg

## Overview

##### Project Type and Purpose
- This is a single-target native iOS application (“Liquid Glass”) whose sole purpose is to render a realistic magnifying-glass effect on images, using a combination of UIKit for the UI, CoreImage for image filtering, and Metal for high-performance custom shaders.

##### File‐Structure Analysis
- Top-level: Xcode project files (`.xcodeproj`), app target folder `lg/`, resources folder `lg/Assets.xcassets`, Metal shader files (`.metal`, `.ci.metal`), source files (`.swift`), storyboards (`.storyboard`), and app lifecycle (`AppDelegate.swift`, `SceneDelegate.swift`).
- No backend/server components—pure client-side.
- Custom filter code lives in `DisplacementMapFilter.swift` + `.ci.metal`; UI and interaction code in `ViewController.swift`, `LGView.swift`, `DragManager.swift`.

##### README Insights
- The README confirms a focus on a custom “Displacement Map Filter” implemented with Metal and CoreImage, with real-time adjustable parameters via sliders.

##### Main Components
a. **UI Layer**
- UIKit Storyboard & Swift UI Controllers:
- LaunchScreen & Main.storyboard
- ViewController.swift (wiring UI elements & sliders)
- LGView.swift (custom UIView subclass that hosts the Metal/CoreImage output)
b. **Interaction Layer**
- DragManager.swift handles touch events (pan, tap) and forwards adjusted lens parameters.
c. **Rendering/Filter Layer**
- DisplacementMapFilter.swift: orchestrates a CoreImage filter chain using a custom CIKernel loaded from `Displacement.ci.metal`.
- Metal runtime: GPU device/context created by CoreImage under the hood; raw Metal entry points in `File.metal` or the CIKernel.
d. **Resources**
- Shader sources (`.metal`, `.ci.metal`).
e. **System & Frameworks**
- iOS Application Lifecycle: AppDelegate + SceneDelegate
- Frameworks: UIKit, CoreImage, Metal

##### Relationships & Data Flow
1. **User Input → DragManager**
- Touch gestures captured by LGView → delegated to DragManager → updates lens parameters.
2. **Parameter Update → ViewController**
- DragManager notifies ViewController (or directly LGView) of new parameter values.
3. **ViewController → DisplacementMapFilter**
- On each frame or slider change, ViewController invokes DisplacementMapFilter, passing the input image + lens parameters.
4. **DisplacementMapFilter → GPU/CI Kernel**
- Filter compiles CIKernel (from `Displacement.ci.metal`), executes on GPU via CoreImage’s Metal-backed context.
5. **Result → LGView**
- The output CIImage (or MTLTexture) is rendered back into LGView’s layer for display.

##### Architectural Patterns & Principles
- MVC-like separation:
- Model: lens parameters (width, radius, scale, etc.)
- View: storyboards + LGView
- Controller: ViewController + DragManager
- Single Responsibility:
- DragManager only deals with gestures
- DisplacementMapFilter only deals with image processing
- GPU Acceleration: offload heavy compute to Metal

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "App Lifecycle"
        AppDelegate["AppDelegate.swift"]:::lifecycle
        SceneDelegate["SceneDelegate.swift"]:::lifecycle
    end

    subgraph "UI Layer"
        Launch["LaunchScreen.storyboard"]:::ui
        Main["Main.storyboard"]:::ui
        VC["ViewController.swift"]:::ui
        LGView["LGView.swift"]:::ui
        Sliders["UI Sliders"]:::ui
    end

    subgraph "Interaction Layer"
        Drag["DragManager.swift"]:::logic
    end

    subgraph "Rendering/Filter Engine"
        Filter["DisplacementMapFilter.swift"]:::compute
        CIKernel["Displacement.ci.metal"]:::compute
        MetalShader["File.metal"]:::compute
    end

    subgraph "Frameworks / GPU"
        CoreImage["CoreImage / Metal Runtime"]:::gpu
    end

    subgraph "Resources"
        Assets["Assets.xcassets"]:::resource
    end

    %% Data Flow
    AppDelegate -->|launches scene| SceneDelegate
    SceneDelegate -->|loads UI| Launch
    SceneDelegate -->|loads UI| Main
    Main -->|instantiates| VC
    VC -->|renders view| LGView
    VC -->|updates on slider| Sliders
    LGView -.->|touch events| Drag
    Drag -->|parameter update| VC
    VC -->|invoke filter| Filter
    Filter -->|loads kernel| CIKernel
    Filter -->|executes on GPU| CoreImage
    CoreImage -->|runs shader| MetalShader
    Filter -->|outputs image| LGView
    Assets -->|image assets| VC

    %% Click Events
    click Launch "https://github.com/fl0p/lg/blob/master/lg/Base.lproj/LaunchScreen.storyboard"
    click Main "https://github.com/fl0p/lg/blob/master/lg/Base.lproj/Main.storyboard"
    click VC "https://github.com/fl0p/lg/blob/master/lg/ViewController.swift"
    click LGView "https://github.com/fl0p/lg/blob/master/lg/LGView.swift"
    click Sliders "https://github.com/fl0p/lg/blob/master/lg/ViewController.swift"
    click Drag "https://github.com/fl0p/lg/blob/master/lg/DragManager.swift"
    click Filter "https://github.com/fl0p/lg/blob/master/lg/DisplacementMapFilter.swift"
    click CIKernel "https://github.com/fl0p/lg/blob/master/lg/Displacement.ci.metal"
    click MetalShader "https://github.com/fl0p/lg/blob/master/lg/File.metal"
    click Assets "https://github.com/fl0p/lg/blob/master/lg/Assets.xcassets"
    click AppDelegate "https://github.com/fl0p/lg/blob/master/lg/AppDelegate.swift"
    click SceneDelegate "https://github.com/fl0p/lg/blob/master/lg/SceneDelegate.swift"

    %% Styles
    classDef ui fill:#e0f7fa,stroke:#0288d1,color:#000;
    classDef logic fill:#fff9c4,stroke:#fbc02d,color:#000;
    classDef compute fill:#c8e6c9,stroke:#388e3c,color:#000;
    classDef gpu fill:#ffccbc,stroke:#e64a19,color:#000;
    classDef resource fill:#e0e0e0,stroke:#757575,color:#000;
    classDef lifecycle fill:#d1c4e9,stroke:#7b1fa2,color:#000;
```
