forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecuteCustomActionCommandPalette.cs
More file actions
114 lines (96 loc) · 3.29 KB
/
Copy pathExecuteCustomActionCommandPalette.cs
File metadata and controls
114 lines (96 loc) · 3.29 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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class ExecuteCustomActionCommandPaletteCmd
{
public Models.CustomAction Action { get; set; }
public bool IsGlobal { get; set; }
public string Name { get => Action.Name; }
public ExecuteCustomActionCommandPaletteCmd(Models.CustomAction action, bool isGlobal)
{
Action = action;
IsGlobal = isGlobal;
}
}
public class ExecuteCustomActionCommandPalette : ICommandPalette
{
public List<ExecuteCustomActionCommandPaletteCmd> VisibleActions
{
get => _visibleActions;
private set => SetProperty(ref _visibleActions, value);
}
public ExecuteCustomActionCommandPaletteCmd Selected
{
get => _selected;
set => SetProperty(ref _selected, value);
}
public string Filter
{
get => _filter;
set
{
if (SetProperty(ref _filter, value))
UpdateVisibleActions();
}
}
public ExecuteCustomActionCommandPalette(Repository repo)
{
_repo = repo;
var actions = repo.GetCustomActions(Models.CustomActionScope.Repository);
foreach (var (action, menu) in actions)
_actions.Add(new(action, menu.IsGlobal));
if (_actions.Count > 0)
{
_actions.Sort((l, r) =>
{
if (l.IsGlobal != r.IsGlobal)
return l.IsGlobal ? -1 : 1;
return l.Name.CompareTo(r.Name, StringComparison.OrdinalIgnoreCase);
});
_visibleActions = _actions;
_selected = _actions[0];
}
}
public void ClearFilter()
{
Filter = string.Empty;
}
public async Task ExecAsync()
{
_actions.Clear();
_visibleActions.Clear();
Close();
if (_selected != null)
await _repo.ExecCustomActionAsync(_selected.Action, null);
}
private void UpdateVisibleActions()
{
var filter = _filter?.Trim();
if (string.IsNullOrEmpty(filter))
{
VisibleActions = _actions;
return;
}
var visible = new List<ExecuteCustomActionCommandPaletteCmd>();
foreach (var act in _actions)
{
if (act.Name.Contains(filter, StringComparison.OrdinalIgnoreCase))
visible.Add(act);
}
var autoSelected = _selected;
if (visible.Count == 0)
autoSelected = null;
else if (_selected == null || !visible.Contains(_selected))
autoSelected = visible[0];
VisibleActions = visible;
Selected = autoSelected;
}
private Repository _repo;
private List<ExecuteCustomActionCommandPaletteCmd> _actions = [];
private List<ExecuteCustomActionCommandPaletteCmd> _visibleActions = [];
private ExecuteCustomActionCommandPaletteCmd _selected = null;
private string _filter;
}
}