sourcegit/src/Views/Repository.axaml.cs

317 lines
11 KiB
C#
Raw Normal View History

using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace SourceGit.Views
{
public partial class Repository : UserControl
{
public Repository()
{
InitializeComponent();
}
2024-07-04 04:12:22 -07:00
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
UpdateLeftSidebarLayout();
2024-07-04 04:12:22 -07:00
}
private void OnSearchCommitPanelPropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e)
{
if (e.Property == IsVisibleProperty && sender is Grid { IsVisible: true })
2024-07-14 00:55:15 -07:00
TxtSearchCommitsBox.Focus();
}
private void OnSearchKeyDown(object _, KeyEventArgs e)
{
var repo = DataContext as ViewModels.Repository;
if (repo == null)
return;
if (e.Key == Key.Enter)
{
if (!string.IsNullOrWhiteSpace(repo.SearchCommitFilter))
repo.StartSearchCommits();
e.Handled = true;
}
else if (e.Key == Key.Down)
{
if (repo.IsSearchCommitSuggestionOpen)
{
SearchSuggestionBox.Focus(NavigationMethod.Tab);
SearchSuggestionBox.SelectedIndex = 0;
}
2024-07-31 01:26:58 -07:00
e.Handled = true;
}
else if (e.Key == Key.Escape)
{
if (repo.IsSearchCommitSuggestionOpen)
{
repo.SearchCommitFilterSuggestion.Clear();
repo.IsSearchCommitSuggestionOpen = false;
}
e.Handled = true;
}
}
private void OnSearchResultDataGridSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is DataGrid { SelectedItem: Models.Commit commit } && DataContext is ViewModels.Repository repo)
{
repo.NavigateToCommit(commit.SHA);
}
e.Handled = true;
}
private void OnBranchTreeRowsChanged(object _, RoutedEventArgs e)
{
UpdateLeftSidebarLayout();
e.Handled = true;
}
private void OnLocalBranchTreeSelectionChanged(object _1, RoutedEventArgs _2)
{
2024-07-14 00:55:15 -07:00
RemoteBranchTree.UnselectAll();
TagsList.UnselectAll();
}
private void OnRemoteBranchTreeSelectionChanged(object _1, RoutedEventArgs _2)
{
2024-07-14 00:55:15 -07:00
LocalBranchTree.UnselectAll();
TagsList.UnselectAll();
}
private void OnTagsRowsChanged(object _, RoutedEventArgs e)
{
UpdateLeftSidebarLayout();
e.Handled = true;
}
private void OnTagsSelectionChanged(object _1, RoutedEventArgs _2)
{
LocalBranchTree.UnselectAll();
RemoteBranchTree.UnselectAll();
}
private void OnSubmoduleContextRequested(object sender, ContextRequestedEventArgs e)
{
if (sender is DataGrid { SelectedItem: Models.Submodule submodule } grid && DataContext is ViewModels.Repository repo)
{
var menu = repo.CreateContextMenuForSubmodule(submodule.Path);
2024-07-14 00:55:15 -07:00
grid.OpenContextMenu(menu);
}
e.Handled = true;
}
2024-06-06 00:31:02 -07:00
2024-06-27 03:25:16 -07:00
private void OnDoubleTappedSubmodule(object sender, TappedEventArgs e)
{
if (sender is DataGrid { SelectedItem: Models.Submodule submodule } && DataContext is ViewModels.Repository repo)
2024-06-27 03:25:16 -07:00
{
repo.OpenSubmodule(submodule.Path);
2024-06-27 03:25:16 -07:00
}
e.Handled = true;
}
private void OnWorktreeContextRequested(object sender, ContextRequestedEventArgs e)
{
2024-07-14 00:55:15 -07:00
if (sender is DataGrid { SelectedItem: Models.Worktree worktree } grid && DataContext is ViewModels.Repository repo)
2024-06-27 03:25:16 -07:00
{
var menu = repo.CreateContextMenuForWorktree(worktree);
grid.OpenContextMenu(menu);
2024-06-27 03:25:16 -07:00
}
e.Handled = true;
}
private void OnDoubleTappedWorktree(object sender, TappedEventArgs e)
{
2024-07-14 00:55:15 -07:00
if (sender is DataGrid { SelectedItem: Models.Worktree worktree } && DataContext is ViewModels.Repository repo)
2024-06-27 03:25:16 -07:00
{
repo.OpenWorktree(worktree);
2024-06-27 03:25:16 -07:00
}
e.Handled = true;
}
private void OnLeftSidebarDataGridPropertyChanged(object _, AvaloniaPropertyChangedEventArgs e)
2024-07-04 04:12:22 -07:00
{
if (e.Property == DataGrid.ItemsSourceProperty || e.Property == DataGrid.IsVisibleProperty)
UpdateLeftSidebarLayout();
}
private void OnLeftSidebarSizeChanged(object _, SizeChangedEventArgs e)
{
if (e.HeightChanged)
UpdateLeftSidebarLayout();
2024-07-04 04:12:22 -07:00
}
private void UpdateLeftSidebarLayout()
{
var vm = DataContext as ViewModels.Repository;
if (vm == null || vm.Settings == null)
return;
if (!IsLoaded)
return;
var leftHeight = LeftSidebarGroups.Bounds.Height - 28.0 * 5 - 4;
2024-07-14 00:55:15 -07:00
var localBranchRows = vm.IsLocalBranchGroupExpanded ? LocalBranchTree.Rows.Count : 0;
var remoteBranchRows = vm.IsRemoteGroupExpanded ? RemoteBranchTree.Rows.Count : 0;
var desiredBranches = (localBranchRows + remoteBranchRows) * 24.0;
var desiredTag = vm.IsTagGroupExpanded ? 24.0 * TagsList.Rows : 0;
2024-07-14 00:55:15 -07:00
var desiredSubmodule = vm.IsSubmoduleGroupExpanded ? SubmoduleList.RowHeight * vm.Submodules.Count : 0;
var desiredWorktree = vm.IsWorktreeGroupExpanded ? WorktreeList.RowHeight * vm.Worktrees.Count : 0;
var desiredOthers = desiredTag + desiredSubmodule + desiredWorktree;
var hasOverflow = (desiredBranches + desiredOthers > leftHeight);
if (vm.IsTagGroupExpanded)
{
var height = desiredTag;
if (hasOverflow)
{
var test = leftHeight - desiredBranches - desiredSubmodule - desiredWorktree;
if (test < 0)
height = Math.Min(200, height);
else
height = Math.Max(200, test);
}
leftHeight -= height;
2024-07-14 00:55:15 -07:00
TagsList.Height = height;
hasOverflow = (desiredBranches + desiredSubmodule + desiredWorktree) > leftHeight;
}
if (vm.IsSubmoduleGroupExpanded)
{
var height = desiredSubmodule;
if (hasOverflow)
{
var test = leftHeight - desiredBranches - desiredWorktree;
if (test < 0)
height = Math.Min(200, height);
else
height = Math.Max(200, test);
}
leftHeight -= height;
2024-07-14 00:55:15 -07:00
SubmoduleList.Height = height;
hasOverflow = (desiredBranches + desiredWorktree) > leftHeight;
}
if (vm.IsWorktreeGroupExpanded)
{
var height = desiredWorktree;
if (hasOverflow)
{
var test = leftHeight - desiredBranches;
if (test < 0)
height = Math.Min(200, height);
else
height = Math.Max(200, test);
}
leftHeight -= height;
2024-07-14 00:55:15 -07:00
WorktreeList.Height = height;
}
if (desiredBranches > leftHeight)
{
var local = localBranchRows * 24.0;
var remote = remoteBranchRows * 24.0;
var half = leftHeight / 2;
if (vm.IsLocalBranchGroupExpanded)
{
if (vm.IsRemoteGroupExpanded)
{
if (local < half)
{
2024-07-14 00:55:15 -07:00
LocalBranchTree.Height = local;
RemoteBranchTree.Height = leftHeight - local;
}
else if (remote < half)
{
2024-07-14 00:55:15 -07:00
RemoteBranchTree.Height = remote;
LocalBranchTree.Height = leftHeight - remote;
}
else
{
2024-07-14 00:55:15 -07:00
LocalBranchTree.Height = half;
RemoteBranchTree.Height = half;
}
}
else
{
2024-07-14 00:55:15 -07:00
LocalBranchTree.Height = leftHeight;
}
}
else if (vm.IsRemoteGroupExpanded)
{
2024-07-14 00:55:15 -07:00
RemoteBranchTree.Height = leftHeight;
}
}
else
{
if (vm.IsLocalBranchGroupExpanded)
{
var height = localBranchRows * 24;
2024-07-14 00:55:15 -07:00
LocalBranchTree.Height = height;
}
if (vm.IsRemoteGroupExpanded)
{
var height = remoteBranchRows * 24;
2024-07-14 00:55:15 -07:00
RemoteBranchTree.Height = height;
}
}
}
private void OnSearchSuggestionBoxKeyDown(object _, KeyEventArgs e)
{
var repo = DataContext as ViewModels.Repository;
if (repo == null)
return;
if (e.Key == Key.Escape)
{
repo.IsSearchCommitSuggestionOpen = false;
repo.SearchCommitFilterSuggestion.Clear();
e.Handled = true;
}
else if (e.Key == Key.Enter && SearchSuggestionBox.SelectedItem is string content)
{
repo.SearchCommitFilter = content;
TxtSearchCommitsBox.CaretIndex = content.Length;
repo.StartSearchCommits();
e.Handled = true;
}
}
2024-07-31 01:26:58 -07:00
private void OnSearchSuggestionDoubleTapped(object sender, TappedEventArgs e)
{
var repo = DataContext as ViewModels.Repository;
if (repo == null)
return;
var content = (sender as StackPanel)?.DataContext as string;
if (!string.IsNullOrEmpty(content))
{
repo.SearchCommitFilter = content;
TxtSearchCommitsBox.CaretIndex = content.Length;
repo.StartSearchCommits();
}
e.Handled = true;
}
}
}