-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ItemAutomationPeer.cs
804 lines (703 loc) · 27.5 KB
/
ItemAutomationPeer.cs
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Windows;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using MS.Internal;
using MS.Internal.Data;
using MS.Win32;
namespace System.Windows.Automation.Peers
{
///<summary>
/// Earlier this class was returning the default value for all properties when there is no wrapper/when it is virtualized,
/// now it will throw ElementNotAvailableException (leaving some exceptions, like properties supported by container to find elements)
/// to notify the client that the full Element does not exist yet. Client may decide to use VirtualizedItemPattern to realize the full item
///</summary>
public abstract class ItemAutomationPeer : AutomationPeer, IVirtualizedItemProvider
{
///
protected ItemAutomationPeer(object item, ItemsControlAutomationPeer itemsControlAutomationPeer): base()
{
Item = item;
_itemsControlAutomationPeer = itemsControlAutomationPeer;
}
///
internal override bool AncestorsInvalid
{
get { return base.AncestorsInvalid; }
set
{
base.AncestorsInvalid = value;
if (value)
return;
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
{
wrapperPeer.AncestorsInvalid = false;
}
}
}
///
override public object GetPattern(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.VirtualizedItem)
{
if(VirtualizedItemPatternIdentifiers.Pattern != null)
{
if(GetWrapperPeer() == null)
return this;
else
{
// ItemsControlAutomationPeer can be null in case of TreeViewItems when parent TreeViewItem is also virtualized
// If the Item is in Automation Tree we consider it has Realized and need not return VirtualizeItem pattern.
if(ItemsControlAutomationPeer != null && !IsItemInAutomationTree())
{
return this;
}
if(ItemsControlAutomationPeer == null)
return this;
}
}
return null;
}
else if(patternInterface == PatternInterface.SynchronizedInput)
{
UIElementAutomationPeer peer = GetWrapperPeer() as UIElementAutomationPeer;
if(peer != null)
{
return peer.GetPattern(patternInterface);
}
}
return null;
}
internal UIElement GetWrapper()
{
UIElement wrapper = null;
ItemsControlAutomationPeer itemsControlAutomationPeer = ItemsControlAutomationPeer;
if (itemsControlAutomationPeer != null)
{
ItemsControl owner = (ItemsControl)(itemsControlAutomationPeer.Owner);
if (owner != null)
{
object item = RawItem;
if (item != DependencyProperty.UnsetValue)
{
if (((MS.Internal.Controls.IGeneratorHost)owner).IsItemItsOwnContainer(item))
wrapper = item as UIElement;
else
wrapper = owner.ItemContainerGenerator.ContainerFromItem(item) as UIElement;
}
}
}
return wrapper;
}
virtual internal AutomationPeer GetWrapperPeer()
{
AutomationPeer wrapperPeer = null;
UIElement wrapper = GetWrapper();
if(wrapper != null)
{
wrapperPeer = UIElementAutomationPeer.CreatePeerForElement(wrapper);
if(wrapperPeer == null) //fall back to default peer if there is no specific one
{
if(wrapper is FrameworkElement)
wrapperPeer = new FrameworkElementAutomationPeer((FrameworkElement)wrapper);
else
wrapperPeer = new UIElementAutomationPeer(wrapper);
}
}
return wrapperPeer;
}
/// <summary>
internal void ThrowElementNotAvailableException()
{
// To avoid the situation on legacy systems which may not have new unmanaged core. this check with old unmanaged core
// avoids throwing exception and provide older behavior returning default values for items which are virtualized rather than throwing exception.
if (VirtualizedItemPatternIdentifiers.Pattern != null && !(this is GridViewItemAutomationPeer) && !IsItemInAutomationTree())
throw new ElementNotAvailableException(SR.VirtualizedElement);
}
private bool IsItemInAutomationTree()
{
AutomationPeer parent = this.GetParent();
if(this.Index != -1 && parent != null && parent.Children != null && this.Index < parent.Children.Count && parent.Children[this.Index] == this)
return true;
else return false;
}
///
override internal bool IgnoreUpdatePeer()
{
// Ignore UpdatePeer if the we're no longer in the automation tree.
// There's no need to update such a peer, as it no longer
// participates in automation. And UpdatePeer actually throws exceptions
// in some cases.
if (!IsItemInAutomationTree())
{
return true;
}
return base.IgnoreUpdatePeer();
}
override internal bool IsDataItemAutomationPeer()
{
return true;
}
override internal void AddToParentProxyWeakRefCache()
{
ItemsControlAutomationPeer itemsControlAutomationPeer = ItemsControlAutomationPeer;
if(itemsControlAutomationPeer != null)
{
itemsControlAutomationPeer.AddProxyToWeakRefStorage(this.ElementProxyWeakReference, this);
}
}
/// <summary>
override internal Rect GetVisibleBoundingRectCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
{
return wrapperPeer.GetVisibleBoundingRectCore();
}
return GetBoundingRectangle();
}
///
override protected string GetItemTypeCore()
{
return string.Empty;
}
///
protected override List<AutomationPeer> GetChildrenCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
{
// The children needs to be updated before GetChildren call as ChildrenValid flag would already be true and GetChildren call won't update the children list.
wrapperPeer.ForceEnsureChildren();
List<AutomationPeer> children = wrapperPeer.GetChildren();
return children;
}
return null;
}
///
protected override Rect GetBoundingRectangleCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
{
return wrapperPeer.GetBoundingRectangle();
}
else
ThrowElementNotAvailableException();
return new Rect();
}
///
protected override bool IsOffscreenCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.IsOffscreen();
else
ThrowElementNotAvailableException();
return true;
}
///
protected override AutomationOrientation GetOrientationCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.GetOrientation();
else
ThrowElementNotAvailableException();
return AutomationOrientation.None;
}
///
override protected AutomationHeadingLevel GetHeadingLevelCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
AutomationHeadingLevel headingLevel = AutomationHeadingLevel.None;
if(wrapperPeer != null)
{
headingLevel = wrapperPeer.GetHeadingLevel();
}
else
{
ThrowElementNotAvailableException();
}
return headingLevel;
}
/// <summary>
/// Gets the position of an item within a set.
/// </summary>
/// <remarks>
/// If <see cref="AutomationProperties.PositionInSetProperty"/> hasn't been set
/// this method will calculate the position of an item based on its parent ItemsControl,
/// if the ItemsControl is grouping the position will be relative to the group containing this item.
/// </remarks>
/// <returns>
/// The value of <see cref="AutomationProperties.PositionInSetProperty"/> if it has been set, or it's position relative to the parent ItemsControl or GroupItem.
/// </returns>
protected override int GetPositionInSetCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
{
int position = wrapperPeer.GetPositionInSet();
if (position == AutomationProperties.AutomationPositionInSetDefault)
{
ItemsControl parentItemsControl = (ItemsControl)ItemsControlAutomationPeer.Owner;
position = GetPositionInSetFromItemsControl(parentItemsControl, Item);
}
return position;
}
else
{
ThrowElementNotAvailableException();
}
return AutomationProperties.AutomationPositionInSetDefault;
}
/// <summary>
/// Gets the size of a set that contains this item.
/// </summary>
/// <remarks>
/// If <see cref="AutomationProperties.SizeOfSetProperty"/> hasn't been set
/// this method will calculate the size of the set containing this item based on its parent ItemsControl,
/// if the ItemsControl is grouping the value will be representative of the group containing this item.
/// </remarks>
/// <returns>
/// The value of <see cref="AutomationProperties.SizeOfSetProperty"/> if it has been set, or the size of the parent ItemsControl or GroupItem.
/// </returns>
protected override int GetSizeOfSetCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
{
int size = wrapperPeer.GetSizeOfSet();
if (size == AutomationProperties.AutomationSizeOfSetDefault)
{
ItemsControl parentItemsControl = (ItemsControl)ItemsControlAutomationPeer.Owner;
size = GetSizeOfSetFromItemsControl(parentItemsControl, Item);
}
return size;
}
else
{
ThrowElementNotAvailableException();
}
return AutomationProperties.AutomationSizeOfSetDefault;
}
internal static int GetPositionInSetFromItemsControl(ItemsControl itemsControl, object item)
{
int position = AutomationProperties.AutomationPositionInSetDefault;
ItemCollection itemCollection = itemsControl.Items;
position = itemCollection.IndexOf(item);
if (itemsControl.IsGrouping)
{
int sizeOfGroup;
position = FindPositionInGroup(itemCollection.Groups, position, out sizeOfGroup);
}
return position + 1;
}
internal static int GetSizeOfSetFromItemsControl(ItemsControl itemsControl, object item)
{
int size = AutomationProperties.AutomationSizeOfSetDefault;
ItemCollection itemCollection = itemsControl.Items;
if (itemsControl.IsGrouping)
{
int position = itemCollection.IndexOf(item);
FindPositionInGroup(itemCollection.Groups, position, out size);
}
else
{
size = itemCollection.Count;
}
return size;
}
/// <summary>
/// Based on the position of an item in the owner's item collection determine which group the item belongs to
/// and return the relative position, also provide the size of the group
/// </summary>
/// <param name="collection">The top-level collection of groups</param>
/// <param name="position">The position of the item in the flattened item collection</param>
/// <param name="sizeOfGroup">out parameter to return the size of the group we found</param>
/// <returns>The position of the item relative to the found group</returns>
private static int FindPositionInGroup(ReadOnlyObservableCollection<object> collection, int position, out int sizeOfGroup)
{
CollectionViewGroupInternal currentGroup = null;
ReadOnlyObservableCollection<object> newCollection = null;
sizeOfGroup = AutomationProperties.AutomationSizeOfSetDefault;
do
{
newCollection = null;
foreach (CollectionViewGroupInternal group in collection)
{
if (position < group.ItemCount)
{
currentGroup = group;
if (currentGroup.IsBottomLevel)
{
newCollection = null;
sizeOfGroup = group.ItemCount;
}
else
{
newCollection = currentGroup.Items;
}
break;
}
else
{
position -= group.ItemCount;
}
}
} while ((collection = newCollection) != null);
return position;
}
///
protected override string GetItemStatusCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.GetItemStatus();
else
ThrowElementNotAvailableException();
return string.Empty;
}
///
protected override bool IsRequiredForFormCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.IsRequiredForForm();
else
ThrowElementNotAvailableException();
return false;
}
///
protected override bool IsKeyboardFocusableCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.IsKeyboardFocusable();
else
ThrowElementNotAvailableException();
return false;
}
///
protected override bool HasKeyboardFocusCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.HasKeyboardFocus();
else
ThrowElementNotAvailableException();
return false;
}
///
protected override bool IsEnabledCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.IsEnabled();
else
ThrowElementNotAvailableException();
return false;
}
///
protected override bool IsDialogCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.IsDialog();
else
ThrowElementNotAvailableException();
return false;
}
///
protected override bool IsPasswordCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.IsPassword();
else
ThrowElementNotAvailableException();
return false;
}
///
protected override string GetAutomationIdCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
string id = null;
object item;
if (wrapperPeer != null)
{
id = wrapperPeer.GetAutomationId();
}
else if ((item = Item) != null)
{
using (RecyclableWrapper recyclableWrapper = ItemsControlAutomationPeer.GetRecyclableWrapperPeer(item))
{
id = recyclableWrapper.Peer.GetAutomationId();
}
}
return id;
}
///
protected override string GetNameCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
string name = null;
object item = Item;
if (wrapperPeer != null)
{
name = wrapperPeer.GetName();
}
// see https://github.com/dotnet/wpf/issues/122
else if (item != null && ItemsControlAutomationPeer is { } itemsControlAutomationPeer)
{
using (RecyclableWrapper recyclableWrapper = itemsControlAutomationPeer.GetRecyclableWrapperPeer(item))
{
name = recyclableWrapper.Peer.GetName();
}
}
if (string.IsNullOrEmpty(name) && item != null)
{
// For FE we can't use ToString as that provides extraneous information than just the plain text
FrameworkElement fe = item as FrameworkElement;
if(fe != null)
name = fe.GetPlainText();
if(string.IsNullOrEmpty(name))
name = item.ToString();
}
return name;
}
///
protected override bool IsContentElementCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.IsContentElement();
return true;
}
///
protected override bool IsControlElementCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.IsControlElement();
return true;
}
///
protected override AutomationPeer GetLabeledByCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.GetLabeledBy();
else
ThrowElementNotAvailableException();
return null;
}
///
protected override AutomationLiveSetting GetLiveSettingCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.GetLiveSetting();
else
ThrowElementNotAvailableException();
return AutomationLiveSetting.Off;
}
///
protected override string GetHelpTextCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.GetHelpText();
else
ThrowElementNotAvailableException();
return string.Empty;
}
///
protected override string GetAcceleratorKeyCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.GetAcceleratorKey();
else
ThrowElementNotAvailableException();
return string.Empty;
}
///
protected override string GetAccessKeyCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.GetAccessKey();
else
ThrowElementNotAvailableException();
return string.Empty;
}
///
protected override Point GetClickablePointCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
return wrapperPeer.GetClickablePoint();
else
ThrowElementNotAvailableException();
return new Point(double.NaN, double.NaN);
}
///
protected override void SetFocusCore()
{
AutomationPeer wrapperPeer = GetWrapperPeer();
if (wrapperPeer != null)
wrapperPeer.SetFocus();
else
ThrowElementNotAvailableException();
}
virtual internal ItemsControlAutomationPeer GetItemsControlAutomationPeer()
{
return _itemsControlAutomationPeer;
}
///
public object Item
{
get
{
ItemWeakReference iwr = _item as ItemWeakReference;
return (iwr != null) ? iwr.Target : _item;
}
private set
{
if (value != null && !value.GetType().IsValueType &&
!FrameworkAppContextSwitches.ItemAutomationPeerKeepsItsItemAlive)
{
_item = new ItemWeakReference(value);
}
else
{
_item = value;
}
}
}
private object RawItem
{
get
{
ItemWeakReference iwr = _item as ItemWeakReference;
if (iwr != null)
{
object item = iwr.Target;
return (item == null) ? DependencyProperty.UnsetValue : item;
}
else
{
return _item;
}
}
}
// Rebuilding the "item" children of an ItemsControlAP or GroupItemAP re-uses
// ItemAutomationPeers for items that already had peers. Usually this happens
// when the items are the same (the same object), but it can also happen
// when the items are different objects but are equal in the Object.Equals sense.
// In the latter case, we need to update the weak reference to point to
// the new item, as the old item has a different lifetime.
internal void ReuseForItem(object item)
{
System.Diagnostics.Debug.Assert(Object.Equals(item, Item), "ItemPeer reuse for an unequal item is not supported");
ItemWeakReference iwr = _item as ItemWeakReference;
if (iwr != null)
{
if (!Object.ReferenceEquals(item, iwr.Target))
{
iwr.Target = item;
}
}
else
{
_item = item;
}
}
///
public ItemsControlAutomationPeer ItemsControlAutomationPeer
{
get
{
return GetItemsControlAutomationPeer();
}
internal set
{
_itemsControlAutomationPeer = value;
}
}
///
void IVirtualizedItemProvider.Realize()
{
RealizeCore();
}
virtual internal void RealizeCore()
{
ItemsControlAutomationPeer itemsControlAutomationPeer = ItemsControlAutomationPeer;
if (itemsControlAutomationPeer != null)
{
ItemsControl parent = itemsControlAutomationPeer.Owner as ItemsControl;
if (parent != null)
{
if (parent.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
{
if (AccessibilitySwitches.UseNetFx472CompatibleAccessibilityFeatures)
{
// Please note that this action must happen before the OnBringItemIntoView call because
// that is a call that synchronously flushes out layout and we want these realized peers
// cached before the UpdateSubtree kicks in OnLayoutUpdated.
if (VirtualizingPanel.GetIsVirtualizingWhenGrouping(parent))
{
itemsControlAutomationPeer.RecentlyRealizedPeers.Add(this);
}
}
parent.OnBringItemIntoView(Item);
}
else
{
// The items aren't generated, try at a later time
Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
(DispatcherOperationCallback)delegate(object arg)
{
if (AccessibilitySwitches.UseNetFx472CompatibleAccessibilityFeatures)
{
// Please note that this action must happen before the OnBringItemIntoView call because
// that is a call that synchronously flushes out layout and we want these realized peers
// cached before the UpdateSubtree kicks in OnLayoutUpdated.
if (VirtualizingPanel.GetIsVirtualizingWhenGrouping(parent))
{
itemsControlAutomationPeer.RecentlyRealizedPeers.Add(this);
}
}
parent.OnBringItemIntoView(arg);
return null;
}, Item);
}
}
}
}
private object _item; // for value-types: item; for reference-types: IWR(item)
private ItemsControlAutomationPeer _itemsControlAutomationPeer;
// a weak reference that is distinguishable from System.WeakReference
private class ItemWeakReference : WeakReference
{
public ItemWeakReference(object o)
: base(o)
{}
}
}
}