Skip to content

Stability fixes #22

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions src/FlaUInspect/Core/HoverMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Input;
using System.Runtime.InteropServices;
using FlaUInspect.ViewModels;

namespace FlaUInspect.Core
{
Expand All @@ -13,15 +15,17 @@ public class HoverMode
private readonly AutomationBase _automation;
private readonly DispatcherTimer _dispatcherTimer;
private AutomationElement _currentHoveredElement;
private MainViewModel _mv;

public event Action<AutomationElement> ElementHovered;

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

public void Start()
Expand All @@ -38,7 +42,7 @@ public void Stop()

private void DispatcherTimerTick(object sender, EventArgs e)
{
if (System.Windows.Input.Keyboard.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control))
if (System.Windows.Input.Keyboard.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control) && System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LWin))
{
var screenPos = Mouse.Position;
try
Expand Down Expand Up @@ -70,6 +74,11 @@ private void DispatcherTimerTick(object sender, EventArgs e)
{
Console.WriteLine($"Exception: {ex.Message}");
}
catch (COMException cex)
{
_mv.ComExceptionDetected = true;
//MessageBox.Show(cex.Message, "DispatcherTimeTick caught COM exception. Please refresh inspector", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/FlaUInspect/ViewModels/ElementViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,17 @@ private List<DetailGroupViewModel> LoadDetails()
}

// Pattern details
var allSupportedPatterns = AutomationElement.GetSupportedPatterns();
//It is here, at GetSupportedPatterns, it crashes if the selected element in inspector, is no longer present.
PatternId[] allSupportedPatterns = new PatternId[0];
try
{
allSupportedPatterns = AutomationElement.GetSupportedPatterns();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Caught '{ex.GetType()}': {ex.Message}");
//Handling exception. Details will show something is wrong. Would be great to show the red menu line aswell.
}
var allPatterns = AutomationElement.Automation.PatternLibrary.AllForCurrentFramework;
var patterns = new List<DetailViewModel>();
foreach (var pattern in allPatterns)
Expand Down
19 changes: 18 additions & 1 deletion src/FlaUInspect/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public bool EnableHoverMode
}
}

public bool ComExceptionDetected
{
get
{
return GetProperty<bool>();
}
set
{
SetProperty(value);
}
}

public bool EnableFocusTrackingMode
{
get { return GetProperty<bool>(); }
Expand Down Expand Up @@ -129,12 +141,16 @@ public void Initialize(AutomationType selectedAutomationType)
_treeWalker = _automation.TreeWalkerFactory.GetControlViewWalker();

// Initialize hover
_hoverMode = new HoverMode(_automation);
_hoverMode = new HoverMode(_automation, this);
_hoverMode.ElementHovered += ElementToSelectChanged;
_hoverMode.Start();
EnableHoverMode = true;

// Initialize focus tracking
_focusTrackingMode = new FocusTrackingMode(_automation);
_focusTrackingMode.ElementFocused += ElementToSelectChanged;

ComExceptionDetected = false;
}

private void ElementToSelectChanged(AutomationElement obj)
Expand Down Expand Up @@ -208,6 +224,7 @@ private void RefreshTree()
{
Elements.Clear();
Initialize(SelectedAutomationType);
ComExceptionDetected = false;
}
}
}
14 changes: 13 additions & 1 deletion src/FlaUInspect/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
<Grid>
<DockPanel>
<Menu DockPanel.Dock="Top">
<Menu.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ComExceptionDetected}" Value="True">
<Setter Property="ContextMenu.Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding ComExceptionDetected}" Value="False">
<Setter Property="ContextMenu.Background" Value="#f0f0f0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Menu.Style>
<MenuItem Header="_File">
<MenuItem Header="_New Instance" Command="{Binding StartNewInstanceCommand}" />
<MenuItem Header="_Capture selected item" Command="{Binding CaptureSelectedItemCommand}" />
Expand All @@ -30,7 +42,7 @@
<MenuItem Header="E_xit" Click="MenuItem_Click" />
</MenuItem>
<MenuItem Header="_Mode">
<MenuItem Header="H_over Mode (use Ctrl)" IsCheckable="True" IsChecked="{Binding EnableHoverMode}" />
<MenuItem Header="H_over Mode (use Ctrl + LWin)" IsCheckable="True" IsChecked="{Binding EnableHoverMode}" />
<MenuItem Header="_Focus Tracking" IsCheckable="True" IsChecked="{Binding EnableFocusTrackingMode}" />
<MenuItem Header="Show _XPath" IsCheckable="True" IsChecked="{Binding EnableXPath}" />
</MenuItem>
Expand Down