Skip to content

Commit cad093a

Browse files
author
EXT-Gweltaz
committed
Multiple options allowed to be active at the same time.
WIP: Issue: Focus mode do duplicate insertions when the element has not been expanded yet (only cosmetic, but annoying)
1 parent 3e9fabc commit cad093a

3 files changed

Lines changed: 20 additions & 31 deletions

File tree

src/FlaUInspect/Core/FocusTrackingMode.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@
55

66
namespace FlaUInspect.Core;
77

8-
public class FocusTrackingMode(AutomationBase? automation, Action<AutomationElement> onFocusChangedAction) {
8+
public class FocusTrackingMode(AutomationBase? automation, Func<AutomationElement, AutomationElement?> onFocusChangedAction) {
99
private AutomationElement? _currentFocusedElement;
1010
private FocusChangedEventHandlerBase? _eventHandler;
1111

1212
public void Start()
1313
// Might give problems because inspect is registered as well.
1414
// MS recommends to call UIA commands on a thread outside a UI thread.
15-
=> Task.Factory.StartNew(() => _eventHandler = automation?.RegisterFocusChangedEvent(OnFocusChanged));
15+
=> _eventHandler = automation?.RegisterFocusChangedEvent(OnFocusChanged);
1616

1717
public void Stop() {
1818
if (_eventHandler != null)
1919
automation?.UnregisterFocusChangedEvent(_eventHandler);
20+
automation?.UnregisterAllEvents();
2021
}
2122

2223
private void OnFocusChanged(AutomationElement? automationElement) {
2324
// Skip items in the current process
2425
// Like Inspect itself or the overlay window
2526
try {
26-
if (automationElement?.Properties.ProcessId.IsSupported == true && automationElement.Properties.ProcessId == Environment.ProcessId)
27+
if (automationElement?.Properties.ProcessId.IsSupported != true || automationElement.Properties.ProcessId == Environment.ProcessId)
2728
return;
2829
}
2930
catch (Exception) {
@@ -32,10 +33,7 @@ private void OnFocusChanged(AutomationElement? automationElement) {
3233
}
3334

3435
if (!Equals(_currentFocusedElement, automationElement)) {
35-
_currentFocusedElement = automationElement;
36-
37-
if (automationElement != null)
38-
Application.Current.Dispatcher.Invoke(() => onFocusChangedAction(automationElement));
36+
_currentFocusedElement = Application.Current.Dispatcher.Invoke(() => onFocusChangedAction(automationElement));
3937
}
4038
}
4139
}

src/FlaUInspect/ViewModels/ElementViewModel.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,17 @@ public List<ElementViewModel> LoadChildren(int loadSubChildren = 0) {
6262
return [];
6363
}
6464
}
65+
6566
public override bool Equals(object? obj) => Equals(obj as ElementViewModel);
6667

6768
public bool Equals(ElementViewModel? y) => y is not null
6869
&& (ReferenceEquals(this, y)
6970
|| (GetType() == y.GetType()
7071
&& Level == y.Level
71-
&& IsSelected == y.IsSelected
72-
&& IsExpanded == y.IsExpanded
73-
&& AutomationId == y.AutomationId
74-
&& ControlType == y.ControlType
75-
&& Name == y.Name));
72+
&& AutomationElement == y.AutomationElement
73+
&& Parent! == y.Parent!));
7674

77-
public override int GetHashCode() => (Level, IsSelected, IsExpanded, AutomationId, ControlType, Name).GetHashCode();
75+
public override int GetHashCode() => (Level, AutomationId, ControlType, Name).GetHashCode();
7876

7977
public static bool operator ==(ElementViewModel lhs, ElementViewModel rhs) => lhs is null
8078
? rhs is null

src/FlaUInspect/ViewModels/ProcessViewModel.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.ObjectModel;
22
using System.Drawing.Imaging;
3+
using System.Runtime.CompilerServices;
34
using System.Windows;
45
using System.Windows.Input;
56
using FlaUI.Core;
@@ -53,7 +54,7 @@ public ProcessViewModel(AutomationBase automation, int processId, IntPtr mainWin
5354
HoverManager.AddListener(_windowHandle,
5455
x => {
5556
if (EnableHoverMode)
56-
ElementToSelectChanged(x);
57+
_ = ElementToSelectChanged(x);
5758
});
5859
HoverManager.Disable(_windowHandle);
5960

@@ -180,7 +181,7 @@ public bool EnableFocusTrackingMode {
180181
private static ElementOverlay CreateTrackHighlighterOverlay() => App.FlaUiAppOptions.SelectionOverlay() ?? App.FlaUiAppOptions.DefaultOverlay();
181182

182183
private void TrackSelectedItem(ElementViewModel? item) {
183-
if (item is null) {
184+
if (item is null || item.Level <= 0) {
184185
_trackHighlighterOverlay?.Dispose();
185186
return;
186187
}
@@ -204,9 +205,6 @@ private void SetMode() {
204205
_trackHighlighterOverlay?.Dispose();
205206
_focusTrackingMode?.Stop();
206207

207-
//if (new[] { EnableHoverMode, EnableHighLightSelectionMode, EnableFocusTrackingMode }.Count(x => x) != 1)
208-
// return;
209-
210208
if (EnableFocusTrackingMode)
211209
_focusTrackingMode?.Start();
212210
else if (EnableHighLightSelectionMode)
@@ -223,22 +221,16 @@ public void Initialize() {
223221

224222
Elements = new ObservableCollection<ElementViewModel>(desktopViewModel.Children);
225223

226-
// Initialize hover
227-
EnableHoverMode = false;
228-
229224
// Initialize focus tracking
230-
_focusTrackingMode = new FocusTrackingMode(_automation,
231-
x => {
232-
if (EnableFocusTrackingMode)
233-
ElementToSelectChanged(x);
234-
});
225+
_focusTrackingMode ??= new FocusTrackingMode(_automation,
226+
x => EnableFocusTrackingMode ? ElementToSelectChanged(x)?.AutomationElement : null);
235227
SelectedItem = desktopViewModel;
236228

237229
OnPropertyChanged(nameof(Elements));
238230
OnPropertyChanged(nameof(ElementPatterns));
239231
}
240232

241-
public void ElementToSelectChanged(AutomationElement? obj, bool forceExpand = false) => SelectedItem = GetNextElementVm(forceExpand, GetPathToRoot(obj, forceExpand), Elements);
233+
public ElementViewModel? ElementToSelectChanged(AutomationElement? obj, bool forceExpand = false) => SelectedItem = GetNextElementVm(forceExpand, GetPathToRoot(obj, forceExpand), Elements);
242234

243235
private Stack<AutomationElement> GetPathToRoot(AutomationElement? obj, bool forceExpand) {
244236
Stack<AutomationElement> pathToRoot = new();
@@ -351,11 +343,12 @@ private static void ExpandElement(ElementViewModel sender, ObservableCollection<
351343
var children = sender.LoadChildren(1);
352344

353345
foreach (var child in children)
354-
// Check if not in tree before insert
355-
// Note: .Contains does not check for .Equals
356-
//if (!elements.Any(child.Equals))
357346
if (!elements.Contains(child))
358-
elements.Insert(senderIndex + 1, child);
347+
try {
348+
elements.Insert(++senderIndex, child);
349+
}
350+
catch (NotSupportedException) { }
351+
catch (InvalidOperationException) { }
359352
}
360353

361354
public void CollapseElement(ElementViewModel sender) => CollapseElement(sender, Elements);

0 commit comments

Comments
 (0)