Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,22 @@ jobs:
cd build-ios-${{ matrix.target }}
cmake --build . --config Release

# Linux build
# Linux builds (GTK 4 default and GTK 3)
build-linux:
strategy:
matrix:
include:
- backend: GTK4
gtk_pkg: libgtk-4-dev
forbidden_lib: libgtk-3
cmake_args: ""
- backend: GTK3
gtk_pkg: libgtk-3-dev
forbidden_lib: libgtk-4
cmake_args: "-DNATIVEAPI_LINUX_BACKEND=GTK3"

runs-on: ubuntu-latest
name: Build Linux
name: Build Linux (${{ matrix.backend }})

steps:
- uses: actions/checkout@v4
Expand All @@ -109,21 +121,32 @@ jobs:
run: |
cmake --version
sudo apt-get update
sudo apt-get install -y ninja-build libgtk-3-dev libx11-dev libxi-dev
sudo apt-get install -y ninja-build ${{ matrix.gtk_pkg }} libx11-dev libxi-dev xvfb

- name: Configure CMake
shell: bash
run: |
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Release ${{ matrix.cmake_args }}

- name: Build Examples
- name: Build Examples and Tests
shell: bash
run: |
cd build
cmake --build . --config Release

- name: Verify GTK Linkage
shell: bash
run: |
ldd build/examples/window_example/window_example | grep -E "gtk|gdk"
! ldd build/examples/window_example/window_example | grep -q "${{ matrix.forbidden_lib }}"

- name: Run Tests
shell: bash
run: |
xvfb-run -a ctest --test-dir build --output-on-failure

# macOS build
build-macos:
runs-on: macos-latest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Language bindings: [Flutter](https://github.com/libnativeapi/nativeapi-flutter)

## Installation

Requires CMake 3.10+ and a C++17 compiler. On Linux, install the GTK 3 headers (`sudo apt install libgtk-3-dev`).
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)).

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

Expand Down
58 changes: 58 additions & 0 deletions docs/linux-gtk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Linux GTK Backends (GTK 4 & GTK 3)

`nativeapi-core` supports both **GTK 4** (default) and **GTK 3** on Linux.

GTK 4 aborts at runtime if GTK 3 symbols are loaded into the same process, while
embedders such as Flutter Linux still run on GTK 3. Supporting both backends at
build time lets standalone and GTK 4 apps use GTK 4 out of the box without
breaking GTK 3 hosts.

## CMake Configuration

Select the Linux backend with `NATIVEAPI_LINUX_BACKEND` (case-insensitive):

| Value | Default | `pkg-config` module | Compile definition |
| --- | --- | --- | --- |
| `GTK4` | Yes | `gtk4` | `NATIVEAPI_LINUX_GTK4` |
| `GTK3` | No | `gtk+-3.0` | `NATIVEAPI_LINUX_GTK3` |

```bash
# Default (GTK 4)
cmake -S . -B build
cmake --build build

# Explicit GTK 3 (e.g. for GTK 3 hosts such as Flutter Linux)
cmake -S . -B build-gtk3 -DNATIVEAPI_LINUX_BACKEND=GTK3
cmake --build build-gtk3
```

`NATIVEAPI_LINUX_GTK4` / `NATIVEAPI_LINUX_GTK3` is exported as a `PUBLIC`
compile definition on the `nativeapi` target. Public C++ (`src/*.h`) and C ABI
(`src/capi/*.h`) headers remain completely backend-agnostic.

## Source-Selection Contract for Downstream Bindings

All Linux platform sources live directly in `src/platform/linux/*.cpp` and
compile cleanly under **both** GTK 4 and GTK 3 — non-CMake build systems do not
need to maintain separate per-GTK file lists:

1. Compile every `src/platform/linux/*.cpp`.
2. Link against either `gtk4` (with `-DNATIVEAPI_LINUX_GTK4`) or `gtk+-3.0`
(with `-DNATIVEAPI_LINUX_GTK3`), plus `x11`, `xi`, and `pthread`.
3. `src/platform/linux/gtk_compat_linux.h` validates that the compile definition
matches `GTK_MAJOR_VERSION` from `<gtk/gtk.h>` and fails compilation if they
disagree.

## Downstream Binding Defaults

