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

Fix Dashboard hang when restoring uninstalled widgets #2256

Merged
merged 3 commits into from
Feb 14, 2024
Merged
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
29 changes: 16 additions & 13 deletions tools/Dashboard/DevHome.Dashboard/Views/DashboardView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,26 @@ private async Task RestorePinnedWidgetsAsync(Widget[] hostWidgets)
}
}

// Merge the dictionaries for easier looping. restoredWidgetsWithoutPosition should be empty, so this should be fast.
var lastOrderedKey = restoredWidgetsWithPosition.Count > 0 ? restoredWidgetsWithPosition.Last().Key : -1;
restoredWidgetsWithoutPosition.ToList().ForEach(x => restoredWidgetsWithPosition.Add(++lastOrderedKey, x.Value));

// Now that we've ordered the widgets, put them in their final collection.
var finalPlace = 0;
foreach (var orderedWidget in restoredWidgetsWithPosition)
krschau marked this conversation as resolved.
Show resolved Hide resolved
{
await PlaceWidget(orderedWidget, finalPlace++);
var widget = orderedWidget.Value;
var size = await widget.GetSizeAsync();
await InsertWidgetInPinnedWidgetsAsync(widget, size, finalPlace++);
}

foreach (var orderedWidget in restoredWidgetsWithoutPosition)
// Go through the newly created list of pinned widgets and update any positions that may have changed.
// For example, if the provider for the widget at position 0 was deleted, the widget at position 1
// should be updated to have position 0, etc.
var updatedPlace = 0;
foreach (var widget in PinnedWidgets)
{
await PlaceWidget(orderedWidget, finalPlace++);
await WidgetHelpers.SetPositionCustomStateAsync(widget.Widget, updatedPlace++);
}
}

Expand All @@ -270,14 +280,6 @@ private async Task DeleteAbandonedWidgetAsync(Widget widget)
Log.Logger()?.ReportInfo("DashboardView", $"After delete, {length} widgets for this host");
}

private async Task PlaceWidget(KeyValuePair<int, Widget> orderedWidget, int finalPlace)
{
var widget = orderedWidget.Value;
var size = await widget.GetSizeAsync();
await InsertWidgetInPinnedWidgetsAsync(widget, size, finalPlace);
await WidgetHelpers.SetPositionCustomStateAsync(widget, finalPlace);
}

private async Task PinDefaultWidgetsAsync()
{
var catalog = await ViewModel.WidgetHostingService.GetWidgetCatalogAsync();
Expand Down Expand Up @@ -581,8 +583,9 @@ private async void WidgetControl_Drop(object sender, DragEventArgs e)
// widgets between the starting and ending indices move up to replace the removed widget. If the widget was
// moved from a higher index to a lower one, then the order of removal and insertion doesn't matter.
PinnedWidgets.RemoveAt(draggedIndex);
var widgetPair = new KeyValuePair<int, Widget>(droppedIndex, draggedWidgetViewModel.Widget);
await PlaceWidget(widgetPair, droppedIndex);
var size = await draggedWidgetViewModel.Widget.GetSizeAsync();
await InsertWidgetInPinnedWidgetsAsync(draggedWidgetViewModel.Widget, size, droppedIndex);
await WidgetHelpers.SetPositionCustomStateAsync(draggedWidgetViewModel.Widget, droppedIndex);

// Update the CustomState Position of any widgets that were moved.
// The widget that has been dropped has already been updated, so don't do it again here.
Expand Down
Loading