Skip to content

Commit

Permalink
Fixing various issues with the popup of InRibbonGallery
Browse files Browse the repository at this point in the history
  • Loading branch information
batzen committed Apr 18, 2020
1 parent dc90836 commit 4018384
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
74 changes: 66 additions & 8 deletions Fluent.Ribbon/Controls/InRibbonGallery.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ReSharper disable once CheckNamespace
// ReSharper disable once CheckNamespace
namespace Fluent
{
using System;
Expand Down Expand Up @@ -41,6 +41,9 @@ namespace Fluent
[TemplatePart(Name = "PART_ContentPresenter", Type = typeof(ContentControl))]
[TemplatePart(Name = "PART_PopupContentPresenter", Type = typeof(ContentControl))]
[TemplatePart(Name = "PART_ScrollViewer", Type = typeof(ScrollViewer))]

[TemplatePart(Name = "PART_PopupMenuPresenter", Type = typeof(FrameworkElement))]
[TemplatePart(Name = "PART_PopupResizeBorder", Type = typeof(FrameworkElement))]
public class InRibbonGallery : Selector, IScalableRibbonControl, IDropDownControl, IRibbonControl, IQuickAccessItemProvider, IRibbonSizeChangedSink, ILargeIconProvider
{
#region Fields
Expand Down Expand Up @@ -80,9 +83,14 @@ public class InRibbonGallery : Selector, IScalableRibbonControl, IDropDownContro

private FrameworkElement layoutRoot;

private FrameworkElement popupMenuPresenter;
private FrameworkElement popupResizeBorder;

[CanBeNull]
internal GalleryPanelState CurrentGalleryPanelState { get; private set; }

internal PopupState CurrentPopupState { get; } = new PopupState();

#endregion

#region Properties
Expand Down Expand Up @@ -186,7 +194,7 @@ public int MinItemsInDropDownRow
/// Using a DependencyProperty as the backing store for MinItemsInDropDownRow. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty MinItemsInDropDownRowProperty =
DependencyProperty.Register(nameof(MinItemsInDropDownRow), typeof(int), typeof(InRibbonGallery), new PropertyMetadata(1));
DependencyProperty.Register(nameof(MinItemsInDropDownRow), typeof(int), typeof(InRibbonGallery), new PropertyMetadata(IntBoxes.One));

#endregion

Expand All @@ -204,7 +212,8 @@ public int MaxItemsInDropDownRow
/// <summary>
/// Using a DependencyProperty as the backing store for MaxItemsInDropDownRow. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty MaxItemsInDropDownRowProperty = DependencyProperty.Register(nameof(MaxItemsInDropDownRow), typeof(int), typeof(InRibbonGallery), new PropertyMetadata(0));
public static readonly DependencyProperty MaxItemsInDropDownRowProperty =
DependencyProperty.Register(nameof(MaxItemsInDropDownRow), typeof(int), typeof(InRibbonGallery), new PropertyMetadata(IntBoxes.Zero));

#endregion

Expand Down Expand Up @@ -1128,6 +1137,9 @@ public override void OnApplyTemplate()
this.popupControlPresenter = this.GetTemplateChild("PART_PopupContentPresenter") as ContentControl;

this.scrollViewer = this.GetTemplateChild("PART_ScrollViewer") as ScrollViewer;

this.popupMenuPresenter = this.GetTemplateChild("PART_PopupMenuPresenter") as FrameworkElement;
this.popupResizeBorder = this.GetTemplateChild("PART_PopupResizeBorder") as FrameworkElement;
}

private void OnPopupPreviewMouseUp(object sender, MouseButtonEventArgs e)
Expand Down Expand Up @@ -1207,6 +1219,8 @@ private void OnDropDownOpened(object sender, EventArgs e)
this.galleryPanel.MinItemsInRow = this.MinItemsInDropDownRow;
this.galleryPanel.MaxItemsInRow = this.MaxItemsInDropDownRow;
this.galleryPanel.IsGrouped = true;

this.CurrentPopupState.Restore(this.galleryPanel, this.menuPanel);
}
}

Expand Down Expand Up @@ -1344,28 +1358,42 @@ private void OnResizeBothDelta(object sender, DragDeltaEventArgs e)
{
this.OnResizeVerticalDelta(sender, e);

this.menuPanel.Width = double.NaN;
if (DoubleUtil.AreClose(e.HorizontalChange, 0))
{
return;
}

if (this.galleryPanel.IsNotNull())
{
if (double.IsNaN(this.galleryPanel.Width))
{
this.galleryPanel.Width = this.galleryPanel.ActualWidth;
this.galleryPanel.SetCurrentValue(WidthProperty, this.galleryPanel.ActualWidth);
}

this.galleryPanel.Width = Math.Max(this.layoutRoot.ActualWidth, this.galleryPanel.Width + e.HorizontalChange);
var minimumWidth = this.snappedImage.ActualWidth;
this.galleryPanel.SetCurrentValue(WidthProperty, Math.Max(minimumWidth, this.galleryPanel.Width + e.HorizontalChange));
}

this.CurrentPopupState.Save(this.galleryPanel, this.menuPanel);
}

// Handles resize vertical drag
private void OnResizeVerticalDelta(object sender, DragDeltaEventArgs e)
{
if (DoubleUtil.AreClose(e.VerticalChange, 0))
{
return;
}

if (double.IsNaN(this.menuPanel.Height))
{
this.menuPanel.Height = this.menuPanel.ActualHeight;
this.menuPanel.SetCurrentValue(HeightProperty, this.menuPanel.ActualHeight);
}

this.menuPanel.Height = Math.Max(this.layoutRoot.ActualHeight, Math.Min(this.menuPanel.Height + e.VerticalChange, this.MaxDropDownHeight));
var minimumHeight = this.layoutRoot.ActualHeight + this.popupMenuPresenter.ActualHeight + this.popupResizeBorder.ActualHeight + 10;
this.menuPanel.SetCurrentValue(HeightProperty, Math.Max(minimumHeight, Math.Min(this.menuPanel.Height + e.VerticalChange, this.MaxDropDownHeight)));

this.CurrentPopupState.Save(this.galleryPanel, this.menuPanel);
}

#endregion
Expand Down Expand Up @@ -1610,5 +1638,35 @@ public void Restore()
this.GalleryPanel.MaxItemsInRow = this.MaxItemsInRow;
}
}

internal class PopupState
{
public double Width { get; private set; } = double.NaN;

public double Height { get; private set; } = double.NaN;

public void Save(FrameworkElement widthControl, FrameworkElement heightControl)
{
this.Width = widthControl.Width;
this.Height = heightControl.Height;
}

public void Restore(FrameworkElement widthControl, FrameworkElement heightControl)
{
if (double.IsNaN(this.Width) == false)
{
widthControl.SetCurrentValue(WidthProperty, this.Width);
}
else
{
widthControl.SetCurrentValue(WidthProperty, widthControl.ActualWidth);
}

if (double.IsNaN(this.Height) == false)
{
heightControl.SetCurrentValue(HeightProperty, this.Height);
}
}
}
}
}
5 changes: 5 additions & 0 deletions Fluent.Ribbon/Internal/KnownBoxes/IntBoxes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ internal static class IntBoxes
/// </summary>
internal static readonly object Zero = 0;

/// <summary>
/// Gets a boxed value for <c>1</c>.
/// </summary>
internal static readonly object One = 1;

/// <summary>
/// Gets a boxed value for <see cref="int.MaxValue"/>.
/// </summary>
Expand Down

0 comments on commit 4018384

Please sign in to comment.