- **C++, Rust, Swift, Go, Python, JS, C#**: follow core's default (**GTK 4**).
- CMake-based bindings (Go, Python, JS, C#): pass `-DNATIVEAPI_LINUX_BACKEND=GTK3`
to build against GTK 3.
- Rust (`cnativeapi` / `nativeapi`): enable the `gtk3` Cargo feature or set
`NATIVEAPI_LINUX_BACKEND=GTK3`.
- Swift (`Package.swift`): set `NATIVEAPI_LINUX_BACKEND=GTK3` to use `CGTK3`
instead of `CGTK4`.
- **Dart / Flutter (`cnativeapi` / `nativeapi_flutter`)**: defaults to **GTK 3**
to match Flutter's Linux embedder. Opt into GTK 4 via
`hooks.user_defines.cnativeapi.linux_backend: gtk4` in `pubspec.yaml` or
`NATIVEAPI_LINUX_BACKEND=GTK4`.
5 changes: 1 addition & 4 deletions examples/menu_c_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ elseif(APPLE)
elseif(WIN32)
target_link_libraries(menu_c_example user32 kernel32)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
target_include_directories(menu_c_example PRIVATE ${GTK3_INCLUDE_DIRS})
target_link_libraries(menu_c_example ${GTK3_LIBRARIES})
# GTK, X11 and Xi are already linked publicly by nativeapi.
elseif(ANDROID)
target_link_libraries(menu_c_example log android)
endif()
5 changes: 1 addition & 4 deletions examples/menu_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ elseif(APPLE)
elseif(WIN32)
target_link_libraries(menu_example user32 kernel32)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
target_include_directories(menu_example PRIVATE ${GTK3_INCLUDE_DIRS})
target_link_libraries(menu_example ${GTK3_LIBRARIES})
# GTK, X11 and Xi are already linked publicly by nativeapi.
elseif(ANDROID)
target_link_libraries(menu_example log android)
endif()
21 changes: 20 additions & 1 deletion examples/tray_color_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,18 @@ class SystemColourSwitch {
g_object_ref_sink(widget_);
view = std::make_shared<View>(widget_);
g_object_unref(widget_); // The View wrapper retains it from here.
gtk_widget_show(widget_);
gtk_widget_set_visible(widget_, TRUE);
#if GTK_MAJOR_VERSION >= 4
signal_ = g_signal_connect(widget_, "toggled", G_CALLBACK(+[](GtkCheckButton*, gpointer data) {
static_cast<SystemColourSwitch*>(data)->Changed();
}),
this);
#else
signal_ = g_signal_connect(widget_, "toggled", G_CALLBACK(+[](GtkToggleButton*, gpointer data) {
static_cast<SystemColourSwitch*>(data)->Changed();
}),
this);
#endif
#endif
view->SetPreferredSize(nativeapi::Size{0, 32});
SetChecked(true);
Expand All @@ -67,7 +74,11 @@ class SystemColourSwitch {
return SendMessageW(static_cast<HWND>(button_->GetNativeObject()), BM_GETCHECK, 0, 0) ==
BST_CHECKED;
#elif defined(__linux__)
#if GTK_MAJOR_VERSION >= 4
return gtk_check_button_get_active(GTK_CHECK_BUTTON(widget_));
#else
return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget_));
#endif
#endif
}

Expand All @@ -80,7 +91,11 @@ class SystemColourSwitch {
SendMessageW(static_cast<HWND>(button_->GetNativeObject()), BM_SETCHECK,
value ? BST_CHECKED : BST_UNCHECKED, 0);
#elif defined(__linux__)
#if GTK_MAJOR_VERSION >= 4
gtk_check_button_set_active(GTK_CHECK_BUTTON(widget_), value);
#else
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget_), value);
#endif
#endif
suppress_ = false;
}
Expand All @@ -92,7 +107,11 @@ class SystemColourSwitch {
#elif defined(_WIN32)
SendMessageW(static_cast<HWND>(button_->GetNativeObject()), BM_CLICK, 0, 0);
#elif defined(__linux__)
#if GTK_MAJOR_VERSION >= 4
gtk_check_button_set_active(GTK_CHECK_BUTTON(widget_), !IsChecked());
#else
gtk_button_clicked(GTK_BUTTON(widget_));
#endif
#endif
}

