Skip to content

Commit 196b454

Browse files
authored
feature: support delete key everywhere (#1412)
1 parent 5494093 commit 196b454

11 files changed

+125
-9
lines changed

src/ViewModels/Repository.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,18 @@ public void CheckoutBranch(Models.Branch branch)
13591359
}
13601360
}
13611361

1362+
public void DeleteBranch(Models.Branch branch)
1363+
{
1364+
if (CanCreatePopup())
1365+
ShowPopup(new DeleteBranch(this, branch));
1366+
}
1367+
1368+
public void DeleteRemote(Models.Remote remote)
1369+
{
1370+
if (CanCreatePopup())
1371+
ShowPopup(new DeleteRemote(this, remote));
1372+
}
1373+
13621374
public void DeleteMultipleBranches(List<Models.Branch> branches, bool isLocal)
13631375
{
13641376
if (CanCreatePopup())
@@ -1383,6 +1395,12 @@ public void CreateNewTag()
13831395
ShowPopup(new CreateTag(this, _currentBranch));
13841396
}
13851397

1398+
public void DeleteTag(Models.Tag tag)
1399+
{
1400+
if (CanCreatePopup())
1401+
ShowPopup(new DeleteTag(this, tag));
1402+
}
1403+
13861404
public void AddRemote()
13871405
{
13881406
if (CanCreatePopup())

src/ViewModels/StashesPage.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ private void RefreshVisible()
314314
}
315315
}
316316

317+
public void Drop(Models.Stash stash)
318+
{
319+
if (_repo.CanCreatePopup())
320+
_repo.ShowPopup(new DropStash(_repo, stash));
321+
}
322+
317323
private Repository _repo = null;
318324
private List<Models.Stash> _stashes = [];
319325
private List<Models.Stash> _visibleStashes = [];

src/Views/BranchTree.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ItemsSource="{Binding #ThisControl.Rows}"
1414
SelectionMode="Multiple"
1515
SelectionChanged="OnNodesSelectionChanged"
16+
KeyDown="OnListKeyDown"
1617
ContextRequested="OnTreeContextRequested">
1718
<ListBox.ItemsPanel>
1819
<ItemsPanelTemplate>

src/Views/BranchTree.axaml.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,44 @@ private void OnTreeContextRequested(object _1, ContextRequestedEventArgs _2)
450450
}
451451
}
452452

453+
private void OnListKeyDown(object _, KeyEventArgs e)
454+
{
455+
if (e.Key is not (Key.Delete or Key.Back))
456+
return;
457+
458+
var repo = DataContext as ViewModels.Repository;
459+
if (repo?.Settings == null)
460+
return;
461+
462+
var selected = BranchesPresenter.SelectedItems;
463+
if (selected == null || selected.Count == 0)
464+
return;
465+
466+
if (selected is [ViewModels.BranchTreeNode { Backend: Models.Remote remote }])
467+
{
468+
repo.DeleteRemote(remote);
469+
e.Handled = true;
470+
return;
471+
}
472+
473+
var branches = new List<Models.Branch>();
474+
foreach (var item in selected)
475+
{
476+
if (item is ViewModels.BranchTreeNode node)
477+
CollectBranchesInNode(branches, node);
478+
}
479+
480+
if (branches.Find(x => x.IsCurrent) != null)
481+
return;
482+
483+
if (branches.Count == 1)
484+
repo.DeleteBranch(branches[0]);
485+
else
486+
repo.DeleteMultipleBranches(branches, branches[0].IsLocal);
487+
488+
e.Handled = true;
489+
}
490+
453491
private void OnDoubleTappedBranchNode(object sender, TappedEventArgs _)
454492
{
455493
if (sender is Grid { DataContext: ViewModels.BranchTreeNode node })

src/Views/StashesPage.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
ItemsSource="{Binding VisibleStashes}"
6666
SelectedItem="{Binding SelectedStash, Mode=TwoWay}"
6767
SelectionMode="Single"
68+
KeyDown="OnStashKeyDown"
6869
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
6970
ScrollViewer.VerticalScrollBarVisibility="Auto">
7071
<ListBox.Styles>

src/Views/StashesPage.axaml.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Avalonia.Controls;
2+
using Avalonia.Input;
23

34
namespace SourceGit.Views
45
{
@@ -33,6 +34,21 @@ private void OnStashContextRequested(object sender, ContextRequestedEventArgs e)
3334
e.Handled = true;
3435
}
3536

37+
private void OnStashKeyDown(object sender, KeyEventArgs e)
38+
{
39+
if (e.Key is not (Key.Delete or Key.Back))
40+
return;
41+
42+
if (DataContext is not ViewModels.StashesPage vm)
43+
return;
44+
45+
if (sender is not ListBox { SelectedValue: Models.Stash stash })
46+
return;
47+
48+
vm.Drop(stash);
49+
e.Handled = true;
50+
}
51+
3652
private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e)
3753
{
3854
if (DataContext is ViewModels.StashesPage vm && sender is Grid grid)

src/Views/TagsView.axaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<ListBox Classes="repo_left_content_list"
2424
ItemsSource="{Binding Rows}"
2525
SelectionMode="Single"
26+
KeyDown="OnKeyDown"
2627
SelectionChanged="OnSelectionChanged">
2728

2829
<ListBox.DataTemplates>
@@ -88,6 +89,7 @@
8889
Margin="8,0,0,0"
8990
ItemsSource="{Binding Tags}"
9091
SelectionMode="Single"
92+
KeyDown="OnKeyDown"
9193
SelectionChanged="OnSelectionChanged">
9294
<ListBox.ItemTemplate>
9395
<DataTemplate DataType="m:Tag">

src/Views/TagsView.axaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,18 @@ private void OnSelectionChanged(object sender, SelectionChangedEventArgs _)
214214
if (selectedTag != null)
215215
RaiseEvent(new RoutedEventArgs(SelectionChangedEvent));
216216
}
217+
218+
private void OnKeyDown(object sender, KeyEventArgs e)
219+
{
220+
if (sender is not ListBox { SelectedValue: Models.Tag tag })
221+
return;
222+
223+
if (DataContext is not ViewModels.Repository repo)
224+
return;
225+
226+
repo.DeleteTag(tag);
227+
e.Handled = true;
228+
}
217229
}
218230
}
219231

