-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathLauncherPagesCommandPalette.cs
More file actions
196 lines (174 loc) · 5.69 KB
/
Copy pathLauncherPagesCommandPalette.cs
File metadata and controls
196 lines (174 loc) · 5.69 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
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
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 void ClearFilter()
{
SearchFilter = string.Empty;
}
public void OpenOrSwitchTo()
{
_opened.Clear();
_visiblePages.Clear();
_visibleRepos.Clear();
Close();
if (_selectedPage != null)
_launcher.ActivePage = _selectedPage;
else if (_selectedRepo != null)
_launcher.OpenRepositoryInTab(_selectedRepo, null);
}
private void UpdateVisible()
{
var pages = new List<LauncherPage>();
CollectVisiblePages(pages);
var repos = new List<RepositoryNode>();
CollectVisibleRepository(repos, Preferences.Instance.RepositoryNodes);
var autoSelectPage = _selectedPage;
var autoSelectRepo = _selectedRepo;
if (_selectedPage != null)
{
if (pages.Contains(_selectedPage))
{
// Keep selection
}
else if (pages.Count > 0)
{
autoSelectPage = pages[0];
}
else if (repos.Count > 0)
{
autoSelectPage = null;
autoSelectRepo = repos[0];
}
else
{
autoSelectPage = null;
}
}
else if (_selectedRepo != null)
{
if (repos.Contains(_selectedRepo))
{
// Keep selection
}
else if (repos.Count > 0)
{
autoSelectRepo = repos[0];
}
else if (pages.Count > 0)
{
autoSelectPage = pages[0];
autoSelectRepo = null;
}
else
{
autoSelectRepo = null;
}
}
else if (pages.Count > 0)
{
autoSelectPage = pages[0];
autoSelectRepo = null;
}
else if (repos.Count > 0)
{
autoSelectPage = null;
autoSelectRepo = repos[0];
}
else
{
autoSelectPage = null;
autoSelectRepo = null;
}
VisiblePages = pages;
VisibleRepos = repos;
SelectedPage = autoSelectPage;
SelectedRepo = autoSelectRepo;
}
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;
}
}