Created
March 29, 2020 17:07
-
-
Save dmitryvk/9ce63c7114d9a4eb570b0292ec4ef813 to your computer and use it in GitHub Desktop.
Gtk-rs async `alert` example
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
[package] | |
name = "async-ui" | |
version = "0.1.0" | |
authors = ["Dmitry Kalyanov <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
futures = "0.3.4" | |
glib = "0.9.3" | |
async-std = "1.5.0" | |
[dependencies.gtk] | |
version = "0.8.1" |
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
pub async fn alert(title: &str, text: &str) { | |
use gtk::prelude::*; | |
use std::rc::Rc; | |
use std::cell::RefCell; | |
use futures::channel::oneshot::channel as oneshot_channel; | |
let (result_sender, result_receiver) = oneshot_channel(); | |
let result_sender = Rc::new(RefCell::new(Some(result_sender))); | |
let dialog = gtk::MessageDialogBuilder::new() | |
.buttons(gtk::ButtonsType::Ok) | |
.message_type(gtk::MessageType::Info) | |
.text(text) | |
.modal(true) | |
.title(title) | |
.type_(gtk::WindowType::Toplevel) | |
.build(); | |
let result_sender2 = result_sender.clone(); | |
dialog.connect_response(move |_, response| { | |
println!("Response={}", response); | |
let result_sender = result_sender2.borrow_mut().take().unwrap(); | |
result_sender.send(()).unwrap(); | |
}); | |
dialog.show_all(); | |
result_receiver.await.unwrap(); | |
dialog.destroy(); | |
} | |
async fn ui_main() { | |
alert("Message", "Hello, world").await; | |
} | |
fn main() { | |
gtk::init().expect("Failed to initialize Gtk+"); | |
glib::MainContext::default().spawn_local(async move { | |
ui_main().await; | |
gtk::main_quit(); | |
}); | |
gtk::main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment