Skip to content

Commit

Permalink
Merge pull request #759 from fluentribbon/issues/#746
Browse files Browse the repository at this point in the history
Fixes #746
  • Loading branch information
batzen authored Dec 1, 2019
2 parents e0fcbfd + 7f1fe2d commit 1a41dc3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 30 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for Fluent.Ribbon

## 7.0.1
- ### Bug fixes
- [#746](../../issues/746) - NullReferenceException after upgrading to 7.0.0

## 7.0.0
- ### Breaking changes
- [#471](../../issues/471) - **Drop support for .Net 4.0**
Expand Down
90 changes: 61 additions & 29 deletions Fluent.Ribbon/Controls/RibbonGroupBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private void UpdateChildSizes()
{
var element = this.ItemContainerGenerator.ContainerFromItem(item);

if (element == null)
if (element is null)
{
continue;
}
Expand Down Expand Up @@ -174,24 +174,63 @@ internal int Scale
{
if (difference > 0)
{
this.IncreaseScalableElement();
this.EnlargeScalableItems();
}
else
{
this.DecreaseScalableElement();
this.ReduceScalableItems();
}
}
}
}

// Finds and increase size of all scalable elements in the given group box
private void IncreaseScalableElement()
private enum ScaleDirection
{
Enlarge,
Reduce
}

// Finds and increases size of all scalable elements in this group box
private void EnlargeScalableItems()
{
this.ScaleScaleableItems(ScaleDirection.Enlarge);
}

// Finds and decreases size of all scalable elements in this group box
private void ReduceScalableItems()
{
this.ScaleScaleableItems(ScaleDirection.Reduce);
}

private void ScaleScaleableItems(ScaleDirection scaleDirection)
{
foreach (var item in this.Items)
{
var scalableRibbonControl = item as IScalableRibbonControl;
var element = this.ItemContainerGenerator.ContainerFromItem(item);

if (element is null
|| (element is UIElement uiElement && uiElement.Visibility != Visibility.Visible))
{
continue;
}

var scalableRibbonControl = element as IScalableRibbonControl;

scalableRibbonControl?.Enlarge();
if (scalableRibbonControl is null)
{
continue;
}

switch (scaleDirection)
{
case ScaleDirection.Enlarge:
scalableRibbonControl.Enlarge();
break;

case ScaleDirection.Reduce:
scalableRibbonControl.Reduce();
break;
}
}
}

Expand All @@ -205,26 +244,19 @@ private void OnScalableControlScaled(object sender, EventArgs e)
/// </summary>
internal bool SuppressCacheReseting { get; set; }

// Finds and decrease size of all scalable elements in the given group box
private void DecreaseScalableElement()
private void UpdateScalableControlSubscritions(bool registerEvents)
{
foreach (var item in this.Items)
{
var scalableRibbonControl = item as IScalableRibbonControl;
var element = this.ItemContainerGenerator.ContainerFromItem(item);

scalableRibbonControl?.Reduce();
}
}
var scalableRibbonControl = element as IScalableRibbonControl;

private void UpdateScalableControlSubscribing()
{
this.UpdateScalableControlSubscribing(true);
}
if (scalableRibbonControl is null)
{
continue;
}

private void UpdateScalableControlSubscribing(bool registerEvents)
{
foreach (var scalableRibbonControl in this.Items.OfType<IScalableRibbonControl>())
{
// Always unregister first to ensure that we don't subscribe twice
scalableRibbonControl.Scaled -= this.OnScalableControlScaled;

Expand Down Expand Up @@ -638,7 +670,7 @@ private void SubscribeEvents()
// Always unsubscribe events to ensure we don't subscribe twice
this.UnSubscribeEvents();

this.UpdateScalableControlSubscribing();
this.UpdateScalableControlSubscritions(true);

if (this.LauncherButton != null)
{
Expand All @@ -654,7 +686,7 @@ private void SubscribeEvents()

private void UnSubscribeEvents()
{
this.UpdateScalableControlSubscribing(false);
this.UpdateScalableControlSubscritions(false);

if (this.LauncherButton != null)
{
Expand Down Expand Up @@ -793,7 +825,7 @@ internal Size DesiredSizeIntermediate
var contentHeight = UIHelper.GetParent<RibbonTabControl>(this)?.ContentHeight ?? RibbonTabControl.DefaultContentHeight;

this.SuppressCacheReseting = true;
this.UpdateScalableControlSubscribing();
this.UpdateScalableControlSubscritions(true);

// Get desired size for these values
var backupState = this.State;
Expand Down Expand Up @@ -845,7 +877,7 @@ internal void InvalidateLayout()

private static void InvalidateMeasureRecursive(UIElement element)
{
if (element == null)
if (element is null)
{
return;
}
Expand All @@ -856,7 +888,7 @@ private static void InvalidateMeasureRecursive(UIElement element)
{
var child = VisualTreeHelper.GetChild(element, i) as UIElement;

if (child == null)
if (child is null)
{
continue;
}
Expand Down Expand Up @@ -911,7 +943,7 @@ private void OnPopupClosed(object sender, EventArgs e)
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
if (ReferenceEquals(e.Source, this) == false
|| this.DropDownPopup == null)
|| this.DropDownPopup is null)
{
return;
}
Expand Down Expand Up @@ -1065,7 +1097,7 @@ private void OnQuickAccessOpened(object sender, EventArgs e)
// Save state
this.IsSnapped = true;

if (this.ItemsSource == null)
if (this.ItemsSource is null)
{
for (var i = 0; i < this.Items.Count; i++)
{
Expand All @@ -1082,7 +1114,7 @@ private void OnQuickAccessClosed(object sender, EventArgs e)
{
var groupBox = (RibbonGroupBox)sender;

if (this.ItemsSource == null)
if (this.ItemsSource is null)
{
for (var i = 0; i < groupBox.Items.Count; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion Fluent.Ribbon/Metro/Behaviours/StylizedBehaviors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private static readonly DependencyProperty OriginalBehaviorProperty

private static Behavior GetOriginalBehavior(DependencyObject obj)
{
return obj.GetValue(OriginalBehaviorProperty) as Behavior;
return (Behavior)obj.GetValue(OriginalBehaviorProperty);
}

private static void SetOriginalBehavior(DependencyObject obj, Behavior value)
Expand Down

0 comments on commit 1a41dc3

Please sign in to comment.