Skip to content

Commit

Permalink
Fix Icon Button for cases where command also toggles icon
Browse files Browse the repository at this point in the history
Ensure that always the correct icon will be used when not overwritten by progress
  • Loading branch information
b-straub committed Jul 7, 2024
1 parent c60b41a commit cf096c4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
13 changes: 9 additions & 4 deletions RxMudBlazorLight/ButtonBase/ButtonBaseRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,23 @@ public RenderFragment RenderProgress() => builder =>
return (startIcon, endIcon, label);
}

public string GetIconButtonParameters(IStateCommandAsync stateCommand, string icon, MBIconVariant? iconVariant)
public void SetButtonIcon(string icon)
{
_buttonIcon = icon;
}

public string GetIconButtonParameters(IStateCommandAsync stateCommand, MBIconVariant? iconVariant)
{
ArgumentNullException.ThrowIfNull(_buttonIcon);
var icon = _buttonIcon;

if (_iconForState is IconForState.None)
{
_iconForState = IconForState.Start;
_buttonIcon = icon;
}

if (!stateCommand.Changing() || stateCommand.ChangeCallerID != _id)
{
ArgumentNullException.ThrowIfNull(_buttonIcon);
icon = _buttonIcon;
_iconForState = IconForState.None;
}
else
Expand Down
8 changes: 7 additions & 1 deletion RxMudBlazorLight/IconButtons/MudIconButtonAsyncRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@
{
ArgumentNullException.ThrowIfNull(_buttonRx);
ArgumentNullException.ThrowIfNull(Icon);

if (!StateCommand.Changing())
{
_buttonRx.SetButtonIcon(Icon);
}

_buttonRx.SetParameter(StateCommand, ExecuteAsyncCallback, CanChangeCallback, DeferredNotification);

Icon = _buttonRx.GetIconButtonParameters(StateCommand, Icon, IconVariant);
Icon = _buttonRx.GetIconButtonParameters(StateCommand, IconVariant);
Color = _buttonRx.Color;
OnClick = (EventCallback<MouseEventArgs>)_buttonRx.OnClick;
Disabled = _buttonRx.Disabled;
Expand Down
21 changes: 14 additions & 7 deletions RxMudBlazorLightTestBase/Components/IconButtons.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
@inherits RxBLServiceSubscriber<TestService>

<MudStack Row=@true AlignItems=@AlignItems.Center>
<MudIconButtonRx Color="Color.Primary" Icon="@Icons.Material.Filled.PlusOne" StateCommand=@Service.Command ExecuteCallback=@Service.IncrementCounter />
<MudIconButtonRx Color="Color.Primary" Icon="@Icons.Material.Filled.Add" StateCommand=@Service.Command ExecuteCallback=@(Service.AddToCounter(5)) />
<MudIconButtonAsyncRx Color="Color.Secondary" Icon="@Icons.Material.Filled.PlusOne" StateCommand=@Service.CommandAsync ExecuteAsyncCallback=@Service.IncrementCounterAsync CanChangeCallback=@(Service.CounterCanChangeLowerBound(4)) />
<MudIconButtonRx Color="Color.Primary" Icon="@Icons.Material.Filled.PlusOne" StateCommand=@Service.Command ExecuteCallback=@Service.IncrementCounter/>
<MudIconButtonRx Color="Color.Primary" Icon="@Icons.Material.Filled.Add" StateCommand=@Service.Command ExecuteCallback=@(Service.AddToCounter(5))/>
<MudIconButtonAsyncRx Color="Color.Secondary" Icon="@Icons.Material.Filled.PlusOne" StateCommand=@Service.CommandAsync ExecuteAsyncCallback=@Service.IncrementCounterAsync CanChangeCallback=@(Service.CounterCanChangeLowerBound(4))/>
<MudIconButtonAsyncRx Color="Color.Secondary" Icon=@GetAddModeIcon() StateCommand=@Service.CommandAsync ExecuteAsyncCallback=@Service.ToggleAddModeDelayedAsync />
</MudStack>

<MudStack>
Expand All @@ -12,13 +13,19 @@
</MudStack>

<MudStack Row=@true AlignItems=@AlignItems.Center>
<MudIconButtonRx Color="Color.Secondary" Variant=@Variant.Filled Icon="@Icons.Material.Filled.Add" StateCommand=@Scope.Command ExecuteCallback=@(TestService.IncrementCounterIndirect(Scope.Counter, v => Scope.Counter = v)) />
<MudIconButtonAsyncRx Color="Color.Secondary" Variant=@Variant.Filled Icon="@Icons.Material.Filled.Add" StateCommand=@Scope.CommandAsync ExecuteAsyncCallback=@Scope.IncrementCounterAsync />
<MudIconButtonAsyncRx Color="Color.Secondary" Variant=@Variant.Filled Icon="@Icons.Material.Filled.Add" StateCommand=@Scope.CancellableCommandAsync ExecuteAsyncCallback=@(Scope.AddToCounterAsync(5)) />
<MudIconButtonRx Color="Color.Secondary" Variant=@Variant.Filled Icon="@Icons.Material.Filled.Add" StateCommand=@Scope.Command ExecuteCallback=@(TestService.IncrementCounterIndirect(Scope.Counter, v => Scope.Counter = v))/>
<MudIconButtonAsyncRx Color="Color.Secondary" Variant=@Variant.Filled Icon="@Icons.Material.Filled.Add" StateCommand=@Scope.CommandAsync ExecuteAsyncCallback=@Scope.IncrementCounterAsync/>
<MudIconButtonAsyncRx Color="Color.Secondary" Variant=@Variant.Filled Icon="@Icons.Material.Filled.Add" StateCommand=@Scope.CancellableCommandAsync ExecuteAsyncCallback=@(Scope.AddToCounterAsync(5))/>
</MudStack>
</MudStack>

@code {

[CascadingParameter]
public required TestService.Scope Scope { get; init; }
}

private string GetAddModeIcon()
{
return Service.AddMode.Value ? Icons.Material.Filled.ExposurePlus1 : Icons.Material.Filled.ExposureNeg1;
}
}
11 changes: 11 additions & 0 deletions RxMudBlazorLightTestBase/Service/TestService.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ public bool AddModeCanChange()
{
return !Command.Changing() && !CommandAsync.Changing();
}

public Func<IStateCommandAsync, Task> ToggleAddModeDelayedAsync => async _ =>
{
await Task.Delay(1000);
Observable.Interval(TimeSpan.FromMilliseconds(1000))
.Take(1)
.Subscribe(_ =>
{
AddMode.Value = !AddMode.Value;
});
};

public bool IncrementStateCanChangeCheck()
{
Expand Down

0 comments on commit cf096c4

Please sign in to comment.