Skip to content

Commit

Permalink
Added try-catch blocks to handle runtime exceptions (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman <r.baeriswyl@gmail.com>
  • Loading branch information
aurax and Roemer authored Apr 8, 2021
1 parent 048adad commit df78b1c
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 109 deletions.
148 changes: 76 additions & 72 deletions src/FlaUInspect/Core/HoverMode.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,76 @@
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Input;

namespace FlaUInspect.Core
{
public class HoverMode
{
private readonly AutomationBase _automation;
private readonly DispatcherTimer _dispatcherTimer;
private AutomationElement _currentHoveredElement;

public event Action<AutomationElement> ElementHovered;

public HoverMode(AutomationBase automation)
{
_automation = automation;
_dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Tick += DispatcherTimerTick;
_dispatcherTimer.Interval = TimeSpan.FromMilliseconds(500);
}

public void Start()
{
_currentHoveredElement = null;
_dispatcherTimer.Start();
}

public void Stop()
{
_currentHoveredElement = null;
_dispatcherTimer.Stop();
}

private void DispatcherTimerTick(object sender, EventArgs e)
{
if (System.Windows.Input.Keyboard.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control))
{
var screenPos = Mouse.Position;
try
{
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);
}
}
}
}
}
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Input;

namespace FlaUInspect.Core
{
public class HoverMode
{
private readonly AutomationBase _automation;
private readonly DispatcherTimer _dispatcherTimer;
private AutomationElement _currentHoveredElement;

public event Action<AutomationElement> ElementHovered;

public HoverMode(AutomationBase automation)
{
_automation = automation;
_dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Tick += DispatcherTimerTick;
_dispatcherTimer.Interval = TimeSpan.FromMilliseconds(500);
}

public void Start()
{
_currentHoveredElement = null;
_dispatcherTimer.Start();
}

public void Stop()
{
_currentHoveredElement = null;
_dispatcherTimer.Stop();
}

private void DispatcherTimerTick(object sender, EventArgs e)
{
if (System.Windows.Input.Keyboard.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control))
{
var screenPos = Mouse.Position;
try
{
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);
}
catch(System.IO.FileNotFoundException ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
}
}
}
84 changes: 47 additions & 37 deletions src/FlaUInspect/ViewModels/ElementViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,38 +144,41 @@ private List<DetailGroupViewModel> LoadDetails()
using (cacheRequest.Activate())
{
var elementCached = AutomationElement.FindFirst(TreeScope.Element, TrueCondition.Default);
// Element identification
var identification = new List<IDetailViewModel>
if (elementCached != null)
{
DetailViewModel.FromAutomationProperty("AutomationId", elementCached.Properties.AutomationId),
DetailViewModel.FromAutomationProperty("Name", elementCached.Properties.Name),
DetailViewModel.FromAutomationProperty("ClassName", elementCached.Properties.ClassName),
DetailViewModel.FromAutomationProperty("ControlType", elementCached.Properties.ControlType),
DetailViewModel.FromAutomationProperty("LocalizedControlType", elementCached.Properties.LocalizedControlType),
new DetailViewModel("FrameworkType", elementCached.FrameworkType.ToString()),
DetailViewModel.FromAutomationProperty("FrameworkId", elementCached.Properties.FrameworkId),
DetailViewModel.FromAutomationProperty("ProcessId", elementCached.Properties.ProcessId),
};
detailGroups.Add(new DetailGroupViewModel("Identification", identification));
// Element identification
var identification = new List<IDetailViewModel>
{
DetailViewModel.FromAutomationProperty("AutomationId", elementCached.Properties.AutomationId),
DetailViewModel.FromAutomationProperty("Name", elementCached.Properties.Name),
DetailViewModel.FromAutomationProperty("ClassName", elementCached.Properties.ClassName),
DetailViewModel.FromAutomationProperty("ControlType", elementCached.Properties.ControlType),
DetailViewModel.FromAutomationProperty("LocalizedControlType", elementCached.Properties.LocalizedControlType),
new DetailViewModel("FrameworkType", elementCached.FrameworkType.ToString()),
DetailViewModel.FromAutomationProperty("FrameworkId", elementCached.Properties.FrameworkId),
DetailViewModel.FromAutomationProperty("ProcessId", elementCached.Properties.ProcessId),
};
detailGroups.Add(new DetailGroupViewModel("Identification", identification));

// Element details
var details = new List<DetailViewModel>
{
DetailViewModel.FromAutomationProperty("IsEnabled", elementCached.Properties.IsEnabled),
DetailViewModel.FromAutomationProperty("IsOffscreen", elementCached.Properties.IsOffscreen),
DetailViewModel.FromAutomationProperty("BoundingRectangle", elementCached.Properties.BoundingRectangle),
DetailViewModel.FromAutomationProperty("HelpText", elementCached.Properties.HelpText),
DetailViewModel.FromAutomationProperty("IsPassword", elementCached.Properties.IsPassword)
};
// Special handling for NativeWindowHandle
var nativeWindowHandle = elementCached.Properties.NativeWindowHandle.ValueOrDefault;
var nativeWindowHandleString = "Not Supported";
if (nativeWindowHandle != default(IntPtr))
{
nativeWindowHandleString = String.Format("{0} ({0:X8})", nativeWindowHandle.ToInt32());
// Element details
var details = new List<DetailViewModel>
{
DetailViewModel.FromAutomationProperty("IsEnabled", elementCached.Properties.IsEnabled),
DetailViewModel.FromAutomationProperty("IsOffscreen", elementCached.Properties.IsOffscreen),
DetailViewModel.FromAutomationProperty("BoundingRectangle", elementCached.Properties.BoundingRectangle),
DetailViewModel.FromAutomationProperty("HelpText", elementCached.Properties.HelpText),
DetailViewModel.FromAutomationProperty("IsPassword", elementCached.Properties.IsPassword)
};
// Special handling for NativeWindowHandle
var nativeWindowHandle = elementCached.Properties.NativeWindowHandle.ValueOrDefault;
var nativeWindowHandleString = "Not Supported";
if (nativeWindowHandle != default(IntPtr))
{
nativeWindowHandleString = String.Format("{0} ({0:X8})", nativeWindowHandle.ToInt32());
}
details.Add(new DetailViewModel("NativeWindowHandle", nativeWindowHandleString));
detailGroups.Add(new DetailGroupViewModel("Details", details));
}
details.Add(new DetailViewModel("NativeWindowHandle", nativeWindowHandleString));
detailGroups.Add(new DetailGroupViewModel("Details", details));
}

