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
//! A widget showing a menu page.

use crate::{description_item, description_listbox, menu};
use gtk::{
    BoxExt, ButtonExt, Cast, ListBoxExt, StyleContextExt, WidgetExt,
};
use indexmap::{IndexMap, IndexSet};
use relm::{ContainerWidget, Relm, Update, Widget};
use uuid::Uuid;

/// The parameter to the widget.
#[derive(Debug, Clone)]
pub struct Param {
    /// The go-back uuid. Is None for the top-level entry of the menu.
    pub go_back: Option<Uuid>,
    /// The header.
    pub header: Option<description_item::Item>,
    /// The menu.
    pub items: IndexMap<Uuid, menu::Item>,
    /// The back button icon.
    pub back_icon: gdk_pixbuf::Pixbuf,
}

/// Messages 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 message from a submenu.
    MenuMsg(description_listbox::OutgoingMsg),
}

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

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

/// Outgoing message.
#[derive(Debug, Clone)]
pub enum OutgoingMsg {
    /// An item was selected.
    ItemActivated(Uuid),
    /// Go up one level;
    GoUp(Uuid),
    /// Go down one level;
    GoDown(Uuid),
}

impl From<OutgoingMsg> for Msg {
    fn from(msg: OutgoingMsg) -> Msg {
        Msg::Outgoing(Outgoing { 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 {
            Msg::Outgoing(_) => {}
            Msg::Internal(msg) => self.update_internal(msg.msg),
        }
    }
}

impl W {
    fn update_internal(&mut self, msg: InternalMsg) {
        use self::InternalMsg::*;
        match msg {
            MenuMsg(msg) => {
                use description_listbox::OutgoingMsg::*;
                match msg {
                    ItemActivated(uuid) => {
                        let msg = if self.submenus.contains(&uuid) {
                            OutgoingMsg::GoDown(uuid)
                        } else {
                            OutgoingMsg::ItemActivated(uuid)
                        };
                        self.relm.stream().emit(Msg::from(msg));
                    }
                }
            }
        }
    }
}

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

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

    fn view(relm: &Relm<Self>, model: Self::Model) -> Self {
        let Param {
            go_back,
            header,
            items,
            back_icon,
        } = model;

        let root = gtk::Box::new(gtk::Orientation::Vertical, 0);
        root.get_style_context().add_class("menu");

        let headerbox = gtk::Box::new(gtk::Orientation::Horizontal, 0);
        headerbox.get_style_context().add_class("menu_header");

        let back_button = if let Some(uuid) = go_back {
            let back_button = gtk::ButtonBuilder::new()
                .image(
                    &gtk::Image::new_from_pixbuf(Some(&back_icon))
                        .upcast(),
                )
                .build();
            connect!(
                relm,
                back_button,
                connect_clicked(_),
                Msg::from(OutgoingMsg::GoUp(uuid))
            );
            headerbox.pack_start(&back_button, false, false, 0);
            Some(back_button)
        } else {
            None
        };

        let title = if let Some(h) = header {
            let title = relm::create_component::<description_item::W>(
                description_item::Param { item: h },
            );
            headerbox.pack_start(
                title.widget().upcast_ref::<gtk::Widget>(),
                true,
                true,
                0,
            );
            root.pack_start(&headerbox, false, false, 0);
            Some(title)
        } else {
            None
        };

        let scroller = gtk::ScrolledWindowBuilder::new()
            .propagate_natural_width(true)
            .propagate_natural_height(true)
            .min_content_height(100)
            .max_content_height(600)
            .build();

        let mut submenus = IndexSet::new();
        let items = items
            .iter()
            .map(|(uuid, item)| {
                use menu::Item::*;
                let item = match item {
                    Description(item) => item.clone(),
                    SubMenu { header, items: _ } => {
                        submenus.insert(*uuid);
                        header.clone()
                    }
                };
                (*uuid, item)
            })
            .collect();

        let menu = scroller.add_widget::<description_listbox::W>(
            description_listbox::Param { items },
        );
        menu.widget().set_selection_mode(gtk::SelectionMode::None);
        connect!(
            menu@description_listbox::Msg::Outgoing(ref e),
            relm,
            Msg::from(InternalMsg::MenuMsg(e.msg.clone())));

        root.pack_start(&scroller, true, true, 0);

        root.show_all();

        W {
            root,
            relm: relm.clone(),
            _menu: menu,
            submenus,
            _back_button: back_button,
            _title: title,
        }
    }
}

/// The widget.
pub struct W {
    root: gtk::Box,
    relm: Relm<Self>,
    _menu: relm::Component<description_listbox::W>,
    submenus: IndexSet<Uuid>,
    _back_button: Option<gtk::Button>,
    _title: Option<relm::Component<description_item::W>>,
}