Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -156,6 +156,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 @@ -152,6 +152,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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit for if I find any issues otherwise we can fix later (or ignore) so we don't lose the nice green checkmark on this PR.

Suggested change
if((bool)GetValue(IsRefreshingProperty))
if (IsRefreshing)

return;
Comment thread
mattleibow marked this conversation as resolved.

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