# linkedinattic/layoutkit architecture

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

- Interactive diagram: https://gitdiagram.com/linkedinattic/layoutkit
- Repository: https://github.com/linkedinattic/layoutkit

## Overview

##### Project type and purpose
- Open-source UI layout library (LayoutKit) for iOS, macOS, tvOS.
- Offers a declarative, immutable, high-performance alternative to Auto Layout.
- Exposes both Swift and Objective-C APIs, with adapters for UITableView/UICollectionView.

##### File structure
Top-level folders and their roles:
- Sources/ – core library code, organized into submodules (Layouts, LayoutArrangement, LayoutMeasurement, Views, ObjCSupport, etc.).
- ExampleLayouts/ – standalone Swift layouts demonstrating API patterns.
- LayoutKitSampleApp/ & LayoutKitObjCSampleApp/ – sample iOS apps (Swift Obj-C) that consume the library.
- LayoutKit.playground/ – interactive playground showing usage.
- LayoutKitTests/ – unit tests for core modules.
- Tests/cocoapods & Tests/swift-package-manager/ – validation of CocoaPods, Carthage, SwiftPM integration.
- docs/ – user documentation, benchmarks, guides.
- Package.swift, *.podspec – package managers configuration.

##### README for insights
- Core features: asynchronous layout computation, immutable “Layout” data structures, view adapters.
- Supported platforms: iOS, macOS, tvOS.
- Integration: CocoaPods, Carthage, Swift Package Manager, Playground.

##### Components
a. Consumer layer:
– Sample Apps (Swift/Obj-C)
– ExampleLayouts
– Playground
b. Package manager layer:
– CocoaPods / Carthage / SwiftPM (Package.swift / podspec)
c. Library core modules (Sources/):
1. Layout Definitions (Sources/Layouts/…): ButtonLayout, StackLayout, InsetLayout, etc.
2. Layout Model & API (Sources/Layout.swift, Alignment.swift, Flexibility.swift)
3. Measurement & Arrangement (Sources/LayoutMeasurement.swift, LayoutArrangement.swift)
4. Views & Adapters (Sources/Views/…): ReloadableView, LayoutAdapterTableView/CollectionView
5. Platform Support: UIKitSupport.swift, AppKitSupport.swift
6. ObjC Bridging (Sources/ObjCSupport/…): Builders, LOKLayout classes, header interfaces
7. Internal Utilities (Sources/Internal/…, Math/)
d. Testing & Documentation:
– Unit tests (LayoutKitTests/)
– Docs site (mkdocs.yml & docs/)

##### Relationships and data flow
- At build time, consumer apps import LayoutKit via CocoaPods/Car​thage/SwiftPM.
- At runtime:
1. Client code constructs a tree of immutable Layout objects (e.g., StackLayout, LabelLayout).
2. Calls layout.arrangement() → produces a LayoutArrangement (position+size for each sublayout).
3. Calls arrangement.makeViews(in: rootView) → instantiates UIViews or NSViews via UIKitSupport/AppKitSupport.
4. If using UITableView/UICollectionView, the Views layer’s LayoutAdapter manages batch updates and cell reuse.
- ObjC consumers call into ObjCSupport layer, which wraps Swift types in Objective-C friendly classes/builders.

