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
232
233
234
235
236
237
238
239
240
241
//! A widget for showing a message.

use gtk;
use gtk::{BoxExt, ButtonExt, LabelExt, StyleContextExt, WidgetExt};
use relm::{Relm, Update, Widget};

/// Model for the widget.
pub struct Model {
    message: String,
    details: Option<String>,
    confirm_button_caption: Option<String>,
}

/// The parameters for creating the model.
pub struct Param {
    /// The message which should be shown.
    pub message: String,
    /// A detailed description of the message. Could be a backtrace
    /// or contents of a log file. When set, an expander will be
    /// added which opens the detailed view.
    pub details: Option<String>,
    /// The text which gets shown on the confirm button.
    ///
    /// If this is `None`, no confirm button is shown. If it is `Some`,
    /// then the confirm button gets shown with the text on it.
    /// If this is used, it will usually be something like "OK",
    /// "Confirm" or "Close".
    pub confirm_button_caption: Option<String>,
}

/// Message for updating the widget.
#[derive(Msg, Debug)]
pub enum Msg {
    /// Incoming message
    Incoming(Incoming),
    /// Outgoing message
    Outgoing(Outgoing),
}

/// An incoming message.
#[derive(Debug)]
pub struct Incoming {
    /// The message.
    pub msg: IncomingMsg,
}

/// A message which can be sent to a W instance.
#[derive(Debug)]
pub enum IncomingMsg {
    /// Update the display.
    Update {
        /// The message which should be shown.
        message: String,
        /// A detailed description of the message. Could be a backtrace
        /// or contents of a log file. When set, an expander will be
        /// added which opens the detailed view.
        details: Option<String>,
        /// The text which gets shown on the confirm button.
        confirm_button_caption: Option<String>,
    },
}

impl From<IncomingMsg> for Msg {
    fn from(msg: IncomingMsg) -> Msg {
        Msg::Incoming(Incoming { msg })
    }
}

/// An outgoing message.
#[derive(Debug)]
pub struct Outgoing {
    msg: OutgoingMsg,
}

impl Outgoing {
    /// Get a reference to the message.
    pub fn msg(&self) -> &OutgoingMsg {
        &self.msg
    }

    /// Consumes the struct, returning the message.
    pub fn into_msg(self) -> OutgoingMsg {
        self.msg
    }
}

/// Outgoing messages.
#[derive(Debug, Clone)]
pub enum OutgoingMsg {
    /// The confirm button got activated.
    Confirmed,
}

impl From<OutgoingMsg> for Msg {
    fn from(msg: OutgoingMsg) -> Msg {
        Msg::Outgoing(Outgoing { msg })
    }
}

/// The message widget.
pub struct W {
    b: gtk::Box,
    message: gtk::Label,
    details_expander: gtk::Expander,
    details: gtk::Label,
    confirm_button: gtk::Button,
}

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::Incoming(e) => self.update_incoming(e.msg),
            Msg::Outgoing(_) => {}
        }
    }
}

impl W {
    fn update_incoming(&mut self, e: IncomingMsg) {
        use self::IncomingMsg as M;
        match e {
            M::Update {
                message,
                details,
                confirm_button_caption,
            } => {
                self.message.set_text(&message);
                self.details_expander.set_visible(details.is_some());
                self.confirm_button
                    .set_visible(confirm_button_caption.is_some());

                if let Some(text) = details {
                    self.details.set_text(&text);
                }

                if let Some(caption) = confirm_button_caption {
                    self.confirm_button.set_label(&caption);
                }
            }
        }
    }
}

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 message = {
            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);
            label
        };

        let details = gtk::LabelBuilder::new()
            .justify(gtk::Justification::Left)
            .build();
        details.get_style_context().add_class("details");

        let details_expander = {
            let scroller = gtk::ScrolledWindowBuilder::new()
                .child(details.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);

            expander
        };

        let confirm_button = gtk::Button::new();
        b.pack_start(&confirm_button, false, false, 3);

        b.show_all();

        if let Some(text) = model.details {
            details.set_label(&text);
        } else {
            details.set_visible(false);
        }

        if let Some(text) = model.confirm_button_caption {
            confirm_button.set_label(&text);
        } else {
            confirm_button.set_visible(false);
        }

        connect!(
            relm,
            confirm_button,
            connect_clicked(_),
            Msg::from(OutgoingMsg::Confirmed)
        );

        W {
            b,
            message,
            details,
            details_expander,
            confirm_button,
        }
    }
}