Skip to content

Commit

Permalink
RefreshView IsEnabled enhancements (dotnet#24290)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaflo committed Sep 4, 2024
1 parent 049bfdf commit 5c0e6d6
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ static Microsoft.Maui.Controls.Shapes.Matrix.operator ==(Microsoft.Maui.Controls
virtual Microsoft.Maui.Controls.VisualElement.IsEnabledCore.get -> bool
Microsoft.Maui.Controls.VisualElement.RefreshIsEnabledProperty() -> void
override Microsoft.Maui.Controls.Button.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.RefreshView.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.ImageButton.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.SearchBar.IsEnabledCore.get -> bool
~Microsoft.Maui.Controls.InputView.FontFamily.get -> string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Microsoft.Maui.Controls.Handlers.BoxViewHandler.BoxViewHandler() -> void
virtual Microsoft.Maui.Controls.VisualElement.IsEnabledCore.get -> bool
Microsoft.Maui.Controls.VisualElement.RefreshIsEnabledProperty() -> void
override Microsoft.Maui.Controls.Button.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.RefreshView.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.ImageButton.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.SearchBar.IsEnabledCore.get -> bool
~Microsoft.Maui.Controls.WebView.UserAgent.get -> string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Microsoft.Maui.Controls.Handlers.BoxViewHandler.BoxViewHandler() -> void
virtual Microsoft.Maui.Controls.VisualElement.IsEnabledCore.get -> bool
Microsoft.Maui.Controls.VisualElement.RefreshIsEnabledProperty() -> void
override Microsoft.Maui.Controls.Button.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.RefreshView.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.ImageButton.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.SearchBar.IsEnabledCore.get -> bool
~Microsoft.Maui.Controls.WebView.UserAgent.get -> string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Microsoft.Maui.Controls.InputView.IsTextPredictionEnabled.set -> void
virtual Microsoft.Maui.Controls.VisualElement.IsEnabledCore.get -> bool
Microsoft.Maui.Controls.VisualElement.RefreshIsEnabledProperty() -> void
override Microsoft.Maui.Controls.Button.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.RefreshView.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.ImageButton.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.SearchBar.IsEnabledCore.get -> bool
~Microsoft.Maui.Controls.InputView.FontFamily.get -> string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ override Microsoft.Maui.Controls.View.ChangeVisualState() -> void
virtual Microsoft.Maui.Controls.VisualElement.IsEnabledCore.get -> bool
Microsoft.Maui.Controls.VisualElement.RefreshIsEnabledProperty() -> void
override Microsoft.Maui.Controls.Button.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.RefreshView.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.ImageButton.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.SearchBar.IsEnabledCore.get -> bool
~Microsoft.Maui.Controls.InputView.FontFamily.get -> string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ static Microsoft.Maui.Controls.Shapes.Matrix.operator ==(Microsoft.Maui.Controls
virtual Microsoft.Maui.Controls.VisualElement.IsEnabledCore.get -> bool
Microsoft.Maui.Controls.VisualElement.RefreshIsEnabledProperty() -> void
override Microsoft.Maui.Controls.Button.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.RefreshView.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.ImageButton.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.SearchBar.IsEnabledCore.get -> bool
~Microsoft.Maui.Controls.InputView.FontFamily.get -> string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ static Microsoft.Maui.Controls.Shapes.Matrix.operator ==(Microsoft.Maui.Controls
virtual Microsoft.Maui.Controls.VisualElement.IsEnabledCore.get -> bool
Microsoft.Maui.Controls.VisualElement.RefreshIsEnabledProperty() -> void
override Microsoft.Maui.Controls.Button.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.RefreshView.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.ImageButton.IsEnabledCore.get -> bool
override Microsoft.Maui.Controls.SearchBar.IsEnabledCore.get -> bool
~Microsoft.Maui.Controls.Element.AddLogicalChild(Microsoft.Maui.Controls.Element element) -> void
Expand Down
49 changes: 20 additions & 29 deletions src/Controls/src/Core/RefreshView/RefreshView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/RefreshView.xml" path="Type[@FullName='Microsoft.Maui.Controls.RefreshView']/Docs/*" />
[ContentProperty(nameof(Content))]
public partial class RefreshView : ContentView, IElementConfiguration<RefreshView>, IRefreshView
public partial class RefreshView : ContentView, IElementConfiguration<RefreshView>, IRefreshView, ICommandElement
{
readonly Lazy<PlatformConfigurationRegistry<RefreshView>> _platformConfigurationRegistry;
public event EventHandler Refreshing;
Expand Down Expand Up @@ -67,19 +68,10 @@ public bool IsRefreshing

/// <summary>Bindable property for <see cref="Command"/>.</summary>
public static readonly BindableProperty CommandProperty =
BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(RefreshView), propertyChanged: OnCommandChanged);
BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(RefreshView),
propertyChanging: CommandElement.OnCommandChanging,
propertyChanged: CommandElement.OnCommandChanged);

static void OnCommandChanged(BindableObject bindable, object oldValue, object newValue)
{
RefreshView refreshView = (RefreshView)bindable;
if (oldValue is ICommand oldCommand)
oldCommand.CanExecuteChanged -= refreshView.RefreshCommandCanExecuteChanged;

if (newValue is ICommand newCommand)
newCommand.CanExecuteChanged += refreshView.RefreshCommandCanExecuteChanged;

refreshView.RefreshCommandCanExecuteChanged(bindable, EventArgs.Empty);
}

/// <include file="../../docs/Microsoft.Maui.Controls/RefreshView.xml" path="//Member[@MemberName='Command']/Docs/*" />
public ICommand Command
Expand All @@ -94,7 +86,7 @@ public ICommand Command
typeof(object),
typeof(RefreshView),
null,
propertyChanged: (bindable, oldvalue, newvalue) => ((RefreshView)(bindable)).RefreshCommandCanExecuteChanged(((RefreshView)(bindable)).Command, EventArgs.Empty));
propertyChanged: CommandElement.OnCommandParameterChanged);

/// <include file="../../docs/Microsoft.Maui.Controls/RefreshView.xml" path="//Member[@MemberName='CommandParameter']/Docs/*" />
public object CommandParameter
Expand All @@ -103,21 +95,6 @@ public object CommandParameter
set { SetValue(CommandParameterProperty, value); }
}

void RefreshCommandCanExecuteChanged(object sender, EventArgs eventArgs)
{
if (IsRefreshing)
return;

if (Command != null)
{
SetValue(IsEnabledProperty, Command.CanExecute(CommandParameter));
}
else
{
SetValue(IsEnabledProperty, true);
}
}

/// <summary>Bindable property for <see cref="RefreshColor"/>.</summary>
public static readonly BindableProperty RefreshColorProperty =
BindableProperty.Create(nameof(RefreshColor), typeof(Color), typeof(RefreshView), null);
Expand All @@ -135,6 +112,20 @@ public IPlatformElementConfiguration<T, RefreshView> On<T>() where T : IConfigPl
return _platformConfigurationRegistry.Value.On<T>();
}

ICommand ICommandElement.Command => Command;

object ICommandElement.CommandParameter => CommandParameter;

protected override bool IsEnabledCore => base.IsEnabledCore && CommandElement.GetCanExecute(this);

void ICommandElement.CanExecuteChanged(object sender, EventArgs e)
{
if((bool)GetValue(IsRefreshingProperty))
return;

RefreshIsEnabledProperty();
}

protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);
Expand Down
11 changes: 11 additions & 0 deletions src/Controls/tests/Core.UnitTests/RefreshViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ public void IsRefreshingStillTogglesTrueWhenCanExecuteToggledDuringExecute()
Assert.True(refreshView.IsRefreshing);
}

[Fact]
public void IsEnabledShouldCoerceCanExecute()
{
RefreshView refreshView = new RefreshView()
{
IsEnabled = false,
Command = new Command(() => { })
};
Assert.False(refreshView.IsEnabled);
}

[Fact]
public void CanExecuteChangesEnabled()
{
Expand Down

0 comments on commit 5c0e6d6

Please sign in to comment.