diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputProcessorProfiles.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputProcessorProfiles.cs index be116765831..c7602cf3321 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputProcessorProfiles.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputProcessorProfiles.cs @@ -1,44 +1,25 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -// -// // // Description: Creates ITfInputProcessorProfiles instances. // -// using System.Runtime.InteropServices; +using System.Globalization; using System.Threading; using MS.Win32; -using System.Globalization; -using System.Collections; namespace System.Windows.Input { - //------------------------------------------------------ - // - // InputProcessorProfiles class - // - //------------------------------------------------------ - /// - /// The InputProcessorProfiles class is always associated with - /// hwndInputLanguage class. + /// The class is always associated with hwndInputLanguage class. /// - internal class InputProcessorProfiles + internal sealed class InputProcessorProfiles { - //------------------------------------------------------ - // - // Constructors - // - //------------------------------------------------------ - /// /// InputProcessorProfiles Constructor; /// - /// Critical - as this sets the value for _ipp. - /// Safe - as this just initializes it to null. internal InputProcessorProfiles() { // _ipp is a ValueType, hence no need for new. @@ -46,14 +27,6 @@ internal InputProcessorProfiles() _cookie = UnsafeNativeMethods.TF_INVALID_COOKIE; } - //------------------------------------------------------ - // - // Internal Methods - // - //------------------------------------------------------ - - #region Internal Methods - /// /// Initialize an interface and notify sink. /// @@ -81,19 +54,11 @@ internal void Uninitialize() { Debug.Assert(_ipp != null, "Uninitialize called without initializing"); - UnadviseNotifySink(); + UnadviseNotifySink(); Marshal.ReleaseComObject(_ipp); _ipp = null; } - #endregion Internal Methods - - //------------------------------------------------------ - // - // Internal Properties - // - //------------------------------------------------------ - /// /// Get the current input language of the current thread. /// @@ -112,7 +77,7 @@ internal short CurrentInputLanguage IntPtr[] hklList = null; int count = (int)SafeNativeMethods.GetKeyboardLayoutList(0, null); - if (count > 1) + if (count > 1) { hklList = new IntPtr[count]; @@ -123,7 +88,7 @@ internal short CurrentInputLanguage { if (value == (short)hklList[i]) { - SafeNativeMethods.ActivateKeyboardLayout(new HandleRef(this,hklList[i]), 0); + SafeNativeMethods.ActivateKeyboardLayout(new HandleRef(this, hklList[i]), 0); break; } } @@ -134,43 +99,29 @@ internal short CurrentInputLanguage } /// - /// Get the list of the input languages that are available in the - /// current thread. + /// Get the list of the input languages that are available in the current thread. /// - internal ArrayList InputLanguageList + internal unsafe CultureInfo[] InputLanguageList { - get - { - int nCount; - IntPtr langids; - - // ITfInputProcessorProfiles::GetLanguageList returns the pointer that was allocated by - // CoTaskMemAlloc(). - _ipp.GetLanguageList(out langids, out nCount); + get + { + // ITfInputProcessorProfiles::GetLanguageList returns the pointer that was allocated by CoTaskMemAlloc(). + _ipp.GetLanguageList(out nint ptrLanguageIDs, out int nCount); - ArrayList arrayLang = new ArrayList(); + ReadOnlySpan languageIDs = new((void*)ptrLanguageIDs, nCount); + CultureInfo[] langArray = new CultureInfo[nCount]; - for (int i = 0; i < nCount; i++) - { - // Unmarshal each langid from short array. - short langid = Marshal.PtrToStructure((IntPtr)((Int64)langids + sizeof(short) * i)); - arrayLang.Add(new CultureInfo(langid)); - } + // Create CultureInfo from each ID and store it + for (int i = 0; i < langArray.Length; i++) + langArray[i] = new CultureInfo(languageIDs[i]); - // Call CoTaskMemFree(). - Marshal.FreeCoTaskMem(langids); + // Call CoTaskMemFree(). + Marshal.FreeCoTaskMem(ptrLanguageIDs); - return arrayLang; - } + return langArray; + } } - - //------------------------------------------------------ - // - // Private Methods - // - //------------------------------------------------------ - /// /// This advices the input language notify sink to /// ITfInputProcessorProfile. @@ -201,16 +152,14 @@ private void UnadviseNotifySink() _cookie = UnsafeNativeMethods.TF_INVALID_COOKIE; } - //------------------------------------------------------ - // - // Private Fields - // - //------------------------------------------------------ - - // The reference to ITfInputProcessorProfile. + /// + /// The reference to . + /// private UnsafeNativeMethods.ITfInputProcessorProfiles _ipp; - // The cookie for the advised sink. + /// + /// The cookie for the advised sink. + /// private int _cookie; } }