@@ -20,7 +20,7 @@ class PopupListWatcher : IDisposable
2020 public bool IsVisible { get ; set ; } = false ;
2121 public string SelectedItemText { get ; set ; } = string . Empty ;
2222 public Rect SelectedItemBounds { get ; set ; } = Rect . Empty ;
23- public bool HasVerticalScrollBar { get ; set ; } = false ;
23+ public Rect ListBounds { get ; set ; } = Rect . Empty ;
2424 public IntPtr PopupListHandle => _hwndPopupList ;
2525
2626 SynchronizationContext _syncContextAuto ;
@@ -86,7 +86,7 @@ void _windowWatcher_PopupListWindowChanged(object sender, WindowWatcher.WindowCh
8686 case WindowWatcher . WindowChangedEventArgs . ChangeType . Hide :
8787 Logger . WindowWatcher . Verbose ( $ "PopupList window hide") ;
8888 IsVisible = false ;
89- UpdateSelectedItem ( _selectedItem , false ) ;
89+ UpdateSelectedItem ( _selectedItem ) ;
9090 break ;
9191 case WindowWatcher . WindowChangedEventArgs . ChangeType . Focus :
9292 case WindowWatcher . WindowChangedEventArgs . ChangeType . Unfocus :
@@ -126,12 +126,11 @@ void _windowWatcher_PopupListWindowChanged(object sender, WindowWatcher.WindowCh
126126 // This runs on an automation event handler thread
127127 void PopupListBoundsChanged ( object sender , AutomationPropertyChangedEventArgs e )
128128 {
129- if ( IsVisible && _selectedItem != null )
130- {
131- // Debug.Print($">>>> PopupListWatcher.LocationChanged on thread {Thread.CurrentThread.ManagedThreadId}");
132- // We assume the scroll bar presence has not changed
133- UpdateSelectedItem ( _selectedItem , HasVerticalScrollBar ) ;
134- }
129+ Debug . Print ( $ "##### PopupList BoundsChanged: { e . NewValue } ") ;
130+ if ( e . NewValue != null )
131+ ListBounds = ( Rect ) e . NewValue ;
132+
133+ // We don't have to trigger the update
135134
136135 //_syncContextAuto.Post(delegate
137136 //{
@@ -168,11 +167,7 @@ void PopupListElementSelectedHandler(object sender, AutomationEventArgs e)
168167 {
169168 Logger . WindowWatcher . Verbose ( $ "PopupList PopupListElementSelectedHandler on thread { Thread . CurrentThread . ManagedThreadId } ") ;
170169 var selectedItem = ( AutomationElement ) sender ;
171- // CONSIDER: Maybe monitor changes to scrollbar as an event? (for performance)
172- var walker = TreeWalker . ControlViewWalker ;
173- var list = walker . GetParent ( walker . GetParent ( selectedItem ) ) ;
174- var hasVerticalScrollBar = ( bool ) list . GetCurrentPropertyValue ( AutomationElement . IsScrollPatternAvailableProperty ) ;
175- UpdateSelectedItem ( selectedItem , hasVerticalScrollBar ) ;
170+ UpdateSelectedItem ( selectedItem ) ;
176171 }
177172
178173 // Runs on our automation thread
@@ -184,8 +179,8 @@ void InstallEventHandlers()
184179 Automation . AddAutomationEventHandler (
185180 SelectionItemPattern . ElementSelectedEvent , _popupList , TreeScope . Descendants /* was .Children */ , PopupListElementSelectedHandler ) ;
186181 Logger . WindowWatcher . Verbose ( $ "PopupList selection event handler added") ;
187- Automation . AddAutomationPropertyChangedEventHandler ( _popupList , TreeScope . Element , PopupListBoundsChanged , AutomationElement . BoundingRectangleProperty ) ;
188- Logger . WindowWatcher . Verbose ( $ "PopupList bounds change event handler added") ;
182+ // Automation.AddAutomationPropertyChangedEventHandler(_popupList, TreeScope.Element, PopupListBoundsChanged, AutomationElement.BoundingRectangleProperty);
183+ // Logger.WindowWatcher.Verbose($"PopupList bounds change event handler added");
189184 }
190185 catch ( Exception ex )
191186 {
@@ -209,14 +204,12 @@ void UpdateSelectedItem()
209204 if ( listElement != null )
210205 {
211206 var selectionPattern = listElement . GetCurrentPattern ( SelectionPattern . Pattern ) as SelectionPattern ;
212- // CONSIDER: We might dig in a big more (e.g. is horizontal scrolling possible?)
213- var hasVerticalScrollBar = ( bool ) listElement . GetCurrentPropertyValue ( AutomationElement . IsScrollPatternAvailableProperty ) ;
214207 var currentSelection = selectionPattern . Current . GetSelection ( ) ;
215208 if ( currentSelection . Length > 0 )
216209 {
217210 try
218211 {
219- UpdateSelectedItem ( currentSelection [ 0 ] , hasVerticalScrollBar ) ;
212+ UpdateSelectedItem ( currentSelection [ 0 ] ) ;
220213 }
221214 catch ( Exception ex )
222215 {
@@ -232,9 +225,9 @@ void UpdateSelectedItem()
232225
233226 // Can run on our automation thread or on any automation event thread (which is also allowed to read properties)
234227 // But might fail, if the newSelectedItem is already gone by the time we run...
235- void UpdateSelectedItem ( AutomationElement newSelectedItem , bool hasVerticalScrollBar )
228+ void UpdateSelectedItem ( AutomationElement newSelectedItem )
236229 {
237- // Debug.Print($"POPUPLISTWATCHER WINDOW CURRENT SELECTION {newSelectedItem}");
230+ Debug . Print ( $ "POPUPLISTWATCHER WINDOW CURRENT SELECTION { newSelectedItem } ") ;
238231
239232 // TODO: Sometimes the IsVisble is not updated, but we are visible and the first selection is set
240233
@@ -250,16 +243,20 @@ void UpdateSelectedItem(AutomationElement newSelectedItem, bool hasVerticalScrol
250243 _selectedItem = null ;
251244 SelectedItemText = string . Empty ;
252245 SelectedItemBounds = Rect . Empty ;
246+ ListBounds = Rect . Empty ;
253247 }
254248 else
255249 {
256250 string selectedItemText = string . Empty ;
257251 Rect selectedItemBounds = Rect . Empty ;
252+ Rect listBounds = Rect . Empty ;
258253
259254 try
260255 {
261256 selectedItemText = ( string ) newSelectedItem . GetCurrentPropertyValue ( AutomationElement . NameProperty ) ;
262257 selectedItemBounds = ( Rect ) newSelectedItem . GetCurrentPropertyValue ( AutomationElement . BoundingRectangleProperty ) ;
258+ listBounds = ( Rect ) _popupList . GetCurrentPropertyValue ( AutomationElement . BoundingRectangleProperty ) ;
259+ Debug . Print ( $ "#### PopupList Update - ListBounds: { listBounds } / SelectedItemBounds: { selectedItemBounds } ") ;
263260 }
264261 catch ( Exception ex )
265262 {
@@ -269,19 +266,19 @@ void UpdateSelectedItem(AutomationElement newSelectedItem, bool hasVerticalScrol
269266 }
270267
271268 _selectedItem = newSelectedItem ;
269+ ListBounds = listBounds ;
272270 SelectedItemText = selectedItemText ;
273271 SelectedItemBounds = selectedItemBounds ;
274272 // Debug.Print($"SelectedItemBounds: {SelectedItemBounds}");
275273 }
276- HasVerticalScrollBar = hasVerticalScrollBar ;
277274 OnSelectedItemChanged ( ) ;
278275 }
279276
280277 // Raises the event on the automation thread (but the SyncContext.Post here is redundant)
281278 void OnSelectedItemChanged ( )
282279 {
283- Logger . WindowWatcher . Verbose ( $ "PopupList SelectedItemChanged { SelectedItemText } ( { ( HasVerticalScrollBar ? "Scroll" : "NoScroll" ) } ) ") ;
284- _syncContextAuto . Post ( _ => SelectedItemChanged ( this , EventArgs . Empty ) , null ) ;
280+ Logger . WindowWatcher . Verbose ( $ "PopupList SelectedItemChanged { SelectedItemText } ListBounds: { ListBounds } ") ;
281+ _syncContextAuto . Post ( _ => SelectedItemChanged ? . Invoke ( this , EventArgs . Empty ) , null ) ;
285282 }
286283
287284 public void Dispose ( )
0 commit comments