// Pattern details
Expand Down Expand Up @@ -313,16 +316,23 @@ private List<DetailGroupViewModel> LoadDetails()
if (allSupportedPatterns.Contains(AutomationElement.Automation.PatternLibrary.TextPattern))
{
var pattern = AutomationElement.Patterns.Text.Pattern;
var foreColor = (int)pattern.DocumentRange.GetAttributeValue(TextAttributes.ForegroundColor);
var backColor = (int)pattern.DocumentRange.GetAttributeValue(TextAttributes.BackgroundColor);
var patternDetails = new List<DetailViewModel>
try
{
new DetailViewModel("ForeColor", $"{System.Drawing.Color.FromArgb(foreColor)} ({foreColor})"),
new DetailViewModel("ForeColor", $"{System.Drawing.Color.FromArgb(backColor)} ({backColor})"),
};
var foreColor = (int)pattern.DocumentRange.GetAttributeValue(TextAttributes.ForegroundColor);
var backColor = (int)pattern.DocumentRange.GetAttributeValue(TextAttributes.BackgroundColor);
var patternDetails = new List<DetailViewModel>
{
new DetailViewModel("ForeColor", $"{System.Drawing.Color.FromArgb(foreColor)} ({foreColor})"),
new DetailViewModel("ForeColor", $"{System.Drawing.Color.FromArgb(backColor)} ({backColor})"),
};

var c = System.Drawing.Color.FromArgb(32768);
detailGroups.Add(new DetailGroupViewModel("Text Pattern", patternDetails));
var c = System.Drawing.Color.FromArgb(32768);
detailGroups.Add(new DetailGroupViewModel("Text Pattern", patternDetails));
}
catch (InvalidCastException ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
// TogglePattern
if (allSupportedPatterns.Contains(AutomationElement.Automation.PatternLibrary.TogglePattern))
Expand Down

0 comments on commit df78b1c

Please sign in to comment.