Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent horizontal shifting in properties grid #1475

Merged
merged 1 commit into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -271,33 +271,6 @@ private DependencyObject GetFirstChildElement<T>(DependencyObject element)
return null;
}

/// <summary>
/// Finds object up parent hierarchy of specified type
/// </summary>
private DependencyObject GetParentElem<T>(DependencyObject obj)
{
try
{
var par = VisualTreeHelper.GetParent(obj);

if (par is T)
{
return par;
}
else
{
return GetParentElem<T>(par);
}
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e)
{
e.ReportException();
return null;
}
#pragma warning restore CA1031 // Do not catch general exception types
}

/// <summary>
/// Custom keyboard nav behavior
/// </summary>
Expand All @@ -314,7 +287,7 @@ private void lviResults_PreviewKeyDown(object sender, KeyEventArgs e)
}
else
{
var parent = GetParentElem<Expander>(sender as DependencyObject) as Expander;
var parent = (sender as DependencyObject).GetParentElem<Expander>() as Expander;
var vb = GetFirstChildElement<Label>(parent as DependencyObject) as Label;
vb.Focus();
}
Expand Down Expand Up @@ -366,7 +339,7 @@ private void lviResults_PreviewKeyDown(object sender, KeyEventArgs e)
}
else
{
(GetParentElem<GroupItem>(sender as DependencyObject) as UIElement).Focus();
((sender as DependencyObject).GetParentElem<GroupItem>() as UIElement).Focus();
}
}
e.Handled = true;
Expand Down Expand Up @@ -457,7 +430,7 @@ private void Exp_Checked(object sender, SizeChangedEventArgs e)
private void ListViewItem_Unselected(object sender, RoutedEventArgs e)
{
var lvi = sender as ListViewItem;
var exp = GetParentElem<Expander>(lvi) as Expander;
var exp = lvi.GetParentElem<Expander>() as Expander;
var cb = GetFirstChildElement<CheckBox>(exp) as CheckBox;
var srvm = lvi.DataContext as RuleResultViewModel;

Expand All @@ -468,7 +441,7 @@ private void ListViewItem_Unselected(object sender, RoutedEventArgs e)
}

SelectedItems.Remove(srvm);
var groupitem = GetParentElem<GroupItem>(exp) as GroupItem;
var groupitem = exp.GetParentElem<GroupItem>() as GroupItem;
var dc = cb.DataContext as CollectionViewGroup;
var itms = dc.Items;
var any = itms.Intersect(SelectedItems).Any();
Expand Down Expand Up @@ -512,7 +485,7 @@ internal void CheckBox_Click(object sender, RoutedEventArgs e)
var cb = sender as CheckBox;
if (cb.IsEnabled)
{
var exp = GetParentElem<Expander>(cb) as Expander;
var exp = cb.GetParentElem<Expander>() as Expander;
var lst = cb.DataContext as CollectionViewGroup;
var itemsSelected = SetItemsChecked(lst.Items, cb.IsChecked.Value);
if (!itemsSelected)
Expand All @@ -521,7 +494,7 @@ internal void CheckBox_Click(object sender, RoutedEventArgs e)
}

