LANCommander/LANCommander.Launcher/UI/Components/PlayButton.razor
Pat Hartl 5a32bbf890 Add localization support to launcher
Adds localization across the launcher and includes English, German, Spanish, French, Italian, Japanese, Korean, Dutch, Portuguese, Ukranian, and Chinese. These localizations were generated by Claude 3.5 and need to be reviewed by a human for accuracy.
2025-08-18 21:29:38 -05:00

176 lines
No EOL
5.4 KiB
Text

@implements IDisposable
@using LANCommander.Launcher.Data.Models
@using LANCommander.Launcher.Models
@using System.Diagnostics
@using LANCommander.SDK.Helpers
@inject SDK.Client Client
@inject GameService GameService
@inject LibraryService LibraryService
@inject InstallService InstallService
@inject ModalService ModalService
@inject MessageService MessageService
@inject MessageBusService MessageBusService
@inject LocalizationService LocalizationService
@if (State == PlayButtonState.Running)
{
<Popconfirm Title="@LocalizationService.GetString("DoYouWantToStopGame")" OkText="@LocalizationService.GetString("Yes")" OnConfirm="() => Stop()">
<Button Type="ButtonType.Primary" Size="ButtonSize.Large" Icon="@IconType.Outline.CaretRight">@LocalizationService.GetString("Running")</Button>
</Popconfirm>
}
else if (State == PlayButtonState.Starting)
{
<Button Type="ButtonType.Primary" Size="ButtonSize.Large" Loading="true">@LocalizationService.GetString("Starting")</Button>
}
else if (State == PlayButtonState.Stopping)
{
<Button Type="ButtonType.Primary" Size="ButtonSize.Large" Loading="true">@LocalizationService.GetString("Stopping")</Button>
}
else
{
<Space Direction="SpaceDirection.Horizontal">
<SpaceItem>
<Button Type="ButtonType.Primary" Size="ButtonSize.Large" Icon="@IconType.Outline.CaretRight" OnClick="() => Run()">@LocalizationService.GetString("Play")</Button>
</SpaceItem>
<SpaceItem>
<Dropdown>
<Overlay>
<Menu>
<LibraryItemContextMenu Model="LibraryItem" MenuExtra="MenuExtra" />
</Menu>
</Overlay>
<ChildContent>
<Button Type="@ButtonType.Default" Size="ButtonSize.Large" Icon="@IconType.Outline.Ellipsis" />
</ChildContent>
</Dropdown>
</SpaceItem>
</Space>
}
@code {
[Parameter] public Guid LibraryItemId { get; set; }
[Parameter] public Models.ListItem LibraryItem { get; set; }
[Parameter] public RenderFragment MenuExtra { get; set; }
Timer StateTimer;
Settings Settings;
PlayButtonState State = PlayButtonState.Idle;
public enum PlayButtonState
{
Idle,
Starting,
Running,
Stopping
}
protected override async Task OnInitializedAsync()
{
StateTimer = new Timer(state => { InvokeAsync(UpdateState); }, null, 0, 250);
Settings = SettingService.GetSettings();
}
protected override async Task OnParametersSetAsync()
{
if (LibraryItem == null && LibraryItemId != Guid.Empty)
LibraryItem = await LibraryService.GetItemAsync(LibraryItemId);
}
async Task UpdateState()
{
if (Client.Games.IsRunning(LibraryItem.Key))
{
await ChangeState(PlayButtonState.Running);
}
else if (State == PlayButtonState.Running && !Client.Games.IsRunning(LibraryItem.Key))
{
await ChangeState(PlayButtonState.Idle);
}
}
async Task ChangeState(PlayButtonState state)
{
State = state;
await InvokeAsync(StateHasChanged);
}
async Task Run()
{
var game = LibraryItem.DataItem as Game;
var actions = await Client.Games.GetActionsAsync(game.InstallDirectory, game.Id);
await ChangeState(PlayButtonState.Starting);
if (actions == null || actions.Count() == 0)
{
MessageService.Error(LocalizationService.GetString("NoActionsFound"));
await ChangeState(PlayButtonState.Idle);
return;
}
var primaryActions = actions.Where(a => a.IsPrimaryAction && (LibraryService.IsInstalled(a.GameId) || a.GameId == Guid.Empty));
if (primaryActions.Count() == 0)
{
MessageService.Error(LocalizationService.GetString("NoPrimaryActionFound"));
await ChangeState(PlayButtonState.Idle);
return;
}
if (primaryActions.Count() == 1)
{
var task = GameService.Run(game, primaryActions.First());
await InvokeAsync(StateHasChanged);
await task;
}
else
{
var modalOptions = new ModalOptions()
{
Title = LocalizationService.GetString("PlayGame", game.Title),
Maximizable = false,
DefaultMaximized = false,
Closable = true,
MaskClosable = true,
Footer = null,
Centered = true,
WrapClassName = "game-actions-dialog",
AfterClose = async () =>
{
Client.Lobbies.ReleaseSteam();
await ChangeState(PlayButtonState.Idle);
}
};
var model = new ActionSelectorDialogOptions
{
Actions = actions.Where(a => a.IsPrimaryAction),
Game = game
};
var modalRef = ModalService.CreateModal<ActionSelectorDialog, ActionSelectorDialogOptions, Process>(modalOptions, model);
}
}
async Task Stop()
{
await ChangeState(PlayButtonState.Stopping);
await Client.Games.Stop(LibraryItem.Key);
await ChangeState(PlayButtonState.Idle);
}
public void Dispose()
{
StateTimer.Dispose();
}
}