diff --git a/src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs b/src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs index 67210168f5..17df503a65 100644 --- a/src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs +++ b/src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs @@ -230,53 +230,47 @@ protected virtual void Dispose(bool disposing) } } - static Command CreateCaptureImageCommand(BindableObject bindable) + Command CreateCaptureImageCommand() { - var cameraView = (CameraView)bindable; - return new(async token => await cameraView.CaptureImage(token).ConfigureAwait(false)); + return new(async token => await CaptureImage(token).ConfigureAwait(false)); } - static Command CreateStartCameraPreviewCommand(BindableObject bindable) + Command CreateStartCameraPreviewCommand() { - var cameraView = (CameraView)bindable; - return new(async token => await cameraView.StartCameraPreview(token).ConfigureAwait(false)); + return new(async token => await StartCameraPreview(token).ConfigureAwait(false)); } - static Command CreateStopCameraPreviewCommand(BindableObject bindable) + Command CreateStopCameraPreviewCommand() { - var cameraView = (CameraView)bindable; - return new(_ => cameraView.StopCameraPreview()); + return new(_ => StopCameraPreview()); } - static Command CreateStartVideoRecordingCommand(BindableObject bindable) + Command CreateStartVideoRecordingCommand() { - var cameraView = (CameraView)bindable; - return new(async stream => await cameraView.StartVideoRecording(stream).ConfigureAwait(false)); + return new(async stream => await StartVideoRecording(stream).ConfigureAwait(false)); } - static Command CreateStopVideoRecordingCommand(BindableObject bindable) + Command CreateStopVideoRecordingCommand() { - var cameraView = (CameraView)bindable; - return new(async token => await cameraView.StopVideoRecording(token).ConfigureAwait(false)); + return new(async token => await StopVideoRecording(token).ConfigureAwait(false)); } - static object CoerceZoom(BindableObject bindable, object value) + object CoerceZoom(object value) { - var cameraView = (CameraView)bindable; var input = (float)value; - if (cameraView.SelectedCamera is null) + if (SelectedCamera is null) { return input; } - if (input < cameraView.SelectedCamera.MinimumZoomFactor) + if (input < SelectedCamera.MinimumZoomFactor) { - input = cameraView.SelectedCamera.MinimumZoomFactor; + input = SelectedCamera.MinimumZoomFactor; } - else if (input > cameraView.SelectedCamera.MaximumZoomFactor) + else if (input > SelectedCamera.MaximumZoomFactor) { - input = cameraView.SelectedCamera.MaximumZoomFactor; + input = SelectedCamera.MaximumZoomFactor; } return input; diff --git a/src/CommunityToolkit.Maui.MediaElement/MediaElement.shared.cs b/src/CommunityToolkit.Maui.MediaElement/MediaElement.shared.cs index 535154f893..8c40b9d2f0 100644 --- a/src/CommunityToolkit.Maui.MediaElement/MediaElement.shared.cs +++ b/src/CommunityToolkit.Maui.MediaElement/MediaElement.shared.cs @@ -338,41 +338,38 @@ protected virtual void Dispose(bool disposing) isDisposed = true; } - static void OnSourcePropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnSourcePropertyChanged(object oldValue, object newValue) { - var mediaElement = (MediaElement)bindable; var source = (MediaSource?)newValue; - mediaElement.ClearTimer(); + ClearTimer(); if (source is not null) { - source.SourceChanged += mediaElement.OnSourceChanged; - SetInheritedBindingContext(source, mediaElement.BindingContext); + source.SourceChanged += OnSourceChanged; + SetInheritedBindingContext(source, BindingContext); } - mediaElement.InvalidateMeasure(); - mediaElement.InitializeTimer(); + InvalidateMeasure(); + InitializeTimer(); } - static void OnSourcePropertyChanging(BindableObject bindable, object oldValue, object newValue) + void OnSourcePropertyChanging(object oldValue, object newValue) { - var mediaElement = (MediaElement)bindable; var oldMediaSource = (MediaSource?)oldValue; - oldMediaSource?.SourceChanged -= mediaElement.OnSourceChanged; + oldMediaSource?.SourceChanged -= OnSourceChanged; } - static void OnCurrentStatePropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnCurrentStatePropertyChanged(object oldValue, object newValue) { - var mediaElement = (MediaElement)bindable; var previousState = (MediaElementState)oldValue; var newState = (MediaElementState)newValue; - mediaElement.OnStateChanged(new MediaStateChangedEventArgs(previousState, newState)); + OnStateChanged(new MediaStateChangedEventArgs(previousState, newState)); } - static void OnVolumeChanging(BindableObject bindable, object oldValue, object newValue) + void OnVolumeChanging(object oldValue, object newValue) { var updatedVolume = (double)newValue; diff --git a/src/CommunityToolkit.Maui.MediaElement/MediaSource/FileMediaSource.shared.cs b/src/CommunityToolkit.Maui.MediaElement/MediaSource/FileMediaSource.shared.cs index 525311ee41..d1d8c4aec6 100644 --- a/src/CommunityToolkit.Maui.MediaElement/MediaSource/FileMediaSource.shared.cs +++ b/src/CommunityToolkit.Maui.MediaElement/MediaSource/FileMediaSource.shared.cs @@ -32,6 +32,6 @@ public sealed partial class FileMediaSource : MediaSource /// public override string ToString() => $"File: {Path}"; - static void OnFileMediaSourceChanged(BindableObject bindable, object oldValue, object newValue) => - ((FileMediaSource)bindable).OnSourceChanged(); + void OnFileMediaSourceChanged(object oldValue, object newValue) => + OnSourceChanged(); } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.MediaElement/MediaSource/ResourceMediaSource.shared.cs b/src/CommunityToolkit.Maui.MediaElement/MediaSource/ResourceMediaSource.shared.cs index 067dcf5e36..7ddeb90f9e 100644 --- a/src/CommunityToolkit.Maui.MediaElement/MediaSource/ResourceMediaSource.shared.cs +++ b/src/CommunityToolkit.Maui.MediaElement/MediaSource/ResourceMediaSource.shared.cs @@ -36,6 +36,6 @@ public sealed partial class ResourceMediaSource : MediaSource /// public override string ToString() => $"Resource: {Path}"; - static void OnResourceMediaSourceMediaSourceChanged(BindableObject bindable, object oldValue, object newValue) => - ((ResourceMediaSource)bindable).OnSourceChanged(); + void OnResourceMediaSourceMediaSourceChanged(object oldValue, object newValue) => + OnSourceChanged(); } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSource.shared.cs b/src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSource.shared.cs index f1e8b3a765..739002e5e8 100644 --- a/src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSource.shared.cs +++ b/src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSource.shared.cs @@ -33,9 +33,9 @@ public sealed partial class UriMediaSource : MediaSource /// public override string ToString() => $"Uri: {Uri}"; - static bool UriValueValidator(BindableObject bindable, object value) => + bool UriValueValidator(object value) => value is null || ((Uri)value).IsAbsoluteUri; - static void OnUriSourceChanged(BindableObject bindable, object oldValue, object newValue) => - ((UriMediaSource)bindable).OnSourceChanged(); + void OnUriSourceChanged(object oldValue, object newValue) => + OnSourceChanged(); } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/CommonUsageTests.cs b/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/CommonUsageTests.cs index 6edfee7d07..73bb3939b7 100644 --- a/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/CommonUsageTests.cs +++ b/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/CommonUsageTests.cs @@ -200,11 +200,11 @@ public partial class {{defaultTestClassName}} : View DefaultValueCreatorMethodName = "CreateDefaultValue")] public partial int Value { get; set; } = 42; - static bool ValidateValue(BindableObject bindable, object value) => true; - static void OnPropertyChanged(BindableObject bindable, object oldValue, object newValue) { } - static void OnPropertyChanging(BindableObject bindable, object oldValue, object newValue) { } - static object CoerceValue(BindableObject bindable, object value) => value; - static object CreateDefaultValue(BindableObject bindable) => 0; + bool ValidateValue(object value) => true; + void OnPropertyChanged(object oldValue, object newValue) { } + void OnPropertyChanging(object oldValue, object newValue) { } + object CoerceValue(object value) => value; + object CreateDefaultValue() => 0; } """; @@ -222,7 +222,7 @@ public partial class {{defaultTestClassName}} /// /// Backing BindableProperty for the property. /// - public static readonly global::Microsoft.Maui.Controls.BindableProperty ValueProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Value", typeof(int), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, (Microsoft.Maui.Controls.BindingMode)1, ValidateValue, OnPropertyChanged, OnPropertyChanging, CoerceValue, CreateDefaultValue); + public static readonly global::Microsoft.Maui.Controls.BindableProperty ValueProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Value", typeof(int), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, (Microsoft.Maui.Controls.BindingMode)1, (b, v) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).ValidateValue(v), (b, o, n) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).OnPropertyChanged(o, n), (b, o, n) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).OnPropertyChanging(o, n), (b, v) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).CoerceValue(v), (b) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).CreateDefaultValue()); public partial int Value { get => false ? field : (int)GetValue(ValueProperty); set => SetValue(ValueProperty, value); } } """; diff --git a/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/EdgeCaseTests.cs b/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/EdgeCaseTests.cs index 5883995f53..528f56d11a 100644 --- a/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/EdgeCaseTests.cs +++ b/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/EdgeCaseTests.cs @@ -602,7 +602,7 @@ public partial class {{defaultTestClassName}} : View [BindablePropertyAttribute(DefaultValueCreatorMethodName = nameof(CreateDefaultText))] public partial string Text { get; set; } = "Initial Value"; - static string CreateDefaultText(BindableObject bindable) + string CreateDefaultText() { return "Initial Value"; } @@ -623,7 +623,7 @@ public partial class {{defaultTestClassName}} /// /// Backing BindableProperty for the property. /// - public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, CreateDefaultText); + public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, (b) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).CreateDefaultText()); public partial string Text { get => false ? field : (string)GetValue(TextProperty); set => SetValue(TextProperty, value); } } """; diff --git a/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyModelTests.cs b/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyModelTests.cs index c1df3de90c..34ce0bcd65 100644 --- a/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyModelTests.cs +++ b/src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyModelTests.cs @@ -87,7 +87,12 @@ public void BindablePropertyModel_WithAllParameters_StoresCorrectValues() Assert.Equal(newKeywordText, model.NewKeywordText); Assert.Equal(hasInitializer, model.HasInitializer); Assert.Equal("TestPropertyProperty", model.BindablePropertyName); - Assert.Equal(defaultValueCreatorMethodName, model.EffectiveDefaultValueCreatorMethodName); + Assert.Equal(model.DefaultValueCreatorMethodName, model.EffectiveDefaultValueCreatorMethodName); + Assert.Equal($"(b,v) => ((TestClass)b).{model.ValidateValueMethodName}(v)", model.ValidateValueMethodExpression); + Assert.Equal($"(b,o,n) => ((TestClass)b).{model.PropertyChangedMethodName}(o,n)", model.PropertyChangedMethodExpression); + Assert.Equal($"(b,o,n) => ((TestClass)b).{model.PropertyChangingMethodName}(o,n)", model.PropertyChangingMethodExpression); + Assert.Equal($"(b,v) => ((TestClass)b).{model.CoerceValueMethodName}(v)", model.CoerceValueMethodExpression); + Assert.Equal($"(b) => ((TestClass)b).{defaultValueCreatorMethodName}()", model.DefaultValueCreatorMethodExpression); Assert.Equal("IsInitializingTestProperty", model.InitializingPropertyName); } diff --git a/src/CommunityToolkit.Maui.SourceGenerators.Internal/Generators/BindablePropertyAttributeSourceGenerator.cs b/src/CommunityToolkit.Maui.SourceGenerators.Internal/Generators/BindablePropertyAttributeSourceGenerator.cs index 85fc81820d..4f78d6cf5b 100644 --- a/src/CommunityToolkit.Maui.SourceGenerators.Internal/Generators/BindablePropertyAttributeSourceGenerator.cs +++ b/src/CommunityToolkit.Maui.SourceGenerators.Internal/Generators/BindablePropertyAttributeSourceGenerator.cs @@ -260,13 +260,13 @@ static void GenerateReadOnlyBindableProperty(StringBuilder sb, in BindableProper .Append("), null, ") .Append(info.DefaultBindingMode) .Append(", ") - .Append(info.ValidateValueMethodName) + .Append(info.ValidateValueMethodExpression) .Append(", ") - .Append(info.PropertyChangedMethodName) + .Append(info.PropertyChangedMethodExpression) .Append(", ") - .Append(info.PropertyChangingMethodName) + .Append(info.PropertyChangingMethodExpression) .Append(", ") - .Append(info.CoerceValueMethodName) + .Append(info.CoerceValueMethodExpression) .Append(", "); if (info.ShouldUsePropertyInitializer) @@ -275,7 +275,7 @@ static void GenerateReadOnlyBindableProperty(StringBuilder sb, in BindableProper .Append('.'); } - sb.Append(info.EffectiveDefaultValueCreatorMethodName) + sb.Append(info.DefaultValueCreatorMethodExpression) .Append(");\n"); // Generate public BindableProperty from the key @@ -321,13 +321,13 @@ static void GenerateBindableProperty(StringBuilder sb, in BindablePropertyModel .Append("), null, ") .Append(info.DefaultBindingMode) .Append(", ") - .Append(info.ValidateValueMethodName) + .Append(info.ValidateValueMethodExpression) .Append(", ") - .Append(info.PropertyChangedMethodName) + .Append(info.PropertyChangedMethodExpression) .Append(", ") - .Append(info.PropertyChangingMethodName) + .Append(info.PropertyChangingMethodExpression) .Append(", ") - .Append(info.CoerceValueMethodName) + .Append(info.CoerceValueMethodExpression) .Append(", "); if (info.ShouldUsePropertyInitializer) @@ -336,7 +336,7 @@ static void GenerateBindableProperty(StringBuilder sb, in BindablePropertyModel .Append('.'); } - sb.Append(info.EffectiveDefaultValueCreatorMethodName) + sb.Append(info.DefaultValueCreatorMethodExpression) .Append(");\n") .Append('\n'); } diff --git a/src/CommunityToolkit.Maui.SourceGenerators.Internal/Models/Records.cs b/src/CommunityToolkit.Maui.SourceGenerators.Internal/Models/Records.cs index 48aca49402..367253116d 100644 --- a/src/CommunityToolkit.Maui.SourceGenerators.Internal/Models/Records.cs +++ b/src/CommunityToolkit.Maui.SourceGenerators.Internal/Models/Records.cs @@ -10,6 +10,14 @@ public record BindablePropertyModel(string PropertyName, ITypeSymbol ReturnType, public string BindablePropertyName => $"{PropertyName}Property"; public string BindablePropertyKeyName => $"{char.ToLower(PropertyName[0])}{PropertyName[1..]}PropertyKey"; public string EffectiveDefaultValueCreatorMethodName => ShouldUsePropertyInitializer ? $"CreateDefault{PropertyName}" : DefaultValueCreatorMethodName; + public string ValidateValueMethodExpression => ValidateValueMethodName is "null" ? ValidateValueMethodName : $"(b,v) => (({DeclaringType})b).{ValidateValueMethodName}(v)"; + public string PropertyChangedMethodExpression => PropertyChangedMethodName is "null" ? PropertyChangedMethodName : $"(b,o,n) => (({DeclaringType})b).{PropertyChangedMethodName}(o,n)"; + public string PropertyChangingMethodExpression => PropertyChangingMethodName is "null" ? PropertyChangingMethodName : $"(b,o,n) => (({DeclaringType})b).{PropertyChangingMethodName}(o,n)"; + public string CoerceValueMethodExpression => CoerceValueMethodName is "null" ? CoerceValueMethodName : $"(b,v) => (({DeclaringType})b).{CoerceValueMethodName}(v)"; + public string DefaultValueCreatorMethodExpression => ShouldUsePropertyInitializer + ? EffectiveDefaultValueCreatorMethodName + : (DefaultValueCreatorMethodName is "null" ? DefaultValueCreatorMethodName : $"(b) => (({DeclaringType})b).{DefaultValueCreatorMethodName}()"); + public string InitializingPropertyName => $"IsInitializing{PropertyName}"; } diff --git a/src/CommunityToolkit.Maui/Behaviors/AnimationBehavior.shared.cs b/src/CommunityToolkit.Maui/Behaviors/AnimationBehavior.shared.cs index cf436199da..d60af80a5d 100644 --- a/src/CommunityToolkit.Maui/Behaviors/AnimationBehavior.shared.cs +++ b/src/CommunityToolkit.Maui/Behaviors/AnimationBehavior.shared.cs @@ -82,20 +82,15 @@ protected override async void OnTriggerHandled(object? sender = null, object? ev base.OnTriggerHandled(sender, eventArgs); } - static void OnAnimateOnTapPropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnAnimateOnTapPropertyChanged(object oldValue, object newValue) { - if (bindable is not AnimationBehavior behavior) - { - return; - } - if ((bool)newValue) { - behavior.AddTapGestureRecognizer(); + AddTapGestureRecognizer(); } else { - behavior.RemoveTapGestureRecognizer(); + RemoveTapGestureRecognizer(); } } @@ -128,18 +123,16 @@ void RemoveTapGestureRecognizer() tapGestureRecognizer = null; } - static Command CreateAnimateCommand(BindableObject bindable) + Command CreateAnimateCommand() { - var animationBehavior = (AnimationBehavior)bindable; - return new Command(async token => await animationBehavior.OnAnimate(token).ConfigureAwait(false)); + return new Command(async token => await OnAnimate(token).ConfigureAwait(false)); } - static void OnAnimateCommandChanging(BindableObject bindable, object oldValue, object newValue) + void OnAnimateCommandChanging(object oldValue, object newValue) { if (newValue is not Command) { - var animationBehavior = (AnimationBehavior)bindable; - throw new InvalidOperationException($"{nameof(AnimateCommand)} must of Type {animationBehavior.AnimateCommand.GetType().FullName}"); + throw new InvalidOperationException($"{nameof(AnimateCommand)} must be of Type {AnimateCommand.GetType().FullName}"); } } diff --git a/src/CommunityToolkit.Maui/Behaviors/EventToCommandBehavior.shared.cs b/src/CommunityToolkit.Maui/Behaviors/EventToCommandBehavior.shared.cs index ca57df8704..a8ed7e74fc 100644 --- a/src/CommunityToolkit.Maui/Behaviors/EventToCommandBehavior.shared.cs +++ b/src/CommunityToolkit.Maui/Behaviors/EventToCommandBehavior.shared.cs @@ -55,8 +55,8 @@ protected override void OnDetachingFrom(VisualElement bindable) base.OnDetachingFrom(bindable); } - static void OnEventNamePropertyChanged(BindableObject bindable, object oldValue, object newValue) - => ((EventToCommandBehavior)bindable).RegisterEvent(); + void OnEventNamePropertyChanged(object oldValue, object newValue) + => RegisterEvent(); void RegisterEvent() { diff --git a/src/CommunityToolkit.Maui/Behaviors/MaskedBehavior.shared.cs b/src/CommunityToolkit.Maui/Behaviors/MaskedBehavior.shared.cs index d2e7b33002..6793006e89 100644 --- a/src/CommunityToolkit.Maui/Behaviors/MaskedBehavior.shared.cs +++ b/src/CommunityToolkit.Maui/Behaviors/MaskedBehavior.shared.cs @@ -69,20 +69,18 @@ protected override async void OnViewPropertyChanged(InputView sender, PropertyCh } } - static object UnmaskedCharacterValueCreator(BindableObject bindable) => 'X'; + object UnmaskedCharacterValueCreator() => 'X'; - static void OnMaskPropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnMaskPropertyChanged(object oldValue, object newValue) { var mask = (string?)newValue; - var maskedBehavior = (MaskedBehavior)bindable; - maskedBehavior.SetMaskPositions(mask); + SetMaskPositions(mask); } - static async void OnUnmaskedCharacterPropertyChanged(BindableObject bindable, object oldValue, object newValue) + async void OnUnmaskedCharacterPropertyChanged(object oldValue, object newValue) { - var maskedBehavior = (MaskedBehavior)bindable; - await maskedBehavior.OnMaskChanged(maskedBehavior.Mask, CancellationToken.None).ConfigureAwait(false); + await OnMaskChanged(Mask, CancellationToken.None).ConfigureAwait(false); } Task OnTextPropertyChanged(CancellationToken token) diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/ImageTouch/ImageTouchBehavior.shared.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/ImageTouch/ImageTouchBehavior.shared.cs index fbbf37ed52..866054584b 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/ImageTouch/ImageTouchBehavior.shared.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/ImageTouch/ImageTouchBehavior.shared.cs @@ -49,38 +49,36 @@ public partial class ImageTouchBehavior : TouchBehavior [BindableProperty] public partial bool ShouldSetImageOnAnimationEnd { get; set; } = ImageTouchBehaviorDefaults.ShouldSetImageOnAnimationEnd; - static void HandleDefaultImageSourceChanged(BindableObject bindable, object? oldValue, object? newValue) + void HandleDefaultImageSourceChanged(object? oldValue, object? newValue) { - var imageTouchBehavior = (ImageTouchBehavior)bindable; var updatedImageSource = (ImageSource?)newValue; - if (!GestureManager.TryGetBindableImageTouchBehaviorElement(imageTouchBehavior, out var imageElement)) + if (!GestureManager.TryGetBindableImageTouchBehaviorElement(this, out var imageElement)) { return; } // GestureManager does not automatically toggle ImageElement.SourceProperty when currently being displayed - switch (imageTouchBehavior.CurrentTouchState, imageTouchBehavior.CurrentHoverState) + switch (CurrentTouchState, CurrentHoverState) { - case (TouchState.Default, HoverState.Hovered) when imageTouchBehavior.HoveredImageSource is null: + case (TouchState.Default, HoverState.Hovered) when HoveredImageSource is null: case (TouchState.Default, HoverState.Default): imageElement.SetValue(ImageElement.SourceProperty, updatedImageSource); break; } } - static void HandlePressedImageSourceChanged(BindableObject bindable, object? oldValue, object? newValue) + void HandlePressedImageSourceChanged(object? oldValue, object? newValue) { - var imageTouchBehavior = (ImageTouchBehavior)bindable; var updatedImageSource = (ImageSource?)newValue; - if (!GestureManager.TryGetBindableImageTouchBehaviorElement(imageTouchBehavior, out var imageElement)) + if (!GestureManager.TryGetBindableImageTouchBehaviorElement(this, out var imageElement)) { return; } // GestureManager does not automatically toggle ImageElement.SourceProperty when currently being displayed - switch (imageTouchBehavior.CurrentTouchState, imageTouchBehavior.CurrentHoverState) + switch (CurrentTouchState, CurrentHoverState) { case (TouchState.Pressed, _): imageElement.SetValue(ImageElement.SourceProperty, updatedImageSource); @@ -88,18 +86,17 @@ static void HandlePressedImageSourceChanged(BindableObject bindable, object? old } } - static void HandleHoveredImageSourceChanged(BindableObject bindable, object? oldValue, object? newValue) + void HandleHoveredImageSourceChanged(object? oldValue, object? newValue) { - var imageTouchBehavior = (ImageTouchBehavior)bindable; var updatedImageSource = (ImageSource?)newValue; - if (!GestureManager.TryGetBindableImageTouchBehaviorElement(imageTouchBehavior, out var imageElement)) + if (!GestureManager.TryGetBindableImageTouchBehaviorElement(this, out var imageElement)) { return; } // GestureManager does not automatically toggle ImageElement.SourceProperty when currently being displayed - switch (imageTouchBehavior.CurrentTouchState, imageTouchBehavior.CurrentHoverState) + switch (CurrentTouchState, CurrentHoverState) { case (TouchState.Default, HoverState.Hovered): imageElement.SetValue(ImageElement.SourceProperty, updatedImageSource); diff --git a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/Touch/TouchBehavior.methods.shared.cs b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/Touch/TouchBehavior.methods.shared.cs index 711e18e5d5..8906cc3f01 100644 --- a/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/Touch/TouchBehavior.methods.shared.cs +++ b/src/CommunityToolkit.Maui/Behaviors/PlatformBehaviors/Touch/TouchBehavior.methods.shared.cs @@ -109,42 +109,34 @@ protected virtual void Dispose(bool disposing) isDisposed = true; } - static async void RaiseCurrentTouchStateChanged(BindableObject bindable, object oldValue, object newValue) + async void RaiseCurrentTouchStateChanged(object oldValue, object newValue) { - var touchBehavior = (TouchBehavior)bindable; - - await Task.WhenAll(touchBehavior.ForceUpdateState(CancellationToken.None), touchBehavior.HandleLongPress(CancellationToken.None)); - touchBehavior.weakEventManager.HandleEvent(touchBehavior, new TouchStateChangedEventArgs(touchBehavior.CurrentTouchState), nameof(CurrentTouchStateChanged)); + await Task.WhenAll(ForceUpdateState(CancellationToken.None), HandleLongPress(CancellationToken.None)); + weakEventManager.HandleEvent(this, new TouchStateChangedEventArgs(CurrentTouchState), nameof(CurrentTouchStateChanged)); } - static void RaiseInteractionStatusChanged(BindableObject bindable, object oldValue, object newValue) + void RaiseInteractionStatusChanged(object oldValue, object newValue) { - var touchBehavior = (TouchBehavior)bindable; - touchBehavior.weakEventManager.HandleEvent(touchBehavior, new TouchInteractionStatusChangedEventArgs(touchBehavior.CurrentInteractionStatus), nameof(InteractionStatusChanged)); + weakEventManager.HandleEvent(this, new TouchInteractionStatusChangedEventArgs(CurrentInteractionStatus), nameof(InteractionStatusChanged)); } - static void RaiseCurrentTouchStatusChanged(BindableObject bindable, object oldValue, object newValue) + void RaiseCurrentTouchStatusChanged(object oldValue, object newValue) { - var touchBehavior = (TouchBehavior)bindable; - touchBehavior.weakEventManager.HandleEvent(touchBehavior, new TouchStatusChangedEventArgs(touchBehavior.CurrentTouchStatus), nameof(CurrentTouchStatusChanged)); + weakEventManager.HandleEvent(this, new TouchStatusChangedEventArgs(CurrentTouchStatus), nameof(CurrentTouchStatusChanged)); } - static async void RaiseHoverStateChanged(BindableObject bindable, object oldValue, object newValue) + async void RaiseHoverStateChanged(object oldValue, object newValue) { - var touchBehavior = (TouchBehavior)bindable; - - await touchBehavior.ForceUpdateState(CancellationToken.None); - touchBehavior.weakEventManager.HandleEvent(touchBehavior, new HoverStateChangedEventArgs(touchBehavior.CurrentHoverState), nameof(HoverStateChanged)); + await ForceUpdateState(CancellationToken.None); + weakEventManager.HandleEvent(this, new HoverStateChangedEventArgs(CurrentHoverState), nameof(HoverStateChanged)); } - static void RaiseHoverStatusChanged(BindableObject bindable, object oldValue, object newValue) + void RaiseHoverStatusChanged(object oldValue, object newValue) { - var touchBehavior = (TouchBehavior)bindable; - - touchBehavior.weakEventManager.HandleEvent(touchBehavior, new HoverStatusChangedEventArgs(touchBehavior.CurrentHoverStatus), nameof(HoverStatusChanged)); + weakEventManager.HandleEvent(this, new HoverStatusChangedEventArgs(CurrentHoverStatus), nameof(HoverStatusChanged)); } - static void HandleDefaultOpacityChanging(BindableObject bindable, object oldValue, object newValue) + void HandleDefaultOpacityChanging(object oldValue, object newValue) { var defaultOpacity = (double)newValue; switch (defaultOpacity) @@ -156,7 +148,7 @@ static void HandleDefaultOpacityChanging(BindableObject bindable, object oldValu } } - static void HandleHoveredOpacityChanging(BindableObject bindable, object oldValue, object newValue) + void HandleHoveredOpacityChanging(object oldValue, object newValue) { var hoveredOpacity = (double)newValue; switch (hoveredOpacity) @@ -168,7 +160,7 @@ static void HandleHoveredOpacityChanging(BindableObject bindable, object oldValu } } - static void HandlePressedOpacityChanging(BindableObject bindable, object oldValue, object newValue) + void HandlePressedOpacityChanging(object oldValue, object newValue) { var pressedOpacity = (double)newValue; switch (pressedOpacity) diff --git a/src/CommunityToolkit.Maui/Behaviors/ProgressBarAnimationBehavior.shared.cs b/src/CommunityToolkit.Maui/Behaviors/ProgressBarAnimationBehavior.shared.cs index c2bccfebff..b047b8fc54 100644 --- a/src/CommunityToolkit.Maui/Behaviors/ProgressBarAnimationBehavior.shared.cs +++ b/src/CommunityToolkit.Maui/Behaviors/ProgressBarAnimationBehavior.shared.cs @@ -37,23 +37,21 @@ public event EventHandler AnimationCompleted [BindableProperty] public partial Easing Easing { get; set; } = ProgressBarAnimationBehaviorDefaults.Easing; - static async void OnAnimateProgressPropertyChanged(BindableObject bindable, object oldValue, object newValue) + async void OnAnimateProgressPropertyChanged(object oldValue, object newValue) { - var progressBarAnimationBehavior = (ProgressBarAnimationBehavior)bindable; - - if (progressBarAnimationBehavior.View is not null) + if (View is not null) { - await AnimateProgress(progressBarAnimationBehavior.View, - progressBarAnimationBehavior.Progress, - progressBarAnimationBehavior.Length, - progressBarAnimationBehavior.Easing, + await AnimateProgress(View, + Progress, + Length, + Easing, CancellationToken.None); - progressBarAnimationBehavior.OnAnimationCompleted(); + OnAnimationCompleted(); } } - static void OnAnimateProgressPropertyChanging(BindableObject bindable, object oldValue, object newValue) + void OnAnimateProgressPropertyChanging(object oldValue, object newValue) { var progress = (double)newValue; switch (progress) diff --git a/src/CommunityToolkit.Maui/Views/AvatarView.shared.cs b/src/CommunityToolkit.Maui/Views/AvatarView.shared.cs index e21ba2c5b7..147aa3518c 100644 --- a/src/CommunityToolkit.Maui/Views/AvatarView.shared.cs +++ b/src/CommunityToolkit.Maui/Views/AvatarView.shared.cs @@ -256,39 +256,34 @@ void IImageSourcePart.UpdateIsLoading(bool isLoading) static double GetAverageCorderRadius(in CornerRadius cornerRadius) => new[] { cornerRadius.TopLeft, cornerRadius.TopRight, cornerRadius.BottomLeft, cornerRadius.BottomRight }.Average(); - static void OnBorderColorPropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnBorderColorPropertyChanged(object oldValue, object newValue) { - AvatarView avatarView = (AvatarView)bindable; - avatarView.Stroke = (Color)newValue; + Stroke = (Color)newValue; } - static void OnBorderWidthPropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnBorderWidthPropertyChanged(object oldValue, object newValue) { - AvatarView avatarView = (AvatarView)bindable; - avatarView.StrokeThickness = (double)newValue; + StrokeThickness = (double)newValue; } - static void OnCornerRadiusPropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnCornerRadiusPropertyChanged(object oldValue, object newValue) { - AvatarView avatarView = (AvatarView)bindable; CornerRadius corderRadius = (CornerRadius)newValue; - avatarView.StrokeShape = new RoundRectangle + StrokeShape = new RoundRectangle { CornerRadius = corderRadius }; } - static void OnImageSourcePropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnImageSourcePropertyChanged(object oldValue, object newValue) { - AvatarView avatarView = (AvatarView)bindable; - avatarView.HandleImageChanged((ImageSource?)newValue); + HandleImageChanged((ImageSource?)newValue); } - static void OnTextPropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnTextPropertyChanged(object oldValue, object newValue) { - AvatarView avatarView = (AvatarView)bindable; - avatarView.avatarLabel.Text = (string)newValue; + avatarLabel.Text = (string)newValue; } void HandleFontChanged() diff --git a/src/CommunityToolkit.Maui/Views/Expander/Expander.shared.cs b/src/CommunityToolkit.Maui/Views/Expander/Expander.shared.cs index f532f7683b..f94f0c8261 100644 --- a/src/CommunityToolkit.Maui/Views/Expander/Expander.shared.cs +++ b/src/CommunityToolkit.Maui/Views/Expander/Expander.shared.cs @@ -103,46 +103,45 @@ public ExpandDirection Direction Grid ContentGrid => (Grid)base.Content; - static void OnContentPropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnContentPropertyChanged(object oldValue, object newValue) { - var expander = (Expander)bindable; if (newValue is View view) { - view.SetBinding(IsVisibleProperty, new Binding(nameof(IsExpanded), source: expander)); + view.SetBinding(IsVisibleProperty, new Binding(nameof(IsExpanded), source: this)); - expander.ContentGrid.Remove(oldValue); - expander.ContentGrid.Add(newValue); - expander.ContentGrid.SetRow(view, expander.Direction switch + ContentGrid.Remove(oldValue); + ContentGrid.Add(newValue); + ContentGrid.SetRow(view, Direction switch { ExpandDirection.Down => 1, ExpandDirection.Up => 0, - _ => throw new NotSupportedException($"{nameof(ExpandDirection)} {expander.Direction} is not yet supported") + _ => throw new NotSupportedException($"{nameof(ExpandDirection)} {Direction} is not yet supported") }); } } - static void OnHeaderPropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnHeaderPropertyChanged(object oldValue, object newValue) { - var expander = (Expander)bindable; if (newValue is View view) { - expander.SetHeaderGestures(view); + SetHeaderGestures(view); - expander.ContentGrid.Remove(oldValue); - expander.ContentGrid.Add(newValue); + ContentGrid.Remove(oldValue); + ContentGrid.Add(newValue); - expander.ContentGrid.SetRow(view, expander.Direction switch + ContentGrid.SetRow(view, Direction switch { ExpandDirection.Down => 0, ExpandDirection.Up => 1, - _ => throw new NotSupportedException($"{nameof(ExpandDirection)} {expander.Direction} is not yet supported") + _ => throw new NotSupportedException($"{nameof(ExpandDirection)} {Direction} is not yet supported") }); } } - static void OnIsExpandedPropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnIsExpandedPropertyChanged(object oldValue, object newValue) { - ((IExpander)bindable).ExpandedChanged(((IExpander)bindable).IsExpanded); + IExpander expander = this; + expander.ExpandedChanged(expander.IsExpanded); } static void OnDirectionPropertyChanged(BindableObject bindable, object oldValue, object newValue) => diff --git a/src/CommunityToolkit.Maui/Views/RatingView/RatingView.shared.cs b/src/CommunityToolkit.Maui/Views/RatingView/RatingView.shared.cs index f17dbc5fa1..e199988269 100644 --- a/src/CommunityToolkit.Maui/Views/RatingView/RatingView.shared.cs +++ b/src/CommunityToolkit.Maui/Views/RatingView/RatingView.shared.cs @@ -166,7 +166,7 @@ public event EventHandler RatingChanged } }; - static object CoerceColorToTransparent(BindableObject bindable, object value) + object CoerceColorToTransparent(object value) { var colorValue = (Color?)value; return colorValue ?? Colors.Transparent; @@ -201,15 +201,14 @@ static ReadOnlyCollection GetVisualTreeDescendantsWithBorderAndSh return result.AsReadOnly(); } - static void OnIsReadOnlyChanged(BindableObject bindable, object oldValue, object newValue) + void OnIsReadOnlyChanged(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; - foreach (var child in ratingView.RatingLayout.Children.Cast()) + foreach (var child in RatingLayout.Children.Cast()) { - if (!ratingView.IsReadOnly) + if (!IsReadOnly) { TapGestureRecognizer tapGestureRecognizer = new(); - tapGestureRecognizer.Tapped += ratingView.OnShapeTapped; + tapGestureRecognizer.Tapped += OnShapeTapped; child.GestureRecognizers.Add(tapGestureRecognizer); continue; } @@ -218,9 +217,8 @@ static void OnIsReadOnlyChanged(BindableObject bindable, object oldValue, object } } - static void OnRatingChanging(BindableObject bindable, object oldValue, object newValue) + void OnRatingChanging(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; var rating = (double)newValue; if (rating < 0) @@ -228,13 +226,13 @@ static void OnRatingChanging(BindableObject bindable, object oldValue, object ne throw new ArgumentOutOfRangeException(nameof(newValue), $"{nameof(Rating)} cannot be less than 0"); } - if (rating > ratingView.MaximumRating) + if (rating > MaximumRating) { throw new ArgumentOutOfRangeException(nameof(newValue), $"{nameof(Rating)} cannot be greater than {nameof(MaximumRating)}"); } } - static void OnMaximumRatingChanging(BindableObject bindable, object oldValue, object newValue) + void OnMaximumRatingChanging(object oldValue, object newValue) { var maximumRating = (int)newValue; @@ -247,10 +245,9 @@ static void OnMaximumRatingChanging(BindableObject bindable, object oldValue, ob } } - static void OnMaximumRatingChanged(BindableObject bindable, object oldValue, object newValue) + void OnMaximumRatingChanged(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; - var layout = ratingView.RatingLayout; + var layout = RatingLayout; var newMaximumRatingValue = (int)newValue; var oldMaximumRatingValue = (int)oldValue; if (newMaximumRatingValue < oldMaximumRatingValue) @@ -260,38 +257,35 @@ static void OnMaximumRatingChanged(BindableObject bindable, object oldValue, obj layout.RemoveAt(lastElement); } - ratingView.UpdateShapeFills(ratingView.FillOption); + UpdateShapeFills(FillOption); } else if (newMaximumRatingValue > oldMaximumRatingValue) { - ratingView.AddChildrenToLayout(oldMaximumRatingValue - 1, newMaximumRatingValue - 1); + AddChildrenToLayout(oldMaximumRatingValue - 1, newMaximumRatingValue - 1); } - if (newMaximumRatingValue < ratingView.Rating) // Ensure Rating is never greater than MaximumRating + if (newMaximumRatingValue < Rating) // Ensure Rating is never greater than MaximumRating { - ratingView.Rating = newMaximumRatingValue; + Rating = newMaximumRatingValue; } } - static void OnRatingChanged(BindableObject bindable, object oldValue, object newValue) + void OnRatingChanged(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; var newRating = (double)newValue; - ratingView.UpdateShapeFills(ratingView.FillOption); - ratingView.OnRatingChangedEvent(new RatingChangedEventArgs(newRating)); + UpdateShapeFills(FillOption); + OnRatingChangedEvent(new RatingChangedEventArgs(newRating)); } - static void OnSpacingChanged(BindableObject bindable, object oldValue, object newValue) + void OnSpacingChanged(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; - ratingView.RatingLayout.Spacing = (double)newValue; + RatingLayout.Spacing = (double)newValue; } - static void OnRatingColorChanged(BindableObject bindable, object oldValue, object newValue) + void OnRatingColorChanged(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; - ratingView.UpdateShapeFills(ratingView.FillOption); + UpdateShapeFills(FillOption); } static LinearGradientBrush GetPartialFillBrush(Color filledColor, double partialFill, Color emptyColor) @@ -305,12 +299,11 @@ static LinearGradientBrush GetPartialFillBrush(Color filledColor, double partial new Point(0, 0), new Point(1, 0)); } - static void OnCustomShapePathPropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnCustomShapePathPropertyChanged(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; var newShape = (string)newValue; - if (ratingView.Shape is not RatingViewShape.Custom) + if (Shape is not RatingViewShape.Custom) { return; } @@ -318,7 +311,7 @@ static void OnCustomShapePathPropertyChanged(BindableObject bindable, object old string newShapePathData; if (string.IsNullOrEmpty(newShape)) { - ratingView.Shape = RatingViewDefaults.Shape; + Shape = RatingViewDefaults.Shape; newShapePathData = PathShapes.Star; } else @@ -326,25 +319,22 @@ static void OnCustomShapePathPropertyChanged(BindableObject bindable, object old newShapePathData = newShape; } - ratingView.ChangeShape(newShapePathData); + ChangeShape(newShapePathData); } - static void OnShapePaddingPropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnShapePaddingPropertyChanged(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; - for (var element = 0; element < ratingView.RatingLayout.Count; element++) + for (var element = 0; element < RatingLayout.Count; element++) { - ((Border)ratingView.RatingLayout.Children[element]).Padding = (Thickness)newValue; + ((Border)RatingLayout.Children[element]).Padding = (Thickness)newValue; } } - static void OnShapeBorderColorChanged(BindableObject bindable, object oldValue, object newValue) + void OnShapeBorderColorChanged(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; - - for (var element = 0; element < ratingView.RatingLayout.Count; element++) + for (var element = 0; element < RatingLayout.Count; element++) { - var border = (Border)ratingView.RatingLayout.Children[element]; + var border = (Border)RatingLayout.Children[element]; if (border.Content is not null) { ((Path)border.Content.GetVisualTreeDescendants()[0]).Stroke = (Color)newValue; @@ -352,7 +342,7 @@ static void OnShapeBorderColorChanged(BindableObject bindable, object oldValue, } } - static void OnShapeBorderThicknessChanging(BindableObject bindable, object oldValue, object newValue) + void OnShapeBorderThicknessChanging(object oldValue, object newValue) { if ((double)newValue < 0) { @@ -360,12 +350,11 @@ static void OnShapeBorderThicknessChanging(BindableObject bindable, object oldVa } } - static void OnShapeBorderThicknessChanged(BindableObject bindable, object oldValue, object newValue) + void OnShapeBorderThicknessChanged(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; - for (var element = 0; element < ratingView.RatingLayout.Count; element++) + for (var element = 0; element < RatingLayout.Count; element++) { - var border = (Border)ratingView.RatingLayout.Children[element]; + var border = (Border)RatingLayout.Children[element]; if (border.Content is not null) { ((Path)border.Content.GetVisualTreeDescendants()[0]).StrokeThickness = (double)newValue; @@ -373,18 +362,16 @@ static void OnShapeBorderThicknessChanged(BindableObject bindable, object oldVal } } - static void OnShapePropertyChanged(BindableObject bindable, object oldValue, object newValue) + void OnShapePropertyChanged(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; - ratingView.ChangeShape(ratingView.GetShapePathData((RatingViewShape)newValue)); + ChangeShape(GetShapePathData((RatingViewShape)newValue)); } - static void OnShapeDiameterSizeChanged(BindableObject bindable, object oldValue, object newValue) + void OnShapeDiameterSizeChanged(object oldValue, object newValue) { - var ratingView = (RatingView)bindable; - for (var element = 0; element < ratingView.RatingLayout.Count; element++) + for (var element = 0; element < RatingLayout.Count; element++) { - var border = (Border)ratingView.RatingLayout.Children[element]; + var border = (Border)RatingLayout.Children[element]; if (border.Content is null) { continue;