-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added try-catch blocks to handle runtime exceptions (#14)
Co-authored-by: Roman <r.baeriswyl@gmail.com>
- Loading branch information
Showing
2 changed files
with
123 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters