Skip to content

Commit 94961b7

Browse files
authored
Merge pull request #107 from libnativeapi/feat/linux-gtk4-backend
feat(linux): support both GTK 4 (default) and GTK 3 backends
2 parents e06e9f6 + 217d736 commit 94961b7

45 files changed

Lines changed: 4595 additions & 162 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/build.yml‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,22 @@ jobs:
9696
cd build-ios-${{ matrix.target }}
9797
cmake --build . --config Release
9898
99-
# Linux build
99+
# Linux builds (GTK 4 default and GTK 3)
100100
build-linux:
101+
strategy:
102+
matrix:
103+
include:
104+
- backend: GTK4
105+
gtk_pkg: libgtk-4-dev
106+
forbidden_lib: libgtk-3
107+
cmake_args: ""
108+
- backend: GTK3
109+
gtk_pkg: libgtk-3-dev
110+
forbidden_lib: libgtk-4
111+
cmake_args: "-DNATIVEAPI_LINUX_BACKEND=GTK3"
112+
101113
runs-on: ubuntu-latest
102-
name: Build Linux
114+
name: Build Linux (${{ matrix.backend }})
103115

104116
steps:
105117
- uses: actions/checkout@v4
@@ -109,21 +121,32 @@ jobs:
109121
run: |
110122
cmake --version
111123
sudo apt-get update
112-
sudo apt-get install -y ninja-build libgtk-3-dev libx11-dev libxi-dev
124+
sudo apt-get install -y ninja-build ${{ matrix.gtk_pkg }} libx11-dev libxi-dev xvfb
113125
114126
- name: Configure CMake
115127
shell: bash
116128
run: |
117129
mkdir build
118130
cd build
119-
cmake .. -DCMAKE_BUILD_TYPE=Release
131+
cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Release ${{ matrix.cmake_args }}
120132
121-
- name: Build Examples
133+
- name: Build Examples and Tests
122134
shell: bash
123135
run: |
124136
cd build
125137
cmake --build . --config Release
126138
139+
- name: Verify GTK Linkage
140+
shell: bash
141+
run: |
142+
ldd build/examples/window_example/window_example | grep -E "gtk|gdk"
143+
! ldd build/examples/window_example/window_example | grep -q "${{ matrix.forbidden_lib }}"
144+
145+
- name: Run Tests
146+
shell: bash
147+
run: |
148+
xvfb-run -a ctest --test-dir build --output-on-failure
149+
127150
# macOS build
128151
build-macos:
129152
runs-on: macos-latest

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Language bindings: [Flutter](https://github.com/libnativeapi/nativeapi-flutter)
1414

1515
## Installation
1616

17-
Requires CMake 3.10+ and a C++17 compiler. On Linux, install the GTK 3 headers (`sudo apt install libgtk-3-dev`).
17+
Requires CMake 3.10+ and a C++17 compiler. On Linux, install the GTK 4 headers (`sudo apt install libgtk-4-dev`; or `libgtk-3-dev` when configuring with `-DNATIVEAPI_LINUX_BACKEND=GTK3` — see [docs/linux-gtk.md](docs/linux-gtk.md)).
1818

1919
Add the repository to your project and link the `nativeapi` target:
2020

‎docs/linux-gtk.md‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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`.

‎examples/menu_c_example/CMakeLists.txt‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ elseif(APPLE)
2121
elseif(WIN32)
2222
target_link_libraries(menu_c_example user32 kernel32)
2323
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
24-
find_package(PkgConfig REQUIRED)
25-
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
26-
target_include_directories(menu_c_example PRIVATE ${GTK3_INCLUDE_DIRS})
27-
target_link_libraries(menu_c_example ${GTK3_LIBRARIES})
24+
# GTK, X11 and Xi are already linked publicly by nativeapi.
2825
elseif(ANDROID)
2926
target_link_libraries(menu_c_example log android)
3027
endif()

‎examples/menu_example/CMakeLists.txt‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ elseif(APPLE)
2525
elseif(WIN32)
2626
target_link_libraries(menu_example user32 kernel32)
2727
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
28-
find_package(PkgConfig REQUIRED)
29-
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
30-
target_include_directories(menu_example PRIVATE ${GTK3_INCLUDE_DIRS})
31-
target_link_libraries(menu_example ${GTK3_LIBRARIES})
28+
# GTK, X11 and Xi are already linked publicly by nativeapi.
3229
elseif(ANDROID)
3330
target_link_libraries(menu_example log android)
3431
endif()

‎examples/tray_color_example/main.cpp‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,18 @@ class SystemColourSwitch {
4242
g_object_ref_sink(widget_);
4343
view = std::make_shared<View>(widget_);
4444
g_object_unref(widget_); // The View wrapper retains it from here.
45-
gtk_widget_show(widget_);
45+
gtk_widget_set_visible(widget_, TRUE);
46+
#if GTK_MAJOR_VERSION >= 4
47+
signal_ = g_signal_connect(widget_, "toggled", G_CALLBACK(+[](GtkCheckButton*, gpointer data) {
48+
static_cast<SystemColourSwitch*>(data)->Changed();
49+
}),
50+
this);
51+
#else
4652
signal_ = g_signal_connect(widget_, "toggled", G_CALLBACK(+[](GtkToggleButton*, gpointer data) {
4753
static_cast<SystemColourSwitch*>(data)->Changed();
4854
}),
4955
this);
56+
#endif
5057
#endif
5158
view->SetPreferredSize(nativeapi::Size{0, 32});
5259
SetChecked(true);
@@ -67,7 +74,11 @@ class SystemColourSwitch {
6774
return SendMessageW(static_cast<HWND>(button_->GetNativeObject()), BM_GETCHECK, 0, 0) ==
6875
BST_CHECKED;
6976
#elif defined(__linux__)
77+
#if GTK_MAJOR_VERSION >= 4
78+
return gtk_check_button_get_active(GTK_CHECK_BUTTON(widget_));
79+
#else
7080
return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget_));
81+
#endif
7182
#endif
7283
}
7384

@@ -80,7 +91,11 @@ class SystemColourSwitch {
8091
SendMessageW(static_cast<HWND>(button_->GetNativeObject()), BM_SETCHECK,
8192
value ? BST_CHECKED : BST_UNCHECKED, 0);
8293
#elif defined(__linux__)
94+
#if GTK_MAJOR_VERSION >= 4
95+
gtk_check_button_set_active(GTK_CHECK_BUTTON(widget_), value);
96+
#else
8397
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget_), value);
98+
#endif
8499
#endif
85100
suppress_ = false;
86101
}
@@ -92,7 +107,11 @@ class SystemColourSwitch {
92107
#elif defined(_WIN32)
93108
SendMessageW(static_cast<HWND>(button_->GetNativeObject()), BM_CLICK, 0, 0);
94109
#elif defined(__linux__)
110+
#if GTK_MAJOR_VERSION >= 4
111+
gtk_check_button_set_active(GTK_CHECK_BUTTON(widget_), !IsChecked());
112+
#else
95113
gtk_button_clicked(GTK_BUTTON(widget_));
114+
#endif
96115
#endif
97116
}
98117

‎examples/tray_stats_example/CMakeLists.txt‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ elseif(APPLE)
2525
elseif(WIN32)
2626
target_link_libraries(tray_stats_example user32 kernel32)
2727
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
28-
find_package(PkgConfig REQUIRED)
29-
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
30-
target_include_directories(tray_stats_example PRIVATE ${GTK3_INCLUDE_DIRS})
31-
target_link_libraries(tray_stats_example ${GTK3_LIBRARIES})
28+
# GTK, X11 and Xi are already linked publicly by nativeapi.
3229
elseif(ANDROID)
3330
target_link_libraries(tray_stats_example log android)
3431
endif()

‎examples/tray_view_example/CMakeLists.txt‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ elseif(APPLE)
2525
elseif(WIN32)
2626
target_link_libraries(tray_view_example user32 kernel32)
2727
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
28-
find_package(PkgConfig REQUIRED)
29-
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
30-
target_include_directories(tray_view_example PRIVATE ${GTK3_INCLUDE_DIRS})
31-
target_link_libraries(tray_view_example ${GTK3_LIBRARIES})
28+
# GTK, X11 and Xi are already linked publicly by nativeapi.
3229
elseif(ANDROID)
3330
target_link_libraries(tray_view_example log android)
3431
endif()

‎examples/view_example/CMakeLists.txt‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ elseif(APPLE)
2525
elseif(WIN32)
2626
target_link_libraries(view_example user32 kernel32)
2727
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
28-
find_package(PkgConfig REQUIRED)
29-
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
30-
target_include_directories(view_example PRIVATE ${GTK3_INCLUDE_DIRS})
31-
target_link_libraries(view_example ${GTK3_LIBRARIES})
28+
# GTK, X11 and Xi are already linked publicly by nativeapi.
3229
elseif(ANDROID)
3330
target_link_libraries(view_example log android)
3431
endif()

‎examples/window_lifecycle_example/main.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ static void CloseNatively(const std::shared_ptr<Window>& window) {
4040
#elif defined(_WIN32)
4141
DestroyWindow(static_cast<HWND>(native));
4242
#elif defined(NATIVEAPI_EXAMPLE_GTK)
43+
#if GTK_MAJOR_VERSION >= 4
44+
gtk_window_destroy(GTK_WINDOW(native));
45+
#else
4346
gtk_widget_destroy(GTK_WIDGET(native));
47+
#endif
4448
#else
4549
(void)native;
4650
#endif

0 commit comments

Comments
 (0)