Skip to content

Commit cc2038f

Browse files
TheCodeTravelerstephenquanCopilot
authored
Remove BindablePropertyAttribute.DefaultValue (#2995)
* Extend BindableProperty source gen to handle partial property initializers * Update CommonUsageTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Only implement new initializer code for when initializers are present * Revert IntegrationTests * Revert setter accessibility changes * Corrected setter * Refactor GetDefaultValueCreatorMethod. Update XML Doc. Apply to readonly BindableProperty. Extend unit test. * Include global root namespace for BindableObject * Corrected typo in GetDefautValueCreatorMethodName * Revert initializer-setter changes (not needed). Refactor getter-initializer implementation. * Remove unneeded test from BindablePropertyModelTests * Refctor EffectiveDefaultValueCreatorMethodName property (replacing GetDefaultValueCreatorMethodName method) * Update BindablePropertyName_WithInitializer_ReturnsCorrectEffectiveDefaultValueCreatorMethodName for clarity * Corrected unit test name GenerateBindableProperty_WithInitializer_GeneratesCorrectCode * Refactor InitializingPropertyName property * Add Edge Case Test `GenerateBindableProperty_WithBothInitializerAndDefault_GeneratedCodeDefaultsToUseDefaultValueCreatorMethod` * Use `"null"` instead of `string.IsNullOrEmpty()` * Remove `DefaultValue` * Move out-of-scope variables to `file static class` * Update `BindablePropertyAttributeSourceGenerator` and Unit Tests * Update `MaxLengthReachedBehavior` * Update StatusBarBehavior * Remove DefaultValue * Update TouchBehaviorTests.cs * Update TouchBehavior.shared.cs * Revert "Update TouchBehaviorTests.cs" This reverts commit b84522e. * Update Unit Tests * Update TouchBehavior * Implement Default Values in `GestureManager` * Update GestureManager.shared.cs * Update ImageTouchBehavior Logic * Update ImageTouchBehavior * Update BackgroundColor Properties * Update TouchBehaviorTests.cs * Use `TryGetBindableImageTouchBehaviorElement` to prevent runtime crash * `dotnet format` * Fix failing unit tests * Avoid using same URL --------- Co-authored-by: Stephen Quan <squan@esri.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a4b3339 commit cc2038f

File tree

33 files changed

+684
-520
lines changed

33 files changed

+684
-520
lines changed

samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/PopupsPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ async void HandleSelfClosingPopupButtonClicked(object? sender, EventArgs e)
149149

150150
await this.ClosePopupAsync();
151151
}
152-
152+
153153
async void HandleCollectionViewPopupClicked(object? sender, EventArgs e)
154154
{
155155
var popupResult = await popupService.ShowPopupAsync<CollectionViewPopup, string>(

samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/CollectionViewPopupViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async Task OnReturnButtonTapped(CancellationToken token)
1414
{
1515
await popupService.ClosePopupAsync<string>(navigation, SelectedTitle ?? string.Empty, token);
1616
}
17-
17+
1818
[ObservableProperty, NotifyCanExecuteChangedFor(nameof(ReturnButtonTappedCommand))]
1919
public partial string? SelectedTitle { get; set; }
2020
}

src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public event EventHandler<MediaCapturedEventArgs> MediaCaptured
3939
static ICameraProvider CameraProvider => IPlatformApplication.Current?.Services.GetRequiredService<ICameraProvider>() ?? throw new CameraException("Unable to retrieve CameraProvider");
4040

4141
/// <inheritdoc cref="ICameraView.IsAvailable"/>
42-
[BindableProperty(DefaultValue = CameraViewDefaults.IsAvailable)]
43-
public partial bool IsAvailable { get; }
42+
[BindableProperty]
43+
public partial bool IsAvailable { get; } = CameraViewDefaults.IsAvailable;
4444

4545
/// <inheritdoc cref="ICameraView.IsBusy"/>
46-
[BindableProperty(DefaultValue = CameraViewDefaults.IsCameraBusy)]
47-
public partial bool IsBusy { get; }
46+
[BindableProperty]
47+
public partial bool IsBusy { get; } = CameraViewDefaults.IsCameraBusy;
4848

4949
/// <summary>
5050
/// Gets the <see cref="Command{CancellationToken}"/> that triggers an image capture.
@@ -92,24 +92,24 @@ public event EventHandler<MediaCapturedEventArgs> MediaCaptured
9292
public partial Command<CancellationToken> StopVideoRecordingCommand { get; }
9393

9494
/// <inheritdoc cref="ICameraView.CameraFlashMode"/>
95-
[BindableProperty(DefaultValueCreatorMethodName = nameof(CreateCameraFlashMode))]
96-
public partial CameraFlashMode CameraFlashMode { get; set; }
95+
[BindableProperty]
96+
public partial CameraFlashMode CameraFlashMode { get; set; } = CameraViewDefaults.CameraFlashMode;
9797

9898
/// <inheritdoc cref="ICameraView.SelectedCamera"/>
9999
[BindableProperty(DefaultBindingMode = BindingMode.TwoWay)]
100100
public partial CameraInfo? SelectedCamera { get; set; }
101101

102102
/// <inheritdoc cref="ICameraView.ZoomFactor"/>
103-
[BindableProperty(DefaultValue = CameraViewDefaults.ZoomFactor, DefaultBindingMode = BindingMode.TwoWay, CoerceValueMethodName = nameof(CoerceZoom))]
104-
public partial float ZoomFactor { get; set; }
103+
[BindableProperty(DefaultBindingMode = BindingMode.TwoWay, CoerceValueMethodName = nameof(CoerceZoom))]
104+
public partial float ZoomFactor { get; set; } = CameraViewDefaults.ZoomFactor;
105105

106106
/// <inheritdoc cref="ICameraView.ImageCaptureResolution"/>
107-
[BindableProperty(DefaultValueCreatorMethodName = nameof(CreateImageCaptureResolution), DefaultBindingMode = BindingMode.TwoWay)]
108-
public partial Size ImageCaptureResolution { get; set; }
107+
[BindableProperty(DefaultBindingMode = BindingMode.TwoWay)]
108+
public partial Size ImageCaptureResolution { get; set; } = CameraViewDefaults.ImageCaptureResolution;
109109

110110
/// <inheritdoc cref="ICameraView.IsTorchOn"/>
111-
[BindableProperty(DefaultValue = CameraViewDefaults.IsTorchOn)]
112-
public partial bool IsTorchOn { get; set; }
111+
[BindableProperty]
112+
public partial bool IsTorchOn { get; set; } = CameraViewDefaults.IsTorchOn;
113113

114114
new CameraViewHandler Handler => (CameraViewHandler)(base.Handler ?? throw new InvalidOperationException("Unable to retrieve Handler"));
115115

@@ -230,10 +230,6 @@ protected virtual void Dispose(bool disposing)
230230
}
231231
}
232232

