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
use gtk;
use gtk::{BoxExt, ButtonExt, StyleContextExt, WidgetExt};
use relm::{Relm, Update, Widget};
pub struct Model {
message: String,
details: Option<String>,
confirm_button_caption: Option<String>,
}
pub struct Param {
pub message: String,
pub details: Option<String>,
pub confirm_button_caption: Option<String>,
}
#[derive(Msg, Debug)]
pub enum Msg {
Outgoing(Outgoing),
}
#[derive(Debug)]
pub struct Outgoing {
msg: OutgoingMsg,
}
impl Outgoing {
pub fn msg(&self) -> &OutgoingMsg {
&self.msg
}
pub fn into_msg(self) -> OutgoingMsg {
self.msg
}
}
#[derive(Debug, Clone)]
pub enum OutgoingMsg {
Confirmed,
}
impl From<OutgoingMsg> for Msg {
fn from(msg: OutgoingMsg) -> Msg {
Msg::Outgoing(Outgoing { msg })
}
}
pub struct W {
b: gtk::Box,
}
impl Update for W {
type Model = Model;
type ModelParam = Param;
type Msg = Msg;
fn model(_relm: &Relm<Self>, details: Param) -> Model {
let Param {
message,
details,
confirm_button_caption,
} = details;
Model {
message,
details,
confirm_button_caption,
}
}
fn update(&mut self, event: Msg) {
match event {
Msg::Outgoing(_) => {}
}
}
}
impl Widget for W {
type Root = gtk::Box;
fn root(&self) -> Self::Root {
self.b.clone()
}
fn view(relm: &Relm<Self>, model: Self::Model) -> Self {
use gtk::Cast;
let b = gtk::Box::new(gtk::Orientation::Vertical, 3);
{
let label =
gtk::LabelBuilder::new().label(&model.message).build();
label.get_style_context().add_class("message");
let scroller = gtk::ScrolledWindowBuilder::new()
.child(label.upcast_ref())
.build();
scroller.get_style_context().add_class("message");
b.pack_start(&scroller, true, true, 3);
}
if let Some(details) = model.details {
let label = gtk::LabelBuilder::new()
.label(&details)
.justify(gtk::Justification::Left)
.build();
label.get_style_context().add_class("details");
let scroller = gtk::ScrolledWindowBuilder::new()
.child(label.upcast_ref())
.build();
scroller.get_style_context().add_class("details");
let expander = gtk::ExpanderBuilder::new()
.label("Details")
.child(scroller.upcast_ref())
.build();
expander.get_style_context().add_class("details");
b.pack_start(&expander, false, false, 3);
}
if let Some(label) = model.confirm_button_caption {
let button = gtk::Button::new_with_label(&label);
b.pack_start(&button, false, false, 3);
connect!(
relm,
button,
connect_clicked(_),
Msg::from(OutgoingMsg::Confirmed)
);
}
b.show_all();
W { b }
}
}