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
//! A widget for showing a message.

use gtk;
use gtk::{BoxExt, ButtonExt, 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 {
    /// Outgoing message
    Outgoing(Outgoing),
}

/// 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,
}

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 }
    }
}