]> piware.de Git - learn-rust.git/commitdiff
Add GTK 4 hello world
authorMartin Pitt <martin@piware.de>
Mon, 18 Apr 2022 09:44:02 +0000 (11:44 +0200)
committerMartin Pitt <martin@piware.de>
Mon, 18 Apr 2022 17:56:58 +0000 (19:56 +0200)
https://gtk-rs.org/gtk4-rs/stable/latest/book/introduction.html

gtk4-hello-world/Cargo.toml [new file with mode: 0644]
gtk4-hello-world/src/main.rs [new file with mode: 0644]

diff --git a/gtk4-hello-world/Cargo.toml b/gtk4-hello-world/Cargo.toml
new file mode 100644 (file)
index 0000000..8a60626
--- /dev/null
@@ -0,0 +1,9 @@
+[package]
+name = "gtk4-hello-world"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+gtk = { version = "0.4", package = "gtk4" }
diff --git a/gtk4-hello-world/src/main.rs b/gtk4-hello-world/src/main.rs
new file mode 100644 (file)
index 0000000..4ebedc0
--- /dev/null
@@ -0,0 +1,36 @@
+use gtk::prelude::*;
+use gtk::{
+    Application, ApplicationWindow,
+    Button
+};
+
+fn build_ui(app: &Application) {
+    let button = Button::builder()
+        .label("Click me!")
+        .margin_top(12)
+        .margin_bottom(12)
+        .margin_start(12)
+        .margin_end(12)
+        .build();
+
+    button.connect_clicked(move |button| {
+        button.set_label("Hello world!");
+    });
+
+    let window = ApplicationWindow::builder()
+        .application(app)
+        .title("Hello GTK")
+        .child(&button)
+        .build();
+
+    window.present();
+}
+
+fn main() {
+    let app = Application::builder()
+        .application_id("ork.gtk-rs.example")
+        .build();
+
+    app.connect_activate(build_ui);
+    app.run();
+}