2024-03-17 18:37:06 -07:00
|
|
|
|
using System;
|
2024-08-22 03:11:25 -07:00
|
|
|
|
using System.Collections.Generic;
|
2024-07-22 00:34:31 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
using Avalonia.Collections;
|
2024-04-27 22:51:44 -07:00
|
|
|
|
using Avalonia.Controls;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class Welcome : ObservableObject
|
|
|
|
|
{
|
2024-05-06 23:12:33 -07:00
|
|
|
|
public static Welcome Instance => _instance;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public AvaloniaList<RepositoryNode> RepositoryNodes
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => Preference.Instance.RepositoryNodes;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string SearchFilter
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _searchFilter;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (SetProperty(ref _searchFilter, value))
|
2024-07-22 00:34:31 -07:00
|
|
|
|
Refresh();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 02:36:43 -07:00
|
|
|
|
public void InitRepository(string path, RepositoryNode parent)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-07-22 00:34:31 -07:00
|
|
|
|
if (!Preference.Instance.IsGitConfigured())
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
App.RaiseException(PopupHost.Active.GetId(), App.Text("NotConfigured"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (PopupHost.CanCreatePopup())
|
|
|
|
|
{
|
2024-05-20 02:36:43 -07:00
|
|
|
|
PopupHost.ShowPopup(new Init(path, parent));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 00:13:59 -07:00
|
|
|
|
public void Clone()
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-07-22 00:34:31 -07:00
|
|
|
|
if (!Preference.Instance.IsGitConfigured())
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-05-30 00:13:59 -07:00
|
|
|
|
App.RaiseException(string.Empty, App.Text("NotConfigured"));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 23:32:27 -07:00
|
|
|
|
if (PopupHost.CanCreatePopup())
|
|
|
|
|
PopupHost.ShowPopup(new Clone());
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void OpenTerminal()
|
|
|
|
|
{
|
2024-07-22 00:34:31 -07:00
|
|
|
|
if (!Preference.Instance.IsGitConfigured())
|
2024-02-05 23:08:37 -08:00
|
|
|
|
App.RaiseException(PopupHost.Active.GetId(), App.Text("NotConfigured"));
|
2024-03-17 18:37:06 -07:00
|
|
|
|
else
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Native.OS.OpenTerminal(null);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void ClearSearchFilter()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SearchFilter = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 22:51:44 -07:00
|
|
|
|
public void AddRootNode()
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (PopupHost.CanCreatePopup())
|
|
|
|
|
PopupHost.ShowPopup(new CreateGroup(null));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void MoveNode(RepositoryNode from, RepositoryNode to)
|
|
|
|
|
{
|
2024-07-22 00:34:31 -07:00
|
|
|
|
Preference.Instance.MoveNode(from, to);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 03:11:25 -07:00
|
|
|
|
public RepositoryNode GetPrevVisible(RepositoryNode node)
|
|
|
|
|
{
|
|
|
|
|
var visibleRows = new List<RepositoryNode>();
|
|
|
|
|
CollectVisibleRows(visibleRows, RepositoryNodes);
|
|
|
|
|
|
|
|
|
|
var idx = visibleRows.IndexOf(node);
|
|
|
|
|
if (idx <= 1)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return visibleRows[idx - 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RepositoryNode GetNextVisible(RepositoryNode node)
|
|
|
|
|
{
|
|
|
|
|
var visibleRows = new List<RepositoryNode>();
|
|
|
|
|
CollectVisibleRows(visibleRows, RepositoryNodes);
|
|
|
|
|
|
|
|
|
|
var idx = visibleRows.IndexOf(node);
|
|
|
|
|
if (idx < 0 || idx >= visibleRows.Count - 1)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return visibleRows[idx + 1];
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 22:51:44 -07:00
|
|
|
|
public ContextMenu CreateContextMenu(RepositoryNode node)
|
|
|
|
|
{
|
|
|
|
|
var menu = new ContextMenu();
|
|
|
|
|
|
2024-06-17 20:07:48 -07:00
|
|
|
|
if (!node.IsRepository && node.SubNodes.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var openAll = new MenuItem();
|
|
|
|
|
openAll.Header = App.Text("Welcome.OpenAllInNode");
|
|
|
|
|
openAll.Icon = App.CreateMenuIcon("Icons.Folder.Open");
|
|
|
|
|
openAll.Click += (_, e) =>
|
|
|
|
|
{
|
2024-07-23 23:32:27 -07:00
|
|
|
|
OpenAllInNode(App.GetLauncer(), node);
|
2024-06-17 20:07:48 -07:00
|
|
|
|
e.Handled = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
menu.Items.Add(openAll);
|
|
|
|
|
menu.Items.Add(new MenuItem() { Header = "-" });
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 22:51:44 -07:00
|
|
|
|
var edit = new MenuItem();
|
|
|
|
|
edit.Header = App.Text("Welcome.Edit");
|
|
|
|
|
edit.Icon = App.CreateMenuIcon("Icons.Edit");
|
|
|
|
|
edit.Click += (_, e) =>
|
|
|
|
|
{
|
|
|
|
|
node.Edit();
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
};
|
|
|
|
|
menu.Items.Add(edit);
|
|
|
|
|
|
|
|
|
|
if (node.IsRepository)
|
|
|
|
|
{
|
|
|
|
|
var explore = new MenuItem();
|
|
|
|
|
explore.Header = App.Text("Repository.Explore");
|
2024-07-15 00:40:15 -07:00
|
|
|
|
explore.Icon = App.CreateMenuIcon("Icons.Explore");
|
2024-04-27 22:51:44 -07:00
|
|
|
|
explore.Click += (_, e) =>
|
|
|
|
|
{
|
|
|
|
|
node.OpenInFileManager();
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
};
|
|
|
|
|
menu.Items.Add(explore);
|
|
|
|
|
|
|
|
|
|
var terminal = new MenuItem();
|
|
|
|
|
terminal.Header = App.Text("Repository.Terminal");
|
|
|
|
|
terminal.Icon = App.CreateMenuIcon("Icons.Terminal");
|
|
|
|
|
terminal.Click += (_, e) =>
|
|
|
|
|
{
|
|
|
|
|
node.OpenTerminal();
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
};
|
|
|
|
|
menu.Items.Add(terminal);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var addSubFolder = new MenuItem();
|
|
|
|
|
addSubFolder.Header = App.Text("Welcome.AddSubFolder");
|
|
|
|
|
addSubFolder.Icon = App.CreateMenuIcon("Icons.Folder.Add");
|
|
|
|
|
addSubFolder.Click += (_, e) =>
|
|
|
|
|
{
|
|
|
|
|
node.AddSubFolder();
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
};
|
|
|
|
|
menu.Items.Add(addSubFolder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var delete = new MenuItem();
|
|
|
|
|
delete.Header = App.Text("Welcome.Delete");
|
|
|
|
|
delete.Icon = App.CreateMenuIcon("Icons.Clear");
|
|
|
|
|
delete.Click += (_, e) =>
|
|
|
|
|
{
|
|
|
|
|
node.Delete();
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
};
|
|
|
|
|
menu.Items.Add(delete);
|
|
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-22 00:34:31 -07:00
|
|
|
|
private void Refresh()
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(_searchFilter))
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
foreach (var node in RepositoryNodes)
|
|
|
|
|
ResetVisibility(node);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
foreach (var node in RepositoryNodes)
|
|
|
|
|
SetVisibilityBySearch(node);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private void ResetVisibility(RepositoryNode node)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
node.IsVisible = true;
|
2024-03-31 01:54:29 -07:00
|
|
|
|
foreach (var subNode in node.SubNodes)
|
|
|
|
|
ResetVisibility(subNode);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private void SetVisibilityBySearch(RepositoryNode node)
|
|
|
|
|
{
|
|
|
|
|
if (!node.IsRepository)
|
|
|
|
|
{
|
|
|
|
|
if (node.Name.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
node.IsVisible = true;
|
2024-03-31 01:54:29 -07:00
|
|
|
|
foreach (var subNode in node.SubNodes)
|
|
|
|
|
ResetVisibility(subNode);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
bool hasVisibleSubNode = false;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var subNode in node.SubNodes)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SetVisibilityBySearch(subNode);
|
|
|
|
|
hasVisibleSubNode |= subNode.IsVisible;
|
|
|
|
|
}
|
|
|
|
|
node.IsVisible = hasVisibleSubNode;
|
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
node.IsVisible = node.Name.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 20:07:48 -07:00
|
|
|
|
private void OpenAllInNode(Launcher launcher, RepositoryNode node)
|
|
|
|
|
{
|
|
|
|
|
foreach (var subNode in node.SubNodes)
|
|
|
|
|
{
|
|
|
|
|
if (subNode.IsRepository)
|
|
|
|
|
launcher.OpenRepositoryInTab(subNode, null);
|
|
|
|
|
else if (subNode.SubNodes.Count > 0)
|
|
|
|
|
OpenAllInNode(launcher, subNode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 03:11:25 -07:00
|
|
|
|
private void CollectVisibleRows(List<RepositoryNode> visible, AvaloniaList<RepositoryNode> collection)
|
|
|
|
|
{
|
|
|
|
|
foreach (var node in collection)
|
|
|
|
|
{
|
|
|
|
|
if (node.IsVisible)
|
|
|
|
|
{
|
|
|
|
|
visible.Add(node);
|
|
|
|
|
|
|
|
|
|
if (!node.IsRepository)
|
|
|
|
|
CollectVisibleRows(visible, node.SubNodes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 23:12:33 -07:00
|
|
|
|
private static Welcome _instance = new Welcome();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private string _searchFilter = string.Empty;
|
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|