Crate gtk4[−][src]
Expand description
Rust GTK 4 bindings
The project website is here.
Rust bindings of GTK 4, part of gtk4-rs.
This library contains safe Rust bindings for GTK 4, a multi-platform GUI toolkit. It is a part of gtk-rs.
Most of this documentation is generated from the C API. Until all parts of the documentation have been reviewed there will be incongruities with the actual Rust API.
For a gentle introduction to gtk-rs we recommend the online book GUI development with Rust and GTK 4.
See also:
Minimum supported Rust version
Currently, the minimum supported Rust version is 1.54.0
.
“Hello, World!” example program
GTK needs to be initialized before use by calling init
. Creating an
Application
will call init
for you.
The gtk4
crate is usually renamed to gtk
. You can find an example in
the features section for how to do this globally in your Cargo.toml
.
use gtk4 as gtk;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow};
fn main() {
let app = Application::builder()
.application_id("org.example.HelloWorld")
.build();
app.connect_activate(|app| {
// We create the main window.
let window = ApplicationWindow::builder()
.application(app)
.default_width(320)
.default_height(200)
.title("Hello, World!")
.build();
// Show the window.
window.show();
});
app.run();
}
The main loop
In a typical GTK application you set up the UI, assign signal handlers and run the main event loop.
use gtk4 as gtk;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button};
fn main() {
let application = Application::builder()
.application_id("com.example.FirstGtkApp")
.build();
application.connect_activate(|app| {
let window = ApplicationWindow::builder()
.application(app)
.title("First GTK Program")
.default_width(350)
.default_height(70)
.build();
let button = Button::with_label("Click me!");
button.connect_clicked(|_| {
eprintln!("Clicked!");
});
window.set_child(Some(&button));
window.show();
});
application.run();
}
Threads
GTK is not thread-safe. Accordingly, none of this crate’s structs implement
Send
or Sync
.
The thread where init
was called is considered the main thread. OS X has
its own notion of the main thread and init
must be called on that thread.
After successful initialization, calling any gtk
or gdk
functions (including init
) from other threads will panic
.
Any thread can schedule a closure to be run by the main loop on the main
thread via glib::idle_add
or
glib::timeout_add
. While
working with GTK you might need the glib::idle_add_local
or glib::timeout_add_local][
fn@glib::timeout_add_local] version without the [
Send` bound. Those may only be called from the main thread.
Panics
The gtk
and gdk
crates have some run-time safety and contract
checks.
-
Any constructor or free function will panic if called before
init][
fn@init` or on a non-main thread. -
Any
&str
or&Path
parameter with an interior null (\0
) character will cause a panic. -
Some functions will panic if supplied out-of-range integer parameters. All such cases will be documented individually but they are not yet.
-
A panic in a closure that handles signals or in any other closure passed to a
gtk
function will abort the process.
Features
Library versions
By default this crate provides only GTK 4.0 APIs. You can access additional
functionality by selecting one of the v4_2
, v4_4
, etc. features.
Cargo.toml
example:
[dependencies.gtk]
package = "gtk4"
version = "0.x.y"
features = ["v4_2"]
Take care when choosing the version to target: some of your users might not have easy access to the latest ones. The higher the version, the fewer users will have it installed.
Documentation
- The Rust API Stable/Development
- Book Stable/Development
- Examples
- The C API
- GTK Installation Instructions
Using
We recommend using crates from crates.io, as demonstrated here.
If you want to track the bleeding edge, use the git dependency instead:
[dependencies]
gtk = { git = "https://github.com/gtk-rs/gtk4-rs.git", package = "gtk4" }
Avoid mixing versioned and git crates like this:
[dependencies]
gdk = {version = "0.1", package = "gdk4"}
gtk = { git = "https://github.com/gtk-rs/gtk4-rs.git", package = "gtk4" }
Features
Feature | Description |
---|---|
v4_4 | Enable the new APIs part of GTK 4.4 |
v4_2 | Enable the new APIs part of GTK 4.2 |
See Also
License
The Rust bindings of gtk4 are available under the MIT License, please refer to it.
Re-exports
pub use ffi;
pub use cairo;
pub use gdk;
pub use gdk_pixbuf;
pub use gio;
pub use glib;
pub use graphene;
pub use gsk;
pub use pango;
Modules
Structs
A builder-pattern type to construct AboutDialog
objects.
A builder-pattern type to construct ActionBar
objects.
A builder-pattern type to construct Adjustment
objects.
A builder-pattern type to construct AlternativeTrigger
objects.
A builder-pattern type to construct AppChooserButton
objects.
A builder-pattern type to construct AppChooserDialog
objects.
A builder-pattern type to construct AppChooserWidget
objects.
A builder-pattern type to construct Application
objects.
A builder-pattern type to construct ApplicationWindow
objects.
A builder-pattern type to construct AspectFrame
objects.
A builder-pattern type to construct Assistant
objects.
A builder-pattern type to construct BoolFilter
objects.
A builder-pattern type to construct Box
objects.
A builder-pattern type to construct BoxLayout
objects.
A builder-pattern type to construct Button
objects.
A builder-pattern type to construct Calendar
objects.
A builder-pattern type to construct CellAreaBox
objects.
A builder-pattern type to construct CellRendererAccel
objects.
A builder-pattern type to construct CellRendererCombo
objects.
A builder-pattern type to construct CellRendererPixbuf
objects.
A builder-pattern type to construct CellRendererProgress
objects.
A builder-pattern type to construct CellRendererSpin
objects.
A builder-pattern type to construct CellRendererSpinner
objects.
A builder-pattern type to construct CellRendererText
objects.
A builder-pattern type to construct CellRendererToggle
objects.
A builder-pattern type to construct CellView
objects.
A builder-pattern type to construct CenterBox
objects.
A builder-pattern type to construct CheckButton
objects.
A builder-pattern type to construct ColorButton
objects.
A builder-pattern type to construct ColorChooserDialog
objects.
A builder-pattern type to construct ColorChooserWidget
objects.
A builder-pattern type to construct ColumnView
objects.
A builder-pattern type to construct ColumnViewColumn
objects.
A builder-pattern type to construct ComboBox
objects.
A builder-pattern type to construct ComboBoxText
objects.
A builder-pattern type to construct Constraint
objects.
A builder-pattern type to construct ConstraintGuide
objects.
A builder-pattern type to construct ConstraintLayoutChild
objects.
A builder-pattern type to construct Dialog
objects.
A builder-pattern type to construct DragIcon
objects.
A builder-pattern type to construct DragSource
objects.
A builder-pattern type to construct DrawingArea
objects.
A builder-pattern type to construct DropControllerMotion
objects.
A builder-pattern type to construct DropDown
objects.
A builder-pattern type to construct DropTargetAsync
objects.
A builder-pattern type to construct DropTarget
objects.
A builder-pattern type to construct EditableLabel
objects.
A builder-pattern type to construct EmojiChooser
objects.
A builder-pattern type to construct EntryBuffer
objects.
A builder-pattern type to construct Entry
objects.
A builder-pattern type to construct EntryCompletion
objects.
A builder-pattern type to construct EventControllerFocus
objects.
A builder-pattern type to construct EventControllerKey
objects.
A builder-pattern type to construct EventControllerLegacy
objects.
A builder-pattern type to construct EventControllerMotion
objects.
A builder-pattern type to construct EventControllerScroll
objects.
A builder-pattern type to construct Expander
objects.
A builder-pattern type to construct FileChooserDialog
objects.
A builder-pattern type to construct FileChooserNative
objects.
A builder-pattern type to construct FileChooserWidget
objects.
A builder-pattern type to construct FilterListModel
objects.
A builder-pattern type to construct Fixed
objects.
A builder-pattern type to construct FixedLayoutChild
objects.
A builder-pattern type to construct FlowBox
objects.
A builder-pattern type to construct FlowBoxChild
objects.
A builder-pattern type to construct FontButton
objects.
A builder-pattern type to construct FontChooserDialog
objects.
A builder-pattern type to construct FontChooserWidget
objects.
A builder-pattern type to construct Frame
objects.
A builder-pattern type to construct GLArea
objects.
A builder-pattern type to construct GestureClick
objects.
A builder-pattern type to construct GestureDrag
objects.
A builder-pattern type to construct GestureLongPress
objects.
A builder-pattern type to construct GesturePan
objects.
A builder-pattern type to construct GestureRotate
objects.
A builder-pattern type to construct GestureStylus
objects.
A builder-pattern type to construct GestureSwipe
objects.
A builder-pattern type to construct GestureZoom
objects.
A builder-pattern type to construct Grid
objects.
A builder-pattern type to construct GridLayout
objects.
A builder-pattern type to construct GridLayoutChild
objects.
A builder-pattern type to construct GridView
objects.
A builder-pattern type to construct HeaderBar
objects.
A builder-pattern type to construct IMContextSimple
objects.
A builder-pattern type to construct IMMulticontext
objects.
A builder-pattern type to construct IconPaintable
objects.
A builder-pattern type to construct IconTheme
objects.
A builder-pattern type to construct IconView
objects.
A builder-pattern type to construct Image
objects.
A builder-pattern type to construct InfoBar
objects.
Whether to propagate the signal to the default handler.
A builder-pattern type to construct Label
objects.
A builder-pattern type to construct LevelBar
objects.
A builder-pattern type to construct LinkButton
objects.
A builder-pattern type to construct ListBox
objects.
A builder-pattern type to construct ListBoxRow
objects.
A builder-pattern type to construct ListView
objects.
A builder-pattern type to construct LockButton
objects.
A builder-pattern type to construct MediaControls
objects.
A builder-pattern type to construct MenuButton
objects.
A builder-pattern type to construct MessageDialog
objects.
A builder-pattern type to construct MountOperation
objects.
A builder-pattern type to construct Notebook
objects.
A builder-pattern type to construct NumericSorter
objects.
A builder-pattern type to construct Overlay
objects.
A builder-pattern type to construct OverlayLayoutChild
objects.
A builder-pattern type to construct PadController
objects.
A builder-pattern type to construct PageSetupUnixDialog
objects.
A builder-pattern type to construct Paned
objects.
A builder-pattern type to construct PasswordEntryBuffer
objects.
A builder-pattern type to construct PasswordEntry
objects.
A builder-pattern type to construct Picture
objects.
A builder-pattern type to construct Popover
objects.
A builder-pattern type to construct PopoverMenuBar
objects.
A builder-pattern type to construct PopoverMenu
objects.
A builder-pattern type to construct PrintJob
objects.
A builder-pattern type to construct PrintOperation
objects.
A builder-pattern type to construct PrintUnixDialog
objects.
A builder-pattern type to construct Printer
objects.
A builder-pattern type to construct ProgressBar
objects.
A builder-pattern type to construct Revealer
objects.
A builder-pattern type to construct Scale
objects.
A builder-pattern type to construct ScaleButton
objects.
A builder-pattern type to construct Scrollbar
objects.
A builder-pattern type to construct ScrolledWindow
objects.
A builder-pattern type to construct SearchBar
objects.
A builder-pattern type to construct SearchEntry
objects.
A builder-pattern type to construct Separator
objects.
A builder-pattern type to construct Settings
objects.
A builder-pattern type to construct Shortcut
objects.
A builder-pattern type to construct ShortcutController
objects.
A builder-pattern type to construct ShortcutLabel
objects.
A builder-pattern type to construct ShortcutsGroup
objects.
A builder-pattern type to construct ShortcutsSection
objects.
A builder-pattern type to construct ShortcutsShortcut
objects.
A builder-pattern type to construct ShortcutsWindow
objects.
A builder-pattern type to construct SingleSelection
objects.
A builder-pattern type to construct SliceListModel
objects.
A builder-pattern type to construct SortListModel
objects.
A builder-pattern type to construct SpinButton
objects.
A builder-pattern type to construct Spinner
objects.
A builder-pattern type to construct Stack
objects.
A builder-pattern type to construct StackSidebar
objects.
A builder-pattern type to construct StackSwitcher
objects.
A builder-pattern type to construct Statusbar
objects.
A builder-pattern type to construct StringFilter
objects.
A builder-pattern type to construct StringSorter
objects.
A builder-pattern type to construct Switch
objects.
A builder-pattern type to construct TextBuffer
objects.
A builder-pattern type to construct Text
objects.
A builder-pattern type to construct TextMark
objects.
A builder-pattern type to construct TextTag
objects.
A builder-pattern type to construct TextView
objects.
A builder-pattern type to construct ToggleButton
objects.
A builder-pattern type to construct TreeExpander
objects.
A builder-pattern type to construct TreeListModel
objects.
A builder-pattern type to construct TreeView
objects.
A builder-pattern type to construct TreeViewColumn
objects.
A builder-pattern type to construct Video
objects.
A builder-pattern type to construct Viewport
objects.
A builder-pattern type to construct VolumeButton
objects.
A builder-pattern type to construct Window
objects.
A builder-pattern type to construct WindowControls
objects.
A builder-pattern type to construct WindowHandle
objects.
Enums
Constants
Statics
Functions
Tries to initialize GTK.
Returns true
if GTK has been initialized.
Returns true
if GTK has been initialized and this is the main thread.
Informs this crate that GTK has been initialized and the current thread is the main one.