Skip to content

Commit 731e5a7

Browse files
committed
refactor: drag & drop folders on repository tree (#1611)
- Allows to drag-drop multiple folders to repository tree - Drag & drop folder to repository tree will not open repository directly Signed-off-by: leo <[email protected]>
1 parent 9629a33 commit 731e5a7

File tree

4 files changed

+65
-33
lines changed

4 files changed

+65
-33
lines changed

src/ViewModels/RepositoryNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public List<RepositoryNode> SubNodes
6868
set;
6969
} = [];
7070

71-
public void OpenOrInit()
71+
public void Open()
7272
{
7373
if (IsRepository)
7474
{
@@ -77,7 +77,7 @@ public void OpenOrInit()
7777
}
7878

7979
foreach (var subNode in SubNodes)
80-
subNode.OpenOrInit();
80+
subNode.Open();
8181
}
8282

8383
public void Edit()

src/ViewModels/Welcome.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Threading.Tasks;
45

56
using Avalonia.Collections;
67
using CommunityToolkit.Mvvm.ComponentModel;
@@ -81,35 +82,32 @@ public void ToggleNodeIsExpanded(RepositoryNode node)
8182
}
8283
}
8384

84-
public void OpenOrInitRepository(string path, RepositoryNode parent, bool bMoveExistedNode)
85+
public async Task<string> GetRepositoryRootAsync(string path)
8586
{
86-
if (!Directory.Exists(path))
87+
if (!Preferences.Instance.IsGitConfigured())
8788
{
88-
if (File.Exists(path))
89-
path = Path.GetDirectoryName(path);
90-
else
91-
return;
89+
App.RaiseException(string.Empty, App.Text("NotConfigured"));
90+
return null;
9291
}
9392

94-
var isBare = new Commands.IsBareRepository(path).GetResultAsync().Result;
95-
var repoRoot = path;
96-
if (!isBare)
93+
var root = path;
94+
if (!Directory.Exists(root))
9795
{
98-
var test = new Commands.QueryRepositoryRootPath(path).GetResultAsync().Result;
99-
if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut))
100-
{
101-
InitRepository(path, parent, test.StdErr);
102-
return;
103-
}
104-
105-
repoRoot = test.StdOut.Trim();
96+
if (File.Exists(root))
97+
root = Path.GetDirectoryName(root);
98+
else
99+
return null;
106100
}
107101

108-
var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(repoRoot, parent, bMoveExistedNode);
109-
Refresh();
102+
var isBare = await new Commands.IsBareRepository(root).GetResultAsync();
103+
if (isBare)
104+
return root;
105+
106+
var rs = await new Commands.QueryRepositoryRootPath(root).GetResultAsync();
107+
if (!rs.IsSuccess || string.IsNullOrWhiteSpace(rs.StdOut))
108+
return null;
110109

111-
var launcher = App.GetLauncher();
112-
launcher?.OpenRepositoryInTab(node, launcher.ActivePage);
110+
return rs.StdOut.Trim();
113111
}
114112

115113
public void InitRepository(string path, RepositoryNode parent, string reason)
@@ -125,6 +123,13 @@ public void InitRepository(string path, RepositoryNode parent, string reason)
125123
activePage.Popup = new Init(activePage.Node.Id, path, parent, reason);
126124
}
127125