##### Architectural patterns & design principles
- Layered modular architecture: clear separation between definition (Layouts), algorithm (Measurement & Arrangement), and rendering (Views).
- Declarative immutable data structures → facilitates background computation and caching.
- Builder pattern in ObjCSupport for Objective-C API ergonomics.
- Adapter pattern for integrating with UITableView/UICollectionView.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Consumer Layer
    subgraph "Consumer Layer"
        A1["Swift Sample App"]:::consumer
        A2["Objective-C Sample App"]:::consumer
        A3["ExampleLayouts"]:::consumer
        A4["Interactive Playground"]:::consumer
    end

    %% Package Manager Layer
    subgraph "Package Manager Integration"
        B1["Swift Package Manager (Package.swift)"]:::package
        B2["CocoaPods (LayoutKit.podspec)"]:::package
        B3["CocoaPods Obj-C (LayoutKitObjC.podspec)"]:::package
        B4["CocoaPods Validation (Tests/cocoapods/)"]:::package
        B5["SwiftPM Validation (Tests/swift-package-manager/)"]:::package
    end

    %% Core Modules
    subgraph "Core Library Modules"
        subgraph "Layouts"
            C1["Layout Definitions"]:::core
        end
        subgraph "Model & API"
            C2["Layout Model & API"]:::core
        end
        subgraph "Measurement & Arrangement"
            C3["Measurement & Arrangement"]:::core
        end
        subgraph "Views & Adapters"
            C4["Views & Adapters"]:::core
        end
        subgraph "Platform Support"
            C5["Platform Support"]:::core
        end
        subgraph "ObjC Bridging"
            C6["ObjC Bridging"]:::objc
        end
        subgraph "Internal Utilities"
            C7["Internal Utilities"]:::core
        end
    end

    %% Testing & Documentation
    subgraph "Testing & Documentation"
        D1["Unit Tests (LayoutKitTests/)"]:::testdoc
        D2["Documentation (docs/, mkdocs.yml)"]:::testdoc
    end

    %% Build Dependencies
    A1 --> B1
    A1 --> B2
    A2 --> B1
    A2 --> B3
    A3 --> B1
    A4 --> B1

    B1 --> C1
    B1 --> C2
    B1 --> C3
    B1 --> C4
    B1 --> C5
    B1 --> C6
    B1 --> C7
    B2 --> C1
    B3 --> C6

    B4 --> B2
    B5 --> B1

    D1 --> C1
    D1 --> C2
    D1 --> C3
    D1 --> C4
    D1 --> C5
    D1 --> C6
    D1 --> C7
    D2 --> C1
    D2 --> C4

    %% Runtime Data Flow
    A1 -->|"constructs Layout tree"| C1
    A2 -->|"constructs Layout tree"| C1
    C1 -->|"computes arrangement"| C3
    C3 -->|"makes views"| C4
    C4 -->|"uses platform support"| C5
    A2 -->|"ObjC call"| C6
    C6 -->|"wraps Swift types"| C1
    C6 -->|"wraps Swift types"| C3

    %% Click Events
    click A1 "https://github.com/linkedinattic/layoutkit/tree/master/LayoutKitSampleApp/"
    click A2 "https://github.com/linkedinattic/layoutkit/tree/master/LayoutKitObjCSampleApp/"
    click A3 "https://github.com/linkedinattic/layoutkit/tree/master/ExampleLayouts/"
    click A4 "https://github.com/linkedinattic/layoutkit/tree/master/LayoutKit.playground/"
    click B1 "https://github.com/linkedinattic/layoutkit/blob/master/Package.swift"
    click B2 "https://github.com/linkedinattic/layoutkit/blob/master/LayoutKit.podspec"
    click B3 "https://github.com/linkedinattic/layoutkit/blob/master/LayoutKitObjC.podspec"
    click B4 "https://github.com/linkedinattic/layoutkit/tree/master/Tests/cocoapods/"
    click B5 "https://github.com/linkedinattic/layoutkit/tree/master/Tests/swift-package-manager/"
    click C1 "https://github.com/linkedinattic/layoutkit/tree/master/Sources/Layouts/"
    click C2 "https://github.com/linkedinattic/layoutkit/blob/master/Sources/Layout.swift"
    click C3 "https://github.com/linkedinattic/layoutkit/blob/master/Sources/LayoutMeasurement.swift"
    click C4 "https://github.com/linkedinattic/layoutkit/tree/master/Sources/Views/"
    click C5 "https://github.com/linkedinattic/layoutkit/blob/master/Sources/UIKitSupport.swift"
    click C6 "https://github.com/linkedinattic/layoutkit/tree/master/Sources/ObjCSupport/"
    click C7 "https://github.com/linkedinattic/layoutkit/tree/master/Sources/Internal/"

    %% Styles
    classDef consumer fill:#a2f5a2,stroke:#3a9d23,color:#000
    classDef package fill:#f5d88a,stroke:#c27a00,color:#000
    classDef core fill:#8ecae6,stroke:#219ebc,color:#000
    classDef objc fill:#d9a1f3,stroke:#9b2fc2,color:#000
    classDef testdoc fill:#d3d3d3,stroke:#a9a9a9,color:#000
```
