1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 16:12:54 +08:00

Extract external link opening to the ExternalLinkOpener class

This commit is contained in:
Roman Kapustin 2018-10-23 23:03:00 +03:00
parent 805f8d98c4
commit 7401fabb5d
3 changed files with 46 additions and 18 deletions

View File

@ -7,11 +7,7 @@ using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Sprites;
using System.Collections.Generic;
using osu.Framework.Configuration;
using osu.Framework.Platform;
using osu.Game.Configuration;
using osu.Game.Overlays;
using osu.Game.Overlays.Chat;
using osu.Game.Overlays.Notifications;
namespace osu.Game.Graphics.Containers
@ -26,20 +22,12 @@ namespace osu.Game.Graphics.Containers
private OsuGame game;
private Action showNotImplementedError;
private GameHost host;
private DialogOverlay dialogOverlay;
private Bindable<bool> warnAboutOpeningExternal;
[BackgroundDependencyLoader(true)]
private void load(OsuGame game, NotificationOverlay notifications, GameHost host, DialogOverlay dialogOverlay, OsuConfigManager config)
private void load(OsuGame game, NotificationOverlay notifications)
{
// will be null in tests
this.game = game;
this.host = host;
this.dialogOverlay = dialogOverlay;
warnAboutOpeningExternal = config.GetBindable<bool>(OsuSetting.WarnAboutOpeningExternalLink);
showNotImplementedError = () => notifications?.Post(new SimpleNotification
{
@ -97,11 +85,7 @@ namespace osu.Game.Graphics.Containers
showNotImplementedError?.Invoke();
break;
case LinkAction.External:
void externalAction() => host.OpenUrlExternally(url);
if (warnAboutOpeningExternal)
dialogOverlay.Push(new ExternalLinkDialog(url, externalAction));
else
externalAction();
game?.OpenUrlExternally(url);
break;
case LinkAction.OpenUserProfile:
if (long.TryParse(linkArgument, out long userId))

View File

@ -0,0 +1,38 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Platform;
using osu.Game.Configuration;
using osu.Game.Overlays;
using osu.Game.Overlays.Chat;
namespace osu.Game.Online.Chat
{
public class ExternalLinkOpener : Component
{
private GameHost host;
private DialogOverlay dialogOverlay;
private Bindable<bool> warnAboutOpeningExternal;
[BackgroundDependencyLoader(true)]
private void load(GameHost host, DialogOverlay dialogOverlay, OsuConfigManager config)
{
this.host = host;
this.dialogOverlay = dialogOverlay;
warnAboutOpeningExternal = config.GetBindable<bool>(OsuSetting.WarnAboutOpeningExternalLink);
}
public void OpenUrlExternally(string url)
{
void externalAction() => host.OpenUrlExternally(url);
if (warnAboutOpeningExternal)
dialogOverlay.Push(new ExternalLinkDialog(url, externalAction));
else
externalAction();
}
}
}

View File

@ -31,6 +31,7 @@ using osu.Game.Overlays.Notifications;
using osu.Game.Rulesets;
using osu.Game.Screens.Play;
using osu.Game.Input.Bindings;
using osu.Game.Online.Chat;
using osu.Game.Rulesets.Mods;
using osu.Game.Skinning;
using OpenTK.Graphics;
@ -103,6 +104,9 @@ namespace osu.Game
private readonly List<OverlayContainer> overlays = new List<OverlayContainer>();
private ExternalLinkOpener externalLinkOpener;
public void OpenUrlExternally(string url) => externalLinkOpener.OpenUrlExternally(url);
// todo: move this to SongSelect once Screen has the ability to unsuspend.
[Cached]
[Cached(Type = typeof(IBindable<IEnumerable<Mod>>))]
@ -383,6 +387,8 @@ namespace osu.Game
dependencies.Cache(notifications);
dependencies.Cache(dialogOverlay);
Add(externalLinkOpener = new ExternalLinkOpener());
var singleDisplaySideOverlays = new OverlayContainer[] { settings, notifications };
overlays.AddRange(singleDisplaySideOverlays);