233-
static object CreateImageCaptureResolution(BindableObject bindable) => CameraViewDefaults.ImageCaptureResolution;
234-
235-
static object CreateCameraFlashMode(BindableObject bindable) => CameraViewDefaults.CameraFlashMode;
236-
237233
static Command<CancellationToken> CreateCaptureImageCommand(BindableObject bindable)
238234
{
239235
var cameraView = (CameraView)bindable;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Windows.Input;
2+
3+
namespace CommunityToolkit.Maui.Core;
4+
5+
static class MaxLengthReachedBehaviorDefaults
6+
{
7+
public const bool ShouldDismissKeyboardAutomatically = false;
8+
public static ICommand? Command { get; } = null;
9+
}

src/CommunityToolkit.Maui.Core/Primitives/Defaults/ProgressBarAnimationBehaviorDefaults.cs renamed to src/CommunityToolkit.Maui.Core/Primitives/Defaults/ProgressBarAnimationBehaviorDefaults.shared.cs

File renamed without changes.

src/CommunityToolkit.Maui.Core/Primitives/Defaults/TouchBehaviorDefaults.shared.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,15 @@ static class TouchBehaviorDefaults
190190
/// <summary>
191191
/// Default Value for TouchBehavior <see cref="DefaultBackgroundColor"/>
192192
/// </summary>
193-
public const Color? DefaultBackgroundColor = null;
193+
public static Color DefaultBackgroundColor { get; } = Colors.Transparent;
194194

195195
/// <summary>
196196
/// Default Value for TouchBehavior <see cref="HoveredBackgroundColor"/>
197197
/// </summary>
198-
public const Color? HoveredBackgroundColor = null;
198+
public static Color HoveredBackgroundColor { get; } = Colors.Transparent;
199199

200200
/// <summary>
201201
/// Default Value for TouchBehavior <see cref="PressedBackgroundColor"/>
202202
/// </summary>
203-
public const Color? PressedBackgroundColor = null;
203+
public static Color PressedBackgroundColor { get; } = Colors.Transparent;
204204
}

src/CommunityToolkit.Maui.Core/Primitives/Defaults/UserStoppedTypingBehaviorDefaults.cs renamed to src/CommunityToolkit.Maui.Core/Primitives/Defaults/UserStoppedTypingBehaviorDefaults.shared.cs

File renamed without changes.

