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
//! A widget showing an expander with a submenu.

use gtk::{ExpanderExt, StyleContextExt, WidgetExt};
use indexmap::IndexMap;
use relm::{Component, ContainerWidget, Relm, Update, Widget};
use uuid::Uuid;
use {description_item, menu_item, menu_listbox};

/// The parameter passed to the widget on creation.
#[derive(Debug, Clone)]
pub struct Param {
    /// The expander label item
    pub item: description_item::Item,
    /// The submenu items
    pub children: IndexMap<Uuid, menu_item::Item>,
}

/// 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 submenu item got activated.
    FromChildren(menu_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),
}

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, msg: Msg) {
        use self::Msg::*;
        match msg {
            Internal(msg) => self.update_internal(msg.msg),
            Outgoing(_) => {}
        }
    }
}

impl W {
    fn update_internal(&mut self, msg: InternalMsg) {
        use self::InternalMsg::*;
        match msg {
            FromChildren(msg) => {
                use menu_listbox::OutgoingMsg::*;
                match msg {
                    ItemActivated(uuid) => {
                        self.root.set_expanded(false);
                        self.relm.stream().emit(Msg::from(
                            OutgoingMsg::ItemActivated(uuid),
                        ));
                    }
                }
            }
        }
    }
}

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

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

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

        let root = gtk::Expander::new(None);

        let label = relm::create_component::<description_item::W>(
            description_item::Param { item },
        );
        root.set_label_widget(Some(label.widget()));
        let children =
            root.add_widget::<menu_listbox::W>(menu_listbox::Param {
                items: children,
            });
        connect!(
            children@menu_listbox::Msg::Outgoing(ref msg),
            relm,
            Msg::from(InternalMsg::FromChildren(msg.msg.clone())));

        children.widget().get_style_context().add_class("indented");

        W {
            root,
            relm: relm.clone(),
            _label: label,
            _children: children,
        }
    }
}

/// The widget.
pub struct W {
    root: gtk::Expander,
    relm: Relm<Self>,
    _label: Component<description_item::W>,
    _children: Component<menu_listbox::W>,
}