-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmsgbox.rs
More file actions
231 lines (201 loc) · 6.61 KB
/
msgbox.rs
File metadata and controls
231 lines (201 loc) · 6.61 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use std::{collections::HashMap, mem::ManuallyDrop, ptr::null_mut};
use cxx::{ExternType, type_id};
use local_sync::oneshot;
use crate::{AsRawWindow, AsWindow, MessageBoxButton, MessageBoxResponse, MessageBoxStyle};
fn msgbox_finished(data: *const u8, res: i32) {
if let Some(tx) = unsafe { (data.cast_mut() as *mut Option<oneshot::Sender<i32>>).as_mut() } {
if let Some(tx) = tx.take() {
tx.send(res).ok();
}
}
}
async fn msgbox_custom(
parent: Option<impl AsWindow>,
msg: String,
title: String,
instr: String,
style: MessageBoxStyle,
btns: MessageBoxButton,
cbtns: Vec<CustomButton>,
) -> MessageBoxResponse {
let parent = parent
.map(|p| p.as_window().as_raw_window())
.unwrap_or(null_mut());
let mut b = unsafe { ffi::new_message_box(parent) };
let mut results = HashMap::<usize, MessageBoxResponse>::new();
if btns.contains(MessageBoxButton::Ok) {
let key = b.pin_mut().addButton(QMessageBoxStandardButton::Ok) as usize;
results.insert(key, MessageBoxResponse::Ok);
}
if btns.contains(MessageBoxButton::Yes) {
let key = b.pin_mut().addButton(QMessageBoxStandardButton::Yes) as usize;
results.insert(key, MessageBoxResponse::Yes);
}
if btns.contains(MessageBoxButton::No) {
let key = b.pin_mut().addButton(QMessageBoxStandardButton::No) as usize;
results.insert(key, MessageBoxResponse::No);
}
if btns.contains(MessageBoxButton::Cancel) {
let key = b.pin_mut().addButton(QMessageBoxStandardButton::Cancel) as usize;
results.insert(key, MessageBoxResponse::Cancel);
}
if btns.contains(MessageBoxButton::Retry) {
let key = b.pin_mut().addButton(QMessageBoxStandardButton::Retry) as usize;
results.insert(key, MessageBoxResponse::Retry);
}
if btns.contains(MessageBoxButton::Close) {
let key = b.pin_mut().addButton(QMessageBoxStandardButton::Close) as usize;
results.insert(key, MessageBoxResponse::Close);
}
for btn in &cbtns {
let key = ffi::message_box_add_button(b.pin_mut(), &btn.text) as usize;
results.insert(key, MessageBoxResponse::Custom(btn.result));
}
b.pin_mut().setWindowTitle(&title.into());
if instr.is_empty() {
b.pin_mut().setText(&msg.into());
} else {
b.pin_mut().setText(&instr.into());
b.pin_mut().setInformativeText(&msg.into());
}
let icon = match style {
MessageBoxStyle::None => QMessageBoxIcon::NoIcon,
MessageBoxStyle::Info => QMessageBoxIcon::Information,
MessageBoxStyle::Warning => QMessageBoxIcon::Warning,
MessageBoxStyle::Error => QMessageBoxIcon::Critical,
};
b.pin_mut().setIcon(icon);
let (tx, rx) = oneshot::channel::<i32>();
let tx = ManuallyDrop::new(Some(tx));
unsafe {
ffi::message_box_connect_finished(
b.pin_mut(),
msgbox_finished,
std::ptr::addr_of!(tx).cast(),
);
}
b.pin_mut().open();
rx.await.unwrap();
let key = b.clickedButton() as usize;
results[&key]
}
#[derive(Debug, Clone)]
pub struct MessageBox {
msg: String,
title: String,
instr: String,
style: MessageBoxStyle,
btns: MessageBoxButton,
cbtns: Vec<CustomButton>,
}
impl Default for MessageBox {
fn default() -> Self {
Self::new()
}
}
impl MessageBox {
pub fn new() -> Self {
Self {
msg: String::new(),
title: String::new(),
instr: String::new(),
style: MessageBoxStyle::None,
btns: MessageBoxButton::None,
cbtns: vec![],
}
}
pub async fn show(self, parent: Option<impl AsWindow>) -> MessageBoxResponse {
msgbox_custom(
parent, self.msg, self.title, self.instr, self.style, self.btns, self.cbtns,
)
.await
}
pub fn message(&mut self, msg: &str) {
self.msg = msg.to_string();
}
pub fn title(&mut self, title: &str) {
self.title = title.to_string();
}
pub fn instruction(&mut self, instr: &str) {
self.instr = instr.to_string();
}
pub fn style(&mut self, style: MessageBoxStyle) {
self.style = style;
}
pub fn buttons(&mut self, btns: MessageBoxButton) {
self.btns = btns;
}
pub fn custom_button(&mut self, btn: CustomButton) {
self.cbtns.push(btn);
}
pub fn custom_buttons(&mut self, btn: impl IntoIterator<Item = CustomButton>) {
self.cbtns.extend(btn);
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CustomButton {
pub result: u16,
pub text: String,
}
impl CustomButton {
pub fn new(result: u16, text: &str) -> Self {
Self {
result,
text: text.to_string(),
}
}
}
#[repr(i32)]
enum QMessageBoxIcon {
NoIcon = 0,
Information = 2,
Warning = 3,
Critical = 4,
}
unsafe impl ExternType for QMessageBoxIcon {
type Id = type_id!("QMessageBoxIcon");
type Kind = cxx::kind::Trivial;
}
#[repr(i32)]
enum QMessageBoxStandardButton {
Ok = 0x00000400,
Yes = 0x00004000,
No = 0x00010000,
Cancel = 0x00400000,
Retry = 0x00080000,
Close = 0x00200000,
}
unsafe impl ExternType for QMessageBoxStandardButton {
type Id = type_id!("QMessageBoxStandardButton");
type Kind = cxx::kind::Trivial;
}
#[cxx::bridge]
mod ffi {
unsafe extern "C++" {
include!("winio/src/ui/qt/msgbox.hpp");
type QMessageBox;
type QMessageBoxIcon = super::QMessageBoxIcon;
type QMessageBoxStandardButton = super::QMessageBoxStandardButton;
type QWidget = crate::ui::QWidget;
type QPushButton;
type QAbstractButton;
type QString = crate::ui::QString;
fn open(self: Pin<&mut QMessageBox>);
fn setIcon(self: Pin<&mut QMessageBox>, icon: QMessageBoxIcon);
fn setWindowTitle(self: Pin<&mut QMessageBox>, s: &QString);
fn setText(self: Pin<&mut QMessageBox>, s: &QString);
fn setInformativeText(self: Pin<&mut QMessageBox>, s: &QString);
fn addButton(
self: Pin<&mut QMessageBox>,
button: QMessageBoxStandardButton,
) -> *mut QPushButton;
fn clickedButton(self: &QMessageBox) -> *mut QAbstractButton;
unsafe fn new_message_box(parent: *mut QWidget) -> UniquePtr<QMessageBox>;
unsafe fn message_box_connect_finished(
b: Pin<&mut QMessageBox>,
callback: unsafe fn(*const u8, i32),
data: *const u8,
);
fn message_box_add_button(b: Pin<&mut QMessageBox>, text: &str) -> *mut QPushButton;
}
}