sourcegit/src/Views/Welcome.axaml.cs

271 lines
8.7 KiB
C#
Raw Normal View History

using System.IO;
using System.Threading.Tasks;
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;
namespace SourceGit.Views
{
public partial class Welcome : UserControl
{
public Welcome()
{
InitializeComponent();
}
2024-07-14 00:55:15 -07:00
private void SetupTreeViewDragAndDrop(object sender, RoutedEventArgs _)
{
if (sender is TreeView view)
{
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 _)
{
if (sender is Grid grid)
{
DragDrop.SetAllowDrop(grid, true);
grid.AddHandler(DragDrop.DragOverEvent, DragOverTreeNode);
grid.AddHandler(DragDrop.DropEvent, DropOnTreeNode);
}
}
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);
grid.OpenContextMenu(menu);
e.Handled = true;
}
}
private void OnPointerPressedTreeNode(object sender, PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(sender as Visual).Properties.IsLeftButtonPressed)
{
_pressedTreeNode = true;
_startDragTreeNode = false;
_pressedTreeNodePosition = e.GetPosition(sender as Grid);
}
else
{
_pressedTreeNode = false;
_startDragTreeNode = false;
}
}
2024-07-14 00:55:15 -07:00
private void OnPointerReleasedOnTreeNode(object _1, PointerReleasedEventArgs _2)
{
_pressedTreeNode = false;
_startDragTreeNode = false;
}
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)
{
var delta = e.GetPosition(grid) - _pressedTreeNodePosition;
var sizeSquired = delta.X * delta.X + delta.Y * delta.Y;
if (sizeSquired < 64)
return;
_startDragTreeNode = true;
var data = new DataObject();
2024-07-14 00:55:15 -07:00
data.Set("MovedRepositoryTreeNode", node);
DragDrop.DoDragDrop(e, data, DragDropEffects.Move);
}
}
2024-07-14 00:55:15 -07:00
private void OnTreeViewLostFocus(object _1, RoutedEventArgs _2)
{
_pressedTreeNode = false;
_startDragTreeNode = false;
}
private void DragOverTreeView(object sender, DragEventArgs e)
{
if (e.Data.Contains("MovedRepositoryTreeNode") || e.Data.Contains(DataFormats.Files))
{
e.DragEffects = DragDropEffects.Move;
e.Handled = true;
}
else
{
e.DragEffects = DragDropEffects.None;
e.Handled = true;
}
}
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)
{
e.Handled = true;
2024-07-14 00:55:15 -07:00
if (DataContext is ViewModels.Welcome vm)
vm.MoveNode(moved, null);
}
else if (e.Data.Contains(DataFormats.Files))
{
e.Handled = true;
var items = e.Data.GetFiles();
2024-07-14 00:55:15 -07:00
if (items != null)
{
2024-07-14 00:55:15 -07:00
foreach (var item in items)
{
await OpenOrInitRepository(item.Path.LocalPath);
break;
}
}
}
_pressedTreeNode = false;
_startDragTreeNode = false;
}
private void DragOverTreeNode(object sender, DragEventArgs e)
{
if (e.Data.Contains("MovedRepositoryTreeNode") || e.Data.Contains(DataFormats.Files))
{
var grid = sender as Grid;
if (grid == null)
return;
var to = grid.DataContext as ViewModels.RepositoryNode;
if (to == null)
return;
if (to.IsRepository)
{
e.DragEffects = DragDropEffects.None;
e.Handled = true;
}
else
{
e.DragEffects = DragDropEffects.Move;
e.Handled = true;
}
}
}
private async void DropOnTreeNode(object sender, DragEventArgs e)
{
2024-07-14 00:55:15 -07:00
if (sender is not Grid grid)
return;
var to = grid.DataContext as ViewModels.RepositoryNode;
if (to == null || to.IsRepository)
{
e.Handled = true;
return;
}
2024-07-14 00:55:15 -07:00
if (e.Data.Get("MovedRepositoryTreeNode") is ViewModels.RepositoryNode moved)
{
e.Handled = true;
2024-07-14 00:55:15 -07:00
if (to != moved && DataContext is ViewModels.Welcome vm)
vm.MoveNode(moved, to);
}
else if (e.Data.Contains(DataFormats.Files))
{
e.Handled = true;
var items = e.Data.GetFiles();
2024-07-14 00:55:15 -07:00
if (items != null)
{
2024-07-14 00:55:15 -07:00
foreach (var item in items)
{
await OpenOrInitRepository(item.Path.LocalPath, to);
break;
}
}
}
_pressedTreeNode = false;
_startDragTreeNode = false;
}
private void OnDoubleTappedTreeNode(object sender, TappedEventArgs e)
{
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 })
return;
2024-07-14 00:55:15 -07:00
var parent = this.FindAncestorOfType<Launcher>();
if (parent?.DataContext is ViewModels.Launcher launcher)
launcher.OpenRepositoryInTab(to, null);
e.Handled = true;
}
2024-07-14 00:55:15 -07:00
private async void OpenLocalRepository(object _1, RoutedEventArgs e)
{
if (!ViewModels.PopupHost.CanCreatePopup())
return;
var topLevel = TopLevel.GetTopLevel(this);
2024-07-14 00:55:15 -07:00
if (topLevel == null)
return;
var options = new FolderPickerOpenOptions() { AllowMultiple = false };
var selected = await topLevel.StorageProvider.OpenFolderPickerAsync(options);
if (selected.Count == 1)
await OpenOrInitRepository(selected[0].Path.LocalPath);
2024-07-14 00:55:15 -07:00
e.Handled = true;
}
private Task OpenOrInitRepository(string path, ViewModels.RepositoryNode parent = null)
{
if (!Directory.Exists(path))
{
if (File.Exists(path))
path = Path.GetDirectoryName(path);
else
return null;
}
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;
return Task.Run(() =>
{
var root = new Commands.QueryRepositoryRootPath(path).Result();
if (string.IsNullOrEmpty(root))
{
2024-07-14 00:55:15 -07:00
Dispatcher.UIThread.Invoke(() => (DataContext as ViewModels.Welcome)?.InitRepository(path, parent));
return;
}
Dispatcher.UIThread.Invoke(() =>
{
var normalizedPath = root.Replace("\\", "/");
var node = ViewModels.Preference.FindOrAddNodeByRepositoryPath(normalizedPath, parent, true);
launcher.OpenRepositoryInTab(node, page);
});
});
}
private bool _pressedTreeNode = false;
private Point _pressedTreeNodePosition = new Point();
private bool _startDragTreeNode = false;
}
}