Created
August 3, 2021 19:07
-
-
Save IllustratedMan-code/bcd6f65cd3698ad6e9dfee7acd858afc to your computer and use it in GitHub Desktop.
Rust gtk4 hello world
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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