Skip to content

Commit

Permalink
Fixes #993 by delaying forward of IsSimplified
Browse files Browse the repository at this point in the history
  • Loading branch information
batzen committed Dec 23, 2021
1 parent 5cd20f1 commit e1429bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 72 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- ### Bug fixes

- [#993](../../issues/993) - Layout issues in simplified ribbon
- [#995](../../issues/995) - Window initialization slows down after upgrade to 9.0

## 9.0.0
Expand Down
80 changes: 8 additions & 72 deletions Fluent.Ribbon/Controls/RibbonGroupBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ private void UpdateChildSizesOfUIElement(DependencyObject? element, RibbonGroupB
element = UIHelper.GetFirstVisualChild(element) ?? element;
}

UpdateIsSimplifiedOfUIElement(element, isSimplified);
RibbonProperties.SetAppropriateSize(element, groupBoxState, isSimplified);
}

Expand Down Expand Up @@ -668,55 +669,20 @@ private static void OnIsSimplifiedChanged(DependencyObject d, DependencyProperty
{
if (d is RibbonGroupBox ribbonGroupBox)
{
var isSimplified = (bool)e.NewValue;
ribbonGroupBox.UpdateChildIsSimplified(isSimplified);
}
}

private void UpdateChildIsSimplified(bool isSimplified)
{
foreach (var item in this.Items)
{
var element = this.ItemContainerGenerator.ContainerFromItem(item);
this.UpdateIsSimplifiedOfUIElement(element, isSimplified);
// We have to run this async in the dispatcher as the panel containing all controls is re-created
ribbonGroupBox.RunInDispatcherAsync(() =>
{
ribbonGroupBox.TryClearCacheAndResetStateAndScaleAndNotifyParentRibbonGroupsContainer();
ribbonGroupBox.updateChildSizesItemContainerGeneratorAction.QueueAction();
}, DispatcherPriority.Background);
}

// Use SizeDefinition or SimplifiedSizeDefinition depending on IsSimplified property to determine the child size.
this.UpdateChildSizes();
this.TryClearCacheAndResetStateAndScaleAndNotifyParentRibbonGroupsContainer();
}

private void UpdateIsSimplifiedOfUIElement(DependencyObject? element, bool isSimplified)
private static void UpdateIsSimplifiedOfUIElement(DependencyObject? element, bool isSimplified)
{
if (element is null)
{
return;
}

if (element is ISimplifiedStateControl simplifiedStateControl)
{
simplifiedStateControl.UpdateSimplifiedState(isSimplified);
return;
}

if (element is Panel panel)
{
for (int i = 0; i < panel.Children.Count; i++)
{
this.UpdateIsSimplifiedOfUIElement(panel.Children[i], isSimplified);
}

return;
}

if (element is ContentPresenter)
{
element = UIHelper.GetFirstVisualChild(element) ?? element;

if (element is ISimplifiedStateControl simplifiedStateControl2)
{
simplifiedStateControl2.UpdateSimplifiedState(isSimplified);
}
}
}

Expand Down Expand Up @@ -789,24 +755,6 @@ public RibbonGroupBox()
this.Unloaded += this.OnUnloaded;

this.updateChildSizesItemContainerGeneratorAction = new ItemContainerGeneratorAction(this.ItemContainerGenerator, this.UpdateChildSizes);

// When initializing at first,
// 1.OnApplyTemplate(Status = GeneratorStatus.NotStarted(UIElements are invalid))
// 2.ItemContainerGenerator_StatusChanged(Status = GeneratorStatus.GeneratingContainers(UIElements are invalid))
// 3.ItemContainerGenerator_StatusChanged(Status = GeneratorStatus.ContainersGenerated(UIElements are valid))
// 4.OnLoaded(Status = GeneratorStatus.ContainersGenerated)
// When changing template after OnLoaded(),
// 1.OnApplyTemplate(Status = GeneratorStatus.ContainersGenerated (but UIElements are invalid)))
// 2.ItemContainerGenerator_StatusChanged(Status = GeneratorStatus.GeneratingContainers (UIElements are invalid))
// 3.ItemContainerGenerator_StatusChanged(Status = GeneratorStatus.ContainersGenerated (UIElements are valid))
// When changing count of Items template after OnLoaded(),
// 1.ItemContainerGenerator_ItemsChanged(Status = GeneratorStatus.ContainersGenerated (but UIElements for new items are invalid))
// 2.ItemContainerGenerator_StatusChanged(Status = GeneratorStatus.GeneratingContainers (UIElements for new items are invalid))
// 3.ItemContainerGenerator_StatusChanged(Status = GeneratorStatus.ContainersGenerated (UIElements for all items are valid))
// So, we always have to handle StatusChanged(Status = GeneratorStatus.ContainersGenerated)
// This event handler must receive event notifications prior to OnLoaded()
this.ItemContainerGenerator.StatusChanged += this.ItemContainerGenerator_StatusChanged;
this.ItemContainerGenerator.ItemsChanged += this.ItemContainerGenerator_ItemsChanged;
}

private void OnLoaded(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -1225,18 +1173,6 @@ private StateScale GetCurrentIntermediateStateScale()

#region Event Handling

private void ItemContainerGenerator_StatusChanged(object? sender, EventArgs e)
{
if (this.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
{
this.UpdateChildIsSimplified(this.IsSimplified);
}
}

private void ItemContainerGenerator_ItemsChanged(object sender, ItemsChangedEventArgs e)
{
}

private void OnPopupOpened(object? sender, EventArgs e)
{
this.DropDownOpened?.Invoke(this, e);
Expand Down

0 comments on commit e1429bf

Please sign in to comment.