|
| 1 | +# Linux GTK Backends (GTK 4 & GTK 3) |
| 2 | + |
| 3 | +`nativeapi-core` supports both **GTK 4** (default) and **GTK 3** on Linux. |
| 4 | + |
| 5 | +GTK 4 aborts at runtime if GTK 3 symbols are loaded into the same process, while |
| 6 | +embedders such as Flutter Linux still run on GTK 3. Supporting both backends at |
| 7 | +build time lets standalone and GTK 4 apps use GTK 4 out of the box without |
| 8 | +breaking GTK 3 hosts. |
| 9 | + |
| 10 | +## CMake Configuration |
| 11 | + |
| 12 | +Select the Linux backend with `NATIVEAPI_LINUX_BACKEND` (case-insensitive): |
| 13 | + |
| 14 | +| Value | Default | `pkg-config` module | Compile definition | |
| 15 | +| --- | --- | --- | --- | |
| 16 | +| `GTK4` | Yes | `gtk4` | `NATIVEAPI_LINUX_GTK4` | |
| 17 | +| `GTK3` | No | `gtk+-3.0` | `NATIVEAPI_LINUX_GTK3` | |
| 18 | + |
| 19 | +```bash |
| 20 | +# Default (GTK 4) |
| 21 | +cmake -S . -B build |
| 22 | +cmake --build build |
| 23 | + |
| 24 | +# Explicit GTK 3 (e.g. for GTK 3 hosts such as Flutter Linux) |
| 25 | +cmake -S . -B build-gtk3 -DNATIVEAPI_LINUX_BACKEND=GTK3 |
| 26 | +cmake --build build-gtk3 |
| 27 | +``` |
| 28 | + |
| 29 | +`NATIVEAPI_LINUX_GTK4` / `NATIVEAPI_LINUX_GTK3` is exported as a `PUBLIC` |
| 30 | +compile definition on the `nativeapi` target. Public C++ (`src/*.h`) and C ABI |
| 31 | +(`src/capi/*.h`) headers remain completely backend-agnostic. |
| 32 | + |
| 33 | +## Source-Selection Contract for Downstream Bindings |
| 34 | + |
| 35 | +All Linux platform sources live directly in `src/platform/linux/*.cpp` and |
| 36 | +compile cleanly under **both** GTK 4 and GTK 3 — non-CMake build systems do not |
| 37 | +need to maintain separate per-GTK file lists: |
| 38 | + |
| 39 | +1. Compile every `src/platform/linux/*.cpp`. |
| 40 | +2. Link against either `gtk4` (with `-DNATIVEAPI_LINUX_GTK4`) or `gtk+-3.0` |
| 41 | + (with `-DNATIVEAPI_LINUX_GTK3`), plus `x11`, `xi`, and `pthread`. |
| 42 | +3. `src/platform/linux/gtk_compat_linux.h` validates that the compile definition |
| 43 | + matches `GTK_MAJOR_VERSION` from `<gtk/gtk.h>` and fails compilation if they |
| 44 | + disagree. |
| 45 | + |
| 46 | +## Downstream Binding Defaults |
| 47 | + |
| 48 | +- **C++, Rust, Swift, Go, Python, JS, C#**: follow core's default (**GTK 4**). |
| 49 | + - CMake-based bindings (Go, Python, JS, C#): pass `-DNATIVEAPI_LINUX_BACKEND=GTK3` |
| 50 | + to build against GTK 3. |
| 51 | + - Rust (`cnativeapi` / `nativeapi`): enable the `gtk3` Cargo feature or set |
| 52 | + `NATIVEAPI_LINUX_BACKEND=GTK3`. |
| 53 | + - Swift (`Package.swift`): set `NATIVEAPI_LINUX_BACKEND=GTK3` to use `CGTK3` |
| 54 | + instead of `CGTK4`. |
| 55 | +- **Dart / Flutter (`cnativeapi` / `nativeapi_flutter`)**: defaults to **GTK 3** |
| 56 | + to match Flutter's Linux embedder. Opt into GTK 4 via |
| 57 | + `hooks.user_defines.cnativeapi.linux_backend: gtk4` in `pubspec.yaml` or |
| 58 | + `NATIVEAPI_LINUX_BACKEND=GTK4`. |
0 commit comments