2024-03-17 18:37:06 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia;
|
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Input;
|
|
|
|
using Avalonia.Interactivity;
|
|
|
|
using Avalonia.Platform.Storage;
|
|
|
|
using Avalonia.Threading;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
namespace SourceGit.Views
|
|
|
|
{
|
|
|
|
public partial class Welcome : UserControl
|
|
|
|
{
|
|
|
|
public Welcome()
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void SetupTreeViewDragAndDrop(object sender, RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
if (sender is TreeView view)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
DragDrop.SetAllowDrop(view, true);
|
|
|
|
view.AddHandler(DragDrop.DragOverEvent, DragOverTreeView);
|
|
|
|
view.AddHandler(DragDrop.DropEvent, DropOnTreeView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void SetupTreeNodeDragAndDrop(object sender, RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
if (sender is Grid grid)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
DragDrop.SetAllowDrop(grid, true);
|
|
|
|
grid.AddHandler(DragDrop.DragOverEvent, DragOverTreeNode);
|
|
|
|
grid.AddHandler(DragDrop.DropEvent, DropOnTreeNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 22:51:44 -07:00
|
|
|
private void OnTreeNodeContextRequested(object sender, ContextRequestedEventArgs e)
|
|
|
|
{
|
|
|
|
if (sender is Grid grid && DataContext is ViewModels.Welcome vm)
|
|
|
|
{
|
|
|
|
var menu = vm.CreateContextMenu(grid.DataContext as ViewModels.RepositoryNode);
|
2024-05-23 06:24:22 -07:00
|
|
|
grid.OpenContextMenu(menu);
|
2024-04-27 22:51:44 -07:00
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnPointerPressedTreeNode(object sender, PointerPressedEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.GetCurrentPoint(sender as Visual).Properties.IsLeftButtonPressed)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_pressedTreeNode = true;
|
|
|
|
_startDragTreeNode = false;
|
|
|
|
_pressedTreeNodePosition = e.GetPosition(sender as Grid);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_pressedTreeNode = false;
|
|
|
|
_startDragTreeNode = false;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnPointerReleasedOnTreeNode(object sender, PointerReleasedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_pressedTreeNode = false;
|
|
|
|
_startDragTreeNode = false;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnPointerMovedOverTreeNode(object sender, PointerEventArgs e)
|
|
|
|
{
|
|
|
|
if (_pressedTreeNode && !_startDragTreeNode && sender is Grid grid)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var delta = e.GetPosition(grid) - _pressedTreeNodePosition;
|
|
|
|
var sizeSquired = delta.X * delta.X + delta.Y * delta.Y;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (sizeSquired < 64)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
_startDragTreeNode = true;
|
|
|
|
|
|
|
|
var data = new DataObject();
|
|
|
|
data.Set("MovedRepositoryTreeNode", grid.DataContext);
|
|
|
|
DragDrop.DoDragDrop(e, data, DragDropEffects.Move);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnTreeViewLostFocus(object sender, RoutedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_pressedTreeNode = false;
|
|
|
|
_startDragTreeNode = false;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void DragOverTreeView(object sender, DragEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Data.Contains("MovedRepositoryTreeNode") || e.Data.Contains(DataFormats.Files))
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
e.DragEffects = DragDropEffects.Move;
|
|
|
|
e.Handled = true;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
e.DragEffects = DragDropEffects.None;
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private async void DropOnTreeView(object sender, DragEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Data.Contains("MovedRepositoryTreeNode"))
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
e.Handled = true;
|
|
|
|
|
|
|
|
var moved = e.Data.Get("MovedRepositoryTreeNode") as ViewModels.RepositoryNode;
|
2024-03-17 18:37:06 -07:00
|
|
|
if (moved != null && DataContext is ViewModels.Welcome vm)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
vm.MoveNode(moved, null);
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else if (e.Data.Contains(DataFormats.Files))
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
e.Handled = true;
|
|
|
|
|
|
|
|
var items = e.Data.GetFiles();
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var item in items)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
await OpenOrInitRepository(item.Path.LocalPath);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_pressedTreeNode = false;
|
|
|
|
_startDragTreeNode = false;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void DragOverTreeNode(object sender, DragEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Data.Contains("MovedRepositoryTreeNode") || e.Data.Contains(DataFormats.Files))
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var grid = sender as Grid;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (grid == null)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var to = grid.DataContext as ViewModels.RepositoryNode;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (to == null)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (to.IsRepository)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
e.DragEffects = DragDropEffects.None;
|
|
|
|
e.Handled = true;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
e.DragEffects = DragDropEffects.Move;
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private async void DropOnTreeNode(object sender, DragEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var grid = sender as Grid;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (grid == null)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var to = grid.DataContext as ViewModels.RepositoryNode;
|
2024-03-17 18:37:06 -07:00
|
|
|
if (to == null || to.IsRepository)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
e.Handled = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (e.Data.Contains("MovedRepositoryTreeNode"))
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
e.Handled = true;
|
|
|
|
|
|
|
|
var moved = e.Data.Get("MovedRepositoryTreeNode") as ViewModels.RepositoryNode;
|
2024-03-17 18:37:06 -07:00
|
|
|
if (to != null && moved != null && to != moved && DataContext is ViewModels.Welcome vm)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
vm.MoveNode(moved, to);
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else if (e.Data.Contains(DataFormats.Files))
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
e.Handled = true;
|
|
|
|
|
|
|
|
var items = e.Data.GetFiles();
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var item in items)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
await OpenOrInitRepository(item.Path.LocalPath, to);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_pressedTreeNode = false;
|
|
|
|
_startDragTreeNode = false;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnDoubleTappedTreeNode(object sender, TappedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var grid = sender as Grid;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (grid == null)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var to = grid.DataContext as ViewModels.RepositoryNode;
|
2024-03-17 18:37:06 -07:00
|
|
|
if (to == null || !to.IsRepository)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var launcher = TopLevel.GetTopLevel(this).DataContext as ViewModels.Launcher;
|
|
|
|
launcher.OpenRepositoryInTab(to, launcher.ActivePage);
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private async void OpenLocalRepository(object sender, RoutedEventArgs e)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (!ViewModels.PopupHost.CanCreatePopup())
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var topLevel = TopLevel.GetTopLevel(this);
|
|
|
|
var options = new FolderPickerOpenOptions() { AllowMultiple = false };
|
|
|
|
var selected = await topLevel.StorageProvider.OpenFolderPickerAsync(options);
|
2024-03-17 18:37:06 -07:00
|
|
|
if (selected.Count == 1)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
await OpenOrInitRepository(selected[0].Path.LocalPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private Task OpenOrInitRepository(string path, ViewModels.RepositoryNode parent = null)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var launcher = TopLevel.GetTopLevel(this).DataContext as ViewModels.Launcher;
|
|
|
|
var page = launcher.ActivePage;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (!Directory.Exists(path))
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (File.Exists(path))
|
|
|
|
path = Path.GetDirectoryName(path);
|
|
|
|
else
|
|
|
|
return null;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
return Task.Run(() =>
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var root = new Commands.QueryRepositoryRootPath(path).Result();
|
2024-03-17 18:37:06 -07:00
|
|
|
if (string.IsNullOrEmpty(root))
|
|
|
|
{
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
{
|
2024-05-20 02:36:43 -07:00
|
|
|
(DataContext as ViewModels.Welcome).InitRepository(path, parent);
|
2024-03-17 18:37:06 -07:00
|
|
|
});
|
2024-02-05 23:08:37 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
{
|
2024-06-30 20:57:13 -07:00
|
|
|
var normalizedPath = root.Replace("\\", "/");
|
|
|
|
var node = ViewModels.Preference.FindOrAddNodeByRepositoryPath(normalizedPath, parent, true);
|
2024-02-05 23:08:37 -08:00
|
|
|
launcher.OpenRepositoryInTab(node, page);
|
2024-03-17 18:37:06 -07:00
|
|
|
});
|
2024-02-05 23:08:37 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool _pressedTreeNode = false;
|
|
|
|
private Point _pressedTreeNodePosition = new Point();
|
|
|
|
private bool _startDragTreeNode = false;
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
}
|