1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 14:57:25 +08:00
osu-lazer/osu.Game/Online/Chat/ExternalLinkOpener.cs

75 lines
2.5 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 16:39:22 +08:00
using System;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
2022-08-24 16:39:22 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Platform;
using osu.Game.Configuration;
2024-05-31 10:44:53 +08:00
using osu.Game.Localisation;
using osu.Game.Overlays;
2022-08-24 16:39:22 +08:00
using osu.Game.Overlays.Dialog;
2024-05-31 10:44:53 +08:00
using CommonStrings = osu.Game.Resources.Localisation.Web.CommonStrings;
namespace osu.Game.Online.Chat
{
2022-11-24 13:32:20 +08:00
public partial class ExternalLinkOpener : Component
{
2020-02-14 23:02:10 +08:00
[Resolved]
2022-08-24 16:39:22 +08:00
private GameHost host { get; set; } = null!;
2020-02-14 21:14:00 +08:00
2023-07-11 17:42:31 +08:00
[Resolved]
private Clipboard clipboard { get; set; } = null!;
2020-02-14 21:59:51 +08:00
[Resolved(CanBeNull = true)]
2022-08-24 16:39:22 +08:00
private IDialogOverlay? dialogOverlay { get; set; }
2020-02-14 21:14:00 +08:00
2022-08-24 16:39:22 +08:00
private Bindable<bool> externalLinkWarning = null!;
[BackgroundDependencyLoader(true)]
2020-02-14 21:14:00 +08:00
private void load(OsuConfigManager config)
{
2018-11-02 04:52:07 +08:00
externalLinkWarning = config.GetBindable<bool>(OsuSetting.ExternalLinkWarning);
}
public void OpenUrlExternally(string url, bool bypassWarning = false)
{
2022-08-24 16:39:22 +08:00
if (!bypassWarning && externalLinkWarning.Value && dialogOverlay != null)
2023-07-11 17:42:31 +08:00
dialogOverlay.Push(new ExternalLinkDialog(url, () => host.OpenUrlExternally(url), () => clipboard.SetText(url)));
else
2018-11-02 04:52:07 +08:00
host.OpenUrlExternally(url);
}
2022-08-24 16:39:22 +08:00
2022-11-24 13:32:20 +08:00
public partial class ExternalLinkDialog : PopupDialog
2022-08-24 16:39:22 +08:00
{
public ExternalLinkDialog(string url, Action openExternalLinkAction, Action copyExternalLinkAction)
{
HeaderText = DialogStrings.Caution;
BodyText = $"Are you sure you want to open the following link in a web browser?\n\n{url}";
2022-08-24 16:39:22 +08:00
Icon = FontAwesome.Solid.ExclamationTriangle;
Buttons = new PopupDialogButton[]
{
new PopupDialogOkButton
{
Text = @"Open in browser",
2022-08-24 16:39:22 +08:00
Action = openExternalLinkAction
},
new PopupDialogCancelButton
{
Text = @"Copy URL to the clipboard",
2022-08-24 16:39:22 +08:00
Action = copyExternalLinkAction
},
new PopupDialogCancelButton
{
Text = CommonStrings.ButtonsCancel,
2022-08-24 16:39:22 +08:00
},
};
}
}
}
}