|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +using Avalonia.Collections; |
| 5 | + |
| 6 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 7 | + |
| 8 | +namespace SourceGit.ViewModels |
| 9 | +{ |
| 10 | + public class ChangeTreeNode : ObservableObject |
| 11 | + { |
| 12 | + public string FullPath { get; set; } = string.Empty; |
| 13 | + public int Depth { get; private set; } = 0; |
| 14 | + public Models.Change Change { get; set; } = null; |
| 15 | + public List<ChangeTreeNode> Children { get; set; } = new List<ChangeTreeNode>(); |
| 16 | + |
| 17 | + public bool IsFolder |
| 18 | + { |
| 19 | + get => Change == null; |
| 20 | + } |
| 21 | + |
| 22 | + public bool IsExpanded |
| 23 | + { |
| 24 | + get => _isExpanded; |
| 25 | + set => SetProperty(ref _isExpanded, value); |
| 26 | + } |
| 27 | + |
| 28 | + public ChangeTreeNode(Models.Change c, int depth) |
| 29 | + { |
| 30 | + FullPath = c.Path; |
| 31 | + Depth = depth; |
| 32 | + Change = c; |
| 33 | + IsExpanded = false; |
| 34 | + } |
| 35 | + |
| 36 | + public ChangeTreeNode(string path, bool isExpanded, int depth) |
| 37 | + { |
| 38 | + FullPath = path; |
| 39 | + Depth = depth; |
| 40 | + IsExpanded = isExpanded; |
| 41 | + } |
| 42 | + |
| 43 | + public static List<ChangeTreeNode> Build(IList<Models.Change> changes, HashSet<string> folded) |
| 44 | + { |
| 45 | + var nodes = new List<ChangeTreeNode>(); |
| 46 | + var folders = new Dictionary<string, ChangeTreeNode>(); |
| 47 | + |
| 48 | + foreach (var c in changes) |
| 49 | + { |
| 50 | + var sepIdx = c.Path.IndexOf('/', StringComparison.Ordinal); |
| 51 | + if (sepIdx == -1) |
| 52 | + { |
| 53 | + nodes.Add(new ChangeTreeNode(c, 0)); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + ChangeTreeNode lastFolder = null; |
| 58 | + var start = 0; |
| 59 | + var depth = 0; |
| 60 | + |
| 61 | + while (sepIdx != -1) |
| 62 | + { |
| 63 | + var folder = c.Path.Substring(0, sepIdx); |
| 64 | + if (folders.TryGetValue(folder, out var value)) |
| 65 | + { |
| 66 | + lastFolder = value; |
| 67 | + } |
| 68 | + else if (lastFolder == null) |
| 69 | + { |
| 70 | + lastFolder = new ChangeTreeNode(folder, !folded.Contains(folder), depth); |
| 71 | + folders.Add(folder, lastFolder); |
| 72 | + InsertFolder(nodes, lastFolder); |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + var cur = new ChangeTreeNode(folder, !folded.Contains(folder), depth); |
| 77 | + folders.Add(folder, cur); |
| 78 | + InsertFolder(lastFolder.Children, cur); |
| 79 | + lastFolder = cur; |
| 80 | + } |
| 81 | + |
| 82 | + start = sepIdx + 1; |
| 83 | + depth++; |
| 84 | + sepIdx = c.Path.IndexOf('/', start); |
| 85 | + } |
| 86 | + |
| 87 | + lastFolder.Children.Add(new ChangeTreeNode(c, depth)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + Sort(nodes); |
| 92 | + |
| 93 | + folders.Clear(); |
| 94 | + return nodes; |
| 95 | + } |
| 96 | + |
| 97 | + private static void InsertFolder(List<ChangeTreeNode> collection, ChangeTreeNode subFolder) |
| 98 | + { |
| 99 | + for (int i = 0; i < collection.Count; i++) |
| 100 | + { |
| 101 | + if (!collection[i].IsFolder) |
| 102 | + { |
| 103 | + collection.Insert(i, subFolder); |
| 104 | + return; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + collection.Add(subFolder); |
| 109 | + } |
| 110 | + |
| 111 | + private static void Sort(List<ChangeTreeNode> nodes) |
| 112 | + { |
| 113 | + foreach (var node in nodes) |
| 114 | + { |
| 115 | + if (node.IsFolder) |
| 116 | + Sort(node.Children); |
| 117 | + } |
| 118 | + |
| 119 | + nodes.Sort((l, r) => |
| 120 | + { |
| 121 | + if (l.IsFolder) |
| 122 | + return r.IsFolder ? string.Compare(l.FullPath, r.FullPath, StringComparison.Ordinal) : -1; |
| 123 | + return r.IsFolder ? 1 : string.Compare(l.FullPath, r.FullPath, StringComparison.Ordinal); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + private bool _isExpanded = true; |
| 128 | + } |
| 129 | + |
| 130 | + public class ChangeCollectionAsTree |
| 131 | + { |
| 132 | + public List<ChangeTreeNode> Tree { get; set; } = new List<ChangeTreeNode>(); |
| 133 | + public AvaloniaList<ChangeTreeNode> Rows { get; set; } = new AvaloniaList<ChangeTreeNode>(); |
| 134 | + } |
| 135 | + |
| 136 | + public class ChangeCollectionAsGrid |
| 137 | + { |
| 138 | + public AvaloniaList<Models.Change> Changes { get; set; } = new AvaloniaList<Models.Change>(); |
| 139 | + } |
| 140 | + |
| 141 | + public class ChangeCollectionAsList |
| 142 | + { |
| 143 | + public AvaloniaList<Models.Change> Changes { get; set; } = new AvaloniaList<Models.Change>(); |
| 144 | + } |
| 145 | +} |
0 commit comments