Skip to content

Commit e078594

Browse files
committed
Restore PopupListWatcher initialization
1 parent 60ee775 commit e078594

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

Source/ExcelDna.IntelliSense/IntelliSenseDisplay.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ bool ShouldProcessFormulaEditTextChange(string formulaPrefix)
118118
// This runs on the main thread
119119
void StateUpdate(object sender, UIStateUpdate update)
120120
{
121-
Debug.Print($"STATE UPDATE ({update.Update}): {update.OldState} => {update.NewState}");
121+
Debug.Print($"STATE UPDATE ({update.Update}): \r\t\t\t{update.OldState} \r\t\t=>\t{update.NewState}");
122122

123123
switch (update.Update)
124124
{

Source/ExcelDna.IntelliSense/Watchers.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ public IntPtr FormulaEditWindow // Move to FormulaEditWatcher ??
7373
public event EventHandler InCellEditWindowChanged;
7474
public event EventHandler FormulaEditFocusChanged;
7575
public event EventHandler<WindowChangedEventArgs> PopupListWindowChanged; // Might start off with nothing. Changes at most once.?????
76-
public event EventHandler IsPopupListWindowsVisibleChanged;
7776
// public event EventHandler PopupListListChanged = delegate { }; // Might start off with nothing. Changes at most once.??????
7877
public event EventHandler<WindowChangedEventArgs> SelectDataSourceWindowChanged;
79-
public event EventHandler IsSelectDataSourceWindowVisibleChanged;
8078

8179
public WindowWatcher(SynchronizationContext syncContextAuto)
8280
{
@@ -155,7 +153,7 @@ void _windowStateChangeHook_WinEventReceived(object sender, WinEventHook.WinEven
155153
if (e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_CREATE ||
156154
e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_SHOW) // SHOW also since we might be installed later...
157155
{
158-
// Debug.Assert(PopupListWindow == IntPtr.Zero || e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_SHOW, "Unexpected new PopupList window...????");
156+
Debug.Assert(PopupListWindow == IntPtr.Zero || PopupListWindow == e.WindowHandle, "Unexpected new PopupList window...????");
159157
PopupListWindow = e.WindowHandle;
160158
PopupListWindowChanged?.Invoke(this,
161159
new WindowChangedEventArgs { Type =
@@ -578,7 +576,7 @@ public void Dispose()
578576
// We ignore the reason for showing, and match purely on the text of the selected item.
579577
class PopupListWatcher : IDisposable
580578
{
581-
AutomationElement _mainWindow;
579+
// AutomationElement _mainWindow;
582580
IntPtr _hwndPopupList;
583581
AutomationElement _popupList;
584582
AutomationElement _popupListList;
@@ -609,6 +607,7 @@ void _windowWatcher_PopupListWindowChanged(object sender, WindowWatcher.WindowCh
609607
switch (e.Type)
610608
{
611609
case WindowWatcher.WindowChangedEventArgs.ChangeType.Create:
610+
Debug.Print("POPUPLISTWATCHER WINDOW CREATED");
612611
// TODO: Confirm that this runs at most once
613612
var hWnd = _windowWatcher.PopupListWindow;
614613
// Debug.Print($">>>> PopupListWatcher - PopupListWindowChanged - New window {hWnd}");
@@ -622,6 +621,7 @@ void _windowWatcher_PopupListWindowChanged(object sender, WindowWatcher.WindowCh
622621
// Automation.AddAutomationPropertyChangedEventHandler(_popupList, TreeScope.Element, PopupListVisibleChangedHandler, AutomationElement.???Visible);
623622
break;
624623
case WindowWatcher.WindowChangedEventArgs.ChangeType.Destroy:
624+
Debug.Print("POPUPLISTWATCHER WINDOW DESTROY");
625625
try
626626
{
627627

@@ -636,9 +636,16 @@ void _windowWatcher_PopupListWindowChanged(object sender, WindowWatcher.WindowCh
636636
// Debug.Assert(false, "PopupList window destroyed...???");
637637
break;
638638
case WindowWatcher.WindowChangedEventArgs.ChangeType.Show:
639+
Debug.Print("POPUPLISTWATCHER WINDOW SHOW");
639640
IsVisible = true;
641+
if (_popupListList == null)
642+
{
643+
// TODO: Clean up this hack
644+
PopupListStructureChangedHandler(_popupList, new StructureChangedEventArgs(StructureChangeType.ChildAdded, new int[0]));
645+
}
640646
break;
641647
case WindowWatcher.WindowChangedEventArgs.ChangeType.Hide:
648+
Debug.Print("POPUPLISTWATCHER WINDOW HIDE");
642649
IsVisible = false;
643650
UpdateSelectedItem(_selectedItem);
644651
break;
@@ -723,7 +730,7 @@ void PopupListElementSelectedHandler(object sender, AutomationEventArgs e)
723730
// Runs on an automation event thread
724731
void PopupListStructureChangedHandler(object sender, StructureChangedEventArgs e)
725732
{
726-
// Debug.Print($">>>> PopupListWatcher.PopupListStructureChangedHandler ({e.StructureChangeType}) on thread {Thread.CurrentThread.ManagedThreadId}");
733+
Debug.Print($">>>> PopupListWatcher.PopupListStructureChangedHandler ({e.StructureChangeType}) on thread {Thread.CurrentThread.ManagedThreadId}");
727734
// Debug.WriteLine($">>> PopupList structure changed - {e.StructureChangeType}");
728735
// CONSIDER: Others too?
729736
if (e.StructureChangeType == StructureChangeType.ChildAdded)
@@ -743,14 +750,16 @@ void PopupListStructureChangedHandler(object sender, StructureChangedEventArgs e
743750
// TestMoveWindow(_popupListList, (int)listRect.X, (int)listRect.Y);
744751
// TestMoveWindow(functionList, 0, 0);
745752

746-
var selPat = listElement.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern;
753+
// If the _popupListList automation element is no plonger valid then we seem to get a InvalidPattern exception here.
754+
var selPat = _popupListList.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern;
747755
Debug.Assert(selPat != null);
748756

749757
// CONSIDER: Send might be a bit disruptive here / might not be necessary...
750758
// _syncContextAuto.Send( _ =>
751759
//{
752760
try
753761
{
762+
Debug.Print("POPUPLISTWATCHER WINDOW SELECTION EVENT HANDLER ADDED");
754763
Automation.AddAutomationEventHandler(
755764
SelectionItemPattern.ElementSelectedEvent, _popupListList, TreeScope.Descendants /* was .Children */, PopupListElementSelectedHandler);
756765
}
@@ -784,11 +793,14 @@ void PopupListStructureChangedHandler(object sender, StructureChangedEventArgs e
784793
{
785794
if (_popupListList != null)
786795
{
796+
var selPat = _popupListList.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern;
797+
Debug.Assert(selPat != null);
787798
// CONSIDER: Send might be a bit disruptive here / might not be necessary...
788799
//_syncContextAuto.Send( _ =>
789800
//{
790801
try
791802
{
803+
Debug.Print("POPUPLISTWATCHER WINDOW SELECTION EVENT HANDLER REMOVED");
792804
Automation.RemoveAutomationEventHandler(SelectionItemPattern.ElementSelectedEvent, _popupListList, PopupListElementSelectedHandler);
793805
}
794806
catch (Exception ex)
@@ -817,6 +829,7 @@ void TestMoveWindow(AutomationElement listWindow, int xOffset, int yOffset)
817829
// But might fail, if the newSelectedItem is already gone by the time we run...
818830
void UpdateSelectedItem(AutomationElement newSelectedItem)
819831
{
832+
Debug.Print($"POPUPLISTWATCHER WINDOW CURRENT SELECTION {newSelectedItem}");
820833
if (!IsVisible || newSelectedItem == null)
821834
{
822835
if (_selectedItem == null &&
@@ -858,6 +871,8 @@ void UpdateSelectedItem(AutomationElement newSelectedItem)
858871
// Raises the event on the automation thread (but the SyncContext.Post here is redundant)
859872
void OnSelectedItemChanged()
860873
{
874+
Debug.Print("POPUPLISTWATCHER WINDOW SELECTEDITEM CHANGED");
875+
861876
_syncContextAuto.Post(_ => SelectedItemChanged(this, EventArgs.Empty), null);
862877
}
863878

0 commit comments

Comments
 (0)