Skip to content

Commit

Permalink
Share gitRefs when reloading after changing sort criterias
Browse files Browse the repository at this point in the history
  • Loading branch information
gerhardol committed Mar 15, 2022
1 parent be68cc8 commit 15c67e6
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 47 deletions.
4 changes: 2 additions & 2 deletions GitUI/BranchTreePanel/RepoObjectsTree.BranchTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ protected override async Task<Nodes> LoadNodesAsync(CancellationToken token, Fun
}

/// <inheritdoc/>
protected internal override void Refresh()
protected internal override void Refresh(Func<RefsFilter, IReadOnlyList<IGitRef>> getRefs)
{
// Break the local cache to ensure the data is requeried to reflect the required sort order.
_loadedBranches = null;

base.Refresh();
base.Refresh(getRefs);
}

private Nodes FillBranchTree(IReadOnlyList<IGitRef> branches, CancellationToken token)
Expand Down
15 changes: 3 additions & 12 deletions GitUI/BranchTreePanel/RepoObjectsTree.ContextActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using GitUI.BranchTreePanel.Interfaces;
using GitUI.CommandsDialogs;
using GitUI.UserControls.RevisionGrid;
using GitUIPluginInterfaces;
using ResourceManager;

namespace GitUI.BranchTreePanel
Expand Down Expand Up @@ -254,18 +255,8 @@ private void RegisterContextActions()
AddContextMenuItems(menuBranch, _menuBranchCopyContextMenuItems);
AddContextMenuItems(menuRemote, _menuRemoteCopyContextMenuItems);

_sortOrderContextMenuItem = new GitRefsSortOrderContextMenuItem(() =>
{
_branchesTree.Refresh();
_remotesTree.Refresh();
_tagTree.Refresh();
});
_sortByContextMenuItem = new GitRefsSortByContextMenuItem(() =>
{
_branchesTree.Refresh();
_remotesTree.Refresh();
_tagTree.Refresh();
});
_sortOrderContextMenuItem = new GitRefsSortOrderContextMenuItem(() => Refresh(new FilteredGitRefsProvider(UICommands.GitModule).GetRefs));
_sortByContextMenuItem = new GitRefsSortByContextMenuItem(() => Refresh(new FilteredGitRefsProvider(UICommands.GitModule).GetRefs));

_localBranchMenuItems = new LocalBranchMenuItems<LocalBranchNode>(this);
AddContextMenuItems(menuBranch, _localBranchMenuItems.Select(s => s.Item), insertAfter: _menuBranchCopyContextMenuItems[1]);
Expand Down
4 changes: 2 additions & 2 deletions GitUI/BranchTreePanel/RepoObjectsTree.Nodes.Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ protected override Task PostRepositoryChangedAsync(GitUIEventArgs e)
}

/// <inheritdoc/>
protected internal override void Refresh()
protected internal override void Refresh(Func<RefsFilter, IReadOnlyList<IGitRef>> getRefs)
{
// Break the local cache to ensure the data is requeried to reflect the required sort order.
_loadedTags = null;

base.Refresh();
base.Refresh(getRefs);
}

protected override async Task<Nodes> LoadNodesAsync(CancellationToken token, Func<RefsFilter, IReadOnlyList<IGitRef>> getRefs)
Expand Down
8 changes: 4 additions & 4 deletions GitUI/BranchTreePanel/RepoObjectsTree.Nodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,15 @@ protected virtual void OnDetached()
/// <summary>
/// Requests to refresh the data tree and to apply filtering, if necessary.
/// </summary>
protected internal virtual void Refresh()
protected internal virtual void Refresh(Func<RefsFilter, IReadOnlyList<IGitRef>> getRefs)
{
// NOTE: descendants may need to break their local caches to ensure the latest data is loaded.

ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
IsFiltering.Value = _isCurrentlyFiltering;
await ReloadNodesAsync(LoadNodesAsync, new FilteredGitRefsProvider(Module).GetRefs);
await ReloadNodesAsync(LoadNodesAsync, getRefs);
});
}

Expand All @@ -242,7 +242,7 @@ protected internal virtual void Refresh()
/// <param name="isFiltering">
/// <see langword="true"/>, if the data is being filtered; otherwise <see langword="false"/>.
/// </param>
internal void ToggleFilterMode(bool isFiltering)
internal void Refresh(bool isFiltering, Func<RefsFilter, IReadOnlyList<IGitRef>> getRefs)
{
// If we're not currently filtering and no need to filter now -> exit.
// Else we need to iterate over the list and rebind the tree - whilst there
Expand All @@ -262,7 +262,7 @@ internal void ToggleFilterMode(bool isFiltering)
{
IsFiltering.Value = true;
await ReloadNodesAsync(LoadNodesAsync, new FilteredGitRefsProvider(Module).GetRefs);
await ReloadNodesAsync(LoadNodesAsync, getRefs);
});
}

