Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup contextmenu by caching #202

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/RepoM.App/Controls/AcrylicMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace RepoM.App.Controls;

public class AcrylicMenuItem : MenuItem
{
private RoutedEventHandler? _evt;
private static readonly Brush _solidColorBrush = new SolidColorBrush(Color.FromArgb(80, 0, 0, 0));

protected override void OnSubmenuOpened(RoutedEventArgs e)
Expand All @@ -31,7 +32,7 @@ private void BlurSubMenu()
return;
}

DependencyObject parent = container;
DependencyObject? parent = container;
var borderIndex = 0;

while (parent != null)
Expand All @@ -51,4 +52,22 @@ private void BlurSubMenu()

AcrylicHelper.EnableBlur(container);
}

public void SetClick(RoutedEventHandler routedEventHandler)
{
ClearClick();
Click += routedEventHandler;
_evt = routedEventHandler;
}

public void ClearClick()
{
if (_evt == null)
{
return;
}

Click -= _evt;
_evt = null;
}
}
73 changes: 53 additions & 20 deletions src/RepoM.App/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@
private readonly IUserMenuActionMenuFactory _userMenuActionFactory;
private readonly IAppDataPathProvider _appDataPathProvider;

private readonly List<Separator> _separators = new();
private readonly List<AcrylicMenuItem> _menuItems = new();
private int _separatorIndex = 0;
private int _menuItemIndex = 0;

private readonly AcrylicMenuItem _loadingMenuItem = new()
{
Header = "Loading ..",
IsEnabled = true,
};

public MainWindow(
IRepositoryInformationAggregator aggregator,
IRepositoryMonitor repositoryMonitor,
Expand Down Expand Up @@ -248,6 +259,7 @@

private async Task<bool> LstRepositoriesContextMenuOpeningAsync(ContextMenu ctxMenu)
{
ResetIndex();
if (lstRepositories.SelectedItem is not RepositoryViewModel vm)
{
return false;
Expand All @@ -267,19 +279,15 @@
// }

ctxMenu.Items.Clear();
ctxMenu.Items.Add(new AcrylicMenuItem
{
Header = "Loading ..",
IsEnabled = true,
});
ctxMenu.Items.Add(_loadingMenuItem);

await foreach (UserInterfaceRepositoryActionBase action in _userMenuActionFactory.CreateMenuAsync(vm.Repository).ConfigureAwait(true))
{
if (action is UserInterfaceSeparatorRepositoryAction)
{
if (items.Count > 0 && items[^1] is not Separator)
{
items.Add(new Separator());
items.Add(GetSeparator());
}
}
else if (action is DeferredSubActionsUserInterfaceRepositoryAction or UserInterfaceRepositoryAction)
Expand Down Expand Up @@ -496,7 +504,7 @@
{
if (action is RepositorySeparatorAction)
{
return new Separator();
return GetSeparator();
}

if (action is not RepositoryAction repositoryAction)
Expand Down Expand Up @@ -525,12 +533,10 @@
}
};

var item = new AcrylicMenuItem
{
Header = repositoryAction.Name,
IsEnabled = repositoryAction.CanExecute,
};
item.Click += new RoutedEventHandler(clickAction);
AcrylicMenuItem item = GetMenuItem();
item.Header = repositoryAction.Name;
item.IsEnabled = repositoryAction.CanExecute;
item.SetClick(new RoutedEventHandler(clickAction));

// this is a deferred submenu. We want to make sure that the context menu can pop up
// fast, while submenus are not evaluated yet. We don't want to make the context menu
Expand Down Expand Up @@ -577,7 +583,7 @@
{
if (action is UserInterfaceSeparatorRepositoryAction)
{
return new Separator();
return GetSeparator();
}

// UserInterfaceRepositoryAction
Expand Down Expand Up @@ -609,12 +615,10 @@
}
};

var item = new AcrylicMenuItem
{
Header = repositoryAction.Name,
IsEnabled = repositoryAction.CanExecute,
};
item.Click += new RoutedEventHandler(clickAction);
AcrylicMenuItem item = GetMenuItem();
item.Header = repositoryAction.Name;
item.IsEnabled = repositoryAction.CanExecute;
item.SetClick(new RoutedEventHandler(clickAction));

// this is a deferred submenu. We want to make sure that the context menu can pop up
// fast, while submenus are not evaluated yet. We don't want to make the context menu
Expand Down Expand Up @@ -665,7 +669,7 @@
// menu item. this item gets removed when the real subitems are created
item.Items.Add("Loading..");

async void SelfDetachingEventHandler1(object _, RoutedEventArgs evtArgs)

Check warning on line 672 in src/RepoM.App/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 672 in src/RepoM.App/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 672 in src/RepoM.App/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
item.SubmenuOpened -= SelfDetachingEventHandler1;
item.Items.Clear();
Expand Down Expand Up @@ -853,6 +857,35 @@
var item = (ListBoxItem)lstRepositories.ItemContainerGenerator.ContainerFromIndex(0);
item?.Focus();
}

private void ResetIndex()
{
_separatorIndex = 0;
_menuItemIndex = 0;
}

private Separator GetSeparator()
{
while (_separatorIndex >= _separators.Count)
{
_separators.Add(new Separator());
}

return _separators[_separatorIndex++];
}

private AcrylicMenuItem GetMenuItem()
{
while (_menuItemIndex >= _menuItems.Count)
{
_menuItems.Add(new AcrylicMenuItem());
}

AcrylicMenuItem result = _menuItems[_menuItemIndex++];
result.Items.Clear();
result.ClearClick();
return result;
}

public bool IsShown => Visibility == Visibility.Visible && IsActive;
}
Loading