2024-03-17 18:37:06 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
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-02-05 23:08:37 -08:00
|
|
|
|
Referesh();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void InitRepository(string path)
|
|
|
|
|
{
|
|
|
|
|
if (!Preference.Instance.IsGitConfigured)
|
|
|
|
|
{
|
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-02-05 23:08:37 -08:00
|
|
|
|
PopupHost.ShowPopup(new Init(path));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void Clone(object param)
|
|
|
|
|
{
|
2024-02-21 19:05:20 -08:00
|
|
|
|
var launcher = param as Launcher;
|
|
|
|
|
var page = launcher.ActivePage;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (!Preference.Instance.IsGitConfigured)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
App.RaiseException(page.GetId(), App.Text("NotConfigured"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (PopupHost.CanCreatePopup())
|
|
|
|
|
{
|
2024-02-21 19:05:20 -08:00
|
|
|
|
PopupHost.ShowPopup(new Clone(launcher, page));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void OpenTerminal()
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
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-02-05 23:08:37 -08:00
|
|
|
|
Preference.MoveNode(from, to);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 22:51:44 -07:00
|
|
|
|
public ContextMenu CreateContextMenu(RepositoryNode node)
|
|
|
|
|
{
|
|
|
|
|
var menu = new ContextMenu();
|
|
|
|
|
var hasRepo = Preference.FindRepository(node.Id) != null;
|
|
|
|
|
|
|
|
|
|
var edit = new MenuItem();
|
|
|
|
|
edit.Header = App.Text("Welcome.Edit");
|
|
|
|
|
edit.Icon = App.CreateMenuIcon("Icons.Edit");
|
|
|
|
|
edit.IsEnabled = !node.IsRepository || hasRepo;
|
|
|
|
|
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");
|
|
|
|
|
explore.Icon = App.CreateMenuIcon("Icons.Folder.Open");
|
|
|
|
|
explore.IsEnabled = hasRepo;
|
|
|
|
|
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.IsEnabled = hasRepo;
|
|
|
|
|
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-03-17 18:37:06 -07:00
|
|
|
|
private void Referesh()
|
|
|
|
|
{
|
|
|
|
|
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-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
|
|
|
|
}
|