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>
|
2022-10-19 00:20:58 -07:00
|
|
|
public partial class Welcome : UserControl {
|
2021-09-12 23:53:24 -07:00
|
|
|
|
|
|
|
public Welcome() {
|
|
|
|
InitializeComponent();
|
2022-10-14 05:38:53 -07:00
|
|
|
UpdateVisibles();
|
2022-10-16 19:18:36 -07:00
|
|
|
Models.Theme.AddListener(this, UpdateVisibles);
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#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) {
|
2022-10-19 00:20:58 -07:00
|
|
|
if (MakeSureReady()) {
|
|
|
|
var dialog = new Clone();
|
|
|
|
dialog.Owner = App.Current.MainWindow;
|
|
|
|
dialog.ShowDialog();
|
|
|
|
}
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
|
2022-10-16 19:12:59 -07:00
|
|
|
private void FillSortMenu(ContextMenu menu, Models.Preference.SortMethod desired, string label) {
|
|
|
|
var item = new MenuItem();
|
|
|
|
item.Header = App.Text(label);
|
|
|
|
item.Click += (s, ev) => {
|
|
|
|
Models.Preference.Instance.General.SortBy = desired;
|
|
|
|
UpdateVisibles();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Models.Preference.Instance.General.SortBy == desired) {
|
|
|
|
var icon = new System.Windows.Shapes.Path();
|
|
|
|
icon.Data = FindResource("Icon.Check") as Geometry;
|
|
|
|
icon.Fill = FindResource("Brush.FG1") as Brush;
|
|
|
|
icon.Width = 12;
|
|
|
|
item.Icon = icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.Items.Add(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnSortMethodClicked(object sender, RoutedEventArgs e) {
|
|
|
|
var menu = new ContextMenu();
|
|
|
|
menu.Placement = PlacementMode.Bottom;
|
|
|
|
menu.PlacementTarget = sender as Button;
|
|
|
|
menu.StaysOpen = false;
|
|
|
|
menu.Focusable = true;
|
|
|
|
|
|
|
|
FillSortMenu(menu, Models.Preference.SortMethod.ByNameASC, "Sort.NameAsc");
|
|
|
|
FillSortMenu(menu, Models.Preference.SortMethod.ByNameDESC, "Sort.NameDesc");
|
|
|
|
FillSortMenu(menu, Models.Preference.SortMethod.ByRecentlyOpened, "Sort.RecentlyOpened");
|
2022-10-18 00:55:39 -07:00
|
|
|
FillSortMenu(menu, Models.Preference.SortMethod.ByBookmark, "Sort.Bookmark");
|
2022-10-16 19:12:59 -07:00
|
|
|
|
|
|
|
menu.IsOpen = true;
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
private void OnRemoveRepository(object sender, RoutedEventArgs e) {
|
|
|
|
var repo = (sender as Button).DataContext as Models.Repository;
|
2022-10-14 05:56:57 -07:00
|
|
|
if (repo == null) return;
|
|
|
|
|
2022-11-10 23:54:34 -08:00
|
|
|
var confirmDialog = new ConfirmDialog(
|
|
|
|
App.Text("Apply.Warn"),
|
|
|
|
App.Text("ConfirmRemoveRepo", repo.Path),
|
|
|
|
() => {
|
|
|
|
Models.Preference.Instance.RemoveRepository(repo.Path);
|
|
|
|
UpdateVisibles();
|
|
|
|
});
|
|
|
|
confirmDialog.Owner = App.Current.MainWindow;
|
|
|
|
confirmDialog.ShowDialog();
|
2021-09-12 20:47:54 -07:00
|
|
|
e.Handled = true;
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
|
2022-10-17 20:24:42 -07:00
|
|
|
private void OnDoubleClickRepository(object sender, MouseButtonEventArgs e) {
|
|
|
|
OnOpenRepository(sender, e);
|
|
|
|
}
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
private void OnOpenRepository(object sender, RoutedEventArgs e) {
|
|
|
|
var repo = (sender as Control).DataContext as Models.Repository;
|
2022-10-14 05:56:57 -07:00
|
|
|
if (repo == null) return;
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
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:49:02 -07:00
|
|
|
private void OnExploreRepository(object sender, RoutedEventArgs e) {
|
|
|
|
var repo = (sender as Control).DataContext as Models.Repository;
|
2022-10-14 05:56:57 -07:00
|
|
|
if (repo == null) return;
|
|
|
|
|
2022-10-14 05:49:02 -07:00
|
|
|
Process.Start("explorer", repo.Path);
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
private void OnOpenRepositoryTerminal(object sender, RoutedEventArgs e) {
|
|
|
|
var repo = (sender as Button).DataContext as Models.Repository;
|
2022-10-14 05:56:57 -07:00
|
|
|
if (repo == null) return;
|
|
|
|
|
2022-10-14 05:38:53 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
private void OnPageDrop(object sender, DragEventArgs e) {
|
|
|
|
bool rebuild = false;
|
|
|
|
|
|
|
|
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) {
|
2022-10-25 19:32:42 -07:00
|
|
|
if (repo.Name.ToLower().IndexOf(curFilter, StringComparison.Ordinal) >= 0 ||
|
|
|
|
repo.Path.ToLower().IndexOf(curFilter, StringComparison.Ordinal) >= 0) {
|
2022-10-14 05:38:53 -07:00
|
|
|
visibles.Add(repo);
|
|
|
|
}
|
2021-09-12 23:53:24 -07:00
|
|
|
}
|
|
|
|
}
|
2022-10-16 19:12:59 -07:00
|
|
|
|
|
|
|
switch (Models.Preference.Instance.General.SortBy) {
|
|
|
|
case Models.Preference.SortMethod.ByNameASC:
|
|
|
|
visibles.Sort((l, r) => l.Name.CompareTo(r.Name));
|
|
|
|
break;
|
|
|
|
case Models.Preference.SortMethod.ByNameDESC:
|
|
|
|
visibles.Sort((l, r) => r.Name.CompareTo(l.Name));
|
|
|
|
break;
|
2022-10-18 00:55:39 -07:00
|
|
|
case Models.Preference.SortMethod.ByRecentlyOpened:
|
2022-10-16 19:12:59 -07:00
|
|
|
visibles.Sort((l, r) => r.LastOpenTime.CompareTo(l.LastOpenTime));
|
|
|
|
break;
|
2022-10-18 00:55:39 -07:00
|
|
|
default:
|
2023-07-18 00:00:27 -07:00
|
|
|
visibles.Sort((l, r) => r.Bookmark - l.Bookmark);
|
2022-10-18 00:55:39 -07:00
|
|
|
break;
|
2022-10-16 19:12:59 -07:00
|
|
|
}
|
2021-09-12 23:53:24 -07:00
|
|
|
|
2023-07-18 00:00:27 -07:00
|
|
|
mask.Visibility = visibles.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
|
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
|
|
|
|
}
|
|
|
|
}
|