# getlantern/systray architecture

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

- Interactive diagram: https://gitdiagram.com/getlantern/systray
- Repository: https://github.com/getlantern/systray

## Overview

##### Project type and purpose
- Purpose: Expose a uniform Go API (`systray.Run`, `AddMenuItem`, etc.) that works on Windows, macOS, and Linux by delegating to underlying native APIs via cgo and platform-specific code.

##### Main components
a. Core API layer (systray.go)
– Exports `Run(onReady, onExit)`, `SetIcon`, `AddMenuItem`, etc.
– Defines common data structures and interface points.
b. Native bindings / backends
– Windows: `systray_windows.go`, `systray_windows_test.go`, `systray.h`, `systray_windows.c`
– macOS: `systray_darwin.go`, `systray_darwin.m`
– Linux:
- GTK: `systray_linux.go`, `systray_linux.c`
- AppIndicator/Ayatana: `systray_linux_appindicator.go`, `systray_linux_ayatana.go`
– Build tags and CGO glue control which backend is compiled.
c. Examples (clients)
– Webview example (`webview_example/`): Combines systray with a webview UI (native WebKit/Edge embedding).
d. External dependencies
– OS frameworks (Win32 Shell API, macOS AppKit `NSStatusBar`, Linux GTK3/AppIndicator3)
– C compiler and headers (via CGO)

##### Relationships and interactions
- The client (`main.go` in examples) invokes the Core API.
- Core API dispatches to a platform-specific implementation, selected at compile time by build tags and CGO.
- Platform backend uses CGO to call native C/Objective-C/Win32 functions.
- On Linux, code may further dispatch between GTK and AppIndicator variants.
- Examples link against the same core + backends; the webview example also pulls in a WebView binding layer.

##### Architectural patterns and design principles
- Adapter/Bridge pattern: Core Go API delegates to different “adapters” per OS.
- Modularity via build tags: Only one platform backend is built into the final binary.
- Minimal surface area: Core remains platform-agnostic; all OS quirks live in backend files.
- Examples as integration tests: Demonstrate usage and co-existence with other UI libraries.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Example Clients
    subgraph "Example Clients"
        direction TB
        Basic["Basic Example Client\nexample/main.go"]:::example
        Icons["Icon Assets & Scripts\nexample/icon/iconunix.go\nexample/icon/iconwin.go\nexample/icon/make_icon.sh\nexample/icon/make_icon.bat"]:::example
        Webview["Webview Example Client\nwebview_example/main.go"]:::example
    end

    %% Core and CGO
    Core["Core API Layer\nsystray.go"]:::core
    Header["CGO Header\nsystray.h"]:::cgo

    %% Platform Backends
    subgraph "Platform Backends"
        direction TB
        Win["Windows Backend Adapter\nsystray_windows.go\nsystray_windows_test.go\nsystray_nonwindows.go"]:::backend
        Mac["macOS Backend Adapter\nsystray_darwin.go\nsystray_darwin.m"]:::backend
        LinuxGTK["Linux GTK Adapter\nsystray_linux.go\nsystray_linux.c"]:::backend
        LinuxApp["Linux AppIndicator Adapter\nsystray_linux_appindicator.go"]:::backend
        LinuxAya["Linux Ayatana Adapter\nsystray_linux_ayatana.go"]:::backend
    end

    %% Native OS APIs
    WinShell[(Win32 Shell API)]:::native
    AppKit[(AppKit\nNSStatusBar)]:::native
    GTK3[(GTK3)]:::native
    AppInd[(libayatana-appindicator3)]:::native

    %% Connections
    Basic -->|"systray.Run etc."| Core
    Icons --> Core
    Webview -->|"systray.Run etc."| Core

    Core -->|"build tags/CGO"| Header

    Header -->|"// +build windows"| Win
    Header -->|"// +build darwin"| Mac
    Header -->|"// +build linux && gtk"| LinuxGTK
    Header -->|"// +build linux && appindicator"| LinuxApp
    Header -->|"// +build linux && ayatana"| LinuxAya

    Win -->|"nativeLoop() call"| WinShell
    Mac -->|"nativeLoop() call"| AppKit
    LinuxGTK -->|"nativeLoop() call"| GTK3
    LinuxApp -->|"nativeLoop() call"| AppInd
    LinuxAya -->|"nativeLoop() call"| AppInd

    %% Click Events
    click Core "https://github.com/getlantern/systray/blob/master/systray.go"
    click Header "https://github.com/getlantern/systray/blob/master/systray.h"
    click Win "https://github.com/getlantern/systray/blob/master/systray_windows.go"
    click Win "https://github.com/getlantern/systray/blob/master/systray_windows_test.go"
    click Win "https://github.com/getlantern/systray/blob/master/systray_nonwindows.go"
    click Mac "https://github.com/getlantern/systray/blob/master/systray_darwin.go"
    click Mac "https://github.com/getlantern/systray/blob/master/systray_darwin.m"
    click LinuxGTK "https://github.com/getlantern/systray/blob/master/systray_linux.go"
    click LinuxGTK "https://github.com/getlantern/systray/blob/master/systray_linux.c"
    click LinuxApp "https://github.com/getlantern/systray/blob/master/systray_linux_appindicator.go"
    click LinuxAya "https://github.com/getlantern/systray/blob/master/systray_linux_ayatana.go"
    click Basic "https://github.com/getlantern/systray/blob/master/example/main.go"
    click Icons "https://github.com/getlantern/systray/blob/master/example/icon/iconunix.go"
    click Icons "https://github.com/getlantern/systray/blob/master/example/icon/iconwin.go"
    click Icons "https://github.com/getlantern/systray/blob/master/example/icon/make_icon.sh"
    click Icons "https://github.com/getlantern/systray/blob/master/example/icon/make_icon.bat"
    click Webview "https://github.com/getlantern/systray/blob/master/webview_example/main.go"

    %% Styles
    classDef example fill:#f9f,stroke:#333,stroke-dasharray:5 5
    classDef core fill:#bbf,stroke:#333
    classDef cgo fill:#bfb,stroke:#333
    classDef backend fill:#fbf,stroke:#333
    classDef native fill:#ffb,stroke:#333
```
