summaryrefslogtreecommitdiff
path: root/src/sidebar/Branch.vala
blob: 23badda83b0db3dfb17a24f10a489986325e9d19 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* Copyright 2011-2014 Yorba Foundation
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

public delegate bool Locator<G>(G item);

public class Sidebar.Branch : Object {
    [Flags]
    public enum Options {
        NONE = 0,
        HIDE_IF_EMPTY,
        AUTO_OPEN_ON_NEW_CHILD,
        STARTUP_EXPAND_TO_FIRST_CHILD,
        STARTUP_OPEN_GROUPING;
        
        public bool is_hide_if_empty() {
            return (this & HIDE_IF_EMPTY) != 0;
        }
        
        public bool is_auto_open_on_new_child() {
            return (this & AUTO_OPEN_ON_NEW_CHILD) != 0;
        }
        
        public bool is_startup_expand_to_first_child() {
            return (this & STARTUP_EXPAND_TO_FIRST_CHILD) != 0;
        }
        
        public bool is_startup_open_grouping() {
            return (this & STARTUP_OPEN_GROUPING) != 0;
        }
    }
    
    private class Node {
        public delegate void PruneCallback(Node node);
        
        public delegate void ChildrenReorderedCallback(Node node);
        
        public Sidebar.Entry entry;
        public weak Node? parent;
        public CompareDataFunc<Sidebar.Entry> comparator;
        public Gee.SortedSet<Node>? children = null;
        
        public Node(Sidebar.Entry entry, Node? parent,
            owned CompareDataFunc<Sidebar.Entry> comparator) {
            this.entry = entry;
            this.parent = parent;
            this.comparator = (owned) comparator;
        }
        
        private static int comparator_wrapper(Node? a, Node? b) {
            if (a == b)
                return 0;
            
            assert(a.parent == b.parent);
            
            return a.parent.comparator(a.entry, b.entry);
        }
        
        public bool has_children() {
            return (children != null && children.size > 0);
        }
        
        public void add_child(Node child) {
            child.parent = this;

            if (children == null)
                children = new Gee.TreeSet<Node>(comparator_wrapper);
            
            bool added = children.add(child);
            assert(added);
        }
        
        public void remove_child(Node child) {
            assert(children != null);
            
            Gee.SortedSet<Node> new_children = new Gee.TreeSet<Node>(comparator_wrapper);
            
            // For similar reasons as in reorder_child(), can't rely on Gee.TreeSet to locate this
            // node because we need reference equality.
            bool found = false;
            foreach (Node c in children) {
                if (c != child)
                    new_children.add(c);
                else
                    found = true;
            }
            
            assert(found);
            
            if (new_children.size != 0)
                children = new_children;
            else
                children = null;
            
            child.parent = null;
        }
        
        public void prune_children(PruneCallback cb) {
            if (children == null)
                return;
            
            foreach (Node child in children)
                child.prune_children(cb);
            
            Gee.SortedSet<Node> old_children = children;
            children = null;
            
            // Although this could've been done in the prior loop, it means notifying that
            // a child has been removed prior to it being removed; this can cause problem
            // if a signal handler calls back into the Tree to examine/add/remove nodes.
            foreach (Node child in old_children)
                cb(child);
        }
        
        // This returns the index of the Node purely by reference equality, making it useful if
        // the criteria the Node is sorted upon has changed.
        public int index_of_by_reference(Node child) {
            if (children == null)
                return -1;
            
            int index = 0;
            foreach (Node c in children) {
                if (child == c)
                    return index;
                
                index++;
            }
            
            return -1;
        }
        
        // Returns true if child moved when reordered.
        public bool reorder_child(Node child) {
            assert(children != null);
            
            int old_index = index_of_by_reference(child);
            assert(old_index >= 0);
            
            // Because Gee.SortedSet uses the comparator for equality, if the Node's entry state
            // has changed in such a way that the item is no longer sorted properly, the SortedSet's
            // search and remove methods are useless.  Makes no difference if children.remove() is
            // called or the set is manually iterated over and removed via the Iterator -- a
            // tree search is performed and the child will not be found.  Only easy solution is
            // to rebuild a new SortedSet and see if the child has moved.
            Gee.SortedSet<Node> new_children = new Gee.TreeSet<Node>(comparator_wrapper);
            bool added = new_children.add_all(children);
            assert(added);
            
            children = new_children;
            
            int new_index = index_of_by_reference(child);
            assert(new_index >= 0);
            
            return (old_index != new_index);
        }
        
