-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathClientEventList.cs
More file actions
205 lines (172 loc) · 7.01 KB
/
ClientEventList.cs
File metadata and controls
205 lines (172 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// (c) Copyright Microsoft, 2012.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Automation;
using UIAutomationClient = Interop.UIAutomationClient;
using IUIAutomationElement = Interop.UIAutomationClient.IUIAutomationElement;
namespace UIAComWrapperInternal
{
internal class EventListener
{
private int _eventId;
private int[] _runtimeId;
private Delegate _handler;
public EventListener(int eventId, int[] runtimeId, Delegate handler)
{
Debug.Assert(handler != null);
this._eventId = eventId;
this._runtimeId = runtimeId;
this._handler = handler;
}
public int EventId
{
get { return _eventId; }
}
public int[] RuntimeId
{
get { return _runtimeId; }
}
public Delegate Handler
{
get { return _handler; }
}
public override bool Equals(object obj)
{
EventListener listener = obj as EventListener;
return (listener != null &&
this._eventId == listener.EventId &&
this._handler == listener.Handler &&
Automation.Compare(this._runtimeId, listener.RuntimeId));
}
public override int GetHashCode()
{
return _handler.GetHashCode();
}
}
internal class FocusEventListener : EventListener, UIAutomationClient.IUIAutomationFocusChangedEventHandler
{
private AutomationFocusChangedEventHandler _focusHandler;
public FocusEventListener(AutomationFocusChangedEventHandler handler) :
base(AutomationElement.AutomationFocusChangedEvent.Id, null, handler)
{
Debug.Assert(handler != null);
this._focusHandler = handler;
}
#region IUIAutomationFocusChangedEventHandler Members
void UIAutomationClient.IUIAutomationFocusChangedEventHandler.HandleFocusChangedEvent(
UIAutomationClient.IUIAutomationElement sender)
{
// Can't set the arguments -- they come from a WinEvent handler.
AutomationFocusChangedEventArgs args = new AutomationFocusChangedEventArgs(0, 0);
_focusHandler(AutomationElement.Wrap(sender), args);
}
#endregion
}
internal class BasicEventListener : EventListener, UIAutomationClient.IUIAutomationEventHandler
{
private AutomationEventHandler _basicHandler;
public BasicEventListener(AutomationEvent eventKind, AutomationElement element, AutomationEventHandler handler) :
base(eventKind.Id, element.GetRuntimeId(), handler)
{
Debug.Assert(handler != null);
this._basicHandler = handler;
}
#region IUIAutomationEventHandler Members
void UIAutomationClient.IUIAutomationEventHandler.HandleAutomationEvent(
UIAutomationClient.IUIAutomationElement sender, int eventId)
{
AutomationEventArgs args;
if (eventId != WindowPatternIdentifiers.WindowClosedEvent.Id)
{
args = new AutomationEventArgs(AutomationEvent.LookupById(eventId));
}
else
{
args = new WindowClosedEventArgs((int[])sender.GetRuntimeId());
}
_basicHandler(AutomationElement.Wrap(sender), args);
}
#endregion
}
internal class PropertyEventListener : EventListener, UIAutomationClient.IUIAutomationPropertyChangedEventHandler
{
private AutomationPropertyChangedEventHandler _propChangeHandler;
public PropertyEventListener(AutomationEvent eventKind, AutomationElement element, AutomationPropertyChangedEventHandler handler) :
base(AutomationElement.AutomationPropertyChangedEvent.Id, element.GetRuntimeId(), handler)
{
Debug.Assert(handler != null);
this._propChangeHandler = handler;
}
#region IUIAutomationPropertyChangedEventHandler Members
void UIAutomationClient.IUIAutomationPropertyChangedEventHandler.HandlePropertyChangedEvent(
UIAutomationClient.IUIAutomationElement sender,
int propertyId,
object newValue)
{
AutomationProperty property = AutomationProperty.LookupById(propertyId);
object wrappedObj = Utility.WrapObjectAsProperty(property, newValue);
AutomationPropertyChangedEventArgs args = new AutomationPropertyChangedEventArgs(
property,
null,
wrappedObj);
this._propChangeHandler(AutomationElement.Wrap(sender), args);
}
#endregion
}
internal class StructureEventListener : EventListener, UIAutomationClient.IUIAutomationStructureChangedEventHandler
{
private StructureChangedEventHandler _structureChangeHandler;
public StructureEventListener(AutomationEvent eventKind, AutomationElement element, StructureChangedEventHandler handler) :
base(AutomationElement.StructureChangedEvent.Id, element.GetRuntimeId(), handler)
{
Debug.Assert(handler != null);
this._structureChangeHandler = handler;
}
public void HandleStructureChangedEvent(IUIAutomationElement sender, UIAutomationClient.StructureChangeType changeType, int[] runtimeId)
{
StructureChangedEventArgs args = new StructureChangedEventArgs(
(StructureChangeType)changeType,
(int[])runtimeId);
this._structureChangeHandler(AutomationElement.Wrap(sender), args);
}
}
internal class ClientEventList
{
private static readonly System.Collections.Generic.LinkedList<EventListener> _events = new LinkedList<EventListener>();
public static void Add(EventListener listener)
{
lock (_events)
{
_events.AddLast(listener);
}
}
public static EventListener Remove(AutomationEvent eventId, AutomationElement element, Delegate handler)
{
// Create a prototype to seek
int[] runtimeId = (element == null) ? null : element.GetRuntimeId();
EventListener prototype = new EventListener(eventId.Id, runtimeId, handler);
lock (_events)
{
LinkedListNode<EventListener> node = _events.Find(prototype);
if (node == null)
{
throw new ArgumentException("event handler not found");
}
EventListener result = node.Value;
_events.Remove(node);
return result;
}
}
public static void Clear()
{
lock (_events)
{
_events.Clear();
}
}
}
}