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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! A widget showing a menu.

use crate::{description_item, menu_page};
use gtk::StackExt;
use indexmap::IndexMap;
use relm::{Relm, Update, Widget};
use uuid::Uuid;

/// The description of a menu item.
#[derive(Debug, Clone)]
pub enum Item {
    /// A single description item that can be selected directly.
    Description(description_item::Item),
    /// A sub menu.
    SubMenu {
        /// The description item displayed in the parent menu.
        header: description_item::Item,
        /// The contents of the sub menu.
        items: IndexMap<Uuid, Item>,
    },
}

/// A menu.
#[derive(Debug, Clone)]
pub struct TopMenu {
    /// The description of the header label
    pub header: Option<description_item::Item>,
    /// The uuid of the menu.
    pub uuid: Uuid,
    /// The children displayed inside the menu.
    pub items: IndexMap<Uuid, Item>,
}

/// The parameter to the widget.
#[derive(Debug, Clone)]
pub struct Param {
    /// The menu.
    pub menu: TopMenu,
    /// The back button icon.
    pub back_icon: gdk_pixbuf::Pixbuf,
}

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

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

/// An incoming message.
#[derive(Debug)]
pub enum IncomingMsg {
    /// Reset the menu.
    Reset,
}

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

#[derive(Debug)]
enum InternalMsg {
    /// A message from a submenu.
    MenuMsg(menu_page::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),
}

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

impl W {
    fn update_internal(&mut self, msg: InternalMsg) {
        use self::InternalMsg::*;
        match msg {
            MenuMsg(msg) => {
                use menu_page::OutgoingMsg::*;
                match msg {
                    GoUp(uuid) => {
                        self.root.set_visible_child_full(
                            &uuid.to_string(),
                            gtk::StackTransitionType::SlideRight,
                        );
                    }
                    GoDown(uuid) => {
                        self.root.set_visible_child_full(
                            &uuid.to_string(),
                            gtk::StackTransitionType::SlideLeft,
                        );
                    }
                    ItemActivated(uuid) => {
                        self.relm.stream().emit(Msg::from(
                            OutgoingMsg::ItemActivated(uuid),
                        ));
                    }
                }
            }
        }
    }

    fn update_incoming(&mut self, msg: IncomingMsg) {
        use self::IncomingMsg::*;
        match msg {
            Reset => {
                self.root.set_visible_child_full(
                    &root_uuid().to_string(),
                    gtk::StackTransitionType::Crossfade,
                );
            }
        }
    }
}

fn root_uuid() -> Uuid {
    Uuid::from_u128(0x428a3431_dc4a_489c_99e1_b4a17f7d4dc8)
}

fn add_menu(
    relm: &Relm<W>,
    stack: &mut gtk::Stack,
    go_back: Option<Uuid>,
    uuid: Uuid,
    header: Option<&description_item::Item>,
    items: &IndexMap<Uuid, Item>,
    back_icon: &gdk_pixbuf::Pixbuf,
) -> IndexMap<Uuid, relm::Component<menu_page::W>> {
    let mut components = IndexMap::new();

    let param = menu_page::Param {
        go_back,
        header: header.cloned(),
        items: items.clone(),
        back_icon: back_icon.clone(),
    };
    let page = relm::create_component::<menu_page::W>(param);
    connect!(
        page@menu_page::Msg::Outgoing(ref e),
        relm,
        Msg::from(InternalMsg::MenuMsg(e.msg.clone())));
    stack.add_named(page.widget(), &uuid.to_string());
    components.insert(uuid, page);

    for (child_uuid, item) in items.iter() {
        use self::Item::*;
        match item {
            Description(_) => {}
            SubMenu { header, items } => {
                let children = add_menu(
                    relm,
                    stack,
                    Some(uuid),
                    *child_uuid,
                    Some(header),
                    &items,
                    back_icon,
                );
                components.extend(children.into_iter());
            }
        }
    }

    components
}

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

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

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

        let mut root = gtk::StackBuilder::new().homogeneous(false).build();
        let components = add_menu(
            relm,
            &mut root,
            None,
            root_uuid(),
            menu.header.as_ref(),
            &menu.items,
            &back_icon,
        );

        root.set_visible_child_full(
            &root_uuid().to_string(),
            gtk::StackTransitionType::None,
        );

        W {
            root,
            relm: relm.clone(),
            _components: components,
        }
    }
}

/// The widget.
pub struct W {
    root: gtk::Stack,
    relm: Relm<Self>,
    _components: IndexMap<Uuid, relm::Component<menu_page::W>>,
}

impl<T: AsRef<IndexMap<Uuid, description_item::Item>>> From<T>
    for TopMenu
{
    fn from(t: T) -> TopMenu {
        TopMenu {
            header: None,
            uuid: root_uuid(),
            items: t
                .as_ref()
                .into_iter()
                .map(|(k, v)| (*k, Item::Description(v.clone())))
                .collect(),
        }
    }
}