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
use crate::{Label, TextButton};
use kas::event::VirtualKeyCode;
use kas::prelude::*;
use kas::text::format::FormattableText;
use kas::WindowId;
#[derive(Clone, Debug, VoidMsg)]
enum DialogButton {
Close,
}
#[derive(Clone, Debug, Widget)]
#[layout(column)]
#[widget(config=noauto)]
pub struct MessageBox<T: FormattableText + 'static> {
#[widget_core]
core: CoreData,
#[layout_data]
layout_data: <Self as kas::LayoutData>::Data,
title: String,
#[widget]
label: Label<T>,
#[widget(handler = handle_button)]
button: TextButton<DialogButton>,
}
impl<T: FormattableText + 'static> MessageBox<T> {
pub fn new<A: ToString>(title: A, message: T) -> Self {
MessageBox {
core: Default::default(),
layout_data: Default::default(),
title: title.to_string(),
label: Label::new(message),
button: TextButton::new_msg("Ok", DialogButton::Close).with_keys(&[
VirtualKeyCode::Return,
VirtualKeyCode::Space,
VirtualKeyCode::NumpadEnter,
]),
}
}
fn handle_button(&mut self, mgr: &mut Manager, msg: DialogButton) -> Response<VoidMsg> {
match msg {
DialogButton::Close => mgr.send_action(TkAction::CLOSE),
};
Response::None
}
}
impl<T: FormattableText + 'static> kas::WidgetConfig for MessageBox<T> {
fn configure(&mut self, mgr: &mut Manager) {
mgr.enable_alt_bypass(true);
}
}
impl<T: FormattableText + 'static> kas::Window for MessageBox<T> {
fn title(&self) -> &str {
&self.title
}
fn icon(&self) -> Option<kas::Icon> {
None
}
fn restrict_dimensions(&self) -> (bool, bool) {
(true, true)
}
fn add_popup(&mut self, _: &mut Manager, _: WindowId, _: kas::Popup) {
panic!("MessageBox does not (currently) support pop-ups");
}
fn remove_popup(&mut self, _: &mut Manager, _: WindowId) {}
fn resize_popups(&mut self, _: &mut Manager) {}
}