Expand Down
5 changes: 1 addition & 4 deletions examples/tray_stats_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ elseif(APPLE)
elseif(WIN32)
target_link_libraries(tray_stats_example user32 kernel32)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
target_include_directories(tray_stats_example PRIVATE ${GTK3_INCLUDE_DIRS})
target_link_libraries(tray_stats_example ${GTK3_LIBRARIES})
# GTK, X11 and Xi are already linked publicly by nativeapi.
elseif(ANDROID)
target_link_libraries(tray_stats_example log android)
endif()
5 changes: 1 addition & 4 deletions examples/tray_view_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ elseif(APPLE)
elseif(WIN32)
target_link_libraries(tray_view_example user32 kernel32)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
target_include_directories(tray_view_example PRIVATE ${GTK3_INCLUDE_DIRS})
target_link_libraries(tray_view_example ${GTK3_LIBRARIES})
# GTK, X11 and Xi are already linked publicly by nativeapi.
elseif(ANDROID)
target_link_libraries(tray_view_example log android)
endif()
5 changes: 1 addition & 4 deletions examples/view_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ elseif(APPLE)
elseif(WIN32)
target_link_libraries(view_example user32 kernel32)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
target_include_directories(view_example PRIVATE ${GTK3_INCLUDE_DIRS})
target_link_libraries(view_example ${GTK3_LIBRARIES})
# GTK, X11 and Xi are already linked publicly by nativeapi.
elseif(ANDROID)
target_link_libraries(view_example log android)
endif()
4 changes: 4 additions & 0 deletions examples/window_lifecycle_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ static void CloseNatively(const std::shared_ptr<Window>& window) {
#elif defined(_WIN32)
DestroyWindow(static_cast<HWND>(native));
#elif defined(NATIVEAPI_EXAMPLE_GTK)
#if GTK_MAJOR_VERSION >= 4
gtk_window_destroy(GTK_WINDOW(native));
#else
gtk_widget_destroy(GTK_WIDGET(native));
#endif
#else
(void)native;
#endif
Expand Down
5 changes: 1 addition & 4 deletions examples/window_taskbar_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ elseif(APPLE)
elseif(WIN32)
target_link_libraries(window_taskbar_example user32 kernel32)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
target_include_directories(window_taskbar_example PRIVATE ${GTK3_INCLUDE_DIRS})
target_link_libraries(window_taskbar_example ${GTK3_LIBRARIES})
# GTK, X11 and Xi are already linked publicly by nativeapi.
elseif(ANDROID)
target_link_libraries(window_taskbar_example log android)
endif()
16 changes: 15 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ cmake_minimum_required(VERSION 3.10)
project(nativeapi VERSION 0.0.1 LANGUAGES CXX)

option(NATIVEAPI_ENABLE_WINUI3 "Use WinUI 3 for Windows UI components (MSVC only)" OFF)
set(NATIVEAPI_LINUX_BACKEND "GTK4" CACHE STRING "Linux GTK backend (GTK4 or GTK3)")
set_property(CACHE NATIVEAPI_LINUX_BACKEND PROPERTY STRINGS GTK4 GTK3)

# Enable Objective-C++
if(APPLE)
Expand All @@ -31,10 +33,21 @@ elseif(ANDROID)
elseif(CMAKE_SYSTEM_NAME STREQUAL "iOS")
file(GLOB PLATFORM_SOURCES "platform/ios/*.mm")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
string(TOUPPER "${NATIVEAPI_LINUX_BACKEND}" _NATIVEAPI_LINUX_BACKEND_UPPER)
if(_NATIVEAPI_LINUX_BACKEND_UPPER STREQUAL "GTK4")
set(NATIVEAPI_LINUX_GTK_PACKAGE "gtk4")
set(NATIVEAPI_LINUX_GTK_DEFINE "NATIVEAPI_LINUX_GTK4")
elseif(_NATIVEAPI_LINUX_BACKEND_UPPER STREQUAL "GTK3")
set(NATIVEAPI_LINUX_GTK_PACKAGE "gtk+-3.0")
set(NATIVEAPI_LINUX_GTK_DEFINE "NATIVEAPI_LINUX_GTK3")
else()
message(FATAL_ERROR
"Invalid NATIVEAPI_LINUX_BACKEND='${NATIVEAPI_LINUX_BACKEND}'. Expected GTK4 or GTK3.")
endif()
file(GLOB PLATFORM_SOURCES "platform/linux/*.cpp")
# Find packages for Linux
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET ${NATIVEAPI_LINUX_GTK_PACKAGE})
pkg_check_modules(X11 REQUIRED IMPORTED_TARGET x11)
pkg_check_modules(XI REQUIRED IMPORTED_TARGET xi)
elseif(APPLE)
Expand Down Expand Up @@ -90,6 +103,7 @@ target_include_directories(nativeapi PUBLIC
)

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_compile_definitions(nativeapi PUBLIC ${NATIVEAPI_LINUX_GTK_DEFINE})
target_include_directories(nativeapi PUBLIC ${GTK_INCLUDE_DIRS})
endif ()

Expand Down
4 changes: 2 additions & 2 deletions src/platform/linux/accessibility_manager_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

// Import GTK headers for accessibility support
#include <glib.h>
#include <gtk/gtk.h>
#include "gtk_compat_linux.h"

namespace nativeapi {

Expand All @@ -18,7 +18,7 @@ void AccessibilityManager::Enable() {
// Initialize GTK if not already initialized (for accessibility bridge)
if (!gdk_display_get_default()) {
// Try to initialize GTK silently if not already done
gtk_init_check(nullptr, nullptr);
linux_gtk::InitCheck();
}

// The accessibility bridge in GTK is automatically enabled when accessibility
Expand Down
10 changes: 8 additions & 2 deletions src/platform/linux/application_linux.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <fcntl.h>
#include <gio/gio.h>
#include <glib.h>
#include <gtk/gtk.h>
#include "gtk_compat_linux.h"
#include <sys/stat.h>
#include <unistd.h>
#include <cstdlib>
Expand All @@ -24,7 +24,11 @@ class Application::Impl {

bool Initialize() {
// Initialize GTK
#if NATIVEAPI_GTK4
linux_gtk::InitCheck();
#else
gtk_init(nullptr, nullptr);
#endif

// Create GTK application with default ID. G_APPLICATION_DEFAULT_FLAGS is
// GLib 2.74+; older distributions (and manylinux wheels) spell it _NONE.
Expand Down Expand Up @@ -133,8 +137,10 @@ class Application::Impl {
return false;
}

// Set application icon
#if !NATIVEAPI_GTK4
// Set application icon (GTK 4 uses icon themes / .desktop icons instead).
gtk_window_set_default_icon(pixbuf);
#endif

g_object_unref(pixbuf);
return true;
Expand Down
Loading
Loading