Expand Down
4 changes: 2 additions & 2 deletions GitUI/BranchTreePanel/RepoObjectsTree.RemoteBranchTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ protected override async Task<Nodes> LoadNodesAsync(CancellationToken token, Fun
}

/// <inheritdoc/>
protected internal override void Refresh()
protected internal override void Refresh(Func<RefsFilter, IReadOnlyList<IGitRef>> getRefs)
{
// Break the local cache to ensure the data is requeried to reflect the required sort order.
_loadedBranches = null;

base.Refresh();
base.Refresh(getRefs);
}

private async Task<Nodes> FillBranchTreeAsync(IReadOnlyList<IGitRef> branches, CancellationToken token)
Expand Down
16 changes: 12 additions & 4 deletions GitUI/BranchTreePanel/RepoObjectsTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,19 @@ public void Initialize(IAheadBehindDataProvider? aheadBehindDataProvider, Action
/// <param name="isFiltering">
/// <see langword="true"/>, if the data is being filtered; otherwise <see langword="false"/>.
/// </param>
public void ToggleFilterMode(bool isFiltering)
/// <param name="getRefs">Function to get refs.</param>
public void Refresh(bool isFiltering, Func<RefsFilter, IReadOnlyList<IGitRef>> getRefs)
{
_branchesTree.ToggleFilterMode(isFiltering);
_remotesTree.ToggleFilterMode(isFiltering);
_tagTree.ToggleFilterMode(isFiltering);
_branchesTree.Refresh(isFiltering, getRefs);
_remotesTree.Refresh(isFiltering, getRefs);
_tagTree.Refresh(isFiltering, getRefs);
}

public void Refresh(Func<RefsFilter, IReadOnlyList<IGitRef>> getRefs)
{
_branchesTree.Refresh(getRefs);
_remotesTree.Refresh(getRefs);
_tagTree.Refresh(getRefs);
}

public void SelectionChanged(IReadOnlyList<GitRevision> selectedRevisions)
Expand Down
2 changes: 1 addition & 1 deletion GitUI/CommandsDialogs/FormBrowse.InitRevisionGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void InitRevisionGrid(ObjectId? selectedId, ObjectId? firstId, bool isBl
// b) filter on specific branch
bool isFiltering = !AppSettings.ShowReflogReferences
&& (AppSettings.ShowCurrentBranchOnly || AppSettings.BranchFilterEnabled);
repoObjectsTree.ToggleFilterMode(isFiltering);
repoObjectsTree.Refresh(isFiltering, e.GetRefs);
};
RevisionGrid.SelectionChanged += (sender, e) =>
{
Expand Down
24 changes: 12 additions & 12 deletions GitUI/UserControls/RevisionGrid/RevisionGridControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public sealed partial class RevisionGridControl : GitModuleControl, IScriptHostC
{
public event EventHandler<DoubleClickRevisionEventArgs>? DoubleClickRevision;
public event EventHandler<FilterChangedEventArgs>? FilterChanged;
public event EventHandler? RevisionGraphLoaded;
public event EventHandler<GitUIEventArgs>? RevisionGraphLoaded;
public event EventHandler? SelectionChanged;

/// <summary>
Expand Down Expand Up @@ -301,7 +301,7 @@ protected override void Dispose(bool disposing)
/// This is used to remove spinners added when loading and to replace the gridview at errors.
/// </summary>
/// <param name="content">The content to show.</param>
private void SetPage(Control content)
private void SetPage(Control content, Lazy<IReadOnlyList<IGitRef>> getRefs)
{
for (int i = Controls.Count - 1; i >= 0; i--)
{
Expand All @@ -317,7 +317,7 @@ private void SetPage(Control content)
Controls.Add(content);
}

RevisionGraphLoaded?.Invoke(content == _gridView ? _gridView : null, EventArgs.Empty);
RevisionGraphLoaded?.Invoke(content == _gridView ? _gridView : null, new GitUIEventArgs(this, UICommands, getRefs));
}

internal int DrawColumnText(DataGridViewCellPaintingEventArgs e, string text, Font font, Color color, Rectangle bounds, bool useEllipsis = true)
Expand Down Expand Up @@ -402,21 +402,21 @@ public void SetAndApplyPathFilter(string filter)
PerformRefreshRevisions();
}

private void InitiateRefAction(IReadOnlyList<IGitRef>? refs, Action<IGitRef> action, FormQuickGitRefSelector.Action actionLabel)
private void InitiateRefAction(IReadOnlyList<IGitRef>? gitRefs, Action<IGitRef> action, FormQuickGitRefSelector.Action actionLabel)
{
if (refs is null || refs.Count < 1)
if (gitRefs is null || gitRefs.Count < 1)
{
return;
}

if (refs.Count == 1)
if (gitRefs.Count == 1)
{
action(refs[0]);
action(gitRefs[0]);
return;
}

using FormQuickGitRefSelector dlg = new();
dlg.Init(actionLabel, refs);
dlg.Init(actionLabel, gitRefs);
dlg.Location = GetQuickItemSelectorLocation();
if (dlg.ShowDialog(ParentForm) != DialogResult.OK || dlg.SelectedRef is null)
{
Expand Down Expand Up @@ -1010,7 +1010,7 @@ public void PerformRefreshRevisions(Func<RefsFilter, IReadOnlyList<IGitRef>> get
}
catch
{
SetPage(new ErrorControl());
SetPage(new ErrorControl(), getUnfilteredRefs);
throw;
}

Expand Down Expand Up @@ -1241,7 +1241,7 @@ void AddArtificialRevisions(bool insertAsFirst)
void OnRevisionReaderError(Exception exception)
{
// This has to happen on the UI thread
this.InvokeAsync(() => SetPage(new ErrorControl()))
this.InvokeAsync(() => SetPage(new ErrorControl(), getUnfilteredRefs))
.FileAndForget();

_refreshRevisionsSequence.CancelCurrent();
Expand All @@ -1267,7 +1267,7 @@ void OnRevisionReadCompleted()
this.InvokeAsync(
() =>
{
SetPage(new EmptyRepoControl(Module.IsBareRepository()));
SetPage(new EmptyRepoControl(Module.IsBareRepository()), getUnfilteredRefs);
_isRefreshingRevisions = false;
})
.FileAndForget();
Expand All @@ -1280,7 +1280,7 @@ void OnRevisionReadCompleted()
await this.SwitchToMainThreadAsync();
_gridView.LoadingCompleted();
SetPage(_gridView);
SetPage(_gridView, getUnfilteredRefs);
_isRefreshingRevisions = false;
CheckAndRepairInitialRevision();
HighlightRevisionsByAuthor(GetSelectedRevisions());
Expand Down
13 changes: 9 additions & 4 deletions Plugins/GitUIPluginInterfaces/FilteredGitRefsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ public sealed class FilteredGitRefsProvider : IFilteredGitRefsProvider
{
public FilteredGitRefsProvider(IGitModule module)
{
_refs = new(() => module.GetRefs(RefsFilter.NoFilter));
_getRefs = new(() => module.GetRefs(RefsFilter.NoFilter));
}

private readonly Lazy<IReadOnlyList<IGitRef>> _refs;
public FilteredGitRefsProvider(Lazy<IReadOnlyList<IGitRef>> getRefs)
{
_getRefs = getRefs;
}

private readonly Lazy<IReadOnlyList<IGitRef>> _getRefs;

/// <inherit/>
public IReadOnlyList<IGitRef> GetRefs(RefsFilter filter)
{
if (filter == RefsFilter.NoFilter)
{
return _refs.Value;
return _getRefs.Value;
}

return _refs.Value.Where(r =>
return _getRefs.Value.Where(r =>
((filter & RefsFilter.Tags) != 0 && r.IsTag)
|| ((filter & RefsFilter.Remotes) != 0 && r.IsRemote)
|| ((filter & RefsFilter.Heads) != 0 && r.IsHead)).ToList();
Expand Down
17 changes: 13 additions & 4 deletions Plugins/GitUIPluginInterfaces/GitUIEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace GitUIPluginInterfaces
{
public class GitUIEventArgs : CancelEventArgs
{
private readonly IFilteredGitRefsProvider _refs;
private readonly IFilteredGitRefsProvider _getRefs;

public GitUIEventArgs(IWin32Window? ownerForm, IGitUICommands gitUICommands, Lazy<IReadOnlyList<IGitRef>> getRefs)
: base(cancel: false)
{
OwnerForm = ownerForm;
GitUICommands = gitUICommands;
_getRefs = new FilteredGitRefsProvider(getRefs);
}

public GitUIEventArgs(IWin32Window? ownerForm, IGitUICommands gitUICommands)
: base(cancel: false)
{
OwnerForm = ownerForm;
GitUICommands = gitUICommands;
_refs = new FilteredGitRefsProvider(GitModule);
_getRefs = new FilteredGitRefsProvider(GitModule);
}

public IGitUICommands GitUICommands { get; }
Expand All @@ -22,6 +31,6 @@ public GitUIEventArgs(IWin32Window? ownerForm, IGitUICommands gitUICommands)

public IGitModule GitModule => GitUICommands.GitModule;

public IReadOnlyList<IGitRef> GetRefs(RefsFilter filter) => _refs.GetRefs(filter);
public IReadOnlyList<IGitRef> GetRefs(RefsFilter filter) => _getRefs.GetRefs(filter);
}
}

0 comments on commit 15c67e6

Please sign in to comment.