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
242
243
244
245
246
247
248
249
//! A widget containing a list view insde a scrolled area.

use glib::StaticType;
use gtk::{
    CellLayoutExt, ContainerExt, GtkListStoreExtManual, TreeModelExt,
    TreeViewColumnExt, TreeViewExt, WidgetExt,
};
use relm::{Relm, Update, Widget};
use uuid::Uuid;
use {gdk_pixbuf, gtk};

#[repr(u8)]
enum ColumnIndex {
    Icon,
    Caption,
    Description,
    Id,
}

impl From<ColumnIndex> for i32 {
    fn from(c: ColumnIndex) -> i32 {
        c as i32
    }
}

impl From<ColumnIndex> for u32 {
    fn from(c: ColumnIndex) -> u32 {
        c as u32
    }
}

/// The parameter passed to the model on creation.
pub struct Param {
    /// The items shown in the model.
    pub items: Vec<Description>,
}

/// Model for the widget.
pub struct Model {
    relm: Relm<W>,
    store: gtk::ListStore,
}

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

/// Internal message.
#[derive(Debug)]
pub struct Internal {
    msg: InternalMsg,
}

#[derive(Debug)]
enum InternalMsg {
    /// A row of the view got activated.
    RowActivated(gtk::TreePath),
}

impl From<InternalMsg> for Msg {
    fn from(msg: InternalMsg) -> Msg {
        Msg::Internal(Internal { msg })
    }
}

/// 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 {
    /// An item got selected.
    ItemSelected(Uuid),
}

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

/// An item that gets shown in the tree.
#[derive(Clone)]
pub struct Description {
    /// The id of the item.
    pub id: Uuid,
    /// The icon of the item.
    pub icon: Option<gdk_pixbuf::Pixbuf>,
    /// The caption of the item.
    pub caption: String,
    /// The description of the item.
    pub description: String,
}

impl Update for W {
    type Model = Model;
    type ModelParam = Param;
    type Msg = Msg;

    fn model(relm: &Relm<Self>, details: Param) -> Self::Model {
        let store = gtk::ListStore::new(&[
            gdk_pixbuf::Pixbuf::static_type(),
            gtk::Type::String,
            gtk::Type::String,
            gtk::Type::String,
        ]);
        for item in details.items {
            use self::ColumnIndex::*;
            store.insert_with_values(
                None,
                &[
                    u32::from(Icon),
                    u32::from(Caption),
                    u32::from(Description),
                    u32::from(Id),
                ],
                &[
                    &item.icon,
                    &item.caption,
                    &item.description,
                    &format!("{}", item.id.to_hyphenated_ref()),
                ],
            );
        }
        Model {
            store,
            relm: relm.clone(),
        }
    }

    fn update(&mut self, event: Msg) {
        use self::Msg::*;
        match event {
            Internal(e) => self.update_internal(e.msg),
            Outgoing(_) => {}
        }
    }
}

impl W {
    fn update_internal(&mut self, e: InternalMsg) {
        use self::ColumnIndex::Id;
        use self::InternalMsg::*;
        match e {
            RowActivated(tree_path) => {
                if let Some(iter) = self.model.store.get_iter(&tree_path) {
                    let uuid = self
                        .model
                        .store
                        .get_value(&iter, i32::from(Id))
                        .get::<String>()
                        .unwrap();
                    let uuid = Uuid::parse_str(&uuid).unwrap();
                    self.model
                        .relm
                        .stream()
                        .emit(Msg::from(OutgoingMsg::ItemSelected(uuid)));
                }
            }
        }
    }
}

/// The description list view widget.
pub struct W {
    scrolled_window: gtk::ScrolledWindow,
    model: Model,
}

impl Widget for W {
    type Root = gtk::ScrolledWindow;

    fn root(&self) -> Self::Root {
        self.scrolled_window.clone()
    }

    fn view(relm: &Relm<Self>, model: Self::Model) -> Self {
        let scrolled_window = gtk::ScrolledWindow::new(
            None::<&gtk::Adjustment>,
            None::<&gtk::Adjustment>,
        );
        scrolled_window.set_vexpand(true);
        scrolled_window.set_hexpand(true);

        let treeview = gtk::TreeView::new_with_model(&model.store);
        scrolled_window.add(&treeview);

        connect!(
            relm,
            treeview,
            connect_row_activated(_, tree_path, _),
            Msg::from(InternalMsg::RowActivated(tree_path.clone()))
        );

        use self::ColumnIndex::*;

        {
            let col = gtk::TreeViewColumn::new();
            let renderer = gtk::CellRendererPixbuf::new();
            col.set_title("Icon");
            col.pack_start(&renderer, false);
            col.add_attribute(&renderer, "pixbuf", i32::from(Icon));
            treeview.append_column(&col);
        }

        {
            let col = gtk::TreeViewColumn::new();
            let renderer = gtk::CellRendererText::new();
            col.set_title("Name");
            col.pack_start(&renderer, false);
            col.add_attribute(&renderer, "text", i32::from(Caption));
            treeview.append_column(&col);
        }

        {
            let col = gtk::TreeViewColumn::new();
            let renderer = gtk::CellRendererText::new();
            col.set_title("Description");
            col.pack_start(&renderer, false);
            col.add_attribute(&renderer, "text", i32::from(Description));
            treeview.append_column(&col);
        }

        W {
            scrolled_window,
            model,
        }
    }
}