src/CommunityToolkit.Maui.MediaElement/MediaElement.shared.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,26 @@ internal event EventHandler StopRequested
102102
/// <summary>
103103
/// Gets the <see cref="MediaHeight"/> in pixels.
104104
/// </summary>
105-
[BindableProperty(DefaultValue = MediaElementDefaults.MediaHeight)]
106-
public partial int MediaHeight { get; }
105+
[BindableProperty]
106+
public partial int MediaHeight { get; } = MediaElementDefaults.MediaHeight;
107107

108108
/// <summary>
109109
/// Gets the <see cref="MediaWidth"/> in pixels.
110110
/// </summary>
111-
[BindableProperty(DefaultValue = MediaElementDefaults.MediaWidth)]
112-
public partial int MediaWidth { get; }
111+
[BindableProperty]
112+
public partial int MediaWidth { get; } = MediaElementDefaults.MediaWidth;
113113

114114
/// <summary>
115115
/// Gets the current <see cref="Position"/> of the media playback.
116116
/// </summary>
117-
[BindableProperty(DefaultValue = MediaElementDefaults.Position)]
118-
public partial TimeSpan Position { get; }
117+
[BindableProperty]
118+
public partial TimeSpan Position { get; } = MediaElementDefaults.Position;
119119

120120
/// <summary>
121121
/// Gets the <see cref="Duration"/> of the media.
122122
/// </summary>
123-
[BindableProperty(DefaultValue = MediaElementDefaults.Duration)]
124-
public partial TimeSpan Duration { get; }
123+
[BindableProperty]
124+
public partial TimeSpan Duration { get; } = MediaElementDefaults.Duration;
125125

126126
/// <summary>
127127
/// Gets or sets the <see cref="AndroidViewType"/> of the media.
@@ -131,38 +131,38 @@ internal event EventHandler StopRequested
131131
/// <summary>
132132
/// Gets or sets the <see cref="Aspect"/> ratio used to display the video content.
133133
/// </summary>
134-
[BindableProperty(DefaultValue = MediaElementDefaults.Aspect)]
135-
public partial Aspect Aspect { get; set; }
134+
[BindableProperty]
135+
public partial Aspect Aspect { get; set; } = MediaElementDefaults.Aspect;
136136

137137
/// <summary>
138138
/// Gets or sets the <see cref="ShouldAutoPlay"/> indicating whether the media should play automatically.
139139
/// </summary>
140-
[BindableProperty(DefaultValue = MediaElementDefaults.ShouldAutoPlay)]
141-
public partial bool ShouldAutoPlay { get; set; }
140+
[BindableProperty]
141+
public partial bool ShouldAutoPlay { get; set; } = MediaElementDefaults.ShouldAutoPlay;
142142

143143
/// <summary>
144144
/// Gets or sets the <see cref="ShouldLoopPlayback"/> indicating whether the media should loop playback.
145145
/// </summary>
146-
[BindableProperty(DefaultValue = MediaElementDefaults.ShouldLoopPlayback)]
147-
public partial bool ShouldLoopPlayback { get; set; }
146+
[BindableProperty]
147+
public partial bool ShouldLoopPlayback { get; set; } = MediaElementDefaults.ShouldLoopPlayback;
148148

149149
/// <summary>
150150
/// Gets or sets the <see cref="ShouldKeepScreenOn"/> indicating whether the screen should be kept on during media playback.
151151
/// </summary>
152-
[BindableProperty(DefaultValue = MediaElementDefaults.ShouldKeepScreenOn)]
153-
public partial bool ShouldKeepScreenOn { get; set; }
152+
[BindableProperty]
153+
public partial bool ShouldKeepScreenOn { get; set; } = MediaElementDefaults.ShouldKeepScreenOn;
154154

155155
/// <summary>
156156
/// Gets or sets the <see cref="ShouldMute"/> indicating whether the media should be muted.
157157
/// </summary>
158-
[BindableProperty(DefaultValue = MediaElementDefaults.ShouldMute)]
159-
public partial bool ShouldMute { get; set; }
158+
[BindableProperty]
159+
public partial bool ShouldMute { get; set; } = MediaElementDefaults.ShouldMute;
160160

161161
/// <summary>
162162
/// Gets or sets the <see cref="ShouldShowPlaybackControls"/> indicating whether playback controls should be shown.
163163
/// </summary>
164-
[BindableProperty(DefaultValue = MediaElementDefaults.ShouldShowPlaybackControls)]
165-
public partial bool ShouldShowPlaybackControls { get; set; }
164+
[BindableProperty]
165+
public partial bool ShouldShowPlaybackControls { get; set; } = MediaElementDefaults.ShouldShowPlaybackControls;
166166

