From 7d30480b38900b5998446f3558a9ec6754a7be7e Mon Sep 17 00:00:00 2001 From: Ken Tucker Date: Wed, 18 Dec 2024 16:39:30 -0500 Subject: [PATCH 1/4] Refactor hex color parsing and update error messages - Remove extraneous character from copyright comment. - Improve null `visualElementNode` exception message. - Simplify hex color parsing logic: - Strip `#` earlier in the process. - Parse `a`, `r`, `g`, and `b` more concisely. - Remove redundant initialization of `a`, `r`, `g`, and `b`. - Simplify `startPosition` calculation with ternary operator. - Streamline parsing of `r`, `g`, and `b` values. --- .../Platforms/uap/AppManifestHelper.cs | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs b/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs index 4e4003a1b..4837cb61e 100644 --- a/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs +++ b/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2012 Tim Heuer // // Licensed under the Microsoft Public License (Ms-PL) (the "License"); @@ -49,8 +49,8 @@ public async static Task GetManifestVisualElementsAsync() SmallLogoUri = new Uri(string.Format("ms-appx:///{0}", vel.Attribute("Square30x30Logo").Value.Replace(@"\", @"/"))), BackgroundColorAsString = vel.Attribute("BackgroundColor").Value }).FirstOrDefault(); - - if (visualElementNode == null) + + if (visualElementNode == null) throw new ArgumentNullException("Could not parse the VisualElements from the app manifest."); return visualElementNode; @@ -79,7 +79,7 @@ private static Windows.UI.Color ToColor(string hexValue) // in order to prevent parsing failures if (String.Equals(hexValue, "transparent", StringComparison.OrdinalIgnoreCase)) return Windows.UI.Colors.Transparent; - + hexValue = hexValue.Replace("#", string.Empty); // some loose validation (not bullet-proof) @@ -88,23 +88,14 @@ private static Windows.UI.Color ToColor(string hexValue) throw new ArgumentException("This does not appear to be a proper hex color number"); } - byte a = 255; - byte r = 255; - byte g = 255; - byte b = 255; + byte a = (hexValue.Length == 8) ? byte.Parse(hexValue.Substring(0, 2), NumberStyles.HexNumber) : (byte)255; - int startPosition = 0; - // the case where alpha is provided - if (hexValue.Length == 8) - { - a = byte.Parse(hexValue.Substring(0, 2), NumberStyles.HexNumber); - startPosition = 2; - } + int startPosition = (hexValue.Length == 8) ? 2 : 0; - r = byte.Parse(hexValue.Substring(startPosition, 2), NumberStyles.HexNumber); - g = byte.Parse(hexValue.Substring(startPosition + 2, 2), NumberStyles.HexNumber); - b = byte.Parse(hexValue.Substring(startPosition + 4, 2), NumberStyles.HexNumber); + byte r = byte.Parse(hexValue.Substring(startPosition, 2), NumberStyles.HexNumber); + byte g = byte.Parse(hexValue.Substring(startPosition + 2, 2), NumberStyles.HexNumber); + byte b = byte.Parse(hexValue.Substring(startPosition + 4, 2), NumberStyles.HexNumber); return Windows.UI.Color.FromArgb(a, r, g, b); } From e811191c99ca2bb5e3c57fcd7bf231497cebd003 Mon Sep 17 00:00:00 2001 From: Ken Tucker Date: Wed, 18 Dec 2024 16:43:41 -0500 Subject: [PATCH 2/4] Remove unused variable `keys` The variable `keys` was assigned the value of `this.Keys` but was not used anywhere in the code. This change removes the unnecessary line, simplifying the code without altering its functionality. --- .../Platforms/Xamarin.Forms/HttpUtility.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Caliburn.Micro.Platform/Platforms/Xamarin.Forms/HttpUtility.cs b/src/Caliburn.Micro.Platform/Platforms/Xamarin.Forms/HttpUtility.cs index 80e6c430f..8fd3710f4 100644 --- a/src/Caliburn.Micro.Platform/Platforms/Xamarin.Forms/HttpUtility.cs +++ b/src/Caliburn.Micro.Platform/Platforms/Xamarin.Forms/HttpUtility.cs @@ -44,7 +44,6 @@ public override string ToString() if (count == 0) return ""; StringBuilder sb = new StringBuilder(); - var keys = this.Keys; foreach (var key in this.Keys) { sb.AppendFormat("{0}={1}&", key, this[key]); From 8d62fc48c546888522085dcc9cd7f72e8110c105 Mon Sep 17 00:00:00 2001 From: Ken Tucker Date: Wed, 18 Dec 2024 16:49:04 -0500 Subject: [PATCH 3/4] Refactor and reformat Maui and Xaml platform providers Refactored `BeginOnUIThread` in `MauiPlatformProvider.cs` to discard the result of `dispatcher.RunAsync` using the discard operator `_`. Reformatted `XamlPlatformProvider.cs` to place opening braces `{` on new lines for namespace, class, constructor, and several methods. Updated `BeginOnUIThread` to use the discard operator `_`. Improved indentation and formatting in various methods for better readability. --- .../Maui/Windows/MauiPlatformProvider.cs | 2 +- .../XamlPlatformProvider.cs | 67 ++++++++++++------- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/Caliburn.Micro.Platform/Platforms/Maui/Windows/MauiPlatformProvider.cs b/src/Caliburn.Micro.Platform/Platforms/Maui/Windows/MauiPlatformProvider.cs index 62a77b4b6..d816bf034 100644 --- a/src/Caliburn.Micro.Platform/Platforms/Maui/Windows/MauiPlatformProvider.cs +++ b/src/Caliburn.Micro.Platform/Platforms/Maui/Windows/MauiPlatformProvider.cs @@ -54,7 +54,7 @@ private bool CheckAccess() public virtual void BeginOnUIThread(System.Action action) { ValidateDispatcher(); - var dummy = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()); + _ = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()); } /// diff --git a/src/Caliburn.Micro.Platform/XamlPlatformProvider.cs b/src/Caliburn.Micro.Platform/XamlPlatformProvider.cs index e4892ed13..fef62caeb 100644 --- a/src/Caliburn.Micro.Platform/XamlPlatformProvider.cs +++ b/src/Caliburn.Micro.Platform/XamlPlatformProvider.cs @@ -1,4 +1,5 @@ -namespace Caliburn.Micro { +namespace Caliburn.Micro +{ using System; using System.Collections.Generic; using System.Threading; @@ -19,7 +20,8 @@ /// /// A implementation for the XAML platfrom. /// - public class XamlPlatformProvider : IPlatformProvider { + public class XamlPlatformProvider : IPlatformProvider + { #if WINDOWS_UWP private readonly CoreDispatcher dispatcher; #else @@ -29,7 +31,8 @@ public class XamlPlatformProvider : IPlatformProvider { /// /// Initializes a new instance of the class. /// - public XamlPlatformProvider() { + public XamlPlatformProvider() + { #if WINDOWS_UWP dispatcher = Window.Current.Dispatcher; #elif AVALONIA @@ -47,16 +50,19 @@ public XamlPlatformProvider() { /// /// Indicates whether or not the framework is in design-time mode. /// - public virtual bool InDesignMode { + public virtual bool InDesignMode + { get { return View.InDesignMode; } } - private void ValidateDispatcher() { + private void ValidateDispatcher() + { if (dispatcher == null) throw new InvalidOperationException("Not initialized with dispatcher."); } - private bool CheckAccess() { + private bool CheckAccess() + { #if WINDOWS_UWP return dispatcher == null || Window.Current != null; #else @@ -68,10 +74,11 @@ private bool CheckAccess() { /// Executes the action on the UI thread asynchronously. /// /// The action to execute. - public virtual void BeginOnUIThread(System.Action action) { + public virtual void BeginOnUIThread(System.Action action) + { ValidateDispatcher(); #if WINDOWS_UWP - var dummy = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()); + _ = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()); #elif AVALONIA dispatcher.Post(action); #else @@ -84,7 +91,8 @@ public virtual void BeginOnUIThread(System.Action action) { /// /// The action to execute. /// - public virtual Task OnUIThreadAsync(Func action) { + public virtual Task OnUIThreadAsync(Func action) + { ValidateDispatcher(); #if WINDOWS_UWP return dispatcher.RunTaskAsync(action); @@ -100,19 +108,24 @@ public virtual Task OnUIThreadAsync(Func action) { /// /// The action to execute. /// - public virtual void OnUIThread(System.Action action) { + public virtual void OnUIThread(System.Action action) + { if (CheckAccess()) action(); - else { + else + { #if WINDOWS_UWP dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()).AsTask().Wait(); #else Exception exception = null; - System.Action method = () => { - try { + System.Action method = () => + { + try + { action(); } - catch(Exception ex) { + catch (Exception ex) + { exception = ex; } }; @@ -120,7 +133,7 @@ public virtual void OnUIThread(System.Action action) { dispatcher.Invoke(method); if (exception != null) - throw new System.Reflection.TargetInvocationException("An error occurred while dispatching a call to the UI Thread", exception); + throw new System.Reflection.TargetInvocationException("An error occurred while dispatching a call to the UI Thread", exception); #endif } } @@ -138,7 +151,8 @@ public virtual void OnUIThread(System.Action action) { /// The WindowManager marks that element as a framework-created element so that it can determine what it created vs. what was intended by the developer. /// Calling GetFirstNonGeneratedView allows the framework to discover what the original element was. /// - public virtual object GetFirstNonGeneratedView(object view) { + public virtual object GetFirstNonGeneratedView(object view) + { return View.GetFirstNonGeneratedView(view); } @@ -158,9 +172,11 @@ public virtual object GetFirstNonGeneratedView(object view) { /// /// The view. /// The handler. - public virtual void ExecuteOnFirstLoad(object view, Action handler) { + public virtual void ExecuteOnFirstLoad(object view, Action handler) + { var element = view as FrameworkElement; - if (element != null && !(bool) element.GetValue(PreviouslyAttachedProperty)) { + if (element != null && !(bool)element.GetValue(PreviouslyAttachedProperty)) + { element.SetValue(PreviouslyAttachedProperty, true); View.ExecuteOnLoad(element, (s, e) => handler(s)); } @@ -171,9 +187,11 @@ public virtual void ExecuteOnFirstLoad(object view, Action handler) { /// /// The view. /// The handler. - public virtual void ExecuteOnLayoutUpdated(object view, Action handler) { + public virtual void ExecuteOnLayoutUpdated(object view, Action handler) + { var element = view as FrameworkElement; - if (element != null) { + if (element != null) + { View.ExecuteOnLayoutUpdated(element, (s, e) => handler(s)); } } @@ -190,7 +208,8 @@ public virtual void ExecuteOnLayoutUpdated(object view, Action handler) /// public virtual Func GetViewCloseAction(object viewModel, ICollection views, bool? dialogResult) { - foreach (var contextualView in views) { + foreach (var contextualView in views) + { var viewType = contextualView.GetType(); #if WINDOWS_UWP var closeMethod = viewType.GetRuntimeMethod("Close", new Type[0]); @@ -200,7 +219,8 @@ public virtual Func GetViewCloseAction(object viewModel var closeMethod = viewType.GetMethod("Close", new Type[0]); #endif if (closeMethod != null) - return ct => { + return ct => + { #if AVALONIA if (dialogResult != null) { @@ -234,7 +254,8 @@ public virtual Func GetViewCloseAction(object viewModel #else var isOpenProperty = viewType.GetProperty("IsOpen"); #endif - if (isOpenProperty != null) { + if (isOpenProperty != null) + { return ct => { isOpenProperty.SetValue(contextualView, false, null); From 28d479efa598a1d4c2e351c5252247d0c257ff57 Mon Sep 17 00:00:00 2001 From: Ken Tucker Date: Wed, 18 Dec 2024 17:55:20 -0500 Subject: [PATCH 4/4] copilot suggestion --- .../Platforms/uap/AppManifestHelper.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs b/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs index 4837cb61e..7c07982da 100644 --- a/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs +++ b/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs @@ -1,5 +1,4 @@ -// -// Copyright (c) 2012 Tim Heuer +// Copyright (c) 2012 Tim Heuer // // Licensed under the Microsoft Public License (Ms-PL) (the "License"); // you may not use this file except in compliance with the License. @@ -12,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -// using System; using System.Globalization;