2021-09-12 23:53:24 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Controls;
|
2022-10-14 05:38:53 -07:00
|
|
|
using System.Windows.Controls.Primitives;
|
2021-09-12 23:53:24 -07:00
|
|
|
using System.Windows.Input;
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
|
|
namespace SourceGit.Views.Widgets {
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 新标签页
|
|
|
|
/// </summary>
|
|
|
|
public partial class Welcome : UserControl, Controls.IPopupContainer {
|
|
|
|
|
|
|
|
/// <summary>
|
2022-10-14 05:38:53 -07:00
|
|
|
/// 修改仓库标签颜色的回调
|
2021-09-12 23:53:24 -07:00
|
|
|
/// </summary>
|
2022-10-14 05:38:53 -07:00
|
|
|
public event Action<Models.Repository> OnBookmarkChanged;
|
2021-09-12 23:53:24 -07:00
|
|
|
|
|
|
|
public Welcome() {
|
|
|
|
InitializeComponent();
|
2022-10-14 05:38:53 -07:00
|
|
|
UpdateVisibles();
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#region POPUP_CONTAINER
|
|
|
|
public void Show(Controls.PopupWidget widget) {
|
|
|
|
popup.Show(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ShowAndStart(Controls.PopupWidget widget) {
|
|
|
|
popup.ShowAndStart(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateProgress(string message) {
|
|
|
|
popup.UpdateProgress(message);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region FUNC_EVENTS
|
|
|
|
private void OnOpenClicked(object sender, RoutedEventArgs e) {
|
|
|
|
var dialog = new Controls.FolderDialog();
|
|
|
|
if (dialog.ShowDialog() == true) CheckAndOpen(dialog.SelectedPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnOpenTerminalClicked(object sender, RoutedEventArgs e) {
|
|
|
|
if (MakeSureReady()) {
|
|
|
|
var bash = Path.Combine(Models.Preference.Instance.Git.Path, "..", "bash.exe");
|
|
|
|
if (!File.Exists(bash)) {
|
|
|
|
Models.Exception.Raise(App.Text("MissingBash"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-20 00:29:56 -07:00
|
|
|
Process.Start(new ProcessStartInfo {
|
|
|
|
FileName = bash,
|
|
|
|
UseShellExecute = true,
|
|
|
|
});
|
2021-09-12 23:53:24 -07:00
|
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnCloneClicked(object sender, RoutedEventArgs e) {
|
|
|
|
if (MakeSureReady()) new Popups.Clone().Show();
|
|
|
|
}
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
private void OnRemoveRepository(object sender, RoutedEventArgs e) {
|
|
|
|
var repo = (sender as Button).DataContext as Models.Repository;
|
|
|
|
Models.Preference.Instance.RemoveRepository(repo.Path);
|
|
|
|
UpdateVisibles();
|
2021-09-12 20:47:54 -07:00
|
|
|
e.Handled = true;
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
private void OnDoubleClickRepository(object sender, MouseButtonEventArgs e) {
|
|
|
|
OnOpenRepository(sender, e);
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
private void OnOpenRepository(object sender, RoutedEventArgs e) {
|
|
|
|
var repo = (sender as Control).DataContext as Models.Repository;
|
|
|
|
CheckAndOpen(repo.Path);
|
2021-09-12 20:47:54 -07:00
|
|
|
e.Handled = true;
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
private void OnChangeRepositoryBookmark(object sender, RoutedEventArgs e) {
|
|
|
|
var btn = (sender as Button);
|
|
|
|
var repo = btn.DataContext as Models.Repository;
|
|
|
|
|
|
|
|
var menu = new ContextMenu();
|
|
|
|
menu.Placement = PlacementMode.Bottom;
|
|
|
|
menu.PlacementTarget = btn;
|
|
|
|
menu.StaysOpen = false;
|
|
|
|
menu.Focusable = true;
|
|
|
|
|
|
|
|
for (int i = 0; i < Converters.IntToBookmarkBrush.COLORS.Length; i++) {
|
|
|
|
var icon = new System.Windows.Shapes.Path();
|
|
|
|
icon.Data = new EllipseGeometry(new Point(0, 0), 8, 8);
|
|
|
|
icon.Fill = i == 0 ? (FindResource("Brush.FG1") as Brush) : Converters.IntToBookmarkBrush.COLORS[i];
|
|
|
|
icon.Width = 12;
|
|
|
|
|
|
|
|
var mark = new MenuItem();
|
|
|
|
mark.Icon = icon;
|
|
|
|
mark.Header = $"{i}";
|
|
|
|
|
|
|
|
var refIdx = i;
|
|
|
|
mark.Click += (o, ev) => {
|
|
|
|
if (repo != null) {
|
|
|
|
repo.Bookmark = refIdx;
|
|
|
|
UpdateVisibles();
|
|
|
|
OnBookmarkChanged?.Invoke(repo);
|
|
|
|
}
|
2021-09-12 23:53:24 -07:00
|
|
|
ev.Handled = true;
|
|
|
|
};
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
menu.Items.Add(mark);
|
|
|
|
}
|
2021-09-12 23:53:24 -07:00
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
btn.ContextMenu = menu;
|
|
|
|
btn.ContextMenu.IsOpen = true;
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
2021-09-12 23:53:24 -07:00
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
private void OnOpenRepositoryTerminal(object sender, RoutedEventArgs e) {
|
|
|
|
var repo = (sender as Button).DataContext as Models.Repository;
|
|
|
|
var bash = Path.Combine(Models.Preference.Instance.Git.Path, "..", "bash.exe");
|
|
|
|
if (!File.Exists(bash)) {
|
|
|
|
Models.Exception.Raise(App.Text("MissingBash"));
|
|
|
|
return;
|
|
|
|
}
|
2021-09-12 23:53:24 -07:00
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
Process.Start(new ProcessStartInfo {
|
|
|
|
WorkingDirectory = repo.Path,
|
|
|
|
FileName = bash,
|
|
|
|
UseShellExecute = true,
|
|
|
|
});
|
|
|
|
}
|
2021-09-12 23:53:24 -07:00
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
private void OnSearchFilterChanged(object sender, TextChangedEventArgs e) {
|
|
|
|
UpdateVisibles();
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
#region DRAG_DROP
|
2021-09-12 23:53:24 -07:00
|
|
|
private void OnPageDragEnter(object sender, DragEventArgs e) {
|
2022-10-14 05:38:53 -07:00
|
|
|
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
|
2021-09-12 23:53:24 -07:00
|
|
|
dropArea.Visibility = Visibility.Visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnPageDragLeave(object sender, DragEventArgs e) {
|
|
|
|
dropArea.Visibility = Visibility.Hidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnPageDrop(object sender, DragEventArgs e) {
|
|
|
|
dropArea.Visibility = Visibility.Hidden;
|
|
|
|
}
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
private void OnDropFolder(object sender, DragEventArgs e) {
|
2021-09-12 23:53:24 -07:00
|
|
|
bool rebuild = false;
|
|
|
|
dropArea.Visibility = Visibility.Hidden;
|
|
|
|
|
|
|
|
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
|
|
|
|
if (!MakeSureReady()) return;
|
|
|
|
|
|
|
|
var paths = e.Data.GetData(DataFormats.FileDrop) as string[];
|
|
|
|
foreach (var path in paths) {
|
|
|
|
var dir = new Commands.QueryGitDir(path).Result();
|
|
|
|
if (dir != null) {
|
|
|
|
var root = new Commands.GetRepositoryRootPath(path).Result();
|
2022-10-14 05:38:53 -07:00
|
|
|
Models.Preference.Instance.AddRepository(root, dir);
|
2021-09-12 23:53:24 -07:00
|
|
|
rebuild = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
if (rebuild) UpdateVisibles();
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region DATA
|
2022-10-14 05:38:53 -07:00
|
|
|
public void UpdateVisibles() {
|
|
|
|
var visibles = new List<Models.Repository>();
|
|
|
|
var curFilter = filter.Text.ToLower();
|
2021-09-12 23:53:24 -07:00
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
if (string.IsNullOrEmpty(curFilter)) {
|
|
|
|
visibles.AddRange(Models.Preference.Instance.Repositories);
|
2021-09-14 19:23:21 -07:00
|
|
|
} else {
|
2022-10-14 05:38:53 -07:00
|
|
|
foreach (var repo in Models.Preference.Instance.Repositories) {
|
|
|
|
if (repo.Name.ToLower().Contains(curFilter, StringComparison.Ordinal) ||
|
|
|
|
repo.Path.ToLower().Contains(curFilter, StringComparison.Ordinal)) {
|
|
|
|
visibles.Add(repo);
|
|
|
|
}
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
repoList.ItemsSource = visibles;
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool MakeSureReady() {
|
|
|
|
if (!Models.Preference.Instance.IsReady) {
|
|
|
|
Models.Exception.Raise(App.Text("NotConfigured"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CheckAndOpen(string path) {
|
|
|
|
if (!MakeSureReady()) return;
|
|
|
|
|
|
|
|
if (!Directory.Exists(path)) {
|
|
|
|
Models.Exception.Raise(App.Text("PathNotFound", path));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var root = new Commands.GetRepositoryRootPath(path).Result();
|
|
|
|
if (root == null) {
|
|
|
|
new Popups.Init(path).Show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var gitDir = new Commands.QueryGitDir(root).Result();
|
2022-10-14 05:38:53 -07:00
|
|
|
var repo = Models.Preference.Instance.AddRepository(root, gitDir);
|
2021-09-12 23:53:24 -07:00
|
|
|
Models.Watcher.Open(repo);
|
2022-10-14 05:38:53 -07:00
|
|
|
UpdateVisibles();
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|