167167
/// <summary>
168168
/// Gets or sets the <see cref="Source"/> of the media.
@@ -174,38 +174,38 @@ internal event EventHandler StopRequested
174174
/// <summary>
175175
/// Gets or sets the <see cref="Speed"/> of the media playback.
176176
/// </summary>
177-
[BindableProperty(DefaultValue = MediaElementDefaults.Speed)]
178-
public partial double Speed { get; set; }
177+
[BindableProperty]
178+
public partial double Speed { get; set; } = MediaElementDefaults.Speed;
179179

180180
/// <summary>
181181
/// Gets or sets the <see cref="MetadataTitle"/> of the media.
182182
/// </summary>
183-
[BindableProperty(DefaultValue = MediaElementDefaults.MetadataTitle)]
184-
public partial string MetadataTitle { get; set; }
183+
[BindableProperty]
184+
public partial string MetadataTitle { get; set; } = MediaElementDefaults.MetadataTitle;
185185

186186
/// <summary>
187187
/// Gets or sets the <see cref="MetadataArtist"/> of the media.
188188
/// </summary>
189-
[BindableProperty(DefaultValue = MediaElementDefaults.MetadataArtist)]
190-
public partial string MetadataArtist { get; set; }
189+
[BindableProperty]
190+
public partial string MetadataArtist { get; set; } = MediaElementDefaults.MetadataArtist;
191191

192192
/// <summary>
193193
/// Gets or sets the <see cref="MetadataArtworkUrl"/> of the media.
194194
/// </summary>
195-
[BindableProperty(DefaultValue = MediaElementDefaults.MetadataArtworkUrl)]
196-
public partial string MetadataArtworkUrl { get; set; }
195+
[BindableProperty]
196+
public partial string MetadataArtworkUrl { get; set; } = MediaElementDefaults.MetadataArtworkUrl;
197197

198198
/// <summary>
199199
/// Gets or sets the <see cref="Volume"/> of the media.
200200
/// </summary>
201-
[BindableProperty(DefaultValue = MediaElementDefaults.Volume, DefaultBindingMode = BindingMode.TwoWay, PropertyChangingMethodName = nameof(OnVolumeChanging))]
202-
public partial double Volume { get; set; }
201+
[BindableProperty(DefaultBindingMode = BindingMode.TwoWay, PropertyChangingMethodName = nameof(OnVolumeChanging))]
202+
public partial double Volume { get; set; } = MediaElementDefaults.Volume;
203203

204204
/// <summary>
205205
/// Gets or sets the <see cref="CurrentState"/> of the media.
206206
/// </summary>
207-
[BindableProperty(DefaultValue = MediaElementDefaults.CurrentState, PropertyChangedMethodName = nameof(OnCurrentStatePropertyChanged))]
208-
public partial MediaElementState CurrentState { get; private set; }
207+
[BindableProperty(PropertyChangedMethodName = nameof(OnCurrentStatePropertyChanged))]
208+
public partial MediaElementState CurrentState { get; private set; } = MediaElementDefaults.CurrentState;
209209

210210
/// <inheritdoc/>
211211
TaskCompletionSource IAsynchronousMediaElementHandler.SeekCompletedTCS => seekCompletedTaskCompletionSource;

src/CommunityToolkit.Maui.MediaElement/Primitives/MediaElementDefaults.shared.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ static class MediaElementDefaults
88

99
public const int MediaWidth = 0;
1010

11-
public const string Position = "00:00:00";
12-
13-
public const string Duration = "00:00:00";
14-
1511
public const bool ShouldAutoPlay = false;
1612

1713
public const bool ShouldLoopPlayback = false;
@@ -33,4 +29,8 @@ static class MediaElementDefaults
3329
public const string MetadataArtworkUrl = "";
3430

3531
public const MediaElementState CurrentState = MediaElementState.None;
32+
33+
public static TimeSpan Position { get; } = TimeSpan.Zero;
34+
35+
public static TimeSpan Duration { get; } = TimeSpan.Zero;
3636
}

src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/BaseBindablePropertyAttributeSourceGeneratorTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ sealed partial class BindablePropertyAttribute : global::System.Attribute
2323
{
2424
public string? PropertyName { get; }
2525
public global::System.Type? DeclaringType { get; set; }
26-
public object? DefaultValue { get; set; }
2726
public global::Microsoft.Maui.Controls.BindingMode DefaultBindingMode { get; set; }
2827
public string ValidateValueMethodName { get; set; } = string.Empty;
2928
public string PropertyChangedMethodName { get; set; } = string.Empty;

0 commit comments

Comments
 (0)