Skip to content

Commit

Permalink
Fix Dashboard hang when restoring uninstalled widgets (#2256)
Browse files Browse the repository at this point in the history
  • Loading branch information
krschau authored Feb 14, 2024
1 parent 638373b commit eb5273b
Showing 1 changed file with 16 additions and 13 deletions.
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 @@ -248,16 +248,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)
{
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 @@ -276,14 +286,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 @@ -587,8 +589,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

0 comments on commit eb5273b

Please sign in to comment.