|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 4 | + |
| 5 | +namespace SourceGit.ViewModels |
| 6 | +{ |
| 7 | + public class QuickLauncher : ObservableObject |
| 8 | + { |
| 9 | + public List<LauncherPage> VisiblePages |
| 10 | + { |
| 11 | + get => _visiblePages; |
| 12 | + private set => SetProperty(ref _visiblePages, value); |
| 13 | + } |
| 14 | + |
| 15 | + public List<RepositoryNode> VisibleRepos |
| 16 | + { |
| 17 | + get => _visibleRepos; |
| 18 | + private set => SetProperty(ref _visibleRepos, value); |
| 19 | + } |
| 20 | + |
| 21 | + public string SearchFilter |
| 22 | + { |
| 23 | + get => _searchFilter; |
| 24 | + set |
| 25 | + { |
| 26 | + if (SetProperty(ref _searchFilter, value)) |
| 27 | + UpdateVisible(); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public LauncherPage SelectedPage |
| 32 | + { |
| 33 | + get => _selectedPage; |
| 34 | + set |
| 35 | + { |
| 36 | + if (SetProperty(ref _selectedPage, value) && value != null) |
| 37 | + SelectedRepo = null; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public RepositoryNode SelectedRepo |
| 42 | + { |
| 43 | + get => _selectedRepo; |
| 44 | + set |
| 45 | + { |
| 46 | + if (SetProperty(ref _selectedRepo, value) && value != null) |
| 47 | + SelectedPage = null; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public QuickLauncher(Launcher launcher) |
| 52 | + { |
| 53 | + _launcher = launcher; |
| 54 | + |
| 55 | + foreach (var page in _launcher.Pages) |
| 56 | + { |
| 57 | + if (page.Node.IsRepository) |
| 58 | + _opened.Add(page.Node.Id); |
| 59 | + } |
| 60 | + |
| 61 | + UpdateVisible(); |
| 62 | + } |
| 63 | + |
| 64 | + public void ClearFilter() |
| 65 | + { |
| 66 | + SearchFilter = string.Empty; |
| 67 | + } |
| 68 | + |
| 69 | + public void OpenOrSwitchTo() |
| 70 | + { |
| 71 | + if (_selectedPage != null) |
| 72 | + _launcher.ActivePage = _selectedPage; |
| 73 | + else if (_selectedRepo != null) |
| 74 | + _launcher.OpenRepositoryInTab(_selectedRepo, null); |
| 75 | + |
| 76 | + _launcher.QuickLauncher = null; |
| 77 | + } |
| 78 | + |
| 79 | + private void UpdateVisible() |
| 80 | + { |
| 81 | + var pages = new List<LauncherPage>(); |
| 82 | + foreach (var page in _launcher.Pages) |
| 83 | + { |
| 84 | + if (string.IsNullOrEmpty(_searchFilter) || |
| 85 | + page.Node.Name.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase) || |
| 86 | + (page.Node.IsRepository && page.Node.Id.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))) |
| 87 | + pages.Add(page); |
| 88 | + } |
| 89 | + |
| 90 | + var repos = new List<RepositoryNode>(); |
| 91 | + CollectVisibleRepository(repos, Preferences.Instance.RepositoryNodes); |
| 92 | + |
| 93 | + VisiblePages = pages; |
| 94 | + VisibleRepos = repos; |
| 95 | + } |
| 96 | + |
| 97 | + private void CollectVisibleRepository(List<RepositoryNode> outs, List<RepositoryNode> nodes) |
| 98 | + { |
| 99 | + foreach (var node in nodes) |
| 100 | + { |
| 101 | + if (!node.IsRepository) |
| 102 | + { |
| 103 | + CollectVisibleRepository(outs, node.SubNodes); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + if (_opened.Contains(node.Id)) |
| 108 | + continue; |
| 109 | + |
| 110 | + if (string.IsNullOrEmpty(_searchFilter) || |
| 111 | + node.Id.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase) || |
| 112 | + node.Name.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase)) |
| 113 | + outs.Add(node); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private Launcher _launcher = null; |
| 118 | + private HashSet<string> _opened = new HashSet<string>(); |
| 119 | + private List<LauncherPage> _visiblePages = []; |
| 120 | + private List<RepositoryNode> _visibleRepos = []; |
| 121 | + private string _searchFilter = string.Empty; |
| 122 | + private LauncherPage _selectedPage = null; |
| 123 | + private RepositoryNode _selectedRepo = null; |
| 124 | + } |
| 125 | +} |
0 commit comments