src/Views/ViewLogs.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
ItemsSource="{Binding Logs}"
5454
SelectedItem="{Binding SelectedLog, Mode=TwoWay}"
5555
SelectionMode="Single"
56+
KeyDown="OnLogKeyDown"
5657
Grid.IsSharedSizeScope="True"
5758
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
5859
ScrollViewer.VerticalScrollBarVisibility="Auto">

src/Views/ViewLogs.axaml.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Avalonia.Controls;
2+
using Avalonia.Input;
23

34
namespace SourceGit.Views
45
{
@@ -39,5 +40,17 @@ private void OnLogContextRequested(object sender, ContextRequestedEventArgs e)
3940

4041
e.Handled = true;
4142
}
43+
44+
private void OnLogKeyDown(object _, KeyEventArgs e)
45+
{
46+
if (e.Key is not (Key.Delete or Key.Back))
47+
return;
48+
49+
if (DataContext is not ViewModels.ViewLogs vm)
50+
return;
51+
52+
vm.Logs.Remove(vm.SelectedLog);
53+
e.Handled = true;
54+
}
4255
}
4356
}

src/Views/Welcome.axaml.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,28 @@ private void SetupTreeNodeDragAndDrop(object sender, RoutedEventArgs _)
9494

9595
private void OnTreeViewKeyDown(object _, KeyEventArgs e)
9696
{
97-
if (TreeContainer.SelectedItem is ViewModels.RepositoryNode node && e.Key == Key.Enter)
97+
if (TreeContainer.SelectedItem is ViewModels.RepositoryNode node)
9898
{
99-
if (node.IsRepository)
99+
if (e.Key == Key.Enter)
100100
{
101-
var parent = this.FindAncestorOfType<Launcher>();
102-
if (parent is { DataContext: ViewModels.Launcher launcher })
103-
launcher.OpenRepositoryInTab(node, null);
101+
if (node.IsRepository)
102+
{
103+
var parent = this.FindAncestorOfType<Launcher>();
104+
if (parent is { DataContext: ViewModels.Launcher launcher })
105+
launcher.OpenRepositoryInTab(node, null);
106+
}
107+
else
108+
{
109+
ViewModels.Welcome.Instance.ToggleNodeIsExpanded(node);
110+
}
111+
112+
e.Handled = true;
104113
}
105-
else
114+
else if (e.Key is Key.Delete or Key.Back)
106115
{
107-
ViewModels.Welcome.Instance.ToggleNodeIsExpanded(node);
116+
node.Delete();
117+
e.Handled = true;
108118
}
109-
110-
e.Handled = true;
111119
}
112120
}
113121

0 commit comments

Comments
 (0)