GTK4Go provides comprehensive Go bindings for GTK4, enabling you to build native, cross-platform GUI applications using Go. This library focuses on providing type-safe, memory-safe, and Go-idiomatic access to GTK4's rich widget set and functionality.
- Complete GTK4 API coverage: Access the full power of GTK4 from Go
- Type-safe bindings: Properly typed interfaces for GTK4 components
- Automatic memory management: Automatic clean-up of GTK resources
- Background worker support: Run long tasks without blocking the UI
- Thread-safe: Proper handling of GTK's UI thread requirements
- Modern widgets: Support for GTK4's new widgets like ListView
- CSS styling: Comprehensive styling support with performance optimizations
GTK4Go is structured around several key components that work together to provide a complete GTK4 experience from Go:
-
CGo Interface Layer
- Uses CGo to interface with GTK4's C libraries
- Exposes GTK functionality through idiomatic Go interfaces
- Handles type conversion between Go and C types
-
UI Thread Management
- GTK requires all UI operations to happen on the main thread
- Implemented in
core/uithreadpackage - Provides a message queue system for thread-safe UI operations
- Automatically routes callbacks and UI updates to the main thread
-
Unified Callback System
- Centralized callback management for all GTK4 signals
- Maps C signal emissions to Go callback functions
- Handles memory management for callbacks to prevent leaks
- Supports various callback signatures for different signal types
-
Background Worker
- Non-blocking background task execution
- Progress reporting back to the UI thread
- Cancellation support
- Error handling
-
Widget Hierarchy
- Object-oriented widget hierarchy mirroring GTK4's structure
- Common base types with specific implementations
- Builder pattern with option functions for widget creation
GTK4Go employs several strategies to ensure proper memory management:
- Automatic Finalization: Uses Go's finalizers to clean up widget resources
- Explicit Destroy Methods: All widgets have Destroy() methods to explicitly free resources
- Reference Tracking: Maintains maps of object references to prevent premature garbage collection
- Signal Handler Cleanup: Automatically disconnects signal handlers when widgets are destroyed
+----------------------------------+
| Go Application |
| |
| +------------------------------+|
| | GTK4Go API ||
| | ||
| | +-----------+ +------------+||
| | | Widget | | Background |||
| | | Hierarchy | | Worker |||
| | +-----------+ +------------+||
| | ||
| | +-----------+ +------------+||
| | | Callback | | UI Thread |||
| | | System | | Management |||
| | +-----------+ +------------+||
| | ||
| +------------------------------+|
| |
+----------------------------------+
|
| CGo
v
+----------------------------------+
| C GTK4 Libraries |
+----------------------------------+
The callback system is one of the most critical components of GTK4Go. It handles the mapping between GTK signals and Go functions:
- When a signal is connected, it's registered in a global callback manager
- A unique ID is generated for the callback
- The callback and its ID are stored in thread-safe maps
- When a signal is emitted from C, the callback is looked up by ID and executed on the UI thread
This system supports various callback signatures and allows for safe disconnection of signals.
GTK4 requires all UI operations to happen on the main thread. GTK4Go handles this through the uithread package:
- At initialization, the main UI thread's ID is captured
RunOnUIThread()function ensures callbacks run on the UI thread- Function calls are queued and executed on the main thread
- Uses GTK's idle functions to schedule execution
This approach ensures thread-safety while maintaining responsiveness.
Widgets follow a pattern:
- Each widget type has a corresponding Go struct with embedded base types
- Constructor functions use options pattern for configuration
- CGo calls are wrapped with type conversion
- Memory management handled through finalizers and cleanup methods
GTK4Go includes several optimizations:
- CSS Rendering Optimization: During window resizing, a lightweight CSS provider is temporarily used
- Window Resize Detection: Uses property notifications to detect window resize operations
- Provider Caching: CSS providers are cached to reduce redundant creation
- Hardware Acceleration: Configuration for GPU-accelerated rendering when available
package main
import (
"fmt"
"os"
"time"
"github.com/justyntemme/gtk4go"
"github.com/justyntemme/gtk4go/gtk4"
)
func main() {
// Initialize GTK (done automatically on import)
if err := gtk4go.Initialize(); err != nil {
fmt.Printf("Failed to initialize GTK: %v\n", err)
os.Exit(1)
}
// Create application
app := gtk4.NewApplication("com.example.HelloWorld")
// Create window
win := gtk4.NewWindow("Hello GTK4 from Go!")
win.SetDefaultSize(400, 300)
// Create a vertical box
box := gtk4.NewBox(gtk4.OrientationVertical, 10)
// Create a label
label := gtk4.NewLabel("Hello, World!")
box.Append(label)
// Create a button
button := gtk4.NewButton("Click Me")
button.ConnectClicked(func() {
label.SetText("Button clicked at " + time.Now().Format(time.RFC3339))
})
box.Append(button)
// Set the window's child
win.SetChild(box)
// Add window to application
app.AddWindow(win)
// Run the application
os.Exit(app.Run())
}- Go 1.18 or later
- GTK4 development libraries (4.8 or later recommended)
sudo apt-get install libgtk-4-devsudo dnf install gtk4-develbrew install gtk4It's recommended to use MSYS2/MinGW:
pacman -S mingw-w64-x86_64-gtk4go get github.com/justyntemme/gtk4goWhen building applications with GTK4Go, make sure to set up CGo correctly:
CGO_ENABLED=1 go buildContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.