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-07-14 00:55:15 -07:00
|
|
|
using Avalonia.VisualTree;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
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-07-14 00:55:15 -07:00
|
|
|
private void SetupTreeViewDragAndDrop(object sender, RoutedEventArgs _)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
|
|
|
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-07-14 00:55:15 -07:00
|
|
|
private void SetupTreeNodeDragAndDrop(object sender, RoutedEventArgs _)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
|
|
|
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-07-14 00:55:15 -07:00
|
|
|
private void OnPointerReleasedOnTreeNode(object _1, PointerReleasedEventArgs _2)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
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)
|
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
if (_pressedTreeNode && !_startDragTreeNode &&
|
|
|
|
sender is Grid { DataContext: ViewModels.RepositoryNode node } grid)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
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();
|
2024-07-14 00:55:15 -07:00
|
|
|
data.Set("MovedRepositoryTreeNode", node);
|
2024-02-05 23:08:37 -08:00
|
|
|
DragDrop.DoDragDrop(e, data, DragDropEffects.Move);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private void OnTreeViewLostFocus(object _1, RoutedEventArgs _2)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
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)
|
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
if (e.Data.Get("MovedRepositoryTreeNode") is ViewModels.RepositoryNode moved)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
e.Handled = true;
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
if (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-07-14 00:55:15 -07:00
|
|
|
if (items != null)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
foreach (var item in items)
|
|
|
|
{
|
|
|
|
await OpenOrInitRepository(item.Path.LocalPath);
|
|
|
|
break;
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_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-07-14 00:55:15 -07:00
|
|
|
if (sender is not Grid grid)
|
2024-03-31 01:54:29 -07:00
|
|
|
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-07-14 00:55:15 -07:00
|
|
|
if (e.Data.Get("MovedRepositoryTreeNode") is ViewModels.RepositoryNode moved)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
e.Handled = true;
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
if (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-07-14 00:55:15 -07:00
|
|
|
if (items != null)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
foreach (var item in items)
|
|
|
|
{
|
|
|
|
await OpenOrInitRepository(item.Path.LocalPath, to);
|
|
|
|
break;
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_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-07-14 00:55:15 -07:00
|
|
|
var to = grid?.DataContext as ViewModels.RepositoryNode;
|
|
|
|
if (to is not { IsRepository: true })
|
2024-03-31 01:54:29 -07:00
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
var parent = this.FindAncestorOfType<Launcher>();
|
|
|
|
if (parent?.DataContext is ViewModels.Launcher launcher)
|
|
|
|
launcher.OpenRepositoryInTab(to, null);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private async void OpenLocalRepository(object _1, RoutedEventArgs e)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
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);
|
2024-07-14 00:55:15 -07:00
|
|
|
if (topLevel == null)
|
|
|
|
return;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
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-07-14 00:55:15 -07:00
|
|
|
|
|
|
|
e.Handled = true;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private Task OpenOrInitRepository(string path, ViewModels.RepositoryNode parent = null)
|
|
|
|
{
|
|
|
|
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-07-14 00:55:15 -07:00
|
|
|
var owner = this.FindAncestorOfType<Launcher>();
|
|
|
|
var launcher = owner?.DataContext as ViewModels.Launcher;
|
|
|
|
if (launcher == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var page = launcher.ActivePage;
|
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))
|
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
Dispatcher.UIThread.Invoke(() => (DataContext as ViewModels.Welcome)?.InitRepository(path, parent));
|
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
|
|
|
}
|