forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLauncherPagesCommandPalette.cs
More file actions
143 lines (122 loc) · 4.17 KB
/
Copy pathLauncherPagesCommandPalette.cs
File metadata and controls
143 lines (122 loc) · 4.17 KB
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
using System;
using System.Collections.Generic;
namespace SourceGit.ViewModels
{
public class LauncherPagesCommandPalette : ICommandPalette
{
public List<LauncherPage> VisiblePages
{
get => _visiblePages;
private set => SetProperty(ref _visiblePages, value);
}
public List<RepositoryNode> VisibleRepos
{
get => _visibleRepos;
private set => SetProperty(ref _visibleRepos, value);
}
public string SearchFilter
{
get => _searchFilter;
set
{
if (SetProperty(ref _searchFilter, value))
UpdateVisible();
}
}
public LauncherPage SelectedPage
{
get => _selectedPage;
set
{
if (SetProperty(ref _selectedPage, value) && value != null)
SelectedRepo = null;
}
}
public RepositoryNode SelectedRepo
{
get => _selectedRepo;
set
{
if (SetProperty(ref _selectedRepo, value) && value != null)
SelectedPage = null;
}
}
public LauncherPagesCommandPalette(Launcher launcher)
{
_launcher = launcher;
foreach (var page in _launcher.Pages)
{
if (page.Node.IsRepository)
_opened.Add(page.Node.Id);
}
UpdateVisible();
}
public override void Cleanup()
{
_launcher = null;
_opened.Clear();
_visiblePages.Clear();
_visibleRepos.Clear();
_searchFilter = null;
_selectedPage = null;
_selectedRepo = null;
}
public void ClearFilter()
{
SearchFilter = string.Empty;
}
public void OpenOrSwitchTo()
{
if (_selectedPage != null)
_launcher.ActivePage = _selectedPage;
else if (_selectedRepo != null)
_launcher.OpenRepositoryInTab(_selectedRepo, null);
_launcher?.CancelCommandPalette();
}
private void UpdateVisible()
{
var pages = new List<LauncherPage>();
CollectVisiblePages(pages);
var repos = new List<RepositoryNode>();
CollectVisibleRepository(repos, Preferences.Instance.RepositoryNodes);
VisiblePages = pages;
VisibleRepos = repos;
}
private void CollectVisiblePages(List<LauncherPage> pages)
{
foreach (var page in _launcher.Pages)
{
if (page == _launcher.ActivePage)
continue;
if (string.IsNullOrEmpty(_searchFilter) ||
page.Node.Name.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase) ||
(page.Node.IsRepository && page.Node.Id.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase)))
pages.Add(page);
}
}
private void CollectVisibleRepository(List<RepositoryNode> outs, List<RepositoryNode> nodes)
{
foreach (var node in nodes)
{
if (!node.IsRepository)
{
CollectVisibleRepository(outs, node.SubNodes);
continue;
}
if (_opened.Contains(node.Id))
continue;
if (string.IsNullOrEmpty(_searchFilter) ||
node.Id.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase) ||
node.Name.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
outs.Add(node);
}
}
private Launcher _launcher = null;
private HashSet<string> _opened = new HashSet<string>();
private List<LauncherPage> _visiblePages = [];
private List<RepositoryNode> _visibleRepos = [];
private string _searchFilter = string.Empty;
private LauncherPage _selectedPage = null;
private RepositoryNode _selectedRepo = null;
}
}