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 ae20d50b564..938cafa46e4 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs @@ -584,24 +584,20 @@ internal static object FireContentRendered(object arg) /// Helper method which returns true when all the given visuals /// are in the same presentation source. /// - internal static bool UnderSamePresentationSource(params DependencyObject[] visuals) + internal static bool IsUnderSamePresentationSource(params ReadOnlySpan visuals) { - if (visuals == null || visuals.Length == 0) - { + if (visuals.IsEmpty) return true; - } PresentationSource baseSource = CriticalFromVisual(visuals[0]); - - int count = visuals.Length; - for (int i = 1; i < count; i++) + for (int i = 1; i < visuals.Length; i++) { - PresentationSource currentSource = CriticalFromVisual(visuals[i]); - if (currentSource != baseSource) + if (baseSource != CriticalFromVisual(visuals[i])) { return false; } } + return true; } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Commands/CommandHelpers.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Commands/CommandHelpers.cs index 0f47f46e942..d54f1c05169 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Commands/CommandHelpers.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Commands/CommandHelpers.cs @@ -15,7 +15,7 @@ internal static class CommandHelpers internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler) { - PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null, null); + PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null); } internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, @@ -39,7 +39,7 @@ internal static void RegisterCommandHandler(Type controlType, RoutedCommand comm internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, CanExecuteRoutedEventHandler canExecuteRoutedEventHandler) { - PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler, null); + PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, canExecuteRoutedEventHandler); } internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, @@ -90,7 +90,7 @@ internal static void RegisterCommandHandler(Type controlType, RoutedCommand comm // 'params' based method is private. Call sites that use this bloat unwittingly due to implicit construction of the params array that goes into IL. private static void PrivateRegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler, - CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, params InputGesture[] inputGestures) + CanExecuteRoutedEventHandler canExecuteRoutedEventHandler, params ReadOnlySpan inputGestures) { // Validate parameters Debug.Assert(controlType != null); @@ -102,12 +102,9 @@ private static void PrivateRegisterCommandHandler(Type controlType, RoutedComman CommandManager.RegisterClassCommandBinding(controlType, new CommandBinding(command, executedRoutedEventHandler, canExecuteRoutedEventHandler)); // Create additional input binding for this command - if (inputGestures != null) + for (int i = 0; i < inputGestures.Length; i++) { - for (int i = 0; i < inputGestures.Length; i++) - { - CommandManager.RegisterClassInputBinding(controlType, new InputBinding(command, inputGestures[i])); - } + CommandManager.RegisterClassInputBinding(controlType, new InputBinding(command, inputGestures[i])); } } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DataGridDetailsPresenter.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DataGridDetailsPresenter.cs index 939ea429282..9da6163d5c0 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DataGridDetailsPresenter.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DataGridDetailsPresenter.cs @@ -105,7 +105,7 @@ private static void OnAnyMouseLeftButtonDownThunk(object sender, MouseButtonEven private void OnAnyMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e) { // Ignore actions if the button down arises from a different presentation source - if (!PresentationSource.UnderSamePresentationSource(e.OriginalSource as DependencyObject, this)) + if (!PresentationSource.IsUnderSamePresentationSource(e.OriginalSource as DependencyObject, this)) { return; } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ScrollViewer.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ScrollViewer.cs index acacc5d81e5..f88ab3e3969 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ScrollViewer.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ScrollViewer.cs @@ -1648,7 +1648,7 @@ private bool ShouldManipulateScroll(ManipulationStartingEventArgs e, ScrollConte { // If the original source is not from the same PresentationSource as of ScrollViewer, // then do not start the manipulation. - if (!PresentationSource.UnderSamePresentationSource(e.OriginalSource as DependencyObject, this)) + if (!PresentationSource.IsUnderSamePresentationSource(e.OriginalSource as DependencyObject, this)) { return false; } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizingStackPanel.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizingStackPanel.cs index 0e3d45b11eb..01877128a89 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizingStackPanel.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizingStackPanel.cs @@ -12117,7 +12117,7 @@ static void Flush() } // for use from VS Immediate window - static void Mark(params object[] args) + static void Mark(params ReadOnlySpan args) { ScrollTraceRecord record = new ScrollTraceRecord(ScrollTraceOp.Mark, null, -1, 0, 0, BuildDetail(args)); lock (s_TargetToTraceListMap) @@ -12229,7 +12229,7 @@ internal static bool IsTracing(VirtualizingStackPanel vsp) return (sti != null && sti.ScrollTracer != null); } - internal static void Trace(VirtualizingStackPanel vsp, ScrollTraceOp op, params object[] args) + internal static void Trace(VirtualizingStackPanel vsp, ScrollTraceOp op, params ReadOnlySpan args) { ScrollTracingInfo sti = ScrollTracingInfoField.GetValue(vsp); ScrollTracer tracer = sti.ScrollTracer; @@ -12272,16 +12272,12 @@ private static string DisplayType(object o) return sb.ToString(); } - private static string BuildDetail(object[] args) + private static string BuildDetail(ReadOnlySpan args) { - int length = (args != null) ? args.Length : 0; - if (length == 0) - return String.Empty; - else - return String.Format(CultureInfo.InvariantCulture, s_format[length], args); + return args.IsEmpty ? string.Empty : string.Format(CultureInfo.InvariantCulture, s_format[args.Length], args); } - private static string[] s_format = new string[] { + private static readonly string[] s_format = new string[] { "", "{0}", "{0} {1}", @@ -12392,7 +12388,7 @@ private void IdentifyTrace(ItemsControl ic, VirtualizingStackPanel vsp) } } - private void AddTrace(VirtualizingStackPanel vsp, ScrollTraceOp op, ScrollTracingInfo sti, params object[] args) + private void AddTrace(VirtualizingStackPanel vsp, ScrollTraceOp op, ScrollTracingInfo sti, params ReadOnlySpan args) { // the trace list contains references back into the VSP that can lead // to memory leaks if the app removes the VSP. To avoid this, treat @@ -12403,10 +12399,8 @@ private void AddTrace(VirtualizingStackPanel vsp, ScrollTraceOp op, ScrollTracin { if (++_luCount > _luThreshold) { - AddTrace(null, ScrollTraceOp.ID, _nullInfo, - "Inactive at", DateTime.Now); - ItemsControl ic; - if (_wrIC.TryGetTarget(out ic)) + AddTrace(null, ScrollTraceOp.ID, _nullInfo, "Inactive at", DateTime.Now); + if (_wrIC.TryGetTarget(out ItemsControl ic)) { ic.LayoutUpdated -= OnLayoutUpdated; } @@ -12421,10 +12415,8 @@ private void AddTrace(VirtualizingStackPanel vsp, ScrollTraceOp op, ScrollTracin if (luCount < 0) { - AddTrace(null, ScrollTraceOp.ID, _nullInfo, - "Reactivate at", DateTime.Now); - ItemsControl ic; - if (_wrIC.TryGetTarget(out ic)) + AddTrace(null, ScrollTraceOp.ID, _nullInfo, "Reactivate at", DateTime.Now); + if (_wrIC.TryGetTarget(out ItemsControl ic)) { ic.LayoutUpdated += OnLayoutUpdated; } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VisualStates.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VisualStates.cs index 8d6900c55ce..6ae486e1bc8 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VisualStates.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VisualStates.cs @@ -4,10 +4,7 @@ namespace System.Windows.Controls { - /// - /// Names and helpers for visual states in the controls. - /// THIS IS A SHARED FILE. PresentationFramework.Design.dll must be rebuilt if changed. - /// + /// Names and helpers for visual states in the controls. internal static class VisualStates { #region CalendarDayButton @@ -414,9 +411,9 @@ internal static class VisualStates /// Ordered list of state names and fallback states to transition into. /// Only the first state to be found will be used. /// - public static void GoToState(Control control, bool useTransitions, params string[] stateNames) + public static void GoToState(Control control, bool useTransitions, params ReadOnlySpan stateNames) { - if (stateNames == null) + if (stateNames.IsEmpty) { return; } 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 9d04787a2c6..6b832584362 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 @@ -4755,7 +4755,7 @@ internal static void Flush() } // for use from VS Immediate window - internal static void Mark(params object[] args) + internal static void Mark(params ReadOnlySpan args) { IMECompositionTraceRecord record = new IMECompositionTraceRecord(IMECompositionTraceOp.Mark, BuildDetail(args)); lock (s_TargetToTraceListMap) @@ -4807,7 +4807,7 @@ internal static bool IsTracing(TextStore textStore) return (cti != null && cti.IMECompositionTracer != null); } - internal static void Trace(TextStore textStore, IMECompositionTraceOp op, params object[] args) + internal static void Trace(TextStore textStore, IMECompositionTraceOp op, params ReadOnlySpan args) { IMECompositionTracingInfo cti = IMECompositionTracingInfoField.GetValue(textStore.UiScope); IMECompositionTracer tracer = cti.IMECompositionTracer; @@ -4850,16 +4850,12 @@ private static string DisplayType(object o) return sb.ToString(); } - private static string BuildDetail(object[] args) + private static string BuildDetail(ReadOnlySpan args) { - int length = (args != null) ? args.Length : 0; - if (length == 0) - return String.Empty; - else - return String.Format(CultureInfo.InvariantCulture, s_format[length], args); + return args.IsEmpty ? string.Empty : string.Format(CultureInfo.InvariantCulture, s_format[args.Length], args); } - private static string[] s_format = new string[] { + private static readonly string[] s_format = new string[] { "", "{0}", "{0} {1}", @@ -4924,7 +4920,7 @@ private void IdentifyTrace(TextStore textStore) AddTrace(textStore, IMECompositionTraceOp.ID, _nullInfo, DisplayType(uiScope)); } - private void AddTrace(TextStore textStore, IMECompositionTraceOp op, IMECompositionTracingInfo cti, params object[] args) + private void AddTrace(TextStore textStore, IMECompositionTraceOp op, IMECompositionTracingInfo cti, params ReadOnlySpan args) { // pop a E* op from the stack if (IMECompositionTraceOp.FirstEndOp <= op && _opStack.Count > 0) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs index bb4f15d920f..007078cd848 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs @@ -66,16 +66,16 @@ public enum PowerLineStatus /// public static class SystemParameters { - public static event System.ComponentModel.PropertyChangedEventHandler StaticPropertyChanged; + public static event PropertyChangedEventHandler StaticPropertyChanged; - private static void OnPropertiesChanged(params string[] propertyNames) + private static void OnPropertiesChanged(params ReadOnlySpan propertyNames) { - System.ComponentModel.PropertyChangedEventHandler handler = StaticPropertyChanged; - if (handler != null) + PropertyChangedEventHandler handler = StaticPropertyChanged; + if (handler is not null) { - for (int i=0; i CurrentStoryboards } } - internal void StartNewThenStopOld(FrameworkElement element, params Storyboard[] newStoryboards) + internal void StartNewThenStopOld(FrameworkElement element, params ReadOnlySpan newStoryboards) { // Remove the old Storyboards. Remove is delayed until the next TimeManager tick, so the // handoff to the new storyboard is unaffected. - for (int index = 0; index < CurrentStoryboards.Count; ++index) + for (int i = 0; i < CurrentStoryboards.Count; i++) { - if (CurrentStoryboards[index] == null) - { + Storyboard currentStoryboard = CurrentStoryboards[i]; + if (currentStoryboard is null) continue; - } - CurrentStoryboards[index].Remove(element); + currentStoryboard.Remove(element); } + CurrentStoryboards.Clear(); // Start the new Storyboards - for (int index = 0; index < newStoryboards.Length; ++index) - { - if (newStoryboards[index] == null) - { + foreach (Storyboard newStoryboard in newStoryboards) + { + if (newStoryboard is null) continue; - } - newStoryboards[index].Begin(element, HandoffBehavior.SnapshotAndReplace, true); + newStoryboard.Begin(element, HandoffBehavior.SnapshotAndReplace, true); // Hold on to the running Storyboards - CurrentStoryboards.Add(newStoryboards[index]); + CurrentStoryboards.Add(newStoryboard); // Silverlight had an issue where initially, a checked CheckBox would not show the check mark // until the second frame. They chose to do a Seek(0) at this point, which this line // is supposed to mimic. It does not seem to be equivalent, though, and WPF ends up // with some odd animation behavior. I haven't seen the CheckBox issue on WPF, so // commenting this out for now. - // newStoryboards[index].SeekAlignedToLastTick(element, TimeSpan.Zero, TimeSeekOrigin.BeginTime); + // newStoryboard.SeekAlignedToLastTick(element, TimeSpan.Zero, TimeSeekOrigin.BeginTime); } } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs index 544f970f290..9c540424c4f 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs @@ -7038,7 +7038,7 @@ protected override void OnManipulationBoundaryFeedback(ManipulationBoundaryFeedb // If the original source is not from the same PresentationSource as of the Window, // then do not act on the PanningFeedback. - if (!PresentationSource.UnderSamePresentationSource(e.OriginalSource as DependencyObject, this)) + if (!PresentationSource.IsUnderSamePresentationSource(e.OriginalSource as DependencyObject, this)) { return; } diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/Trace.cs b/src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/Trace.cs index 42d76eaa8b5..8617acd4eaa 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/Trace.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/Trace.cs @@ -1,9 +1,7 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -// Description: Utility class for Trace switches and methods for XpsViewer. +using System; using System.Diagnostics; using System.Globalization; @@ -13,82 +11,68 @@ namespace MS.Internal.Documents.Application /// Utility class for Trace switches and methods for XpsViewer. /// internal static class Trace -{ - #region Internal Methods - //-------------------------------------------------------------------------- - // Internal Methods - //-------------------------------------------------------------------------- - - /// - /// Will permit only internet zone permissions for TraceListeners and is - /// safe to use inside of asserts for partial trust code. - /// - internal static void SafeWrite( - BooleanSwitch boolSwitch, string format, params object[] args) { - if (AvTrace.IsWpfTracingEnabledInRegistry()) + #region Internal Methods + //-------------------------------------------------------------------------- + // Internal Methods + //-------------------------------------------------------------------------- + + internal static void SafeWrite(BooleanSwitch boolSwitch, string format, params ReadOnlySpan args) { - System.Diagnostics.Trace.WriteLineIf( - boolSwitch.Enabled, - string.Format( - CultureInfo.CurrentCulture, - format, - args), - boolSwitch.DisplayName); + if (AvTrace.IsWpfTracingEnabledInRegistry()) + { + System.Diagnostics.Trace.WriteLineIf( + boolSwitch.Enabled, + string.Format( + CultureInfo.CurrentCulture, + format, + args), + boolSwitch.DisplayName); + } } - } - /// - /// Will permit only internet zone permissions for TraceListeners and is - /// safe to use inside of asserts for partial trust code. - /// - internal static void SafeWriteIf( - bool condition, - BooleanSwitch boolSwitch, - string format, - params object[] args) - { - if (AvTrace.IsWpfTracingEnabledInRegistry()) + internal static void SafeWriteIf( + bool condition, + BooleanSwitch boolSwitch, + string format, + params ReadOnlySpan args) { - System.Diagnostics.Trace.WriteLineIf( - boolSwitch.Enabled && condition, - string.Format( - CultureInfo.CurrentCulture, - format, - args), - boolSwitch.DisplayName); + if (AvTrace.IsWpfTracingEnabledInRegistry()) + { + System.Diagnostics.Trace.WriteLineIf( + boolSwitch.Enabled && condition, + string.Format( + CultureInfo.CurrentCulture, + format, + args), + boolSwitch.DisplayName); + } } - } - #endregion Internal Methods + #endregion Internal Methods - #region Internal Fields - //-------------------------------------------------------------------------- - // Internal Fields - //-------------------------------------------------------------------------- + #region Internal Fields + //-------------------------------------------------------------------------- + // Internal Fields + //-------------------------------------------------------------------------- - internal static BooleanSwitch File = new BooleanSwitch( - FileSwitchName, FileSwitchName, "1"); - internal static BooleanSwitch Packaging = new BooleanSwitch( - PackagingSwitchName, PackagingSwitchName, "1"); - internal static BooleanSwitch Presentation = new BooleanSwitch( - PresentationSwitchName, PresentationSwitchName, "1"); - internal static BooleanSwitch Rights = new BooleanSwitch( - RightsSwitchName, RightsSwitchName, "1"); - internal static BooleanSwitch Signatures = new BooleanSwitch( - SignaturesSwitchName, SignaturesSwitchName, "1"); - #endregion Internal Fields + internal static readonly BooleanSwitch File = new(FileSwitchName, FileSwitchName, "1"); + internal static readonly BooleanSwitch Packaging = new(PackagingSwitchName, PackagingSwitchName, "1"); + internal static readonly BooleanSwitch Presentation = new(PresentationSwitchName, PresentationSwitchName, "1"); + internal static readonly BooleanSwitch Rights = new(RightsSwitchName, RightsSwitchName, "1"); + internal static readonly BooleanSwitch Signatures = new(SignaturesSwitchName, SignaturesSwitchName, "1"); + #endregion Internal Fields - #region Private Fields - //-------------------------------------------------------------------------- - // Private Fields - //-------------------------------------------------------------------------- + #region Private Fields + //-------------------------------------------------------------------------- + // Private Fields + //-------------------------------------------------------------------------- - private const string FileSwitchName = "XpsViewerFile"; - private const string PackagingSwitchName = "XpsViewerPackaging"; - private const string PresentationSwitchName = "XpsViewerUI"; - private const string RightsSwitchName = "XpsViewerRights"; - private const string SignaturesSwitchName = "XpsViewerSignatures"; - #endregion Private Fields -} + private const string FileSwitchName = "XpsViewerFile"; + private const string PackagingSwitchName = "XpsViewerPackaging"; + private const string PresentationSwitchName = "XpsViewerUI"; + private const string RightsSwitchName = "XpsViewerRights"; + private const string SignaturesSwitchName = "XpsViewerSignatures"; + #endregion Private Fields + } } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Windows.Input.Manipulations/System/Windows/Input/Manipulations/Exceptions.cs b/src/Microsoft.DotNet.Wpf/src/System.Windows.Input.Manipulations/System/Windows/Input/Manipulations/Exceptions.cs index 1e5ce8c2cc9..77fea3767a5 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Windows.Input.Manipulations/System/Windows/Input/Manipulations/Exceptions.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Windows.Input.Manipulations/System/Windows/Input/Manipulations/Exceptions.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Globalization; namespace System.Windows.Input.Manipulations { @@ -233,13 +234,10 @@ private static bool IsPropertyName(string paramName) /// /// /// - private static string Format(string format, params object[] args) + private static string Format(string format, params ReadOnlySpan args) { Debug.Assert(format != null); - return string.Format( - System.Globalization.CultureInfo.CurrentCulture, - format, - args); + return string.Format(CultureInfo.CurrentCulture, format, args); } } }