        public void reorder_children(bool recursive, ChildrenReorderedCallback cb) {
            if (children == null)
                return;
            
            Gee.SortedSet<Node> reordered = new Gee.TreeSet<Node>(comparator_wrapper);
            reordered.add_all(children);
            children = reordered;
            
            if (recursive) {
                foreach (Node child in children)
                    child.reorder_children(true, cb);
            }
            
            cb(this);
        }
        
        public void change_comparator(owned CompareDataFunc<Sidebar.Entry> comparator, bool recursive,
            ChildrenReorderedCallback cb) {
            this.comparator = (owned) comparator;
            
            // reorder children, but need to do manual recursion to set comparator
            reorder_children(false, cb);
            
            if (recursive) {
                foreach (Node child in children)
                    child.change_comparator((owned) comparator, true, cb);
            }
        }
    }
    
    private Node root;
    private Options options;
    private bool shown = true;
    private CompareDataFunc<Sidebar.Entry> default_comparator;
    private Gee.HashMap<Sidebar.Entry, Node> map = new Gee.HashMap<Sidebar.Entry, Node>();
    
    public signal void entry_added(Sidebar.Entry entry);
    
    public signal void entry_removed(Sidebar.Entry entry);
    
    public signal void entry_moved(Sidebar.Entry entry);
    
    public signal void entry_reparented(Sidebar.Entry entry, Sidebar.Entry old_parent);
    
    public signal void children_reordered(Sidebar.Entry entry);
    
    public signal void show_branch(bool show);
    
    public Branch(Sidebar.Entry root, Options options, 
        owned CompareDataFunc<Sidebar.Entry> default_comparator,
        owned CompareDataFunc<Sidebar.Entry>? root_comparator = null) {
        this.default_comparator = (owned) default_comparator;
        
        CompareDataFunc<Sidebar.Entry>? broken_ternary_workaround;
        
        if (root_comparator != null)
            broken_ternary_workaround = (owned) root_comparator;
        else
            broken_ternary_workaround = (owned) default_comparator;

        this.root = new Node(root, null, (owned) broken_ternary_workaround);
        this.options = options;
        
        map.set(root, this.root);
        
        if (options.is_hide_if_empty())
            set_show_branch(false);
    }
    
    public Sidebar.Entry get_root() {
        return root.entry;
    }
    
    public void set_show_branch(bool shown) {
        if (this.shown == shown)
            return;
        
        this.shown = shown;
        show_branch(shown);
    }
    
    public bool get_show_branch() {
        return shown;
    }
    
    public bool is_auto_open_on_new_child() {
        return options.is_auto_open_on_new_child();
    }
    
    public bool is_startup_expand_to_first_child() {
        return options.is_startup_expand_to_first_child();
    }
    
    public bool is_startup_open_grouping() {
        return options.is_startup_open_grouping();
    }
    
    public void graft(Sidebar.Entry parent, Sidebar.Entry entry,
        owned CompareDataFunc<Sidebar.Entry>? comparator = null) {
        assert(map.has_key(parent));
        assert(!map.has_key(entry));
        
        if (options.is_hide_if_empty())
            set_show_branch(true);
        
        Node parent_node = map.get(parent);
        
        CompareDataFunc<Sidebar.Entry>? broken_ternary_workaround;
        
        if (comparator != null)
            broken_ternary_workaround = (owned) comparator;
        else
            broken_ternary_workaround = (owned) default_comparator;
        
        Node entry_node = new Node(entry, parent_node, (owned) broken_ternary_workaround);
        
        parent_node.add_child(entry_node);
        map.set(entry, entry_node);
        
        entry_added(entry);
    }
    
    // Cannot prune the root.  The Branch should simply be removed from the Tree.
    public void prune(Sidebar.Entry entry) {
        assert(entry != root.entry);
        assert(map.has_key(entry));
        
        Node entry_node = map.get(entry);
        
        entry_node.prune_children(prune_callback);
        
        assert(entry_node.parent != null);
        entry_node.parent.remove_child(entry_node);
        
        bool removed = map.unset(entry);
        assert(removed);
        
        entry_removed(entry);
        
        if (options.is_hide_if_empty() && !root.has_children())
            set_show_branch(false);
    }
    
