1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use crate::prelude::*;
use crate::Dialog;
use crate::DialogFlags;
use crate::ResponseType;
use crate::Widget;
use crate::Window;
use glib::translate::*;
use std::cell::Cell;
use std::future::Future;
use std::pin::Pin;
use std::ptr;
impl Dialog {
#[doc(alias = "gtk_dialog_new_with_buttons")]
pub fn with_buttons<T: IsA<Window>>(
title: Option<&str>,
parent: Option<&T>,
flags: DialogFlags,
buttons: &[(&str, ResponseType)],
) -> Dialog {
assert_initialized_main_thread!();
let ret: Dialog = unsafe {
Widget::from_glib_none(ffi::gtk_dialog_new_with_buttons(
title.to_glib_none().0,
parent.map(|p| p.as_ref()).to_glib_none().0,
flags.into_glib(),
ptr::null_mut(),
))
.unsafe_cast()
};
ret.add_buttons(buttons);
ret
}
}
pub trait DialogExtManual: 'static {
#[doc(alias = "gtk_dialog_add_buttons")]
fn add_buttons(&self, buttons: &[(&str, ResponseType)]);
fn run_future<'a>(&'a self) -> Pin<Box<dyn Future<Output = ResponseType> + 'a>>;
}
impl<O: IsA<Dialog> + IsA<Widget>> DialogExtManual for O {
fn add_buttons(&self, buttons: &[(&str, ResponseType)]) {
for &(text, id) in buttons {
O::add_button(self, text, id);
}
}
fn run_future<'a>(&'a self) -> Pin<Box<dyn Future<Output = ResponseType> + 'a>> {
Box::pin(async move {
let (sender, receiver) = futures_channel::oneshot::channel();
let sender = Cell::new(Some(sender));
let response_handler = self.connect_response(move |_, response_type| {
if let Some(m) = sender.replace(None) {
let _result = m.send(response_type);
}
});
self.show();
if let Ok(response) = receiver.await {
if response != ResponseType::DeleteEvent {
self.disconnect(response_handler);
}
response
} else {
ResponseType::None
}
})
}
}