2024-08-22 06:10:23 -07:00
|
|
|
using System;
|
2024-03-17 18:37:06 -07:00
|
|
|
using System.IO;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia;
|
|
|
|
using Avalonia.Controls;
|
2024-08-22 06:10:23 -07:00
|
|
|
using Avalonia.Controls.Primitives;
|
2024-02-05 23:08:37 -08:00
|
|
|
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
|
|
|
|
{
|
2024-08-22 06:10:23 -07:00
|
|
|
public class RepositoryTreeNodeToggleButton : ToggleButton
|
|
|
|
{
|
|
|
|
protected override Type StyleKeyOverride => typeof(ToggleButton);
|
|
|
|
|
|
|
|
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed &&
|
|
|
|
DataContext is ViewModels.RepositoryNode { IsRepository: false } node)
|
|
|
|
{
|
|
|
|
ViewModels.Welcome.Instance.ToggleNodeIsExpanded(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public partial class Welcome : UserControl
|
|
|
|
{
|
|
|
|
public Welcome()
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
|
2024-08-22 06:10:23 -07:00
|
|
|
protected override void OnKeyDown(KeyEventArgs e)
|
|
|
|
{
|
|
|
|
base.OnKeyDown(e);
|
|
|
|
|
|
|
|
if (!e.Handled)
|
|
|
|
{
|
|
|
|
if (e.Key == Key.Down && ViewModels.Welcome.Instance.Rows.Count > 0)
|
|
|
|
{
|
|
|
|
TreeContainer.SelectedIndex = 0;
|
|
|
|
TreeContainer.Focus(NavigationMethod.Directional);
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
2024-08-22 19:39:09 -07:00
|
|
|
else if (e.Key == Key.Escape)
|
|
|
|
{
|
|
|
|
ViewModels.Welcome.Instance.ClearSearchFilter();
|
|
|
|
e.Handled = true;
|
2024-08-22 06:10:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private void SetupTreeViewDragAndDrop(object sender, RoutedEventArgs _)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-08-22 06:10:23 -07:00
|
|
|
if (sender is ListBox view)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
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 OnTreeViewKeyDown(object sender, KeyEventArgs e)
|
|
|
|
{
|
2024-08-22 06:10:23 -07:00
|
|
|
if (TreeContainer.SelectedItem is ViewModels.RepositoryNode node && e.Key == Key.Enter)
|
2024-08-22 00:45:50 -07:00
|
|
|
{
|
2024-08-22 06:10:23 -07:00
|
|
|
if (node.IsRepository)
|
2024-08-22 03:11:25 -07:00
|
|
|
{
|
|
|
|
var parent = this.FindAncestorOfType<Launcher>();
|
2024-08-22 06:10:23 -07:00
|
|
|
if (parent is { DataContext: ViewModels.Launcher launcher })
|
2024-08-22 03:11:25 -07:00
|
|
|
launcher.OpenRepositoryInTab(node, null);
|
|
|
|
}
|
2024-08-22 06:10:23 -07:00
|
|
|
else
|
2024-08-22 03:11:25 -07:00
|
|
|
{
|
2024-08-22 06:10:23 -07:00
|
|
|
ViewModels.Welcome.Instance.ToggleNodeIsExpanded(node);
|
2024-08-22 03:11:25 -07:00
|
|
|
}
|
|
|
|
|
2024-08-22 06:10:23 -07:00
|
|
|
e.Handled = true;
|
2024-08-22 00:45:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 22:51:44 -07:00
|
|
|
private void OnTreeNodeContextRequested(object sender, ContextRequestedEventArgs e)
|
|
|
|
{
|
2024-08-22 06:23:05 -07:00
|
|
|
if (sender is Grid { DataContext: ViewModels.RepositoryNode node } grid)
|
2024-04-27 22:51:44 -07:00
|
|
|
{
|
2024-08-22 06:23:05 -07:00
|
|
|
var menu = ViewModels.Welcome.Instance.CreateContextMenu(node);
|
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-08-22 06:10:23 -07:00
|
|
|
if (sender is Grid { DataContext: ViewModels.RepositoryNode node })
|
|
|
|
{
|
|
|
|
if (node.IsRepository)
|
|
|
|
{
|
|
|
|
var parent = this.FindAncestorOfType<Launcher>();
|
|
|
|
if (parent is { DataContext: ViewModels.Launcher launcher })
|
|
|
|
launcher.OpenRepositoryInTab(node, null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ViewModels.Welcome.Instance.ToggleNodeIsExpanded(node);
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-08-22 06:10:23 -07:00
|
|
|
e.Handled = true;
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
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-08-27 00:35:10 -07:00
|
|
|
var test = new Commands.QueryRepositoryRootPath(path).ReadToEnd();
|
|
|
|
if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut))
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-08-27 00:35:10 -07:00
|
|
|
ViewModels.Welcome.Instance.InitRepository(path, parent, test.StdErr);
|
2024-07-14 09:30:31 -07:00
|
|
|
return;
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-08-27 00:35:10 -07:00
|
|
|
var normalizedPath = test.StdOut.Trim().Replace("\\", "/");
|
2024-07-22 00:34:31 -07:00
|
|
|
var node = ViewModels.Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, parent, true);
|
2024-08-22 06:10:23 -07:00
|
|
|
ViewModels.Welcome.Instance.Refresh();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
private bool _pressedTreeNode = false;
|
|
|
|
private Point _pressedTreeNodePosition = new Point();
|
|
|
|
private bool _startDragTreeNode = false;
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
}
|