2024-03-17 18:37:06 -07:00
|
|
|
using System.IO;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia;
|
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Input;
|
|
|
|
using Avalonia.Interactivity;
|
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-08-22 00:45:50 -07:00
|
|
|
private void OnSearchBoxKeyDown(object sender, KeyEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Key == Key.Down || e.Key == Key.FnDownArrow)
|
|
|
|
{
|
|
|
|
var containers = ReposTree.GetRealizedContainers();
|
|
|
|
if (containers == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
foreach (var c in containers)
|
|
|
|
{
|
|
|
|
if (c is TreeViewItem { IsVisible: true } item)
|
|
|
|
{
|
|
|
|
ReposTree.SelectedItem = item.DataContext;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTreeViewKeyDown(object sender, KeyEventArgs e)
|
|
|
|
{
|
2024-08-22 03:11:25 -07:00
|
|
|
if (ReposTree.SelectedItem is ViewModels.RepositoryNode node)
|
2024-08-22 00:45:50 -07:00
|
|
|
{
|
2024-08-22 03:11:25 -07:00
|
|
|
if (e.Key == Key.Space && node.IsRepository)
|
|
|
|
{
|
|
|
|
var parent = this.FindAncestorOfType<Launcher>();
|
|
|
|
if (parent?.DataContext is ViewModels.Launcher launcher)
|
|
|
|
launcher.OpenRepositoryInTab(node, null);
|
2024-08-22 00:45:50 -07:00
|
|
|
|
2024-08-22 03:11:25 -07:00
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
else if (e.Key == Key.Down)
|
|
|
|
{
|
|
|
|
var next = ViewModels.Welcome.Instance.GetNextVisible(node);
|
|
|
|
if (next != null)
|
|
|
|
ReposTree.SelectedItem = next;
|
|
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
else if (e.Key == Key.Up)
|
|
|
|
{
|
|
|
|
var prev = ViewModels.Welcome.Instance.GetPrevVisible(node);
|
|
|
|
if (prev != null)
|
|
|
|
ReposTree.SelectedItem = prev;
|
|
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTreeViewSelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
|
|
{
|
|
|
|
if (ReposTree.SelectedItem is ViewModels.RepositoryNode node)
|
|
|
|
{
|
|
|
|
var item = FindTreeViewItemByNode(node, ReposTree);
|
|
|
|
item?.Focus(NavigationMethod.Directional);
|
2024-08-22 00:45:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 22:51:44 -07:00
|
|
|
private void OnTreeNodeContextRequested(object sender, ContextRequestedEventArgs e)
|
|
|
|
{
|
2024-07-22 00:34:31 -07:00
|
|
|
if (sender is Grid grid)
|
2024-04-27 22:51:44 -07:00
|
|
|
{
|
2024-07-22 00:34:31 -07:00
|
|
|
var menu = ViewModels.Welcome.Instance.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-07-14 09:30:31 -07:00
|
|
|
private void DropOnTreeView(object sender, DragEventArgs e)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
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-22 00:34:31 -07:00
|
|
|
ViewModels.Welcome.Instance.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)
|
|
|
|
{
|
2024-07-14 09:30:31 -07:00
|
|
|
OpenOrInitRepository(item.Path.LocalPath);
|
2024-07-14 00:55:15 -07:00
|
|
|
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-07-14 09:30:31 -07:00
|
|
|
private void DropOnTreeNode(object sender, DragEventArgs e)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
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-22 00:34:31 -07:00
|
|
|
if (to != moved)
|
|
|
|
ViewModels.Welcome.Instance.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)
|
|
|
|
{
|
2024-07-14 09:30:31 -07:00
|
|
|
OpenOrInitRepository(item.Path.LocalPath, to);
|
2024-07-14 00:55:15 -07:00
|
|
|
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 09:30:31 -07:00
|
|
|
private void OpenOrInitRepository(string path, ViewModels.RepositoryNode parent = null)
|
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
|
2024-07-14 09:30:31 -07:00
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-14 09:30:31 -07:00
|
|
|
var root = new Commands.QueryRepositoryRootPath(path).Result();
|
|
|
|
if (string.IsNullOrEmpty(root))
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-22 00:34:31 -07:00
|
|
|
ViewModels.Welcome.Instance.InitRepository(path, parent);
|
2024-07-14 09:30:31 -07:00
|
|
|
return;
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-14 09:30:31 -07:00
|
|
|
var normalizedPath = root.Replace("\\", "/");
|
2024-07-22 00:34:31 -07:00
|
|
|
var node = ViewModels.Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, parent, true);
|
2024-07-14 09:30:31 -07:00
|
|
|
var launcher = this.FindAncestorOfType<Launcher>()?.DataContext as ViewModels.Launcher;
|
|
|
|
launcher?.OpenRepositoryInTab(node, launcher.ActivePage);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-08-22 03:11:25 -07:00
|
|
|
private TreeViewItem FindTreeViewItemByNode(ViewModels.RepositoryNode node, ItemsControl container)
|
|
|
|
{
|
|
|
|
var items = container.GetRealizedContainers();
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
|
|
|
if (item is TreeViewItem { DataContext: ViewModels.RepositoryNode test } treeViewItem)
|
|
|
|
{
|
|
|
|
if (test == node)
|
|
|
|
return treeViewItem;
|
|
|
|
|
|
|
|
var child = FindTreeViewItemByNode(node, treeViewItem);
|
|
|
|
if (child != null)
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|