fix: fix various issues from copilot review.

This commit is contained in:
Dong Bin
2025-04-18 18:59:56 +08:00
parent 91c67c9490
commit 3eeba12d4f
2 changed files with 13 additions and 19 deletions

View File

@@ -33,7 +33,7 @@
CancelCommand="{Binding Path=CancelCommand}"> CancelCommand="{Binding Path=CancelCommand}">
<Button Content="Hello World" /> <Button Content="Hello World" />
</u:PopConfirm> </u:PopConfirm>
<TextBlock Text="Hover to trigger" Margin="0 16" /> <TextBlock Text="GotFocus to open, LostFocus to close" Margin="0 16" />
<u:PopConfirm PopupHeader="确定是否要保存此修改?" <u:PopConfirm PopupHeader="确定是否要保存此修改?"
PopupContent="此修改将不可逆" PopupContent="此修改将不可逆"
TriggerMode="Focus" TriggerMode="Focus"

View File

@@ -1,5 +1,4 @@
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Input; using System.Windows.Input;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
@@ -69,12 +68,6 @@ public class PopConfirm : ContentControl
Popup.PlacementProperty.AddOwner<PopConfirm>(new StyledPropertyMetadata<PlacementMode>()); Popup.PlacementProperty.AddOwner<PopConfirm>(new StyledPropertyMetadata<PlacementMode>());
public static readonly StyledProperty<object?> IconProperty = Banner.IconProperty.AddOwner<PopConfirm>(); public static readonly StyledProperty<object?> IconProperty = Banner.IconProperty.AddOwner<PopConfirm>();
public object? Icon
{
get => GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
private Button? _cancelButton; private Button? _cancelButton;
@@ -91,6 +84,12 @@ public class PopConfirm : ContentControl
pop.OnTriggerModeChanged(args)); pop.OnTriggerModeChanged(args));
} }
public object? Icon
{
get => GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
public object? PopupHeader public object? PopupHeader
{ {
get => GetValue(PopupHeaderProperty); get => GetValue(PopupHeaderProperty);
@@ -166,7 +165,7 @@ public class PopConfirm : ContentControl
private void OnTriggerModeChanged(AvaloniaPropertyChangedEventArgs<PopConfirmTriggerMode> args) private void OnTriggerModeChanged(AvaloniaPropertyChangedEventArgs<PopConfirmTriggerMode> args)
{ {
var child = Presenter?.Child; var child = Presenter?.Child;
TeardownChildrenEventSubscriptions(child, args.GetOldValue<PopConfirmTriggerMode>()); TeardownChildrenEventSubscriptions(child);
SetupChildrenEventSubscriptions(child, args.GetNewValue<PopConfirmTriggerMode>()); SetupChildrenEventSubscriptions(child, args.GetNewValue<PopConfirmTriggerMode>());
} }
@@ -179,16 +178,16 @@ public class PopConfirm : ContentControl
Button.ClickEvent.AddHandler(OnButtonClicked, _confirmButton, _cancelButton); Button.ClickEvent.AddHandler(OnButtonClicked, _confirmButton, _cancelButton);
} }
private async void OnButtonClicked(object sender, RoutedEventArgs e) private void OnButtonClicked(object sender, RoutedEventArgs e)
{ {
if (!HandleAsyncCommand) _popup?.SetValue(Popup.IsOpenProperty, false); if (!HandleAsyncCommand) _popup?.SetValue(Popup.IsOpenProperty, false);
// This is a hack for MVVM toolkit and Prism that uses INotifyPropertyChanged for async command. It counts the number of // This is a hack for MVVM toolkit and Prism that uses INotifyPropertyChanged for async command. It counts the number of
// IsRunning property changes to determine when the command is finished. // IsRunning property changes to determine when the command is finished.
if (sender is Button button && button.Command is { } command and (INotifyPropertyChanged or IDisposable)) if (sender is Button { Command: { } command and (INotifyPropertyChanged or IDisposable) } button)
{ {
var count = 0; var count = 0;
void OnCanExecuteChanged(object? _, System.EventArgs e) void OnCanExecuteChanged(object? _, System.EventArgs a)
{ {
count++; count++;
if (count != 2) return; if (count != 2) return;
@@ -212,20 +211,15 @@ public class PopConfirm : ContentControl
{ {
var result = base.RegisterContentPresenter(presenter); var result = base.RegisterContentPresenter(presenter);
if (result) if (result)
{
_childChangeDisposable = presenter.GetPropertyChangedObservable(ContentPresenter.ChildProperty) _childChangeDisposable = presenter.GetPropertyChangedObservable(ContentPresenter.ChildProperty)
.Subscribe(OnChildChanged); .Subscribe(OnChildChanged);
}
return result; return result;
} }
private void OnChildChanged(AvaloniaPropertyChangedEventArgs arg) private void OnChildChanged(AvaloniaPropertyChangedEventArgs arg)
{ {
TeardownChildrenEventSubscriptions(arg.GetOldValue<Control?>(), TriggerMode); TeardownChildrenEventSubscriptions(arg.GetOldValue<Control?>());
SetupChildrenEventSubscriptions(arg.GetNewValue<Control?>(), TriggerMode); SetupChildrenEventSubscriptions(arg.GetNewValue<Control?>(), TriggerMode);
if (arg.GetNewValue<Control?>() is null) return;
if (arg.GetNewValue<Control?>() is Button button)
button.Click += (o, e) => { SetCurrentValue(IsDropdownOpenProperty, true); };
} }
private void SetupChildrenEventSubscriptions(Control? child, PopConfirmTriggerMode mode) private void SetupChildrenEventSubscriptions(Control? child, PopConfirmTriggerMode mode)
@@ -257,7 +251,7 @@ public class PopConfirm : ContentControl
SetCurrentValue(IsDropdownOpenProperty, true); SetCurrentValue(IsDropdownOpenProperty, true);
} }
private void TeardownChildrenEventSubscriptions(Control? child, PopConfirmTriggerMode mode) private void TeardownChildrenEventSubscriptions(Control? child)
{ {
if (child is null) return; if (child is null) return;
PointerPressedEvent.RemoveHandler(OnMainElementPressed, child); PointerPressedEvent.RemoveHandler(OnMainElementPressed, child);