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
//! A widget showing an item with an icon, a caption and a description.

use gtk::{GridExt, StyleContextExt, WidgetExt};
use relm::{Relm, Update, Widget};

/// An item for the widget.
#[derive(Debug, Clone)]
pub struct Item {
    /// The icon.
    pub icon: Option<gdk_pixbuf::Pixbuf>,
    /// The caption.
    pub caption: String,
    /// The description.
    pub description: String,
}

/// The parameter passed to the widget on creation.
#[derive(Debug, Clone)]
pub struct Param {
    /// The item
    pub item: Item,
}

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

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

    fn model(_relm: &Relm<Self>, details: Param) -> Self::Model {
        details
    }

    fn update(&mut self, event: Msg) {
        match event {}
    }
}

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

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

    fn view(_relm: &Relm<Self>, model: Self::Model) -> Self {
        let Param {
            item:
                Item {
                    icon,
                    caption,
                    description,
                },
        } = model;

        let root = gtk::Grid::new();
        root.get_style_context().add_class("description");

        root.set_column_spacing(5);
        root.set_row_spacing(0);
        root.set_row_homogeneous(true);

        let image = gtk::Image::new_from_pixbuf(icon.as_ref());
        let caption = gtk::LabelBuilder::new()
            .label(&caption)
            .ellipsize(pango::EllipsizeMode::End)
            .halign(gtk::Align::Start)
            .valign(gtk::Align::Center)
            .hexpand(true)
            .vexpand(true)
            .build();
        let description = gtk::LabelBuilder::new()
            .label(&description)
            .ellipsize(pango::EllipsizeMode::End)
            .halign(gtk::Align::Start)
            .valign(gtk::Align::Center)
            .hexpand(true)
            .vexpand(true)
            .build();

        root.attach(&image, 0, 0, 1, 2);
        root.attach(&caption, 1, 0, 1, 1);
        root.attach(&description, 1, 1, 1, 1);

        caption.get_style_context().add_class("caption");

        description.get_style_context().add_class("description");

        W { root }
    }
}

/// The widget.
pub struct W {
    root: gtk::Grid,
}