11using System . Collections . ObjectModel ;
22using System . Drawing . Imaging ;
3+ using System . Runtime . CompilerServices ;
34using System . Windows ;
45using System . Windows . Input ;
56using 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