RefreshView IsEnabled enhancements (#24290)

This commit is contained in:
Jakub Florkowski 2024-09-04 17:44:56 +02:00 коммит произвёл GitHub
Родитель 049bfdf27f
Коммит 5c0e6d64a1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
9 изменённых файлов: 38 добавлений и 29 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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;
@ -67,19 +68,10 @@ namespace Microsoft.Maui.Controls
/// <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
@ -94,7 +86,7 @@ namespace Microsoft.Maui.Controls
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
@ -103,21 +95,6 @@ namespace Microsoft.Maui.Controls
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);
@ -135,6 +112,20 @@ namespace Microsoft.Maui.Controls
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);

Просмотреть файл

@ -56,6 +56,17 @@ namespace Microsoft.Maui.Controls.Core.UnitTests
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()
{