forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositoryCommandPalette.cs
More file actions
144 lines (123 loc) · 4.12 KB
/
RepositoryCommandPalette.cs
File metadata and controls
144 lines (123 loc) · 4.12 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
using System;
using System.Collections.Generic;
namespace SourceGit.ViewModels
{
public class RepositoryCommandPaletteCmd
{
public string Key { get; set; }
public Action Action { get; set; }
public string Label => $"{App.Text(Key)}...";
public RepositoryCommandPaletteCmd(string key, Action action)
{
Key = key;
Action = action;
}
}
public class RepositoryCommandPalette : ICommandPalette
{
public List<RepositoryCommandPaletteCmd> VisibleCmds
{
get => _visibleCmds;
private set => SetProperty(ref _visibleCmds, value);
}
public RepositoryCommandPaletteCmd SelectedCmd
{
get => _selectedCmd;
set => SetProperty(ref _selectedCmd, value);
}
public string Filter
{
get => _filter;
set
{
if (SetProperty(ref _filter, value))
UpdateVisible();
}
}
public RepositoryCommandPalette(Launcher launcher, Repository repo)
{
_launcher = launcher;
_repo = repo;
_cmds.Add(new("Blame", () =>
{
var sub = new BlameCommandPalette(_launcher, _repo.FullPath);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("Checkout", () =>
{
var sub = new CheckoutCommandPalette(_launcher, _repo);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("Compare.WithHead", () =>
{
var sub = new CompareCommandPalette(_launcher, _repo, _repo.CurrentBranch);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("FileHistory", () =>
{
var sub = new FileHistoryCommandPalette(_launcher, _repo.FullPath);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("Merge", () =>
{
var sub = new MergeCommandPalette(_launcher, _repo);
_launcher.OpenCommandPalette(sub);
}));
_cmds.Add(new("OpenFile", () =>
{
var sub = new OpenFileCommandPalette(_launcher, _repo.FullPath);
_launcher.OpenCommandPalette(sub);
}));
_visibleCmds = _cmds;
_selectedCmd = _cmds[0];
}
public override void Cleanup()
{
_launcher = null;
_repo = null;
_cmds.Clear();
_visibleCmds.Clear();
_selectedCmd = null;
_filter = null;
}
public void ClearFilter()
{
Filter = string.Empty;
}
public void Exec()
{
if (_selectedCmd != null)
_selectedCmd.Action?.Invoke();
else
_launcher?.CancelCommandPalette();
}
private void UpdateVisible()
{
if (string.IsNullOrEmpty(_filter))
{
VisibleCmds = _cmds;
}
else
{
var visible = new List<RepositoryCommandPaletteCmd>();
foreach (var cmd in _cmds)
{
if (cmd.Key.Contains(_filter, StringComparison.OrdinalIgnoreCase) ||
cmd.Label.Contains(_filter, StringComparison.OrdinalIgnoreCase))
visible.Add(cmd);
}
var autoSelected = _selectedCmd;
if (!visible.Contains(_selectedCmd))
autoSelected = visible.Count > 0 ? visible[0] : null;
VisibleCmds = visible;
SelectedCmd = autoSelected;
}
}
private Launcher _launcher = null;
private Repository _repo = null;
private List<RepositoryCommandPaletteCmd> _cmds = [];
private List<RepositoryCommandPaletteCmd> _visibleCmds = [];
private RepositoryCommandPaletteCmd _selectedCmd = null;
private string _filter;
}
}