Skip to content

Instantly share code, notes, and snippets.

@IllustratedMan-code
Created August 3, 2021 19:07
Show Gist options
  • Save IllustratedMan-code/bcd6f65cd3698ad6e9dfee7acd858afc to your computer and use it in GitHub Desktop.
Save IllustratedMan-code/bcd6f65cd3698ad6e9dfee7acd858afc to your computer and use it in GitHub Desktop.
Rust gtk4 hello world
use gtk4::prelude::*;
use gtk4::{
Application, ApplicationWindow, Button, DrawingArea, EventControllerKey, EventControllerMotion,
Inhibit,
};
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();
let button = Button::with_label("Click me!");
let mouse = EventControllerMotion::new();
let keys = EventControllerKey::new();
let canvas = DrawingArea::new();
canvas.add_controller(&mouse);
canvas.add_controller(&keys);
keys.connect_key_pressed(move |_, a, b, c| {
eprintln!("keyval {}", a);
eprintln!("keycode {}", b);
eprintln!("state {}", c);
Inhibit(false)
});
mouse.connect_motion(|_, x, y| {
eprintln!("x:{}, y:{}", x, y);
});
button.connect_clicked(|_| {
eprintln!("Clicked!");
});
window.set_child(Some(&canvas));
// Show the window.
window.show();
});
app.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment