Skip to content

Commit

Permalink
Fixes #572 by adding row snapping in all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
batzen committed Jun 26, 2018
1 parent e0d0fbd commit 32fb0cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- [#552](../../issues/552) - RibbonGroupBox should resize when font family or size are changed
- [#562](../../issues/562) - Pressing "right" arrow key to open submenu on menuitem causes NullRef exception when there is no submenu
- [#564](../../issues/564) - Gallery overflow panel (with menu items) doesn't close when clicking once in the application
- [#572](../../issues/572) - KeyTip.Keys Position

- ### Enhancements
- [#516](../../issues/516) - Add options to hide the row containing RibbonTabItems
Expand Down
59 changes: 32 additions & 27 deletions Fluent.Ribbon/Adorners/KeyTipAdorner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -715,35 +715,13 @@ private void UpdateKeyTipPositions()
if (RibbonProperties.GetSize(keyTipInformation.AssociatedElement) != RibbonControlSize.Large
|| IsTextBoxShapedControl(keyTipInformation.AssociatedElement))
{
var withinRibbonToolbar = IsWithinRibbonToolbarInTwoLine(keyTipInformation.VisualTarget);
var x = keyTipInformation.KeyTip.DesiredSize.Width / 2.0;
var y = keyTipInformation.KeyTip.DesiredSize.Height / 2.0;
var point = new Point(x, y);
var translatedPoint = keyTipInformation.VisualTarget.TranslatePoint(point, this.AdornedElement);

// Snapping to rows if it present
if (rows != null)
{
var index = 0;
var mindistance = Math.Abs(rows[0] - translatedPoint.Y);
for (var j = 1; j < rows.Length; j++)
{
if (withinRibbonToolbar
&& j == 1)
{
continue;
}

var distance = Math.Abs(rows[j] - translatedPoint.Y);
if (distance < mindistance)
{
mindistance = distance;
index = j;
}
}

translatedPoint.Y = rows[index] - (keyTipInformation.KeyTip.DesiredSize.Height / 2.0);
}
SnapToRowsIfPresent(rows, keyTipInformation, translatedPoint);

keyTipInformation.Position = translatedPoint;
}
Expand All @@ -754,10 +732,8 @@ private void UpdateKeyTipPositions()
var point = new Point(x, y);
var translatedPoint = keyTipInformation.VisualTarget.TranslatePoint(point, this.AdornedElement);

if (rows != null)
{
translatedPoint.Y = rows[2] - (keyTipInformation.KeyTip.DesiredSize.Height / 2.0);
}
// Snapping to rows if it present
SnapToRowsIfPresent(rows, keyTipInformation, translatedPoint);

keyTipInformation.Position = translatedPoint;
}
Expand Down Expand Up @@ -796,6 +772,35 @@ private static bool IsWithinQuickAccessToolbar(DependencyObject element)
return UIHelper.GetParent<QuickAccessToolBar>(element) != null;
}

private static void SnapToRowsIfPresent(double[] rows, KeyTipInformation keyTipInformation, Point translatedPoint)
{
if (rows == null)
{
return;
}

var withinRibbonToolbar = IsWithinRibbonToolbarInTwoLine(keyTipInformation.VisualTarget);

var index = 0;
var mindistance = Math.Abs(rows[0] - translatedPoint.Y);
for (var j = 1; j < rows.Length; j++)
{
if (withinRibbonToolbar && j == 1)
{
continue;
}

var distance = Math.Abs(rows[j] - translatedPoint.Y);
if (distance < mindistance)
{
mindistance = distance;
index = j;
}
}

translatedPoint.Y = rows[index] - (keyTipInformation.KeyTip.DesiredSize.Height / 2.0);
}

/// <inheritdoc />
protected override int VisualChildrenCount => this.keyTipInformations.Count;

Expand Down

0 comments on commit 32fb0cd

Please sign in to comment.