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
273
274
275
276
277
use crate::error::{AddViewError, RemoveViewError, SwitchError};
use crate::node::Node;
use crate::path::SearchPath;
use crate::{Mux, Orientation, View};

/// Identifier for views in binary tree of mux, typically returned after adding a new view to the multiplexer.
pub type Id = indextree::NodeId;

impl Mux {
    /// Removes the given id from the multiplexer, returns an error if not a valid id contained in the tree or the lone root of the tree.
    /// When successful the Id of the removed Node is returned.
    /// # Example
    /// ```
    /// # fn main () {
    /// # let mut mux = cursive_multiplex::Mux::new();
    /// # let node1 = mux.add_right_of(cursive::views::DummyView, mux.root().build().unwrap()).unwrap();
    /// let new_node = mux.add_below(cursive::views::DummyView, node1).unwrap();
    /// mux.remove_id(new_node);
    /// # }
    /// ```
    pub fn remove_id(&mut self, id: Id) -> Result<Id, RemoveViewError> {
        let desc: Vec<Id> = self.root.descendants(&self.tree).collect();
        if desc.contains(&id) {
            let sib_id: Id;
            if id.preceding_siblings(&self.tree).count() > 1 {
                sib_id = id.preceding_siblings(&self.tree).nth(1).unwrap();
            } else if id.following_siblings(&self.tree).count() > 1 {
                sib_id = id.following_siblings(&self.tree).nth(1).unwrap();
            } else {
                return Err(RemoveViewError::Generic {});
            }
            let parent = id.ancestors(&self.tree).nth(1).unwrap();
            id.detach(&mut self.tree);
            if let Some(anker) = parent.ancestors(&self.tree).nth(1) {
                if anker.children(&self.tree).next().unwrap() == parent {
                    parent.detach(&mut self.tree);
                    anker.prepend(sib_id, &mut self.tree);
                    self.focus = sib_id;
                    Ok(id)
                } else {
                    parent.detach(&mut self.tree);
                    anker.append(sib_id, &mut self.tree);
                    self.focus = sib_id;
                    Ok(id)
                }
            } else {
                self.root = sib_id;
                self.focus = sib_id;
                Ok(id)
            }
        } else {
            Err(RemoveViewError::InvalidId { id: id })
        }
    }

    /// Add the given view, below the given Id.
    /// The new view and the indexed one will share the space previously given to the give Id.
    /// When successful `Ok()` will contain the assigned `Id`
    /// # Example
    /// ```
    /// # extern crate cursive;
    /// # fn main () {
    /// let mut mux = cursive_multiplex::Mux::new();
    /// let node1 = mux.add_right_of(cursive::views::DummyView, mux.root().build().unwrap()).unwrap();
    /// let new_node = mux.add_below(cursive::views::DummyView, node1).unwrap();
    /// # }
    /// ```
    pub fn add_below<T>(&mut self, v: T, id: Id) -> Result<Id, AddViewError>
    where
        T: View,
    {
        self.add_node_id(v, id, Orientation::Vertical, SearchPath::Down)
    }

    /// Add the given view, above the given Id.
    /// The new view and the indexed one will share the space previously given to the give Id.
    /// When successful `Ok()` will contain the assigned `Id`
    /// # Example
    /// ```
    /// # extern crate cursive;
    /// # fn main () {
    /// let mut mux = cursive_multiplex::Mux::new();
    /// let node1 = mux.add_right_of(cursive::views::DummyView, mux.root().build().unwrap()).unwrap();
    /// let new_node = mux.add_above(cursive::views::DummyView, node1).unwrap();
    /// # }
    /// ```
    pub fn add_above<T>(&mut self, v: T, id: Id) -> Result<Id, AddViewError>
    where
        T: View,
    {
        self.add_node_id(v, id, Orientation::Vertical, SearchPath::Up)
    }

    /// Add the given view, left of the given Id.
    /// The new view and the indexed one will share the space previously given to the give Id.
    /// When successful `Ok()` will contain the assigned `Id`
    /// # Example
    /// ```
    /// # extern crate cursive;
    /// # fn main () {
    /// let mut mux = cursive_multiplex::Mux::new();
    /// let node1 = mux.add_right_of(cursive::views::DummyView, mux.root().build().unwrap()).unwrap();
    /// let new_node = mux.add_left_of(cursive::views::DummyView, node1).unwrap();
    /// # }
    /// ```
    pub fn add_left_of<T>(&mut self, v: T, id: Id) -> Result<Id, AddViewError>
    where
        T: View,
    {
        self.add_node_id(v, id, Orientation::Horizontal, SearchPath::Left)
    }

    /// Add the given view, right of the given Id.
    /// The new view and the indexed one will share the space previously given to the give Id.
    /// When successful `Ok()` will contain the assigned `Id`
    /// # Example
    /// ```
    /// # extern crate cursive;
    /// # fn main () {
    /// let mut mux = cursive_multiplex::Mux::new();
    /// let node1 = mux.add_right_of(cursive::views::DummyView, mux.root().build().unwrap()).unwrap();
    /// let new_node = mux.add_right_of(cursive::views::DummyView, node1).unwrap();
    /// # }
    /// ```
    pub fn add_right_of<T>(&mut self, v: T, id: Id) -> Result<Id, AddViewError>
    where
        T: View,
    {
        self.add_node_id(v, id, Orientation::Horizontal, SearchPath::Right)
    }