    // Cannot reparent the root.
    public void reparent(Sidebar.Entry new_parent, Sidebar.Entry entry) {
        assert(entry != root.entry);
        assert(map.has_key(entry));
        assert(map.has_key(new_parent));
        
        Node entry_node = map.get(entry);
        Node new_parent_node = map.get(new_parent);
        
        assert(entry_node.parent != null);
        Sidebar.Entry old_parent = entry_node.parent.entry;
        
        entry_node.parent.remove_child(entry_node);
        new_parent_node.add_child(entry_node);
        
        entry_reparented(entry, old_parent);
    }
    
    public bool has_entry(Sidebar.Entry entry) {
        return (root.entry == entry || map.has_key(entry));
    }
    
    // Call when a value related to the comparison of this entry has changed.  The root cannot be 
    // reordered.
    public void reorder(Sidebar.Entry entry) {
        assert(entry != root.entry);
        
        Node? entry_node = map.get(entry);
        assert(entry_node != null);
        
        assert(entry_node.parent != null);
        if (entry_node.parent.reorder_child(entry_node))
            entry_moved(entry);
    }
    
    // Call when the entire tree needs to be reordered.
    public void reorder_all() {
        root.reorder_children(true, children_reordered_callback);
    }
    
    // Call when the children of the entry need to be reordered.
    public void reorder_children(Sidebar.Entry entry, bool recursive) {
        Node? entry_node = map.get(entry);
        assert(entry_node != null);
        
        entry_node.reorder_children(recursive, children_reordered_callback);
    }
    
    public void change_all_comparators(owned CompareDataFunc<Sidebar.Entry>? comparator) {
        root.change_comparator((owned) comparator, true, children_reordered_callback);
    }
    
    public void change_comparator(Sidebar.Entry entry, bool recursive,
        owned CompareDataFunc<Sidebar.Entry>? comparator) {
        Node? entry_node = map.get(entry);
        assert(entry_node != null);
        
        entry_node.change_comparator((owned) comparator, recursive, children_reordered_callback);
    }
    
    public int get_child_count(Sidebar.Entry parent) {
        Node? parent_node = map.get(parent);
        assert(parent_node != null);
        
        return (parent_node.children != null) ? parent_node.children.size : 0;
    }
    
    // Gets a snapshot of the children of the entry; this list will not be changed as the
    // branch is updated.
    public Gee.List<Sidebar.Entry>? get_children(Sidebar.Entry parent) {
        assert(map.has_key(parent));
        
        Node parent_node = map.get(parent);
        if (parent_node.children == null)
            return null;
        
        Gee.List<Sidebar.Entry> child_entries = new Gee.ArrayList<Sidebar.Entry>();
        foreach (Node child in parent_node.children)
            child_entries.add(child.entry);
        
        return child_entries;
    }
    
    public Sidebar.Entry? find_first_child(Sidebar.Entry parent, Locator<Sidebar.Entry> locator) {
        Node? parent_node = map.get(parent);
        assert(parent_node != null);
        
        if (parent_node.children == null)
            return null;
        
        foreach (Node child in parent_node.children) {
            if (locator(child.entry))
                return child.entry;
        }
        
        return null;
    }
    
    // Returns null if entry is root;
    public Sidebar.Entry? get_parent(Sidebar.Entry entry) {
        if (entry == root.entry)
            return null;
        
        Node? entry_node = map.get(entry);
        assert(entry_node != null);
        assert(entry_node.parent != null);
        
        return entry_node.parent.entry;
    }
    
    // Returns null if entry is root;
    public Sidebar.Entry? get_previous_sibling(Sidebar.Entry entry) {
        if (entry == root.entry)
            return null;
        
        Node? entry_node = map.get(entry);
        assert(entry_node != null);
        assert(entry_node.parent != null);
        assert(entry_node.parent.children != null);
        
        Node? sibling = entry_node.parent.children.lower(entry_node);
        
        return (sibling != null) ? sibling.entry : null;
    }
    
    // Returns null if entry is root;
    public Sidebar.Entry? get_next_sibling(Sidebar.Entry entry) {
        if (entry == root.entry)
            return null;
        
        Node? entry_node = map.get(entry);
        assert(entry_node != null);
        assert(entry_node.parent != null);
        assert(entry_node.parent.children != null);
        
        Node? sibling = entry_node.parent.children.higher(entry_node);
        
        return (sibling != null) ? sibling.entry : null;
    }
    
    private void prune_callback(Node node) {
        entry_removed(node.entry);
    }
    
    private void children_reordered_callback(Node node) {
        children_reordered(node.entry);
    }
}