126+
public void AddRepository(string path, RepositoryNode parent, bool moveNode, bool open)
127+
{
128+
var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(path, parent, moveNode);
129+
if (open)
130+
node.Open();
131+
}
132+
128133
public void Clone()
129134
{
130135
if (!Preferences.Instance.IsGitConfigured())

src/Views/Welcome.axaml.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
32
using Avalonia;
43
using Avalonia.Controls;
54
using Avalonia.Controls.Primitives;
@@ -121,7 +120,7 @@ private void OnTreeNodeContextRequested(object sender, ContextRequestedEventArgs
121120
openAll.Icon = App.CreateMenuIcon("Icons.Folder.Open");
122121
openAll.Click += (_, e) =>
123122
{
124-
node.OpenOrInit();
123+
node.Open();
125124
e.Handled = true;
126125
};
127126

@@ -136,7 +135,7 @@ private void OnTreeNodeContextRequested(object sender, ContextRequestedEventArgs
136135
open.Icon = App.CreateMenuIcon("Icons.Folder.Open");
137136
open.Click += (_, e) =>
138137
{
139-
node.OpenOrInit();
138+
node.Open();
140139
e.Handled = true;
141140
};
142141

@@ -273,7 +272,7 @@ private void DragOverTreeView(object sender, DragEventArgs e)
273272
}
274273
}
275274

276-
private void DropOnTreeView(object sender, DragEventArgs e)
275+
private async void DropOnTreeView(object sender, DragEventArgs e)
277276
{
278277
if (e.Data.Contains("MovedRepositoryTreeNode") && e.Data.Get("MovedRepositoryTreeNode") is ViewModels.RepositoryNode moved)
279278
{
@@ -287,11 +286,20 @@ private void DropOnTreeView(object sender, DragEventArgs e)
287286
var items = e.Data.GetFiles();
288287
if (items != null)
289288
{
289+
var refresh = false;
290+
290291
foreach (var item in items)
291292
{
292-
ViewModels.Welcome.Instance.OpenOrInitRepository(item.Path.LocalPath, null, true);
293-
break;
293+
var path = await ViewModels.Welcome.Instance.GetRepositoryRootAsync(item.Path.LocalPath);
294+
if (!string.IsNullOrEmpty(path))
295+
{
296+
ViewModels.Welcome.Instance.AddRepository(path, null, true, false);
297+
refresh = true;
298+
}
294299
}
300+
301+
if (refresh)
302+
ViewModels.Welcome.Instance.Refresh();
295303
}
296304
}
297305

@@ -313,7 +321,7 @@ private void DragOverTreeNode(object sender, DragEventArgs e)
313321
}
314322
}
315323

316-
private void DropOnTreeNode(object sender, DragEventArgs e)
324+
private async void DropOnTreeNode(object sender, DragEventArgs e)
317325
{
318326
if (sender is not Grid grid)
319327
return;
@@ -342,11 +350,20 @@ private void DropOnTreeNode(object sender, DragEventArgs e)
342350
var items = e.Data.GetFiles();
343351
if (items != null)
344352
{
353+
var refresh = false;
354+
345355
foreach (var item in items)
346356
{
347-
ViewModels.Welcome.Instance.OpenOrInitRepository(item.Path.LocalPath, to, true);
348-
break;
357+
var path = await ViewModels.Welcome.Instance.GetRepositoryRootAsync(item.Path.LocalPath);
358+
if (!string.IsNullOrEmpty(path))
359+
{
360+
ViewModels.Welcome.Instance.AddRepository(path, to, true, false);
361+
refresh = true;
362+
}
349363
}
364+
365+
if (refresh)
366+
ViewModels.Welcome.Instance.Refresh();
350367
}
351368
}
352369

src/Views/WelcomeToolbar.axaml.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,17 @@ private async void OpenLocalRepository(object _1, RoutedEventArgs e)
4444
{
4545
var folder = selected[0];
4646
var folderPath = folder is { Path: { IsAbsoluteUri: true } path } ? path.LocalPath : folder?.Path.ToString();
47-
ViewModels.Welcome.Instance.OpenOrInitRepository(folderPath, null, false);
47+
var repoPath = await ViewModels.Welcome.Instance.GetRepositoryRootAsync(folderPath);
48+
if (!string.IsNullOrEmpty(repoPath))
49+
{
50+
ViewModels.Welcome.Instance.AddRepository(repoPath, null, false, true);
51+
ViewModels.Welcome.Instance.Refresh();
52+
}
53+
else if (Directory.Exists(folderPath))
54+
{
55+
var test = await new Commands.QueryRepositoryRootPath(folderPath).GetResultAsync();
56+
ViewModels.Welcome.Instance.InitRepository(folderPath, null, test.StdErr);
57+
}
4858
}
4959
}
5060
catch (Exception exception)

0 commit comments

Comments
 (0)