// update tag for whether the group item has children highlighted or not
var groupitem = GetParentElem<GroupItem>(exp) as GroupItem;
var groupitem = exp.GetParentElem<GroupItem>() as GroupItem;
groupitem.Tag = cb.IsChecked.Value ? "all" : "zero";
}
}
Expand All @@ -542,7 +515,7 @@ private void ListViewItem_Selected(object sender, RoutedEventArgs e)
if (ScreenshotAvailable)
{
var lvi = sender as ListViewItem;
var exp = GetParentElem<Expander>(lvi) as Expander;
var exp = lvi.GetParentElem<Expander>() as Expander;
var cb = GetFirstChildElement<CheckBox>(exp) as CheckBox;
var itm = lvi.DataContext as RuleResultViewModel;
if (!SelectedItems.Contains(itm))
Expand All @@ -551,7 +524,7 @@ private void ListViewItem_Selected(object sender, RoutedEventArgs e)
UpdateSelectAll();
ImageOverlayDriver.GetDefaultInstance().AddElement(_controlContext.ElementContext.Id, itm.Element.UniqueId);
}
var groupitem = GetParentElem<GroupItem>(exp) as GroupItem;
var groupitem = exp.GetParentElem<GroupItem>() as GroupItem;
var dc = cb.DataContext as CollectionViewGroup;
var itms = dc.Items;
var any = itms.Except(SelectedItems).Any();
Expand All @@ -573,7 +546,7 @@ private void ListViewItem_Selected(object sender, RoutedEventArgs e)
/// </summary>
private void GroupItem_PreviewKeyDown(object sender, KeyEventArgs e)
{
var listViewItemParent = GetParentElem<ListViewItem>(Keyboard.FocusedElement as DependencyObject);
var listViewItemParent = (Keyboard.FocusedElement as DependencyObject).GetParentElem<ListViewItem>();
if (Keyboard.FocusedElement is ListViewItem || listViewItemParent != null)
{
// Let it be handled by the listviewitem previewkeydown handler
Expand All @@ -583,7 +556,7 @@ private void GroupItem_PreviewKeyDown(object sender, KeyEventArgs e)

var gi = sender as GroupItem;
var sp = GetFirstChildElement<StackPanel>(gi) as StackPanel;
var exp = GetParentElem<Expander>(sp) as Expander;
var exp = sp.GetParentElem<Expander>() as Expander;

if (e.Key == Key.Right)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</UserControl.Resources>
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" >
<Grid >
<Grid RequestBringIntoView="Grid_RequestBringIntoView">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using AccessibilityInsights.CommonUxComponents.Controls;
using AccessibilityInsights.SharedUx.Dialogs;
using AccessibilityInsights.SharedUx.Interfaces;
using AccessibilityInsights.SharedUx.Settings;
using AccessibilityInsights.SharedUx.Utilities;
using AccessibilityInsights.SharedUx.ViewModels;
using Axe.Windows.Core.Bases;
using Axe.Windows.Core.Types;
Expand All @@ -15,6 +16,7 @@
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace AccessibilityInsights.SharedUx.Controls
{
Expand Down Expand Up @@ -239,5 +241,30 @@ private void lvProperties_KeyDown(object sender, KeyEventArgs e)
e.Handled = true;
}
}

/// <summary>
/// Override WPF's behavior to automatically bring elements into focus via horizontal scrolling, since we treat this
/// grid as a list and it causes a jumpy appearance.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Grid_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
var scrollViewer = (sender as Grid).GetParentElem<ScrollViewer>() as ScrollViewer;

// If target rectangle is out of view vertically, scroll to it
var topIsVisible = e.TargetRect.Top >= scrollViewer.VerticalOffset;
var bottomIsVisible = e.TargetRect.Bottom <= (scrollViewer.ViewportHeight + scrollViewer.VerticalOffset);
if (!topIsVisible)
{
scrollViewer.ScrollToVerticalOffset(e.TargetRect.Top);
}
else if (!bottomIsVisible)
{
scrollViewer.ScrollToVerticalOffset(e.TargetRect.Bottom - scrollViewer.ViewportHeight);
}

e.Handled = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,32 @@ internal static bool IsVisibleLocation(this Rectangle p)
where s.Bounds.Contains(p)
select s).Any();
}

/// <summary>
/// Finds object up parent hierarchy of specified type
/// </summary>
internal static DependencyObject GetParentElem<T>(this DependencyObject obj)
{
try
{
var par = VisualTreeHelper.GetParent(obj);

if (par is T)
{
return par;
}
else
{
return GetParentElem<T>(par);
}
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e)
{
e.ReportException();
return null;
}
#pragma warning restore CA1031 // Do not catch general exception types
}
}
}