Skip to content

Commit

Permalink
Added various exception handlers (FlaUI#10)
Browse files Browse the repository at this point in the history
* Change for 2.0.0-rc1

* add exception handler due to accessing ui element with higher integrity level

* add exception handler due to exception 'The requested property 'BoundingRectangle [#30001]' is not supported'.

how to reproduce: fast changing of focus on multiple UI elements via mouse movements

* add exception handler due to null pointer exception in LoadDetails
how to reproduce: in focus tracking mode change fast the focus on elements and one of the pending elements from ui

* add exception handler

* Update packages.config

* Delete packages.config

Co-authored-by: Martin.Moenks <[email protected]>
Co-authored-by: Roman <[email protected]>
  • Loading branch information
3 people authored Apr 8, 2021
1 parent 6b0e630 commit b9b8fe2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 46 deletions.
10 changes: 9 additions & 1 deletion src/FlaUInspect/Core/ElementHighlighter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
using System.Drawing;
using System.Threading.Tasks;
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Exceptions;

namespace FlaUInspect.Core
{
public static class ElementHighlighter
{
public static void HighlightElement(AutomationElement automationElement)
{
Task.Run(() => automationElement.DrawHighlight(false, Color.Red, TimeSpan.FromSeconds(1)));
try
{
Task.Run(() => automationElement.DrawHighlight(false, Color.Red, TimeSpan.FromSeconds(1)));
}
catch (PropertyNotSupportedException ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
}
}
3 changes: 1 addition & 2 deletions src/FlaUInspect/Core/FocusTrackingMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public void Start()

public void Stop()
{
_eventHandler.Dispose();
_eventHandler = null;
_automation.UnregisterFocusChangedEvent(_eventHandler);
}

private void OnFocusChanged(AutomationElement automationElement)
Expand Down
48 changes: 29 additions & 19 deletions src/FlaUInspect/Core/HoverMode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
Expand Down Expand Up @@ -36,26 +37,35 @@ public void Stop()
}

private void DispatcherTimerTick(object sender, EventArgs e)
{
if (System.Windows.Input.Keyboard.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control))
{
var screenPos = Mouse.Position;
var hoveredElement = _automation.FromPoint(screenPos);
// Skip items in the current process
// Like Inspect itself or the overlay window
if (hoveredElement.Properties.ProcessId == Process.GetCurrentProcess().Id)
{
return;
}
if (!Equals(_currentHoveredElement, hoveredElement))
{
_currentHoveredElement = hoveredElement;
ElementHovered?.Invoke(hoveredElement);
}
else
{
if (System.Windows.Input.Keyboard.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control))
{
var screenPos = Mouse.Position;
try
{
ElementHighlighter.HighlightElement(hoveredElement);
}
var hoveredElement = _automation.FromPoint(screenPos);
// Skip items in the current process
// Like Inspect itself or the overlay window
if (hoveredElement.Properties.ProcessId == Process.GetCurrentProcess().Id)
{
return;
}
if (!Equals(_currentHoveredElement, hoveredElement))
{
_currentHoveredElement = hoveredElement;
ElementHovered?.Invoke(hoveredElement);
}
else
{
ElementHighlighter.HighlightElement(hoveredElement);
}
}
catch (UnauthorizedAccessException)
{
string caption = "FlaUInspect - Unauthorized access exception";
string message = "You are accessing a protected UI element in hover mode.\nTry to start FlaUInspect as administrator.";
MessageBox.Show(message, caption, MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
}
}
Expand Down
67 changes: 43 additions & 24 deletions src/FlaUInspect/ViewModels/ElementViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,38 @@ public bool IsSelected
get { return GetProperty<bool>(); }
set
{
SetProperty(value);
if (value)
try
{
ElementHighlighter.HighlightElement(AutomationElement);
// Async load details
var unused = Task.Run(() =>
if (value)
{
var details = LoadDetails();
return details;
}).ContinueWith(items =>
{
if (items.IsFaulted)
ElementHighlighter.HighlightElement(AutomationElement);

// Async load details
var unused = Task.Run(() =>
{
if (items.Exception != null)
var details = LoadDetails();
return details;
}).ContinueWith(items =>
{
if (items.IsFaulted)
{
MessageBox.Show(items.Exception.ToString());
if (items.Exception != null)
{
MessageBox.Show(items.Exception.ToString());
}
}
}
ItemDetails.Reset(items.Result);
}, TaskScheduler.FromCurrentSynchronizationContext());
ItemDetails.Reset(items.Result);
}, TaskScheduler.FromCurrentSynchronizationContext());

// Fire the selection event
SelectionChanged?.Invoke(this);
}

// Fire the selection event
SelectionChanged?.Invoke(this);
SetProperty(value);
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
}
Expand Down Expand Up @@ -90,17 +99,27 @@ public void LoadChildren(bool loadInnerChildren)
{
child.SelectionChanged -= SelectionChanged;
}

var childrenViewModels = new List<ElementViewModel>();
foreach (var child in AutomationElement.FindAllChildren())
try
{
var childViewModel = new ElementViewModel(child);
childViewModel.SelectionChanged += SelectionChanged;
childrenViewModels.Add(childViewModel);
if (loadInnerChildren)
foreach (var child in AutomationElement.FindAllChildren())
{
childViewModel.LoadChildren(false);
var childViewModel = new ElementViewModel(child);
childViewModel.SelectionChanged += SelectionChanged;
childrenViewModels.Add(childViewModel);

if (loadInnerChildren)
{
childViewModel.LoadChildren(false);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}

Children.Reset(childrenViewModels);
}

Expand Down Expand Up @@ -160,7 +179,7 @@ private List<DetailGroupViewModel> LoadDetails()
}

// Pattern details
var allSupportedPatterns = AutomationElement.FrameworkAutomationElement.GetSupportedPatterns();
var allSupportedPatterns = AutomationElement.GetSupportedPatterns();
var allPatterns = AutomationElement.Automation.PatternLibrary.AllForCurrentFramework;
var patterns = new List<DetailViewModel>();
foreach (var pattern in allPatterns)
Expand Down

0 comments on commit b9b8fe2

Please sign in to comment.