XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
using XFEWarframe.Desktop.Editing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Windows.Storage.Pickers;
using WinRT.Interop;
using XFEExtension.NetCore.WinUIHelper.Implements.Services;
using XFEExtension.NetCore.WinUIHelper.Interface.Services;
using XFEExtension.NetCore.WinUIHelper.Utilities;
using XFEWarframe.Player.Interface.Services;

namespace XFEWarframe.Player.Implements.Services;

public sealed class InteractionService : GlobalServiceBase, IInteractionService
{
    private readonly IDialogService dialogs = ServiceManager.GetService<IDialogService>();
    private readonly SemaphoreSlim dialogGate = new(1, 1);

    public async Task<bool> ConfirmAsync(string title, string message) =>
        await ShowAsync(new ContentDialog
        {
            Title = title, Content = new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap },
            PrimaryButtonText = UiText.Translate("确认"), CloseButtonText = UiText.Translate("取消"), DefaultButton = ContentDialogButton.Close
        }) == ContentDialogResult.Primary;

    public async Task<ContentDialogResult> ShowAsync(ContentDialog dialog)
    {
        await dialogGate.WaitAsync(App.Lifetime);
        try
        {
            App.Lifetime.ThrowIfCancellationRequested();
            dialog.Name = "ActiveDialog";
            dialog.XamlRoot = ((FrameworkElement)App.MainWindow.Content).XamlRoot;
            dialog.RequestedTheme = ((FrameworkElement)App.MainWindow.Content).ActualTheme;
            dialogs.DialogDictionary[dialog.Name] = dialog;
            using var cancellation = App.Lifetime.Register(() => App.MainWindow.DispatcherQueue.TryEnqueue(dialog.Hide));
            return await dialogs.ShowDialog(dialog.Name);
        }
        finally { dialogs.DialogDictionary.Remove("ActiveDialog"); dialogGate.Release(); }
    }

    public async Task<string?> PickFolderAsync()
    {
        var picker = new FolderPicker();
        picker.FileTypeFilter.Add("*");
        InitializeWithWindow.Initialize(picker, WindowNative.GetWindowHandle(App.MainWindow));
        return (await picker.PickSingleFolderAsync())?.Path;
    }

    public async Task<string?> OpenJsonAsync()
    {
        var picker = new FileOpenPicker();
        picker.FileTypeFilter.Add(".json");
        InitializeWithWindow.Initialize(picker, WindowNative.GetWindowHandle(App.MainWindow));
        return (await picker.PickSingleFileAsync())?.Path;
    }

    public async Task<string?> SaveJsonAsync(string suggestedName)
    {
        var picker = new FileSavePicker { SuggestedFileName = suggestedName };
        picker.FileTypeChoices.Add(UiText.Translate("JSON 文件"), new List<string> { ".json" });
        InitializeWithWindow.Initialize(picker, WindowNative.GetWindowHandle(App.MainWindow));
        return (await picker.PickSaveFileAsync())?.Path;
    }
}