osu/osu.Game/Online/Chat/ExternalLinkOpener.cs

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

70 lines
2.4 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.
2022-08-24 08:39:22 +00:00
using System;
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
2022-08-24 08:39:22 +00:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Platform;
using osu.Game.Configuration;
using osu.Game.Overlays;
2022-08-24 08:39:22 +00:00
using osu.Game.Overlays.Dialog;
namespace osu.Game.Online.Chat
{
public class ExternalLinkOpener : Component
{
2020-02-14 15:02:10 +00:00
[Resolved]
2022-08-24 08:39:22 +00:00
private GameHost host { get; set; } = null!;
2020-02-14 13:14:00 +00:00
2020-02-14 13:59:51 +00:00
[Resolved(CanBeNull = true)]
2022-08-24 08:39:22 +00:00
private IDialogOverlay? dialogOverlay { get; set; }
2020-02-14 13:14:00 +00:00
2022-08-24 08:39:22 +00:00
private Bindable<bool> externalLinkWarning = null!;
[BackgroundDependencyLoader(true)]
2020-02-14 13:14:00 +00:00
private void load(OsuConfigManager config)
{
2018-11-01 20:52:07 +00:00
externalLinkWarning = config.GetBindable<bool>(OsuSetting.ExternalLinkWarning);
}
public void OpenUrlExternally(string url, bool bypassWarning = false)
{
2022-08-24 08:39:22 +00:00
if (!bypassWarning && externalLinkWarning.Value && dialogOverlay != null)
dialogOverlay.Push(new ExternalLinkDialog(url, () => host.OpenUrlExternally(url), () => host.GetClipboard()?.SetText(url)));
else
2018-11-01 20:52:07 +00:00
host.OpenUrlExternally(url);
}
2022-08-24 08:39:22 +00:00
public class ExternalLinkDialog : PopupDialog
{
public ExternalLinkDialog(string url, Action openExternalLinkAction, Action copyExternalLinkAction)
{
HeaderText = "Just checking...";
BodyText = $"You are about to leave osu! and open the following link in a web browser:\n\n{url}";
Icon = FontAwesome.Solid.ExclamationTriangle;
Buttons = new PopupDialogButton[]
{
new PopupDialogOkButton
{
Text = @"Yes. Go for it.",
Action = openExternalLinkAction
},
new PopupDialogCancelButton
{
Text = @"Copy URL to the clipboard instead.",
Action = copyExternalLinkAction
},
new PopupDialogCancelButton
{
Text = @"No! Abort mission!"
},
};
}
}
}
}