    fn add_node_id<T>(
        &mut self,
        v: T,
        id: Id,
        orientation: Orientation,
        direction: SearchPath,
    ) -> Result<Id, AddViewError>
    where
        T: View,
    {
        let new_node = self.tree.new_node(Node::new(v, Orientation::Horizontal));

        let mut node_id;
        if let Some(parent) = id.ancestors(&self.tree).nth(1) {
            node_id = parent;
        } else {
            node_id = id;
        }

        if node_id.children(&self.tree).count() < 2
            && !self.tree.get(node_id).unwrap().get().has_view()
        {
            match direction {
                SearchPath::Up | SearchPath::Left => node_id.prepend(new_node, &mut self.tree),
                SearchPath::Down | SearchPath::Right => node_id.append(new_node, &mut self.tree),
            }
            self.tree.get_mut(node_id).unwrap().get_mut().orientation = orientation;
        } else {
            // First element is node itself, second direct parent
            let parent = node_id;
            node_id = id;

            let position: SearchPath;
            if parent.children(&self.tree).next().unwrap() == node_id {
                position = SearchPath::Left;
            } else {
                position = SearchPath::Right;
            }

            node_id.detach(&mut self.tree);

            let new_intermediate = self.tree.new_node(Node::new_empty(orientation));
            match position {
                SearchPath::Right | SearchPath::Down => {
                    parent.append(new_intermediate, &mut self.tree);
                }
                SearchPath::Left | SearchPath::Up => {
                    parent.prepend(new_intermediate, &mut self.tree);
                }
            }
            match direction {
                SearchPath::Up | SearchPath::Left => {
                    new_intermediate.append(new_node, &mut self.tree);
                    new_intermediate.append(node_id, &mut self.tree);
                }
                SearchPath::Down | SearchPath::Right => {
                    new_intermediate.append(node_id, &mut self.tree);
                    new_intermediate.append(new_node, &mut self.tree);
                }
            }
            debug!("Changed order");
        }

        if self.tree.get_mut(new_node).unwrap().get_mut().take_focus() {
            self.focus = new_node;
            debug!("Changed Focus: {}", new_node);
        }
        Ok(new_node)
    }

    /// Allows for position switching of two views, returns error if ids not in multiplexer.
    /// When successful empty `Ok(())`
    /// # Example
    /// ```
    /// # extern crate cursive;
    /// # fn main () {
    /// # let mut mux = cursive_multiplex::Mux::new();
    /// # let node1 = mux.add_right_of(cursive::views::DummyView, mux.root().build().unwrap()).unwrap();
    /// let daniel = mux.add_below(cursive::views::DummyView, node1).unwrap();
    /// let the_cooler_daniel = mux.add_below(cursive::views::DummyView, node1).unwrap();
    /// // Oops I wanted the cooler daniel in another spot
    /// mux.switch_views(daniel, the_cooler_daniel);
    /// # }
    /// ```
    pub fn switch_views(&mut self, fst: Id, snd: Id) -> Result<(), SwitchError> {
        if let Some(parent1) = fst.ancestors(&self.tree).nth(1) {
            if let Some(parent2) = snd.ancestors(&self.tree).nth(1) {
                if parent1.children(&self.tree).next().unwrap() == fst {
                    if parent2.children(&self.tree).next().unwrap() == snd {
                        fst.detach(&mut self.tree);
                        snd.detach(&mut self.tree);
                        parent1.checked_prepend(snd, &mut self.tree)?;
                        parent2.checked_prepend(fst, &mut self.tree)?;
                        Ok(())
                    } else {
                        fst.detach(&mut self.tree);
                        snd.detach(&mut self.tree);
                        parent1.checked_prepend(snd, &mut self.tree)?;
                        parent2.checked_append(fst, &mut self.tree)?;
                        Ok(())
                    }
                } else {
                    if parent2.children(&self.tree).next().unwrap() == snd {
                        fst.detach(&mut self.tree);
                        snd.detach(&mut self.tree);
                        parent1.checked_append(snd, &mut self.tree)?;
                        parent2.checked_prepend(fst, &mut self.tree)?;
                        Ok(())
                    } else {
                        fst.detach(&mut self.tree);
                        snd.detach(&mut self.tree);
                        parent1.checked_append(snd, &mut self.tree)?;
                        parent2.checked_append(fst, &mut self.tree)?;
                        Ok(())
                    }
                }
            } else {
                Err(SwitchError::NoParent { from: snd, to: fst })
            }
        } else {
            Err(SwitchError::NoParent { from: fst, to: snd })
        }
    }
}

#[cfg(test)]
mod test {
    use super::Mux;
    use cursive::views::DummyView;

    #[test]
    fn left_to_right() {
        let mut mux = Mux::new();
        let node1 = mux.add_left_of(DummyView, mux.root).unwrap();
        let node2 = mux.add_left_of(DummyView, node1).unwrap();
        assert!(mux.switch_views(node1, node2).is_ok());
    }

    #[test]
    fn right_to_left() {
        let mut mux = Mux::new();
        let node1 = mux.add_right_of(DummyView, mux.root).unwrap();
        let node2 = mux.add_left_of(DummyView, node1).unwrap();
        assert!(mux.switch_views(node2, node1).is_ok());
    }
}