1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 20:07:24 +08:00
osu-lazer/osu.Game/Screens/Edit/Setup/FileChooserLabelledTextBox.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
2.9 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Database;
using osu.Game.Graphics.UserInterfaceV2;
using osuTK;
namespace osu.Game.Screens.Edit.Setup
{
/// <summary>
/// A labelled textbox which reveals an inline file chooser when clicked.
/// </summary>
internal class FileChooserLabelledTextBox : LabelledTextBoxWithPopover, ICanAcceptFiles
{
private readonly string[] handledExtensions;
public IEnumerable<string> HandledExtensions => handledExtensions;
private readonly Bindable<FileInfo?> currentFile = new Bindable<FileInfo?>();
[Resolved]
private OsuGameBase game { get; set; } = null!;
public FileChooserLabelledTextBox(params string[] handledExtensions)
{
this.handledExtensions = handledExtensions;
}
protected override void LoadComplete()
{
base.LoadComplete();
game.RegisterImportHandler(this);
currentFile.BindValueChanged(onFileSelected);
}
private void onFileSelected(ValueChangedEvent<FileInfo?> file)
{
if (file.NewValue == null)
return;
2021-08-23 23:01:01 +08:00
this.HidePopover();
Current.Value = file.NewValue.FullName;
}
Task ICanAcceptFiles.Import(params string[] paths)
{
Schedule(() => currentFile.Value = new FileInfo(paths.First()));
return Task.CompletedTask;
}
Task ICanAcceptFiles.Import(params ImportTask[] tasks) => throw new NotImplementedException();
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (game.IsNotNull())
game.UnregisterImportHandler(this);
}
public override Popover GetPopover() => new FileChooserPopover(handledExtensions, currentFile);
private class FileChooserPopover : OsuPopover
{
public FileChooserPopover(string[] handledExtensions, Bindable<FileInfo?> currentFile)
{
Child = new Container
{
Size = new Vector2(600, 400),
Child = new OsuFileSelector(currentFile.Value?.DirectoryName, handledExtensions)
{
RelativeSizeAxes = Axes.Both,
CurrentFile = { BindTarget = currentFile }
},
};
}
}
}
}