From 41c2602d2e4596490632c2560e2c5a466607608f Mon Sep 17 00:00:00 2001 From: Anjali Sheel Date: Fri, 1 Jul 2022 15:46:16 +0530 Subject: [PATCH 01/13] Fix for Bug 1493635: MenuItems with IsCheckable and StaysOpenOnClick set to true do not announce to Narrator when IsChecked is changed (#6706) * Fix for Bug 1493635: MenuItems with IsCheckable and StaysOpenOnClick set to true do not announce to Narrator when IsChecked is changed * Fix for Bug 1493635: MenuItems with IsCheckable and StaysOpenOnClick set to true do not announce to Narrator when IsChecked is changed * Fix for Bug 1493635: MenuItems with IsCheckable and StaysOpenOnClick set to true do not announce to Narrator when IsChecked is changed * Fix for Bug 1493635: MenuItems with IsCheckable and StaysOpenOnClick set to true do not announce to Narrator when IsChecked is changed * Fix for Bug 1493635: MenuItems with IsCheckable and StaysOpenOnClick set to true do not announce to Narrator when IsChecked is changed --- .../Automation/Peers/MenuItemAutomationPeer.cs | 9 +++++++++ .../System/Windows/Controls/MenuItem.cs | 11 ++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs index 0cf50a7b53d..07edb2ad590 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs @@ -316,6 +316,15 @@ internal void RaiseExpandCollapseAutomationEvent(bool oldValue, bool newValue) newValue ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed); } + internal void RaiseToggleStatePropertyChangedEvent(bool oldValue, bool newValue) + { + RaisePropertyChangedEvent(TogglePatternIdentifiers.ToggleStateProperty, + oldValue ? ConvertToToggleState(oldValue) : ConvertToToggleState(newValue), + newValue ? ConvertToToggleState(oldValue) : ConvertToToggleState(newValue)); + } + + private static ToggleState ConvertToToggleState(bool value) => value ? ToggleState.On : ToggleState.Off; + // Return the base without the AccessKey character /// override protected string GetNameCore() diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/MenuItem.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/MenuItem.cs index 18b01f43097..5891c18c4fc 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/MenuItem.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/MenuItem.cs @@ -946,7 +946,10 @@ private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyCha { MenuItem menuItem = (MenuItem) d; - if ((bool) e.NewValue) + bool oldValue = (bool)e.OldValue; + bool newValue = (bool)e.NewValue; + + if (newValue) { menuItem.OnChecked(new RoutedEventArgs(CheckedEvent)); } @@ -954,6 +957,12 @@ private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyCha { menuItem.OnUnchecked(new RoutedEventArgs(UncheckedEvent)); } + + MenuItemAutomationPeer peer = UIElementAutomationPeer.FromElement(menuItem) as MenuItemAutomationPeer; + if (peer != null) + { + peer.RaiseToggleStatePropertyChangedEvent(oldValue, newValue); + } } /// From ea86eac0342ce71c3e8262a86756104be7a1ced2 Mon Sep 17 00:00:00 2001 From: Anjali Sheel Date: Mon, 4 Jul 2022 17:17:30 +0530 Subject: [PATCH 02/13] Added NoInlining attribute (#6750) --- .../System/Windows/Automation/Peers/MenuItemAutomationPeer.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs index 07edb2ad590..10bce1bcb12 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/MenuItemAutomationPeer.cs @@ -316,6 +316,7 @@ internal void RaiseExpandCollapseAutomationEvent(bool oldValue, bool newValue) newValue ? ExpandCollapseState.Expanded : ExpandCollapseState.Collapsed); } + [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] internal void RaiseToggleStatePropertyChangedEvent(bool oldValue, bool newValue) { RaisePropertyChangedEvent(TogglePatternIdentifiers.ToggleStateProperty, From 2ac68918e2cd0795ec0827950fd99a1e1bef61cf Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Mon, 4 Jul 2022 17:54:28 +0600 Subject: [PATCH 03/13] Fix ZeroForNow parameter (#6657) `int` is not the same as `const TF_HALTCOND *` on x64 arch --- .../Shared/MS/Win32/UnsafeNativeMethodsTextServices.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsTextServices.cs b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsTextServices.cs index 4de87412c39..e2cfe212245 100644 --- a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsTextServices.cs +++ b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsTextServices.cs @@ -1972,14 +1972,14 @@ void SetText(int ec, /*SetTextFlags*/ int flags, // [out] LONG *pcch, // [in, unique] const TF_HALTCOND *pHalt); /// - void ShiftStart(int ec, int count, out int result, int ZeroForNow); // "ZeroForNow" should be a struct ptr if we ever use this + void ShiftStart(int ec, int count, out int result, IntPtr pHalt); //HRESULT ShiftEnd([in] TfEditCookie ec, // [in] LONG cchReq, // [out] LONG *pcch, // [in, unique] const TF_HALTCOND *pHalt); /// - void ShiftEnd(int ec, int count, out int result, int ZeroForNow); // "ZeroForNow" should be a struct ptr if we ever use this + void ShiftEnd(int ec, int count, out int result, IntPtr pHalt); //HRESULT ShiftStartToRange([in] TfEditCookie ec, // [in] ITfRange *pRange, @@ -2117,14 +2117,14 @@ void SetText(int ec, /*SetTextFlags*/ int flags, // [out] LONG *pcch, // [in, unique] const TF_HALTCOND *pHalt); /// - void ShiftStart(int ec, int count, out int result, int ZeroForNow); // "ZeroForNow" should be a struct ptr if we ever use this + void ShiftStart(int ec, int count, out int result, IntPtr pHalt); //HRESULT ShiftEnd([in] TfEditCookie ec, // [in] LONG cchReq, // [out] LONG *pcch, // [in, unique] const TF_HALTCOND *pHalt); /// - void ShiftEnd(int ec, int count, out int result, int ZeroForNow); // "ZeroForNow" should be a struct ptr if we ever use this + void ShiftEnd(int ec, int count, out int result, IntPtr pHalt); //HRESULT ShiftStartToRange([in] TfEditCookie ec, // [in] ITfRange *pRange, From 41881c7dfbe7508a2fd4fe739b47e6293ecbe670 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Mon, 4 Jul 2022 17:54:05 +0600 Subject: [PATCH 04/13] Fix QueryInsertEmbedded parameter type (#6622) * Fix QueryInsertEmbedded parameter type * Fix compilation error --- .../PresentationFramework/System/Windows/Documents/TextStore.cs | 2 +- .../src/Shared/MS/Win32/UnsafeNativeMethodsTextServices.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextStore.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextStore.cs index eca91af6199..ac7c8d4ab04 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextStore.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextStore.cs @@ -586,7 +586,7 @@ public void GetEmbedded(int index, ref Guid guidService, ref Guid riid, out obje } // See msdn's ITextStoreACP documentation for a full description. - public void QueryInsertEmbedded(ref Guid guidService, int formatEtc, out bool insertable) + public void QueryInsertEmbedded(ref Guid guidService, IntPtr formatEtc, out bool insertable) { #if true // diff --git a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsTextServices.cs b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsTextServices.cs index e2cfe212245..389a273ea5b 100644 --- a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsTextServices.cs +++ b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsTextServices.cs @@ -1630,7 +1630,7 @@ void SetText(SetTextFlags flags, int start, int end, //HRESULT QueryInsertEmbedded([in] const GUID *pguidService, // [in] const FORMATETC *pFormatEtc, // [out] BOOL *pfInsertable); - void QueryInsertEmbedded(ref Guid guidService, int /*ref Win32.FORMATETC*/ formatEtc, [MarshalAs(UnmanagedType.Bool)] out bool insertable); + void QueryInsertEmbedded(ref Guid guidService, IntPtr /*ref Win32.FORMATETC*/ formatEtc, [MarshalAs(UnmanagedType.Bool)] out bool insertable); /// //HRESULT InsertEmbedded([in] DWORD dwFlags, From 089c49c09bcbf62395643372666b1abfd2bc6892 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Singh Date: Tue, 12 Jul 2022 20:19:51 +0530 Subject: [PATCH 05/13] Fix Zlib and ds lowercase issues (#6781) --- .../src/Common/src/Interop/Windows/zlib/zlib.cs | 2 +- .../System/IO/Compression/DeflateZLib/ZLibNative.cs | 12 ++++++------ .../System/Windows/Documents/FixedDSBuilder.cs | 4 ++-- .../Compoundfile/CompoundFileDeflateTransform.cs | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/Common/src/Interop/Windows/zlib/zlib.cs b/src/Microsoft.DotNet.Wpf/src/Common/src/Interop/Windows/zlib/zlib.cs index ca6e67a7ad8..fa9aac6717f 100644 --- a/src/Microsoft.DotNet.Wpf/src/Common/src/Interop/Windows/zlib/zlib.cs +++ b/src/Microsoft.DotNet.Wpf/src/Common/src/Interop/Windows/zlib/zlib.cs @@ -7,7 +7,7 @@ internal static partial class Interop { - internal static partial class zlib + internal static partial class Zlib { internal static readonly byte[] ZLibVersion = { (byte)'1', (byte)'.', (byte)'2', (byte)'.', (byte)'3', 0 }; [DllImport(Libraries.CompressionNative, EntryPoint = "CompressionNative_DeflateInit2_")] diff --git a/src/Microsoft.DotNet.Wpf/src/Common/src/System/IO/Compression/DeflateZLib/ZLibNative.cs b/src/Microsoft.DotNet.Wpf/src/Common/src/System/IO/Compression/DeflateZLib/ZLibNative.cs index 352d9f2248d..5c73688e384 100644 --- a/src/Microsoft.DotNet.Wpf/src/Common/src/System/IO/Compression/DeflateZLib/ZLibNative.cs +++ b/src/Microsoft.DotNet.Wpf/src/Common/src/System/IO/Compression/DeflateZLib/ZLibNative.cs @@ -252,7 +252,7 @@ public ErrorCode DeflateInit2_(CompressionLevel level, int windowBits, int memLe EnsureNotDisposed(); EnsureState(State.NotInitialized); - ErrorCode errC = Interop.zlib.DeflateInit2_(ref _zStream, level, CompressionMethod.Deflated, windowBits, memLevel, strategy); + ErrorCode errC = Interop.Zlib.DeflateInit2_(ref _zStream, level, CompressionMethod.Deflated, windowBits, memLevel, strategy); _initializationState = State.InitializedForDeflate; return errC; @@ -263,7 +263,7 @@ public ErrorCode Deflate(FlushCode flush) { EnsureNotDisposed(); EnsureState(State.InitializedForDeflate); - return Interop.zlib.Deflate(ref _zStream, flush); + return Interop.Zlib.Deflate(ref _zStream, flush); } @@ -272,7 +272,7 @@ public ErrorCode DeflateEnd() EnsureNotDisposed(); EnsureState(State.InitializedForDeflate); - ErrorCode errC = Interop.zlib.DeflateEnd(ref _zStream); + ErrorCode errC = Interop.Zlib.DeflateEnd(ref _zStream); _initializationState = State.Disposed; return errC; @@ -284,7 +284,7 @@ public ErrorCode InflateInit2_(int windowBits) EnsureNotDisposed(); EnsureState(State.NotInitialized); - ErrorCode errC = Interop.zlib.InflateInit2_(ref _zStream, windowBits); + ErrorCode errC = Interop.Zlib.InflateInit2_(ref _zStream, windowBits); _initializationState = State.InitializedForInflate; return errC; @@ -295,7 +295,7 @@ public ErrorCode Inflate(FlushCode flush) { EnsureNotDisposed(); EnsureState(State.InitializedForInflate); - return Interop.zlib.Inflate(ref _zStream, flush); + return Interop.Zlib.Inflate(ref _zStream, flush); } @@ -304,7 +304,7 @@ public ErrorCode InflateEnd() EnsureNotDisposed(); EnsureState(State.InitializedForInflate); - ErrorCode errC = Interop.zlib.InflateEnd(ref _zStream); + ErrorCode errC = Interop.Zlib.InflateEnd(ref _zStream); _initializationState = State.Disposed; return errC; diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDSBuilder.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDSBuilder.cs index fb6b94acf6b..5436049845f 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDSBuilder.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDSBuilder.cs @@ -17,7 +17,7 @@ namespace System.Windows.Documents using System.Windows.Markup; using System.Windows.Shapes; using System.Windows.Documents.DocumentStructures; - using ds=System.Windows.Documents.DocumentStructures; + using Ds=System.Windows.Documents.DocumentStructures; using System; using System.Collections; using System.Collections.Generic; @@ -219,7 +219,7 @@ private void AddChildofFixedNodeinFlow(int[] childIndex, NamedElement ne) private void SpecialProcessing(SemanticBasicElement sbe) { - ds.ListItemStructure listItem = sbe as ds.ListItemStructure; + Ds.ListItemStructure listItem = sbe as Ds.ListItemStructure; if (listItem != null && listItem.Marker != null) { NameHashFixedNode fen; diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/Compoundfile/CompoundFileDeflateTransform.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/Compoundfile/CompoundFileDeflateTransform.cs index 397a0cdd96d..fddf844935b 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/Compoundfile/CompoundFileDeflateTransform.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/Compoundfile/CompoundFileDeflateTransform.cs @@ -23,7 +23,7 @@ using MS.Internal.IO.Packaging; // for PackagingUtilities using System.Security; // for SecurityCritical and SecurityTreatAsSafe using MS.Internal.WindowsBase; -using static Interop.zlib; // workaround namespace collision with MS.Internal.interop +using static Interop.Zlib; // workaround namespace collision with MS.Internal.interop namespace MS.Internal.IO.Packaging.CompoundFile { From d0332b37302dceb506c2ce9e1a68502d54c088c3 Mon Sep 17 00:00:00 2001 From: Bradley Grainger Date: Wed, 13 Jul 2022 04:59:11 -0700 Subject: [PATCH 06/13] Eliminate memory copy when reading font data. Fixes #6236 (#6254) --- .../CPP/DWriteWrapper/FontFileStream.cpp | 41 +++---------------- .../CPP/DWriteWrapper/FontFileStream.h | 4 +- 2 files changed, 8 insertions(+), 37 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileStream.cpp b/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileStream.cpp index 7e9b51adde8..e1c7339e9bc 100644 --- a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileStream.cpp +++ b/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileStream.cpp @@ -17,17 +17,15 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface // guarantee that this problem will be fixed so we will use the GetUnmanagedStream(). Note: This path will only // be taken for embedded fonts among which XPS is a main scenario. For local fonts we use DWrite's APIs. _fontSourceStream = fontSource->GetUnmanagedStream(); + _fontSourcePointer = _fontSourceStream->PositionPointer - _fontSourceStream->Position; try { _lastWriteTime = fontSource->GetLastWriteTimeUtc().ToFileTimeUtc(); - } + } catch(System::ArgumentOutOfRangeException^) //The resulting file time would represent a date and time before 12:00 midnight January 1, 1601 C.E. UTC. { _lastWriteTime = -1; - } - - // Create lock to control access to font source stream. - _fontSourceStreamLock = gcnew Object(); + } } FontFileStream::~FontFileStream() @@ -61,31 +59,9 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface return E_INVALIDARG; } - int fragmentSizeInt = (int)fragmentSize; - array^ buffer = gcnew array(fragmentSizeInt); - - // DWrite may call this method from multiple threads. We need to ensure thread safety by making Seek and Read atomic. - System::Threading::Monitor::Enter(_fontSourceStreamLock); - try - { - _fontSourceStream->Seek(fileOffset, //long - System::IO::SeekOrigin::Begin); - - _fontSourceStream->Read(buffer, //byte[] - 0, //int - fragmentSizeInt //int - ); - } - finally - { - System::Threading::Monitor::Exit(_fontSourceStreamLock); - } - - GCHandle gcHandle = GCHandle::Alloc(buffer, GCHandleType::Pinned); - - *fragmentStart = (byte*)(gcHandle.AddrOfPinnedObject().ToPointer()); - - *fragmentContext = GCHandle::ToIntPtr(gcHandle).ToPointer(); + // Return a pointer to the font data that is already loaded in memory (because the font source resource is mmapped into the process' address space). + *fragmentStart = _fontSourcePointer + fileOffset; + *fragmentContext = nullptr; } catch(System::Exception^ exception) { @@ -102,11 +78,6 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface void* fragmentContext ) { - if (fragmentContext != NULL) - { - GCHandle gcHandle = GCHandle::FromIntPtr(IntPtr(fragmentContext)); - gcHandle.Free(); - } } [ComVisible(true)] diff --git a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileStream.h b/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileStream.h index fa1f3a50c24..49cf479b0b4 100644 --- a/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileStream.h +++ b/src/Microsoft.DotNet.Wpf/src/DirectWriteForwarder/CPP/DWriteWrapper/FontFileStream.h @@ -20,9 +20,9 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface private ref class FontFileStream : public IDWriteFontFileStreamMirror { private: - Stream^ _fontSourceStream; + UnmanagedMemoryStream^ _fontSourceStream; + Byte* _fontSourcePointer; INT64 _lastWriteTime; - Object^ _fontSourceStreamLock; public: From 50614d5b77b43bfdf8182a0cd9677024c8ceb0ad Mon Sep 17 00:00:00 2001 From: Bastian Schmidt Date: Wed, 13 Jul 2022 13:58:55 +0200 Subject: [PATCH 07/13] Replacing expensive type checks with "is" checks (#4964) * Replacing "IsInstanceOfType" with "is" * Replacing type check helpers with "is" * Removing todo * Applying suggested changes * Removing unused DType fields * Adding back exceptions for invalid types --- .../MS/internal/SynchronizedInputHelper.cs | 9 +- .../MS/internal/UIElementHelper.cs | 2 +- .../Windows/Input/Command/CommandManager.cs | 8 +- .../Windows/Input/Command/RoutedCommand.cs | 12 +- .../System/Windows/Input/InputElement.cs | 68 ++++------ .../System/Windows/Input/InputManager.cs | 12 +- .../System/Windows/Input/KeyboardDevice.cs | 48 ++++--- .../System/Windows/Input/MouseDevice.cs | 112 +++++++++-------- .../Input/Stylus/Pointer/PointerLogic.cs | 94 ++++++++------ .../Windows/Input/Stylus/Wisp/WispLogic.cs | 118 ++++++++++-------- .../Input/Stylus/Wisp/WispStylusDevice.cs | 52 ++++---- .../Windows/Media/Effects/ShaderEffect.cs | 9 +- .../System/Windows/Media/VisualTreeHelper.cs | 7 +- .../System/Windows/PresentationSource.cs | 44 ++++--- .../System/Windows/UIElement.cs | 4 +- .../System/Windows/UIElement3D.cs | 4 +- .../Annotations/Anchoring/PathNode.cs | 9 +- .../MS/Internal/FrameworkObject.cs | 34 +---- .../MS/Internal/Helper.cs | 8 +- .../MS/Internal/Ink/ClipboardProcessor.cs | 4 +- .../MS/Internal/PrePostDescendentsWalker.cs | 2 +- .../System/Windows/BroadcastEventHelper.cs | 24 ++-- .../Windows/Controls/PopupControlService.cs | 12 +- .../Controls/Primitives/DatePickerTextBox.cs | 2 +- .../System/Windows/DescendentsWalker.cs | 31 ++--- .../System/Windows/DescendentsWalkerBase.cs | 3 +- .../System/Windows/FrameworkElement.cs | 6 +- .../Generated/FrameworkContentElement.cs | 13 -- .../Windows/Generated/FrameworkElement.cs | 13 -- .../Windows/Media/Animation/Storyboard.cs | 4 +- .../System/Windows/StyleHelper.cs | 8 +- .../System/Windows/TreeWalkHelper.cs | 2 +- .../ReachDocumentSequenceSerializer.cs | 2 +- .../ReachDocumentSequenceSerializerAsync.cs | 2 +- .../manager/ReachFixedDocumentSerializer.cs | 2 +- .../ReachFixedDocumentSerializerAsync.cs | 2 +- .../manager/ReachFixedPageSerializer.cs | 2 +- .../manager/ReachFixedPageSerializerAsync.cs | 2 +- .../manager/XpsOMFixedPageSerializer.cs | 2 +- 39 files changed, 379 insertions(+), 413 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/SynchronizedInputHelper.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/SynchronizedInputHelper.cs index 3d020a8e1cc..28cfd0a05ef 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/SynchronizedInputHelper.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/SynchronizedInputHelper.cs @@ -297,21 +297,18 @@ internal static RoutedEvent[] MapInputTypeToRoutedEvents(SynchronizedInputType i internal static void RaiseAutomationEvents() { - if (InputElement.IsUIElement(InputManager.ListeningElement)) + if (InputManager.ListeningElement is UIElement e) { - UIElement e = (UIElement)InputManager.ListeningElement; //Raise InputDiscarded automation event SynchronizedInputHelper.RaiseAutomationEvent(e.GetAutomationPeer()); } - else if (InputElement.IsContentElement(InputManager.ListeningElement)) + else if (InputManager.ListeningElement is ContentElement ce) { - ContentElement ce = (ContentElement)InputManager.ListeningElement; //Raise InputDiscarded automation event SynchronizedInputHelper.RaiseAutomationEvent(ce.GetAutomationPeer()); } - else if (InputElement.IsUIElement3D(InputManager.ListeningElement)) + else if (InputManager.ListeningElement is UIElement3D e3D) { - UIElement3D e3D = (UIElement3D)InputManager.ListeningElement; //Raise InputDiscarded automation event SynchronizedInputHelper.RaiseAutomationEvent(e3D.GetAutomationPeer()); } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/UIElementHelper.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/UIElementHelper.cs index 4496a6a014f..b699b840377 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/UIElementHelper.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/UIElementHelper.cs @@ -141,7 +141,7 @@ internal static DependencyObject GetUIParent(DependencyObject child, bool contin [FriendAccessAllowed] internal static bool IsUIElementOrUIElement3D(DependencyObject o) { - return (o is UIElement || o is UIElement3D); + return (o is UIElement or UIElement3D); } [FriendAccessAllowed] diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandManager.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandManager.cs index 26f934c821f..1434794fb60 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandManager.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandManager.cs @@ -352,9 +352,9 @@ internal static void TranslateInput(IInputElement targetElement, InputEventArgs // Determine UIElement/ContentElement/Neither type DependencyObject targetElementAsDO = targetElement as DependencyObject; - bool isUIElement = InputElement.IsUIElement(targetElementAsDO); - bool isContentElement = !isUIElement && InputElement.IsContentElement(targetElementAsDO); - bool isUIElement3D = !isUIElement && !isContentElement && InputElement.IsUIElement3D(targetElementAsDO); + bool isUIElement = targetElementAsDO is UIElement; + bool isContentElement = !isUIElement && targetElementAsDO is ContentElement; + bool isUIElement3D = !isUIElement && !isContentElement && targetElementAsDO is UIElement3D; // Step 1: Check local input bindings InputBindingCollection localInputBindings = null; @@ -370,6 +370,7 @@ internal static void TranslateInput(IInputElement targetElement, InputEventArgs { localInputBindings = ((UIElement3D)targetElement).InputBindingsInternal; } + if (localInputBindings != null) { InputBinding inputBinding = localInputBindings.FindMatch(targetElement, inputEventArgs); @@ -423,6 +424,7 @@ internal static void TranslateInput(IInputElement targetElement, InputEventArgs { localCommandBindings = ((UIElement3D)targetElement).CommandBindingsInternal; } + if (localCommandBindings != null) { command = localCommandBindings.FindMatch(targetElement, inputEventArgs); diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/RoutedCommand.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/RoutedCommand.cs index bf6a5f44320..a7c543e83fb 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/RoutedCommand.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/RoutedCommand.cs @@ -362,17 +362,17 @@ private void CriticalCanExecuteWrapper(object parameter, IInputElement target, b // both of which derive from DO DependencyObject targetAsDO = (DependencyObject)target; - if (InputElement.IsUIElement(targetAsDO)) + if (targetAsDO is UIElement uie) { - ((UIElement)targetAsDO).RaiseEvent(args, trusted); + uie.RaiseEvent(args, trusted); } - else if (InputElement.IsContentElement(targetAsDO)) + else if (targetAsDO is ContentElement ce) { - ((ContentElement)targetAsDO).RaiseEvent(args, trusted); + ce.RaiseEvent(args, trusted); } - else if (InputElement.IsUIElement3D(targetAsDO)) + else if (targetAsDO is UIElement3D uie3D) { - ((UIElement3D)targetAsDO).RaiseEvent(args, trusted); + uie3D.RaiseEvent(args, trusted); } } internal bool ExecuteCore(object parameter, IInputElement target, bool userInitiated) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs index 5881358c6ec..80b780a9016 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs @@ -25,62 +25,42 @@ internal static bool IsValid(IInputElement e) internal static bool IsValid(DependencyObject o) { - return IsUIElement(o) || IsContentElement(o) || IsUIElement3D(o); + return o is UIElement or ContentElement or UIElement3D; } - // Returns whether the given DynamicObject is a UIElement or not. - internal static bool IsUIElement(DependencyObject o) - { - return UIElementType.IsInstanceOfType(o); - } - - // Returns whether the given DynamicObject is a UIElement3D or not. - internal static bool IsUIElement3D(DependencyObject o) - { - return UIElement3DType.IsInstanceOfType(o); - } - - // Returns whether the given DynamicObject is a ContentElement or not. - internal static bool IsContentElement(DependencyObject o) - { - return ContentElementType.IsInstanceOfType(o); - } - // Returns the containing input element of the given DynamicObject. // If onlyTraverse2D is set to true, then we stop once we see a 3D object and return null internal static DependencyObject GetContainingUIElement(DependencyObject o, bool onlyTraverse2D) { DependencyObject container = null; - Visual v; - Visual3D v3D; if(o != null) { - if(IsUIElement(o)) + if(o is UIElement) { container = o; } - else if (IsUIElement3D(o) && !onlyTraverse2D) + else if (o is UIElement3D && !onlyTraverse2D) { container = o; } - else if(IsContentElement(o)) + else if(o is ContentElement contentElement) { - DependencyObject parent = ContentOperations.GetParent((ContentElement)o); + DependencyObject parent = ContentOperations.GetParent(contentElement); if(parent != null) { container = GetContainingUIElement(parent, onlyTraverse2D); } else { - parent = ((ContentElement)o).GetUIParentCore(); + parent = contentElement.GetUIParentCore(); if(parent != null) { container = GetContainingUIElement(parent, onlyTraverse2D); } } } - else if ((v = o as Visual) != null) + else if (o is Visual v) { DependencyObject parent = VisualTreeHelper.GetParent(v); if(parent != null) @@ -88,7 +68,7 @@ internal static DependencyObject GetContainingUIElement(DependencyObject o, bool container = GetContainingUIElement(parent, onlyTraverse2D); } } - else if (!onlyTraverse2D && (v3D = o as Visual3D) != null) + else if (!onlyTraverse2D && o is Visual3D v3D) { DependencyObject parent = VisualTreeHelper.GetParent(v3D); if (parent != null) @@ -113,24 +93,22 @@ internal static DependencyObject GetContainingUIElement(DependencyObject o) internal static IInputElement GetContainingInputElement(DependencyObject o, bool onlyTraverse2D) { IInputElement container = null; - Visual v; - Visual3D v3D; if(o != null) { - if(IsUIElement(o)) + if(o is UIElement uiElement) { - container = (UIElement) o; + container = uiElement; } - else if(IsContentElement(o)) + else if(o is ContentElement contentElement) { - container = (ContentElement) o; + container = contentElement; } - else if (IsUIElement3D(o) && !onlyTraverse2D) + else if (o is UIElement3D uIElement3D && !onlyTraverse2D) { - container = (UIElement3D)o; + container = uIElement3D; } - else if((v = o as Visual) != null) + else if(o is Visual v) { DependencyObject parent = VisualTreeHelper.GetParent(v); if(parent != null) @@ -138,7 +116,7 @@ internal static IInputElement GetContainingInputElement(DependencyObject o, bool container = GetContainingInputElement(parent, onlyTraverse2D); } } - else if (!onlyTraverse2D && (v3D = o as Visual3D) != null) + else if (!onlyTraverse2D && o is Visual3D v3D) { DependencyObject parent = VisualTreeHelper.GetParent(v3D); if (parent != null) @@ -165,24 +143,24 @@ internal static DependencyObject GetContainingVisual(DependencyObject o) if(o != null) { - if(IsUIElement(o)) + if(o is UIElement uiElement) { - v = (Visual)o; + v = uiElement; } - else if (IsUIElement3D(o)) + else if (o is Visual3D visual3D) { - v = (Visual3D)o; + v = visual3D; } - else if(IsContentElement(o)) + else if(o is ContentElement contentElement) { - DependencyObject parent = ContentOperations.GetParent((ContentElement)o); + DependencyObject parent = ContentOperations.GetParent(contentElement); if(parent != null) { v = GetContainingVisual(parent); } else { - parent = ((ContentElement)o).GetUIParentCore(); + parent = contentElement.GetUIParentCore(); if(parent != null) { v = GetContainingVisual(parent); diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputManager.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputManager.cs index 351358837ae..aa38d9298e7 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputManager.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputManager.cs @@ -801,22 +801,16 @@ private bool ProcessStagingArea() { if (eventSource != null) { - if (InputElement.IsUIElement(eventSource)) + if (eventSource is UIElement e) { - UIElement e = (UIElement)eventSource; - e.RaiseEvent(input, true); // Call the "trusted" flavor of RaiseEvent. } - else if (InputElement.IsContentElement(eventSource)) + else if (eventSource is ContentElement ce) { - ContentElement ce = (ContentElement)eventSource; - ce.RaiseEvent(input, true);// Call the "trusted" flavor of RaiseEvent. } - else if (InputElement.IsUIElement3D(eventSource)) + else if (eventSource is UIElement3D e3D) { - UIElement3D e3D = (UIElement3D)eventSource; - e3D.RaiseEvent(input, true); // Call the "trusted" flavor of RaiseEvent } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs index a2234754b1a..d5bb4638440 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs @@ -420,45 +420,53 @@ private void ChangeFocus(DependencyObject focus, int timestamp) if(oldFocus != null) { o = oldFocus; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged -= _isEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged -= _isVisibleChangedEventHandler; - ((UIElement)o).FocusableChanged -= _focusableChangedEventHandler; + uie.IsEnabledChanged -= _isEnabledChangedEventHandler; + uie.IsVisibleChanged -= _isVisibleChangedEventHandler; + uie.FocusableChanged -= _focusableChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged -= _isEnabledChangedEventHandler; + ce.IsEnabledChanged -= _isEnabledChangedEventHandler; // NOTE: there is no IsVisible property for ContentElements. - ((ContentElement)o).FocusableChanged -= _focusableChangedEventHandler; + ce.FocusableChanged -= _focusableChangedEventHandler; + } + else if (o is UIElement3D uie3D) + { + uie3D.IsEnabledChanged -= _isEnabledChangedEventHandler; + uie3D.IsVisibleChanged -= _isVisibleChangedEventHandler; + uie3D.FocusableChanged -= _focusableChangedEventHandler; } else { - ((UIElement3D)o).IsEnabledChanged -= _isEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged -= _isVisibleChangedEventHandler; - ((UIElement3D)o).FocusableChanged -= _focusableChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, o.GetType())); } } if(_focus != null) { o = _focus; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged += _isEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged += _isVisibleChangedEventHandler; - ((UIElement)o).FocusableChanged += _focusableChangedEventHandler; + uie.IsEnabledChanged += _isEnabledChangedEventHandler; + uie.IsVisibleChanged += _isVisibleChangedEventHandler; + uie.FocusableChanged += _focusableChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged += _isEnabledChangedEventHandler; + ce.IsEnabledChanged += _isEnabledChangedEventHandler; // NOTE: there is no IsVisible property for ContentElements. - ((ContentElement)o).FocusableChanged += _focusableChangedEventHandler; + ce.FocusableChanged += _focusableChangedEventHandler; + } + else if (o is UIElement3D uie3D) + { + uie3D.IsEnabledChanged += _isEnabledChangedEventHandler; + uie3D.IsVisibleChanged += _isVisibleChangedEventHandler; + uie3D.FocusableChanged += _focusableChangedEventHandler; } else { - ((UIElement3D)o).IsEnabledChanged += _isEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged += _isVisibleChangedEventHandler; - ((UIElement3D)o).FocusableChanged += _focusableChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, o.GetType())); } } } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseDevice.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseDevice.cs index d8233c586b4..4559f01778c 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseDevice.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseDevice.cs @@ -739,17 +739,17 @@ private object ReevaluateCaptureAsync(object arg) // First, check things like IsEnabled, IsVisible, etc. on a // UIElement vs. ContentElement basis. // - if (InputElement.IsUIElement(dependencyObject)) + if (dependencyObject is UIElement uie) { - killCapture = !ValidateUIElementForCapture((UIElement)_mouseCapture); + killCapture = !ValidateUIElementForCapture(uie); } - else if (InputElement.IsContentElement(dependencyObject)) + else if (dependencyObject is ContentElement ce) { - killCapture = !ValidateContentElementForCapture((ContentElement)_mouseCapture); + killCapture = !ValidateContentElementForCapture(ce); } - else if (InputElement.IsUIElement3D(dependencyObject)) + else if (dependencyObject is UIElement3D uie3D) { - killCapture = !ValidateUIElement3DForCapture((UIElement3D)_mouseCapture); + killCapture = !ValidateUIElement3DForCapture(uie3D); } // @@ -993,51 +993,51 @@ private void ChangeMouseOver(IInputElement mouseOver, int timestamp) if(oldMouseOver != null) { o = oldMouseOver as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged -= _overIsEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged -= _overIsVisibleChangedEventHandler; - ((UIElement)o).IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + uie.IsEnabledChanged -= _overIsEnabledChangedEventHandler; + uie.IsVisibleChanged -= _overIsVisibleChangedEventHandler; + uie.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged -= _overIsEnabledChangedEventHandler; + ce.IsEnabledChanged -= _overIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged -= _overIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged -= _overIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsUIElement3D(o)) + else if (o is UIElement3D uie3D) { - ((UIElement3D)o).IsEnabledChanged -= _overIsEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged -= _overIsVisibleChangedEventHandler; - ((UIElement3D)o).IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + uie3D.IsEnabledChanged -= _overIsEnabledChangedEventHandler; + uie3D.IsVisibleChanged -= _overIsVisibleChangedEventHandler; + uie3D.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; } } if(_mouseOver != null) { o = _mouseOver as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged += _overIsEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged += _overIsVisibleChangedEventHandler; - ((UIElement)o).IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + uie.IsEnabledChanged += _overIsEnabledChangedEventHandler; + uie.IsVisibleChanged += _overIsVisibleChangedEventHandler; + uie.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged += _overIsEnabledChangedEventHandler; + ce.IsEnabledChanged += _overIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged += _overIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged += _overIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsUIElement3D(o)) + else if (o is UIElement3D uie3D) { - ((UIElement3D)o).IsEnabledChanged += _overIsEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged += _overIsVisibleChangedEventHandler; - ((UIElement3D)o).IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + uie3D.IsEnabledChanged += _overIsEnabledChangedEventHandler; + uie3D.IsVisibleChanged += _overIsVisibleChangedEventHandler; + uie3D.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; } } } @@ -1087,51 +1087,51 @@ private void ChangeMouseCapture(IInputElement mouseCapture, IMouseInputProvider if (oldMouseCapture != null) { o = oldMouseCapture as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged -= _captureIsEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged -= _captureIsVisibleChangedEventHandler; - ((UIElement)o).IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; + uie.IsEnabledChanged -= _captureIsEnabledChangedEventHandler; + uie.IsVisibleChanged -= _captureIsVisibleChangedEventHandler; + uie.IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged -= _captureIsEnabledChangedEventHandler; + ce.IsEnabledChanged -= _captureIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged -= _captureIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged -= _captureIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsUIElement3D(o)) + else if (o is UIElement3D uie3D) { - ((UIElement3D)o).IsEnabledChanged -= _captureIsEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged -= _captureIsVisibleChangedEventHandler; - ((UIElement3D)o).IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; + uie3D.IsEnabledChanged -= _captureIsEnabledChangedEventHandler; + uie3D.IsVisibleChanged -= _captureIsVisibleChangedEventHandler; + uie3D.IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; } } if (_mouseCapture != null) { o = _mouseCapture as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged += _captureIsEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged += _captureIsVisibleChangedEventHandler; - ((UIElement)o).IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; + uie.IsEnabledChanged += _captureIsEnabledChangedEventHandler; + uie.IsVisibleChanged += _captureIsVisibleChangedEventHandler; + uie.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged += _captureIsEnabledChangedEventHandler; + ce.IsEnabledChanged += _captureIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged += _captureIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged += _captureIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsUIElement3D(o)) + else if (o is UIElement3D uie3D) { - ((UIElement3D)o).IsEnabledChanged += _captureIsEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged += _captureIsVisibleChangedEventHandler; - ((UIElement3D)o).IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; + uie3D.IsEnabledChanged += _captureIsEnabledChangedEventHandler; + uie3D.IsVisibleChanged += _captureIsVisibleChangedEventHandler; + uie3D.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; } } } @@ -1538,9 +1538,13 @@ private void PreNotifyInput(object sender, NotifyInputEventArgs e) // If we are over something else (like a raw visual) // find the containing element. if (!InputElement.IsValid(mouseOver)) + { mouseOver = InputElement.GetContainingInputElement(mouseOver as DependencyObject); + } if ((rawMouseOver != null) && !InputElement.IsValid(rawMouseOver)) + { rawMouseOver = InputElement.GetContainingInputElement(rawMouseOver as DependencyObject); + } } break; diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerLogic.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerLogic.cs index 437e2e6732e..6f29645dcc8 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerLogic.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Pointer/PointerLogic.cs @@ -530,48 +530,52 @@ internal override void UpdateStylusCapture(StylusDeviceBase stylusDevice, IInput if (oldCapture != null) { o = oldCapture as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement element) { - UIElement element = o as UIElement; element.IsEnabledChanged -= _captureIsEnabledChangedEventHandler; element.IsVisibleChanged -= _captureIsVisibleChangedEventHandler; element.IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. - ((ContentElement)o).IsEnabledChanged -= _captureIsEnabledChangedEventHandler; + ce.IsEnabledChanged -= _captureIsEnabledChangedEventHandler; + } + else if (o is UIElement3D element3D) + { + element3D.IsEnabledChanged -= _captureIsEnabledChangedEventHandler; + element3D.IsVisibleChanged -= _captureIsVisibleChangedEventHandler; + element3D.IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; } else { - UIElement3D element = o as UIElement3D; - element.IsEnabledChanged -= _captureIsEnabledChangedEventHandler; - element.IsVisibleChanged -= _captureIsVisibleChangedEventHandler; - element.IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, oldCapture.GetType())); } } if (_stylusCapture != null) { o = _stylusCapture as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement element) { - UIElement element = o as UIElement; element.IsEnabledChanged += _captureIsEnabledChangedEventHandler; element.IsVisibleChanged += _captureIsVisibleChangedEventHandler; element.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. - ((ContentElement)o).IsEnabledChanged += _captureIsEnabledChangedEventHandler; + ce.IsEnabledChanged += _captureIsEnabledChangedEventHandler; + } + else if (o is UIElement3D element3D) + { + element3D.IsEnabledChanged += _captureIsEnabledChangedEventHandler; + element3D.IsVisibleChanged += _captureIsVisibleChangedEventHandler; + element3D.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; } else { - UIElement3D element = o as UIElement3D; - element.IsEnabledChanged += _captureIsEnabledChangedEventHandler; - element.IsVisibleChanged += _captureIsVisibleChangedEventHandler; - element.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, _stylusCapture.GetType())); } } @@ -613,55 +617,59 @@ internal override void UpdateOverProperty(StylusDeviceBase stylusDevice, IInputE if (oldOver != null) { o = oldOver as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement element) { - UIElement element = o as UIElement; element.IsEnabledChanged -= _overIsEnabledChangedEventHandler; element.IsVisibleChanged -= _overIsVisibleChangedEventHandler; element.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged -= _overIsEnabledChangedEventHandler; + ce.IsEnabledChanged -= _overIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged -= _overIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged -= _overIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + } + else if (o is UIElement3D element3D) + { + element3D.IsEnabledChanged -= _overIsEnabledChangedEventHandler; + element3D.IsVisibleChanged -= _overIsVisibleChangedEventHandler; + element3D.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; } else { - UIElement3D element = o as UIElement3D; - element.IsEnabledChanged -= _overIsEnabledChangedEventHandler; - element.IsVisibleChanged -= _overIsVisibleChangedEventHandler; - element.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, oldOver.GetType())); } } if (_stylusOver != null) { o = _stylusOver as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement element) { - UIElement element = o as UIElement; element.IsEnabledChanged += _overIsEnabledChangedEventHandler; element.IsVisibleChanged += _overIsVisibleChangedEventHandler; element.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged += _overIsEnabledChangedEventHandler; + ce.IsEnabledChanged += _overIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged += _overIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged += _overIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + } + else if (o is UIElement3D element3D) + { + element3D.IsEnabledChanged += _overIsEnabledChangedEventHandler; + element3D.IsVisibleChanged += _overIsVisibleChangedEventHandler; + element3D.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; } else { - UIElement3D element = o as UIElement3D; - element.IsEnabledChanged += _overIsEnabledChangedEventHandler; - element.IsVisibleChanged += _overIsVisibleChangedEventHandler; - element.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, _stylusOver.GetType())); } } @@ -783,17 +791,21 @@ private object ReevaluateCaptureAsync(object arg) // First, check things like IsEnabled, IsVisible, etc. on a // UIElement vs. ContentElement basis. // - if (InputElement.IsUIElement(dependencyObject)) + if (dependencyObject is UIElement uie) + { + killCapture = !ValidateUIElementForCapture(uie); + } + else if (dependencyObject is ContentElement ce) { - killCapture = !ValidateUIElementForCapture((UIElement)_stylusCapture); + killCapture = !ValidateContentElementForCapture(ce); } - else if (InputElement.IsContentElement(dependencyObject)) + else if (dependencyObject is UIElement3D uie3D) { - killCapture = !ValidateContentElementForCapture((ContentElement)_stylusCapture); + killCapture = !ValidateUIElement3DForCapture(uie3D); } else { - killCapture = !ValidateUIElement3DForCapture((UIElement3D)_stylusCapture); + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, _stylusCapture.GetType())); } // diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispLogic.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispLogic.cs index f31395e3ef6..5d089505bb9 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispLogic.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispLogic.cs @@ -2059,51 +2059,59 @@ internal override void UpdateStylusCapture(StylusDeviceBase stylusDevice, IInput if (oldCapture != null) { o = oldCapture as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged -= _captureIsEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged -= _captureIsVisibleChangedEventHandler; - ((UIElement)o).IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; + uie.IsEnabledChanged -= _captureIsEnabledChangedEventHandler; + uie.IsVisibleChanged -= _captureIsVisibleChangedEventHandler; + uie.IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged -= _captureIsEnabledChangedEventHandler; + ce.IsEnabledChanged -= _captureIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged -= _captureIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged -= _captureIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; + } + else if (o is UIElement3D uie3D) + { + uie3D.IsEnabledChanged -= _captureIsEnabledChangedEventHandler; + uie3D.IsVisibleChanged -= _captureIsVisibleChangedEventHandler; + uie3D.IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; } else { - ((UIElement3D)o).IsEnabledChanged -= _captureIsEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged -= _captureIsVisibleChangedEventHandler; - ((UIElement3D)o).IsHitTestVisibleChanged -= _captureIsHitTestVisibleChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, oldCapture.GetType())); } } if (_stylusCapture != null) { o = _stylusCapture as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged += _captureIsEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged += _captureIsVisibleChangedEventHandler; - ((UIElement)o).IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; + uie.IsEnabledChanged += _captureIsEnabledChangedEventHandler; + uie.IsVisibleChanged += _captureIsVisibleChangedEventHandler; + uie.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged += _captureIsEnabledChangedEventHandler; + ce.IsEnabledChanged += _captureIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged += _captureIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged += _captureIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; + } + else if (o is UIElement3D uie3D) + { + uie3D.IsEnabledChanged += _captureIsEnabledChangedEventHandler; + uie3D.IsVisibleChanged += _captureIsVisibleChangedEventHandler; + uie3D.IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; } else { - ((UIElement3D)o).IsEnabledChanged += _captureIsEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged += _captureIsVisibleChangedEventHandler; - ((UIElement3D)o).IsHitTestVisibleChanged += _captureIsHitTestVisibleChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, _stylusCapture.GetType())); } } @@ -2139,51 +2147,59 @@ internal override void UpdateOverProperty(StylusDeviceBase stylusDevice, IInputE if (oldOver != null) { o = oldOver as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged -= _overIsEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged -= _overIsVisibleChangedEventHandler; - ((UIElement)o).IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + uie.IsEnabledChanged -= _overIsEnabledChangedEventHandler; + uie.IsVisibleChanged -= _overIsVisibleChangedEventHandler; + uie.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged -= _overIsEnabledChangedEventHandler; + ce.IsEnabledChanged -= _overIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged -= _overIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged -= _overIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + } + else if (o is UIElement3D uie3D) + { + uie3D.IsEnabledChanged -= _overIsEnabledChangedEventHandler; + uie3D.IsVisibleChanged -= _overIsVisibleChangedEventHandler; + uie3D.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; } else { - ((UIElement3D)o).IsEnabledChanged -= _overIsEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged -= _overIsVisibleChangedEventHandler; - ((UIElement3D)o).IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, oldOver.GetType())); } } if (_stylusOver != null) { o = _stylusOver as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged += _overIsEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged += _overIsVisibleChangedEventHandler; - ((UIElement)o).IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + uie.IsEnabledChanged += _overIsEnabledChangedEventHandler; + uie.IsVisibleChanged += _overIsVisibleChangedEventHandler; + uie.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged += _overIsEnabledChangedEventHandler; + ce.IsEnabledChanged += _overIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged += _overIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged += _overIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + } + else if (o is UIElement3D uie3D) + { + uie3D.IsEnabledChanged += _overIsEnabledChangedEventHandler; + uie3D.IsVisibleChanged += _overIsVisibleChangedEventHandler; + uie3D.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; } else { - ((UIElement3D)o).IsEnabledChanged += _overIsEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged += _overIsVisibleChangedEventHandler; - ((UIElement3D)o).IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, _stylusOver.GetType())); } } @@ -2409,17 +2425,21 @@ private object ReevaluateCaptureAsync(object arg) // First, check things like IsEnabled, IsVisible, etc. on a // UIElement vs. ContentElement basis. // - if (InputElement.IsUIElement(dependencyObject)) + if (dependencyObject is UIElement uie) + { + killCapture = !ValidateUIElementForCapture(uie); + } + else if (dependencyObject is ContentElement ce) { - killCapture = !ValidateUIElementForCapture((UIElement)_stylusCapture); + killCapture = !ValidateContentElementForCapture(ce); } - else if (InputElement.IsContentElement(dependencyObject)) + else if (dependencyObject is UIElement3D uie3D) { - killCapture = !ValidateContentElementForCapture((ContentElement)_stylusCapture); + killCapture = !ValidateUIElement3DForCapture(uie3D); } else { - killCapture = !ValidateUIElement3DForCapture((UIElement3D)_stylusCapture); + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, _stylusCapture.GetType())); } // diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispStylusDevice.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispStylusDevice.cs index bf933843a7c..1f3125ee99d 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispStylusDevice.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispStylusDevice.cs @@ -418,51 +418,59 @@ private void UpdateOverProperty(IInputElement oldOver, IInputElement newOver) if (oldOver != null) { o = oldOver as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged -= _overIsEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged -= _overIsVisibleChangedEventHandler; - ((UIElement)o).IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + uie.IsEnabledChanged -= _overIsEnabledChangedEventHandler; + uie.IsVisibleChanged -= _overIsVisibleChangedEventHandler; + uie.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged -= _overIsEnabledChangedEventHandler; + ce.IsEnabledChanged -= _overIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged -= _overIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged -= _overIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + } + else if (o is UIElement3D uie3D) + { + uie3D.IsEnabledChanged -= _overIsEnabledChangedEventHandler; + uie3D.IsVisibleChanged -= _overIsVisibleChangedEventHandler; + uie3D.IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; } else { - ((UIElement3D)o).IsEnabledChanged -= _overIsEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged -= _overIsVisibleChangedEventHandler; - ((UIElement3D)o).IsHitTestVisibleChanged -= _overIsHitTestVisibleChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, oldOver.GetType())); } } if (_stylusOver != null) { o = _stylusOver as DependencyObject; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - ((UIElement)o).IsEnabledChanged += _overIsEnabledChangedEventHandler; - ((UIElement)o).IsVisibleChanged += _overIsVisibleChangedEventHandler; - ((UIElement)o).IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + uie.IsEnabledChanged += _overIsEnabledChangedEventHandler; + uie.IsVisibleChanged += _overIsVisibleChangedEventHandler; + uie.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; } - else if (InputElement.IsContentElement(o)) + else if (o is ContentElement ce) { - ((ContentElement)o).IsEnabledChanged += _overIsEnabledChangedEventHandler; + ce.IsEnabledChanged += _overIsEnabledChangedEventHandler; // NOTE: there are no IsVisible or IsHitTestVisible properties for ContentElements. // - // ((ContentElement)o).IsVisibleChanged += _overIsVisibleChangedEventHandler; - // ((ContentElement)o).IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + // ce.IsVisibleChanged += _overIsVisibleChangedEventHandler; + // ce.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + } + else if (o is UIElement3D uie3D) + { + uie3D.IsEnabledChanged += _overIsEnabledChangedEventHandler; + uie3D.IsVisibleChanged += _overIsVisibleChangedEventHandler; + uie3D.IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; } else { - ((UIElement3D)o).IsEnabledChanged += _overIsEnabledChangedEventHandler; - ((UIElement3D)o).IsVisibleChanged += _overIsVisibleChangedEventHandler; - ((UIElement3D)o).IsHitTestVisibleChanged += _overIsHitTestVisibleChangedEventHandler; + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, _stylusOver.GetType())); } } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/ShaderEffect.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/ShaderEffect.cs index d7b8a191096..6e4f4fa01bb 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/ShaderEffect.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/ShaderEffect.cs @@ -436,11 +436,10 @@ private void UpdateShaderSampler(DependencyProperty dp, object newValue, int reg if (newValue != null) { - if (!(typeof(VisualBrush).IsInstanceOfType(newValue) || - typeof(BitmapCacheBrush).IsInstanceOfType(newValue) || - typeof(ImplicitInputBrush).IsInstanceOfType(newValue) || - typeof(ImageBrush).IsInstanceOfType(newValue)) - ) + if (newValue is not VisualBrush + and not BitmapCacheBrush + and not ImplicitInputBrush + and not ImageBrush) { // Note that if the type of the brush is ImplicitInputBrush and the value is non null, the value is actually // Effect.ImplicitInput. This is because ImplicitInputBrush is internal and the user can only get to the singleton diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualTreeHelper.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualTreeHelper.cs index 3d8d9e3b2db..a9356fdfe5f 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualTreeHelper.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualTreeHelper.cs @@ -254,14 +254,11 @@ internal static bool IsAncestorOf(DependencyObject ancestor, DependencyObject de while ((current != null) && (current != ancestor) && !stopType.IsInstanceOfType(current)) { - Visual visual; - Visual3D visual3D; - - if ((visual = current as Visual) != null) + if (current is Visual visual) { current = visual.InternalVisualParent; } - else if ((visual3D = current as Visual3D) != null) + else if (current is Visual3D visual3D) { current = visual3D.InternalVisualParent; } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs index 330d77a2f4c..41515937df3 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs @@ -145,9 +145,8 @@ public static void AddSourceChangedHandler(IInputElement element, SourceChangedE { FrugalObjectList info; - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - UIElement uie = o as UIElement; uie.AddHandler(SourceChangedEvent, handler); info = uie.EventHandlersStore[SourceChangedEvent]; if (1 == info.Count) @@ -156,9 +155,8 @@ public static void AddSourceChangedHandler(IInputElement element, SourceChangedE AddElementToWatchList(uie); } } - else if (InputElement.IsUIElement3D(o)) + else if (o is UIElement3D uie3D) { - UIElement3D uie3D = o as UIElement3D; uie3D.AddHandler(SourceChangedEvent, handler); info = uie3D.EventHandlersStore[SourceChangedEvent]; if (1 == info.Count) @@ -167,13 +165,18 @@ public static void AddSourceChangedHandler(IInputElement element, SourceChangedE AddElementToWatchList(uie3D); } } - else + else if (o is ContentElement ce) { - ContentElement ce = o as ContentElement; ce.AddHandler(SourceChangedEvent, handler); info = ce.EventHandlersStore[SourceChangedEvent]; if (1 == info.Count) + { AddElementToWatchList(ce); + } + } + else + { + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, o.GetType())); } } } @@ -211,9 +214,8 @@ public static void RemoveSourceChangedHandler(IInputElement e, SourceChangedEven EventHandlersStore store; // Either UIElement or ContentElement. - if (InputElement.IsUIElement(o)) + if (o is UIElement uie) { - UIElement uie = o as UIElement; uie.RemoveHandler(SourceChangedEvent, handler); store = uie.EventHandlersStore; if (store != null) @@ -226,9 +228,8 @@ public static void RemoveSourceChangedHandler(IInputElement e, SourceChangedEven RemoveElementFromWatchList(uie); } } - else if (InputElement.IsUIElement3D(o)) + else if (o is UIElement3D uie3D) { - UIElement3D uie3D = o as UIElement3D; uie3D.RemoveHandler(SourceChangedEvent, handler); store = uie3D.EventHandlersStore; if (store != null) @@ -241,9 +242,8 @@ public static void RemoveSourceChangedHandler(IInputElement e, SourceChangedEven RemoveElementFromWatchList(uie3D); } } - else + else if (o is ContentElement ce) { - ContentElement ce = o as ContentElement; ce.RemoveHandler(SourceChangedEvent, handler); store = ce.EventHandlersStore; if (store != null) @@ -255,6 +255,10 @@ public static void RemoveSourceChangedHandler(IInputElement e, SourceChangedEven RemoveElementFromWatchList(ce); } } + else + { + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, o.GetType())); + } } } @@ -548,7 +552,7 @@ protected void ClearContentRenderedListeners() /// Event Args. internal static void OnVisualAncestorChanged(DependencyObject uie, AncestorChangedEventArgs e) { - Debug.Assert(InputElement.IsUIElement3D(uie) || InputElement.IsUIElement(uie)); + Debug.Assert(uie is UIElement3D or UIElement); if (true == (bool)uie.GetValue(GetsSourceChangedEventProperty)) { @@ -740,17 +744,21 @@ private static bool UpdateSourceOfElement(DependencyObject doTarget, SourceChangedEventArgs args = new SourceChangedEventArgs(cachedSource, realSource); args.RoutedEvent=SourceChangedEvent; - if (InputElement.IsUIElement(doTarget)) + if (doTarget is UIElement uiElement) { - ((UIElement)doTarget).RaiseEvent(args); + uiElement.RaiseEvent(args); } - else if (InputElement.IsContentElement(doTarget)) + else if (doTarget is ContentElement contentElement) + { + contentElement.RaiseEvent(args); + } + else if (doTarget is UIElement3D uiElement3D) { - ((ContentElement)doTarget).RaiseEvent(args); + uiElement3D.RaiseEvent(args); } else { - ((UIElement3D)doTarget).RaiseEvent(args); + throw new InvalidOperationException(SR.Get(SRID.Invalid_IInputElement, doTarget.GetType())); } calledOut = true; diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs index 14e0a26b6d4..49d9a88f4f3 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs @@ -1443,7 +1443,7 @@ protected internal override void OnVisualParentChanged(DependencyObject oldParen { DependencyObject parent = _parent; - if (!InputElement.IsUIElement(parent) && !InputElement.IsUIElement3D(parent)) + if (parent is not UIElement and not UIElement3D) { Visual parentAsVisual = parent as Visual; @@ -1490,7 +1490,7 @@ protected internal override void OnVisualParentChanged(DependencyObject oldParen { DependencyObject parent = oldParent; - if (!InputElement.IsUIElement(parent) && !InputElement.IsUIElement3D(parent)) + if (parent is not UIElement and not UIElement3D) { // We are being unplugged from a non-UIElement visual. This // means that our parent didn't play by the same rules we diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement3D.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement3D.cs index f6761dfe11d..33efb11b54f 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement3D.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement3D.cs @@ -148,7 +148,7 @@ protected internal override void OnVisualParentChanged(DependencyObject oldParen { DependencyObject parent = InternalVisualParent; - if (!InputElement.IsUIElement(parent) && !InputElement.IsUIElement3D(parent)) + if (parent is not UIElement and not UIElement3D) { Visual parentAsVisual = parent as Visual; @@ -192,7 +192,7 @@ protected internal override void OnVisualParentChanged(DependencyObject oldParen { DependencyObject parent = oldParent; - if (!InputElement.IsUIElement(parent) && !InputElement.IsUIElement3D(parent)) + if (parent is not UIElement and not UIElement3D) { // We are being unplugged from a non-UIElement visual. This // means that our parent didn't play by the same rules we diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/PathNode.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/PathNode.cs index 1ff7bf6c7b6..7f5dd44dec9 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/PathNode.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/PathNode.cs @@ -229,7 +229,7 @@ internal static DependencyObject GetParent(DependencyObject node) Debug.Assert(node != null, "node can not be null"); DependencyObject current = node; - DependencyObject parent = null; + DependencyObject parent; while (true) { @@ -255,16 +255,15 @@ internal static DependencyObject GetParent(DependencyObject node) } // Check if located a parent, if so, check if it's the correct type - if ((parent == null) || - FrameworkElement.DType.IsInstanceOfType(parent) || - FrameworkContentElement.DType.IsInstanceOfType(parent)) + if (parent is null + or FrameworkElement + or FrameworkContentElement) { break; } // Parent found but not of correct type, continue current = parent; - parent = null; } return parent; diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/FrameworkObject.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/FrameworkObject.cs index 4234020478c..bde4847e41a 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/FrameworkObject.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/FrameworkObject.cs @@ -57,21 +57,8 @@ internal FrameworkObject(DependencyObject d) // [code should be identical to Reset(d)] _do = d; - if (FrameworkElement.DType.IsInstanceOfType(d)) - { - _fe = (FrameworkElement)d; - _fce = null; - } - else if (FrameworkContentElement.DType.IsInstanceOfType(d)) - { - _fe = null; - _fce = (FrameworkContentElement)d; - } - else - { - _fe = null; - _fce = null; - } + _fe = d as FrameworkElement; + _fce = d as FrameworkContentElement; } internal FrameworkObject(DependencyObject d, bool throwIfNeither) @@ -99,21 +86,8 @@ internal void Reset(DependencyObject d) { _do = d; - if (FrameworkElement.DType.IsInstanceOfType(d)) - { - _fe = (FrameworkElement)d; - _fce = null; - } - else if (FrameworkContentElement.DType.IsInstanceOfType(d)) - { - _fe = null; - _fce = (FrameworkContentElement)d; - } - else - { - _fe = null; - _fce = null; - } + _fe = d as FrameworkElement; + _fce = d as FrameworkContentElement; } #endregion Constructors diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs index a45d7b9df42..197122f118c 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs @@ -286,15 +286,15 @@ internal static void DowncastToFEorFCE(DependencyObject d, out FrameworkElement fe, out FrameworkContentElement fce, bool throwIfNeither) { - if (FrameworkElement.DType.IsInstanceOfType(d)) + if (d is FrameworkElement frameworkElement) { - fe = (FrameworkElement)d; + fe = frameworkElement; fce = null; } - else if (FrameworkContentElement.DType.IsInstanceOfType(d)) + else if (d is FrameworkContentElement frameworkContentElement) { fe = null; - fce = (FrameworkContentElement)d; + fce = frameworkContentElement; } else if (throwIfNeither) { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Ink/ClipboardProcessor.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Ink/ClipboardProcessor.cs index 40aecf7e5e8..e7160832144 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Ink/ClipboardProcessor.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Ink/ClipboardProcessor.cs @@ -213,9 +213,9 @@ internal bool PasteData(IDataObject dataObject, ref StrokeCollection newStrokes, // If the Xaml data has been set in an InkCanvas, the top element will be a container InkCanvas. // In this case, the new elements will be the children of the container. // Otherwise, the new elements will be whatever data from the data object. - if (elements.Count == 1 && ClipboardProcessor.InkCanvasDType.IsInstanceOfType(elements[0])) + if (elements.Count == 1 && elements[0] is InkCanvas inkCanvas) { - TearDownInkCanvasContainer((InkCanvas)( elements[0] ), ref newStrokes, ref newElements); + TearDownInkCanvasContainer(inkCanvas, ref newStrokes, ref newElements); } else { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/PrePostDescendentsWalker.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/PrePostDescendentsWalker.cs index e40e1032aa9..4a4f884039d 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/PrePostDescendentsWalker.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/PrePostDescendentsWalker.cs @@ -72,7 +72,7 @@ public override void StartWalk(DependencyObject startNode, bool skipStartNode) { // This type checking is done in DescendentsWalker. Doing it here // keeps us consistent. - if (FrameworkElement.DType.IsInstanceOfType(startNode) || FrameworkContentElement.DType.IsInstanceOfType(startNode)) + if (startNode is FrameworkElement or FrameworkContentElement) { _postCallback(startNode, this.Data, _priority == TreeWalkPriority.VisualTree); } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/BroadcastEventHelper.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/BroadcastEventHelper.cs index 056f12d17e6..ace65f98990 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/BroadcastEventHelper.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/BroadcastEventHelper.cs @@ -292,12 +292,10 @@ private static bool OnBroadcastCallback(DependencyObject d, BroadcastEventData d DependencyObject root = data.Root; RoutedEvent routedEvent = data.RoutedEvent; List eventRoute = data.EventRoute; - - if (FrameworkElement.DType.IsInstanceOfType(d)) - { - // If this is a FrameworkElement - FrameworkElement fe = (FrameworkElement)d; + // If this is a FrameworkElement + if (d is FrameworkElement fe) + { if (fe != root && routedEvent == FrameworkElement.LoadedEvent && fe.UnloadedPending != null) { // If there is a pending Unloaded event wait till we've broadcast @@ -422,22 +420,22 @@ private static bool OnBroadcastCallback(DependencyObject d, BroadcastEventData d private static bool SubtreeHasLoadedChangeHandlerHelper(DependencyObject d) { - if (FrameworkElement.DType.IsInstanceOfType(d)) + if (d is FrameworkElement fe) { - return ((FrameworkElement)d).SubtreeHasLoadedChangeHandler; + return fe.SubtreeHasLoadedChangeHandler; } - else if (FrameworkContentElement.DType.IsInstanceOfType(d)) + else if (d is FrameworkContentElement fce) { - return ((FrameworkContentElement)d).SubtreeHasLoadedChangeHandler; + return fce.SubtreeHasLoadedChangeHandler; } return false; } private static void FireLoadedOnDescendentsHelper(DependencyObject d) { - if (FrameworkElement.DType.IsInstanceOfType(d)) + if (d is FrameworkElement fe) { - ((FrameworkElement)d).FireLoadedOnDescendentsInternal(); + fe.FireLoadedOnDescendentsInternal(); } else { @@ -447,9 +445,9 @@ private static void FireLoadedOnDescendentsHelper(DependencyObject d) private static void FireUnloadedOnDescendentsHelper(DependencyObject d) { - if (FrameworkElement.DType.IsInstanceOfType(d)) + if (d is FrameworkElement fe) { - ((FrameworkElement)d).FireUnloadedOnDescendentsInternal(); + fe.FireUnloadedOnDescendentsInternal(); } else { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/PopupControlService.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/PopupControlService.cs index b39aea2cb2c..08c0e6c1e75 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/PopupControlService.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/PopupControlService.cs @@ -942,17 +942,17 @@ private bool RaiseContextMenuOpeningEvent(IInputElement source, double x, double DependencyObject sourceDO = source as DependencyObject; if (userInitiated && sourceDO != null) { - if (InputElement.IsUIElement(sourceDO)) + if (sourceDO is UIElement uiElement) { - ((UIElement)sourceDO).RaiseEvent(args, userInitiated); + uiElement.RaiseEvent(args, userInitiated); } - else if (InputElement.IsContentElement(sourceDO)) + else if (sourceDO is ContentElement contentElement) { - ((ContentElement)sourceDO).RaiseEvent(args, userInitiated); + contentElement.RaiseEvent(args, userInitiated); } - else if (InputElement.IsUIElement3D(sourceDO)) + else if (sourceDO is UIElement3D uiElement3D) { - ((UIElement3D)sourceDO).RaiseEvent(args, userInitiated); + uiElement3D.RaiseEvent(args, userInitiated); } else { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DatePickerTextBox.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DatePickerTextBox.cs index c9fff7ac270..2b63c8484a7 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DatePickerTextBox.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DatePickerTextBox.cs @@ -153,7 +153,7 @@ private T ExtractTemplatePart(string partName) where T : DependencyObject private static T ExtractTemplatePart(string partName, DependencyObject obj) where T : DependencyObject { Debug.Assert( - obj == null || typeof(T).IsInstanceOfType(obj), + obj == null || obj is T, string.Format(CultureInfo.InvariantCulture, SR.Get(SRID.DatePickerTextBox_TemplatePartIsOfIncorrectType), partName, typeof(T).Name)); return obj as T; } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DescendentsWalker.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DescendentsWalker.cs index 8dbaa7d402c..7338bf29dfb 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DescendentsWalker.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DescendentsWalker.cs @@ -56,8 +56,8 @@ public virtual void StartWalk(DependencyObject startNode, bool skipStartNode) if (!skipStartNode) { - if (FrameworkElement.DType.IsInstanceOfType(_startNode) || - FrameworkContentElement.DType.IsInstanceOfType(_startNode)) + if (_startNode is FrameworkElement + or FrameworkContentElement) { // Callback for the root of the subtree continueWalk = _callback(_startNode, _data, _priority == TreeWalkPriority.VisualTree); @@ -79,9 +79,8 @@ private void IterateChildren(DependencyObject d) { _recursionDepth++; - if (FrameworkElement.DType.IsInstanceOfType(d)) + if (d is FrameworkElement fe) { - FrameworkElement fe = (FrameworkElement) d; bool hasLogicalChildren = fe.HasLogicalChildren; // FrameworkElement have both a visual and a logical tree. @@ -100,11 +99,10 @@ private void IterateChildren(DependencyObject d) Debug.Assert( false, "Tree walk priority should be Visual first or Logical first - but this instance of DescendentsWalker has an invalid priority setting that's neither of the two." ); } } - else if (FrameworkContentElement.DType.IsInstanceOfType(d)) + else if (d is FrameworkContentElement fce) { // FrameworkContentElement only has a logical tree, so we // Walk logical children - FrameworkContentElement fce = d as FrameworkContentElement; if (fce.HasLogicalChildren) { WalkLogicalChildren( null, fce, fce.LogicalChildren ); @@ -114,18 +112,13 @@ private void IterateChildren(DependencyObject d) { // Neither a FrameworkElement nor FrameworkContentElement. See // if it's a Visual and if so walk the Visual collection - Visual v = d as Visual; - if (v != null) + if (d is Visual v) { WalkVisualChildren(v); } - else + else if (d is Visual3D v3D) { - Visual3D v3D = d as Visual3D; - if (v3D != null) - { - WalkVisualChildren(v3D); - } + WalkVisualChildren(v3D); } } @@ -324,12 +317,12 @@ private void WalkFrameworkElementLogicalThenVisualChildren( for(int i = 0; i < count; i++) { Visual child = feParent.InternalGetVisualChild(i); - if (child != null && FrameworkElement.DType.IsInstanceOfType(child)) + if (child != null && child is FrameworkElement fe) { // For the case that both parents are identical, this node should // have already been visited when walking through logical // children, hence we short-circuit here - if (VisualTreeHelper.GetParent(child) != ((FrameworkElement) child).Parent) + if (VisualTreeHelper.GetParent(child) != fe.Parent) { bool visitedViaVisualTree = true; VisitNode(child, visitedViaVisualTree); @@ -404,11 +397,11 @@ private void VisitNode(DependencyObject d, bool visitedViaVisualTree) { if (_recursionDepth <= ContextLayoutManager.s_LayoutRecursionLimit) { - if (FrameworkElement.DType.IsInstanceOfType(d)) + if (d is FrameworkElement fe) { - VisitNode(d as FrameworkElement, visitedViaVisualTree); + VisitNode(fe, visitedViaVisualTree); } - else if (FrameworkContentElement.DType.IsInstanceOfType(d)) + else if (d is FrameworkContentElement) { _VisitNode(d, visitedViaVisualTree); } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DescendentsWalkerBase.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DescendentsWalkerBase.cs index 86e62723dbd..50fe3198db9 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DescendentsWalkerBase.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DescendentsWalkerBase.cs @@ -38,9 +38,8 @@ internal bool WasVisited(DependencyObject d) { DependencyObject logicalParent; - if (FrameworkElement.DType.IsInstanceOfType(ancestor)) + if (ancestor is FrameworkElement fe) { - FrameworkElement fe = ancestor as FrameworkElement; logicalParent = fe.Parent; // FrameworkElement DependencyObject dependencyObjectParent = VisualTreeHelper.GetParent(fe); diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs index 95274b6d670..325623a60d2 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs @@ -2233,8 +2233,10 @@ protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { //let incrementally-updating FrameworkElements to mark the vicinity of the affected child //to perform partial update. - if(FrameworkElement.DType.IsInstanceOfType(layoutParent)) - ((FrameworkElement)layoutParent).ParentLayoutInvalidated(this); + if(layoutParent is FrameworkElement fe) + { + fe.ParentLayoutInvalidated(this); + } if (affectsParentMeasure) { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkContentElement.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkContentElement.cs index c49f4b1f165..801648178a4 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkContentElement.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkContentElement.cs @@ -1218,19 +1218,6 @@ internal event InheritedPropertyChangedEventHandler InheritedPropertyChanged #endregion Internal Properties - //------------------------------------------------------ - // - // Internal Fields - // - //------------------------------------------------------ - - #region Internal Fields - - // Optimization, to avoid calling FromSystemType too often - internal new static DependencyObjectType DType = DependencyObjectType.FromSystemTypeInternal(typeof(FrameworkContentElement)); - - #endregion Internal Fields - //------------------------------------------------------ // // Private Fields diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs index 1066e2efac8..c5ab045f7c3 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs @@ -1267,19 +1267,6 @@ internal event InheritedPropertyChangedEventHandler InheritedPropertyChanged #endregion Internal Properties - //------------------------------------------------------ - // - // Internal Fields - // - //------------------------------------------------------ - - #region Internal Fields - - // Optimization, to avoid calling FromSystemType too often - internal new static DependencyObjectType DType = DependencyObjectType.FromSystemTypeInternal(typeof(FrameworkElement)); - - #endregion Internal Fields - //------------------------------------------------------ // // Private Fields diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Media/Animation/Storyboard.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Media/Animation/Storyboard.cs index 073e59f15f3..34d61b14856 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Media/Animation/Storyboard.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Media/Animation/Storyboard.cs @@ -929,13 +929,13 @@ private bool PropertyCloningRequired( object targetPropertyValue ) /// private void VerifyComplexPathSupport( DependencyObject targetObject ) { - if( FrameworkElement.DType.IsInstanceOfType(targetObject) ) + if(targetObject is FrameworkElement) { // FrameworkElement and derived types are supported. return; } - if( FrameworkContentElement.DType.IsInstanceOfType(targetObject) ) + if(targetObject is FrameworkContentElement) { // FrameworkContentElement and derived types are supported. return; diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs index 9d76b939e4e..508435ee9d9 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs @@ -2327,15 +2327,15 @@ internal static void SpecialDowncastToFEorFCE(DependencyObject d, out FrameworkElement fe, out FrameworkContentElement fce, bool throwIfNeither) { - if (FrameworkElement.DType.IsInstanceOfType(d)) + if (d is FrameworkElement frameworkElement) { - fe = (FrameworkElement)d; + fe = frameworkElement; fce = null; } - else if (FrameworkContentElement.DType.IsInstanceOfType(d)) + else if (d is FrameworkContentElement frameworkContentElement) { fe = null; - fce = (FrameworkContentElement)d; + fce = frameworkContentElement; } else if (throwIfNeither && !(d is System.Windows.Media.Media3D.Visual3D) ) { diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TreeWalkHelper.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TreeWalkHelper.cs index a109668b0e1..1489ff80f3b 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TreeWalkHelper.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TreeWalkHelper.cs @@ -1000,7 +1000,7 @@ private static bool OnInheritablePropertyChanged( // only then do we need to Invalidate the property if (BaseValueSourceInternal.Inherited >= oldEntry.BaseValueSourceInternal) { - if (visitedViaVisualTree && FrameworkElement.DType.IsInstanceOfType(d)) + if (visitedViaVisualTree && d is FrameworkElement) { DependencyObject logicalParent = LogicalTreeHelper.GetParent(d); if (logicalParent != null) diff --git a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentSequenceSerializer.cs b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentSequenceSerializer.cs index 83c6e68732e..e1496822ee8 100644 --- a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentSequenceSerializer.cs +++ b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentSequenceSerializer.cs @@ -194,7 +194,7 @@ SerializablePropertyContext serializablePropertyContext propertyValue); - if (typeof(Type).IsInstanceOfType(propertyValue)) + if (propertyValue is Type) { int index = valueAsString.LastIndexOf('.'); valueAsString = string.Concat( diff --git a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentSequenceSerializerAsync.cs b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentSequenceSerializerAsync.cs index 1f1225bbd6f..e0c04abbfc7 100644 --- a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentSequenceSerializerAsync.cs +++ b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentSequenceSerializerAsync.cs @@ -232,7 +232,7 @@ SerializablePropertyContext serializablePropertyContext propertyValue); - if (typeof(Type).IsInstanceOfType(propertyValue)) + if (propertyValue is Type) { int index = valueAsString.LastIndexOf('.'); valueAsString = string.Concat( diff --git a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedDocumentSerializer.cs b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedDocumentSerializer.cs index a85bd29b31b..ed8cfff47de 100644 --- a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedDocumentSerializer.cs +++ b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedDocumentSerializer.cs @@ -277,7 +277,7 @@ SerializablePropertyContext serializablePropertyContext propertyValue); - if (typeof(Type).IsInstanceOfType(propertyValue)) + if (propertyValue is Type) { int index = valueAsString.LastIndexOf('.'); valueAsString = string.Concat( diff --git a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedDocumentSerializerAsync.cs b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedDocumentSerializerAsync.cs index 477f4cc6198..249da14c521 100644 --- a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedDocumentSerializerAsync.cs +++ b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedDocumentSerializerAsync.cs @@ -317,7 +317,7 @@ SerializablePropertyContext serializablePropertyContext propertyValue); - if (typeof(Type).IsInstanceOfType(propertyValue)) + if (propertyValue is Type) { int index = valueAsString.LastIndexOf('.'); valueAsString = string.Concat( diff --git a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedPageSerializer.cs b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedPageSerializer.cs index 76d6e3c1413..ff1899ab917 100644 --- a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedPageSerializer.cs +++ b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedPageSerializer.cs @@ -350,7 +350,7 @@ SerializablePropertyContext serializablePropertyContext propertyValue); - if (typeof(Type).IsInstanceOfType(propertyValue)) + if (propertyValue is Type) { int index = valueAsString.LastIndexOf('.'); valueAsString = string.Concat( diff --git a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedPageSerializerAsync.cs b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedPageSerializerAsync.cs index 186f5d0bee0..8cd45d0fa58 100644 --- a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedPageSerializerAsync.cs +++ b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachFixedPageSerializerAsync.cs @@ -322,7 +322,7 @@ SerializablePropertyContext serializablePropertyContext propertyValue); - if (typeof(Type).IsInstanceOfType(propertyValue)) + if (propertyValue is Type) { int index = valueAsString.LastIndexOf('.'); valueAsString = string.Concat( diff --git a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/XpsOMFixedPageSerializer.cs b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/XpsOMFixedPageSerializer.cs index bf5204f9bfd..fd1b65885fc 100644 --- a/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/XpsOMFixedPageSerializer.cs +++ b/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/XpsOMFixedPageSerializer.cs @@ -308,7 +308,7 @@ SerializablePropertyContext serializablePropertyContext propertyValue); - if (typeof(Type).IsInstanceOfType(propertyValue)) + if (propertyValue is Type) { int index = valueAsString.LastIndexOf('.'); valueAsString = string.Concat( From 9239565e69a9648e9217603282fdc1d85f215290 Mon Sep 17 00:00:00 2001 From: lindexi Date: Thu, 21 Jul 2022 13:17:21 +0800 Subject: [PATCH 08/13] Merge pattern in DispatcherOperation (#5296) --- .../WindowsBase/System/Windows/Threading/DispatcherOperation.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/DispatcherOperation.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/DispatcherOperation.cs index 766a619dc20..9ef8dfa13d2 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/DispatcherOperation.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/DispatcherOperation.cs @@ -425,7 +425,7 @@ internal void Invoke() EventHandler handler; // either completed or aborted lock(DispatcherLock) { - if(_exception != null && _exception is OperationCanceledException) + if(_exception is OperationCanceledException) { // A new way to abort/cancel an operation is to raise an // OperationCanceledException exception. This only works From 4fb54c6bcf417a74ebf9f6495ac11ce8f31b7203 Mon Sep 17 00:00:00 2001 From: ThomasGoulet73 <51839772+ThomasGoulet73@users.noreply.github.com> Date: Fri, 17 Jun 2022 10:43:29 -0400 Subject: [PATCH 09/13] Remove duplicate Microsoft.Private.Winforms (#6648) --- .../src/PresentationUI/PresentationUI.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationUI/PresentationUI.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationUI/PresentationUI.csproj index 421b0514185..2cb661f507d 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationUI/PresentationUI.csproj +++ b/src/Microsoft.DotNet.Wpf/src/PresentationUI/PresentationUI.csproj @@ -216,7 +216,6 @@ - From 1d217ae6af0b847d3d940542aa5f4ba91353cf14 Mon Sep 17 00:00:00 2001 From: Dipesh Kumar <85861525+dipeshmsft@users.noreply.github.com> Date: Fri, 17 Jun 2022 20:13:53 +0530 Subject: [PATCH 10/13] Fixing the C++/CLI projects failing due to NU1504 (#6698) --- eng/WpfArcadeSdk/tools/Wpf.Cpp.targets | 10 ++++++++++ .../DirectWriteForwarder/DirectWriteForwarder.vcxproj | 5 +++++ .../src/System.Printing/System.Printing.vcxproj | 5 +++++ 3 files changed, 20 insertions(+) diff --git a/eng/WpfArcadeSdk/tools/Wpf.Cpp.targets b/eng/WpfArcadeSdk/tools/Wpf.Cpp.targets index f1a7de9a9bb..ad973b35172 100644 --- a/eng/WpfArcadeSdk/tools/Wpf.Cpp.targets +++ b/eng/WpfArcadeSdk/tools/Wpf.Cpp.targets @@ -353,6 +353,16 @@ using namespace System::Runtime::Versioning; + + + + + + .NETCoreApp v6.0 Unknown + + true diff --git a/src/Microsoft.DotNet.Wpf/src/System.Printing/System.Printing.vcxproj b/src/Microsoft.DotNet.Wpf/src/System.Printing/System.Printing.vcxproj index 08aefddac41..5183124e00d 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Printing/System.Printing.vcxproj +++ b/src/Microsoft.DotNet.Wpf/src/System.Printing/System.Printing.vcxproj @@ -31,6 +31,11 @@ .NETCoreApp v6.0 Unknown + + true From f4f4c380116b3eb0d32195ad22be8c009c60b5e4 Mon Sep 17 00:00:00 2001 From: lindexi Date: Wed, 7 Sep 2022 16:04:48 +0800 Subject: [PATCH 11/13] Fix Compile for 50614d5 --- .../System/Windows/Input/InputElement.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs index 80b780a9016..96bd8eed761 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs @@ -28,6 +28,24 @@ internal static bool IsValid(DependencyObject o) return o is UIElement or ContentElement or UIElement3D; } + // Returns whether the given DynamicObject is a UIElement or not. + internal static bool IsUIElement(DependencyObject o) + { + return UIElementType.IsInstanceOfType(o); + } + + // Returns whether the given DynamicObject is a UIElement3D or not. + internal static bool IsUIElement3D(DependencyObject o) + { + return UIElement3DType.IsInstanceOfType(o); + } + + // Returns whether the given DynamicObject is a ContentElement or not. + internal static bool IsContentElement(DependencyObject o) + { + return ContentElementType.IsInstanceOfType(o); + } + // Returns the containing input element of the given DynamicObject. // If onlyTraverse2D is set to true, then we stop once we see a 3D object and return null internal static DependencyObject GetContainingUIElement(DependencyObject o, bool onlyTraverse2D) From 0c5a8b63c221eccb66a704c257bf1ff2bf119e27 Mon Sep 17 00:00:00 2001 From: lindexi Date: Wed, 7 Sep 2022 17:51:18 +0800 Subject: [PATCH 12/13] Revert "Fix Compile for 50614d5" This reverts commit f4f4c380116b3eb0d32195ad22be8c009c60b5e4. --- .../System/Windows/Input/InputElement.cs | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs index 96bd8eed761..80b780a9016 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputElement.cs @@ -28,24 +28,6 @@ internal static bool IsValid(DependencyObject o) return o is UIElement or ContentElement or UIElement3D; } - // Returns whether the given DynamicObject is a UIElement or not. - internal static bool IsUIElement(DependencyObject o) - { - return UIElementType.IsInstanceOfType(o); - } - - // Returns whether the given DynamicObject is a UIElement3D or not. - internal static bool IsUIElement3D(DependencyObject o) - { - return UIElement3DType.IsInstanceOfType(o); - } - - // Returns whether the given DynamicObject is a ContentElement or not. - internal static bool IsContentElement(DependencyObject o) - { - return ContentElementType.IsInstanceOfType(o); - } - // Returns the containing input element of the given DynamicObject. // If onlyTraverse2D is set to true, then we stop once we see a 3D object and return null internal static DependencyObject GetContainingUIElement(DependencyObject o, bool onlyTraverse2D) From 63df3a249580de60cd8ff0dda97d14c9c6b00ea3 Mon Sep 17 00:00:00 2001 From: Robin Lindner Date: Mon, 3 Jan 2022 12:02:55 +0100 Subject: [PATCH 13/13] FindCommandBinding improvements (#5693) * General code improvements for CommandManager.FindCommandBinding * Further improvements --- .../Windows/Input/Command/CommandBinding.cs | 123 +++++++----------- .../Input/Command/CommandBindingCollection.cs | 109 ++++++---------- .../Windows/Input/Command/CommandManager.cs | 105 +++++++-------- 3 files changed, 140 insertions(+), 197 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBinding.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBinding.cs index 0890cda1424..02acc51b56a 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBinding.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBinding.cs @@ -7,6 +7,7 @@ using System; using System.ComponentModel; +using System.Diagnostics; using System.Windows; using System.Windows.Markup; using MS.Internal; @@ -60,18 +61,13 @@ public CommandBinding(ICommand command, ExecutedRoutedEventHandler executed) /// Handler associated with determining if the command can execute. public CommandBinding(ICommand command, ExecutedRoutedEventHandler executed, CanExecuteRoutedEventHandler canExecute) { - if (command == null) - { - throw new ArgumentNullException("command"); - } + _command = command ?? throw new ArgumentNullException(nameof(command)); - _command = command; - - if (executed != null) + if (executed is not null) { Executed += executed; } - if (canExecute != null) + if (canExecute is not null) { CanExecute += canExecute; } @@ -87,20 +83,8 @@ public CommandBinding(ICommand command, ExecutedRoutedEventHandler executed, Can [Localizability(LocalizationCategory.NeverLocalize)] // cannot be localized public ICommand Command { - get - { - return _command; - } - - set - { - if (value == null) - { - throw new ArgumentNullException("value"); - } - - _command = value; - } + get => _command; + set => _command = value ?? throw new ArgumentNullException(nameof(value)); } #endregion @@ -138,49 +122,46 @@ public ICommand Command /// Event arguments. internal void OnCanExecute(object sender, CanExecuteRoutedEventArgs e) { - if (!e.Handled) + if (e.Handled) return; + if (e.RoutedEvent == CommandManager.CanExecuteEvent) { - if (e.RoutedEvent == CommandManager.CanExecuteEvent) + if (CanExecute is null) { - if (CanExecute != null) - { - CanExecute(sender, e); - if (e.CanExecute) - { - e.Handled = true; - } - } - else if (!e.CanExecute) - { - // If there is an Executed handler, then the command can be executed. - if (Executed != null) - { - e.CanExecute = true; - e.Handled = true; - } - } + if (e.CanExecute) return; + // If there is an Executed handler, then the command can be executed. + if (Executed is null) return; + e.CanExecute = true; + e.Handled = true; } - else // e.RoutedEvent == CommandManager.PreviewCanExecuteEvent + else { - if (PreviewCanExecute != null) + CanExecute(sender, e); + if (e.CanExecute) { - PreviewCanExecute(sender, e); - if (e.CanExecute) - { - e.Handled = true; - } + e.Handled = true; } } } + else // e.RoutedEvent == CommandManager.PreviewCanExecuteEvent + { + if (PreviewCanExecute is null) return; + PreviewCanExecute(sender, e); + if (e.CanExecute) + { + e.Handled = true; + } + } } private bool CheckCanExecute(object sender, ExecutedRoutedEventArgs e) { - CanExecuteRoutedEventArgs canExecuteArgs = new CanExecuteRoutedEventArgs(e.Command, e.Parameter); - canExecuteArgs.RoutedEvent = CommandManager.CanExecuteEvent; + CanExecuteRoutedEventArgs canExecuteArgs = new(e.Command, e.Parameter) + { + RoutedEvent = CommandManager.CanExecuteEvent, + // Since we don't actually raise this event, we have to explicitly set the source. + Source = e.OriginalSource + }; - // Since we don't actually raise this event, we have to explicitly set the source. - canExecuteArgs.Source = e.OriginalSource; canExecuteArgs.OverrideSource(e.Source); OnCanExecute(sender, canExecuteArgs); @@ -195,30 +176,22 @@ private bool CheckCanExecute(object sender, ExecutedRoutedEventArgs e) /// Event arguments. internal void OnExecuted(object sender, ExecutedRoutedEventArgs e) { - if (!e.Handled) + if (e.Handled) return; + if (e.RoutedEvent == CommandManager.ExecutedEvent) { - if (e.RoutedEvent == CommandManager.ExecutedEvent) - { - if (Executed != null) - { - if (CheckCanExecute(sender, e)) - { - Executed(sender, e); - e.Handled = true; - } - } - } - else // e.RoutedEvent == CommandManager.PreviewExecutedEvent - { - if (PreviewExecuted != null) - { - if (CheckCanExecute(sender, e)) - { - PreviewExecuted(sender, e); - e.Handled = true; - } - } - } + if (Executed is null) return; + if (!CheckCanExecute(sender, e)) return; + Debug.Assert(Executed != null, nameof(Executed) + " != null"); + Executed(sender, e); + e.Handled = true; + } + else // e.RoutedEvent == CommandManager.PreviewExecutedEvent + { + if (PreviewExecuted is null) return; + if (!CheckCanExecute(sender, e)) return; + Debug.Assert(PreviewExecuted != null, nameof(PreviewExecuted) + " != null"); + PreviewExecuted(sender, e); + e.Handled = true; } } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBindingCollection.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBindingCollection.cs index 98725d64267..8adf9e4d232 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBindingCollection.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandBindingCollection.cs @@ -52,9 +52,9 @@ public CommandBindingCollection() /// CommandBinding array public CommandBindingCollection(IList commandBindings) { - if (commandBindings != null && commandBindings.Count > 0) + if (commandBindings is { Count: > 0 }) { - AddRange(commandBindings as ICollection); + AddRange(commandBindings); } } @@ -76,12 +76,9 @@ public CommandBindingCollection(IList commandBindings) /// /// commandbinding array to copy into /// start index in current list to copy - void ICollection.CopyTo(System.Array array, int index) + void ICollection.CopyTo(System.Array array, int index) { - if (_innerCBList != null) - { - ((ICollection)_innerCBList).CopyTo(array, index); - } + ((ICollection)_innerCBList)?.CopyTo(array, index); } #endregion Implementation of ICollection @@ -92,7 +89,7 @@ void ICollection.CopyTo(System.Array array, int index) /// true - if found, false - otherwise bool IList.Contains(object key) { - return this.Contains(key as CommandBinding) ; + return Contains(key as CommandBinding) ; } /// @@ -102,8 +99,7 @@ bool IList.Contains(object key) /// int IList.IndexOf(object value) { - CommandBinding commandBinding = value as CommandBinding; - return ((commandBinding != null) ? this.IndexOf(commandBinding) : -1); + return ((value is CommandBinding commandBinding) ? IndexOf(commandBinding) : -1); } /// @@ -113,7 +109,7 @@ int IList.IndexOf(object value) /// item to insert void IList.Insert(int index, object value) { - this.Insert(index, value as CommandBinding); + Insert(index, value as CommandBinding); } /// @@ -122,7 +118,7 @@ void IList.Insert(int index, object value) /// CommandBinding object to add int IList.Add(object commandBinding) { - return this.Add(commandBinding as CommandBinding); + return Add(commandBinding as CommandBinding); } /// @@ -131,7 +127,7 @@ int IList.Add(object commandBinding) /// CommandBinding object to remove void IList.Remove(object commandBinding) { - this.Remove(commandBinding as CommandBinding); + Remove(commandBinding as CommandBinding); } /// @@ -145,8 +141,7 @@ object IList.this[int index] } set { - CommandBinding commandBinding = value as CommandBinding; - if (commandBinding == null) + if (value is not CommandBinding commandBinding) throw new NotSupportedException(SR.Get(SRID.CollectionOnlyAcceptsCommandBindings)); this[index] = commandBinding; @@ -160,7 +155,7 @@ public CommandBinding this[int index] { get { - return (_innerCBList != null ? _innerCBList[index] : null); + return (_innerCBList?[index]); } set { @@ -179,8 +174,7 @@ public int Add(CommandBinding commandBinding) { if (commandBinding != null) { - if (_innerCBList == null) - _innerCBList = new System.Collections.Generic.List(1); + _innerCBList ??= new Collections.Generic.List(1); _innerCBList.Add(commandBinding); return 0; // ICollection.Add no longer returns the indice @@ -200,27 +194,23 @@ public int Add(CommandBinding commandBinding) public void AddRange(ICollection collection) { if (collection==null) - throw new ArgumentNullException("collection"); - - if (collection.Count > 0) - { - if (_innerCBList == null) - _innerCBList = new System.Collections.Generic.List(collection.Count); + throw new ArgumentNullException(nameof(collection)); + + if (collection.Count <= 0) return; + _innerCBList ??= new System.Collections.Generic.List(collection.Count); - IEnumerator collectionEnum = collection.GetEnumerator(); - while(collectionEnum.MoveNext()) + IEnumerator collectionEnum = collection.GetEnumerator(); + while(collectionEnum.MoveNext()) + { + if (collectionEnum.Current is CommandBinding cmdBinding) { - CommandBinding cmdBinding = collectionEnum.Current as CommandBinding; - if (cmdBinding != null) - { - _innerCBList.Add(cmdBinding); - } - else - { - throw new NotSupportedException(SR.Get(SRID.CollectionOnlyAcceptsCommandBindings)); - } - } - } + _innerCBList.Add(cmdBinding); + } + else + { + throw new NotSupportedException(SR.Get(SRID.CollectionOnlyAcceptsCommandBindings)); + } + } } /// @@ -232,8 +222,7 @@ public void Insert(int index, CommandBinding commandBinding) { if (commandBinding != null) { - if (_innerCBList != null) - _innerCBList.Insert(index, commandBinding); + _innerCBList?.Insert(index, commandBinding); } else { @@ -257,8 +246,7 @@ public void Remove(CommandBinding commandBinding) /// index at which the item needs to be removed public void RemoveAt(int index) { - if (_innerCBList != null) - _innerCBList.RemoveAt(index); + _innerCBList?.RemoveAt(index); } /// @@ -276,11 +264,7 @@ public bool IsSynchronized { get { - if (_innerCBList != null) - { - return ((IList)_innerCBList).IsSynchronized; - } - return false; + return _innerCBList is not null && ((IList)_innerCBList).IsSynchronized; } } @@ -310,7 +294,7 @@ public int Count { get { - return (_innerCBList != null ? _innerCBList.Count : 0); + return _innerCBList?.Count ?? 0; } } @@ -319,11 +303,9 @@ public int Count /// public void Clear() { - if (_innerCBList != null) - { - _innerCBList.Clear(); - _innerCBList = null; - } + if (_innerCBList is null) return; + _innerCBList.Clear(); + _innerCBList = null; } /// @@ -333,7 +315,7 @@ public void Clear() /// public int IndexOf(CommandBinding value) { - return ((_innerCBList != null) ? _innerCBList.IndexOf(value) : -1); + return _innerCBList?.IndexOf(value) ?? -1; } /// @@ -355,10 +337,9 @@ public bool Contains(CommandBinding commandBinding) /// /// type-safe (CommandBinding) array /// start index in current list to copy - public void CopyTo(CommandBinding[] commandBindings, int index) + public void CopyTo(CommandBinding[] commandBindings, int index) { - if (_innerCBList != null) - _innerCBList.CopyTo(commandBindings, index); + _innerCBList?.CopyTo(commandBindings, index); } #region Implementation of Enumerable @@ -385,17 +366,11 @@ internal ICommand FindMatch(object targetElement, InputEventArgs inputEventArgs) for (int i = 0; i < Count; i++) { CommandBinding commandBinding = this[i]; - RoutedCommand routedCommand = commandBinding.Command as RoutedCommand; - if (routedCommand != null) + if (commandBinding.Command is not RoutedCommand routedCommand) continue; + InputGestureCollection inputGestures = routedCommand.InputGesturesInternal; + if (inputGestures?.FindMatch(targetElement, inputEventArgs) != null) { - InputGestureCollection inputGestures = routedCommand.InputGesturesInternal; - if (inputGestures != null) - { - if (inputGestures.FindMatch(targetElement, inputEventArgs) != null) - { - return routedCommand; - } - } + return routedCommand; } } @@ -423,7 +398,7 @@ internal CommandBinding FindMatch(ICommand command, ref int index) // //------------------------------------------------------ #region Private Fields - private System.Collections.Generic.List _innerCBList; + private Collections.Generic.List _innerCBList; #endregion Private Fields } } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandManager.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandManager.cs index 1434794fb60..3a0454e3212 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandManager.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/CommandManager.cs @@ -601,64 +601,56 @@ internal static void OnCommandDevice(object sender, CommandDeviceEventArgs e) private static void FindCommandBinding(object sender, RoutedEventArgs e, ICommand command, bool execute) { // Check local command bindings - CommandBindingCollection commandBindings = null; - DependencyObject senderAsDO = sender as DependencyObject; - if (InputElement.IsUIElement(senderAsDO)) + CommandBindingCollection commandBindings = sender switch { - commandBindings = ((UIElement)senderAsDO).CommandBindingsInternal; - } - else if (InputElement.IsContentElement(senderAsDO)) - { - commandBindings = ((ContentElement)senderAsDO).CommandBindingsInternal; - } - else if (InputElement.IsUIElement3D(senderAsDO)) - { - commandBindings = ((UIElement3D)senderAsDO).CommandBindingsInternal; - } - if (commandBindings != null) + UIElement uiElement => uiElement.CommandBindingsInternal, + ContentElement contentElement => contentElement.CommandBindingsInternal, + UIElement3D uiElement3d => uiElement3d.CommandBindingsInternal, + _ => default + }; + if (commandBindings is not null) { FindCommandBinding(commandBindings, sender, e, command, execute); } + Type senderType = sender.GetType(); + // If no command binding is found, check class command bindings // First find the relevant command bindings, under the lock. // Most of the time there are no such bindings; most of the rest of // the time there is only one. Lazy-allocate with this in mind. - Tuple tuple = null; // zero or one binding - List> list = null; // more than one - + ValueTuple? tuple = default; // zero or one binding + List> list = default; // more than one + lock (_classCommandBindings.SyncRoot) { // Check from the current type to all the base types - Type classType = sender.GetType(); - while (classType != null) + Type classType = senderType; + while (classType is not null) { - CommandBindingCollection classCommandBindings = _classCommandBindings[classType] as CommandBindingCollection; - if (classCommandBindings != null) + if (_classCommandBindings[classType] is CommandBindingCollection classCommandBindings) { int index = 0; while (true) { CommandBinding commandBinding = classCommandBindings.FindMatch(command, ref index); - if (commandBinding != null) + if (commandBinding is null) { - if (tuple == null) - { - tuple = new Tuple(classType, commandBinding); - } - else - { - if (list == null) - { - list = new List>(); - list.Add(tuple); - } - list.Add(new Tuple(classType, commandBinding)); - } + break; + } + + if (tuple is null) + { + tuple = ValueTuple.Create(classType, commandBinding); } else { - break; + list ??= new List>(8) + { + // We know that tuple cannot be null here + tuple.Value + }; + list.Add(new ValueTuple(classType, commandBinding)); } } } @@ -668,37 +660,35 @@ private static void FindCommandBinding(object sender, RoutedEventArgs e, IComman // execute the bindings. This can call into user code, so it must // be done outside the lock to avoid deadlock. - if (list != null) + if (list is not null) { // more than one binding - ExecutedRoutedEventArgs exArgs = execute ? (ExecutedRoutedEventArgs)e : null; - CanExecuteRoutedEventArgs canExArgs = execute ? null : (CanExecuteRoutedEventArgs)e; - for (int i=0; i