feat: expose AddNotification API on UiBuilder

This commit is contained in:
goat 2021-08-24 14:39:39 +02:00
parent 66daa03da3
commit 33cbad9f16
No known key found for this signature in database
GPG key ID: F18F057873895461
7 changed files with 90 additions and 78 deletions

View file

@ -13,6 +13,7 @@ using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling; using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads; using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Interface.Internal; using Dalamud.Interface.Internal;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.IoC; using Dalamud.IoC;
using Dalamud.IoC.Internal; using Dalamud.IoC.Internal;
using Dalamud.Plugin.Internal; using Dalamud.Plugin.Internal;
@ -252,7 +253,7 @@ namespace Dalamud.Game
var configuration = Service<DalamudConfiguration>.Get(); var configuration = Service<DalamudConfiguration>.Get();
var pluginManager = Service<PluginManager>.Get(); var pluginManager = Service<PluginManager>.Get();
var dalamudInterface = Service<DalamudInterface>.Get(); var dalamudInterface = Service<DalamudInterface>.Get();
var notifications = Service<Notifications>.Get(); var notifications = Service<NotificationManager>.Get();
var assemblyVersion = Assembly.GetAssembly(typeof(ChatHandlers)).GetName().Version.ToString(); var assemblyVersion = Assembly.GetAssembly(typeof(ChatHandlers)).GetName().Version.ToString();
@ -298,7 +299,7 @@ namespace Dalamud.Game
if (configuration.AutoUpdatePlugins) if (configuration.AutoUpdatePlugins)
{ {
pluginManager.PrintUpdatedPlugins(updatedPlugins, Loc.Localize("DalamudPluginAutoUpdate", "Auto-update:")); pluginManager.PrintUpdatedPlugins(updatedPlugins, Loc.Localize("DalamudPluginAutoUpdate", "Auto-update:"));
notifications.AddNotification(Loc.Localize("NotificationUpdatedPlugins", "{0} of your plugins were updated.").Format(updatedPlugins.Count), Loc.Localize("NotificationAutoUpdate", "Auto-Update"), Notifications.Notification.Type.Info); notifications.AddNotification(Loc.Localize("NotificationUpdatedPlugins", "{0} of your plugins were updated.").Format(updatedPlugins.Count), Loc.Localize("NotificationAutoUpdate", "Auto-Update"), NotificationType.Info);
} }
else else
{ {

View file

@ -15,6 +15,7 @@ using Dalamud.Game.Gui.Internal;
using Dalamud.Game.Internal.DXGI; using Dalamud.Game.Internal.DXGI;
using Dalamud.Hooking; using Dalamud.Hooking;
using Dalamud.Hooking.Internal; using Dalamud.Hooking.Internal;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Utility; using Dalamud.Utility;
using ImGuiNET; using ImGuiNET;
using ImGuiScene; using ImGuiScene;
@ -58,7 +59,7 @@ namespace Dalamud.Interface.Internal
/// </summary> /// </summary>
public InterfaceManager() public InterfaceManager()
{ {
Service<Notifications>.Set(); Service<NotificationManager>.Set();
var scanner = Service<SigScanner>.Get(); var scanner = Service<SigScanner>.Get();
@ -150,11 +151,6 @@ namespace Dalamud.Interface.Internal
/// </summary> /// </summary>
public static ImFontPtr MonoFont { get; private set; } public static ImFontPtr MonoFont { get; private set; }
/// <summary>
/// Gets the manager for notifications/toasts.
/// </summary>
public Notifications Notifications { get; init; } = new();
/// <summary> /// <summary>
/// Gets or sets an action that is exexuted when fonts are rebuilt. /// Gets or sets an action that is exexuted when fonts are rebuilt.
/// </summary> /// </summary>
@ -644,7 +640,7 @@ namespace Dalamud.Interface.Internal
this.lastWantCapture = this.LastImGuiIoPtr.WantCaptureMouse; this.lastWantCapture = this.LastImGuiIoPtr.WantCaptureMouse;
this.Draw?.Invoke(); this.Draw?.Invoke();
Service<Notifications>.Get().Draw(); Service<NotificationManager>.Get().Draw();
} }
} }
} }

View file

@ -7,13 +7,13 @@ using Dalamud.Interface.Colors;
using Dalamud.Utility; using Dalamud.Utility;
using ImGuiNET; using ImGuiNET;
namespace Dalamud.Interface.Internal namespace Dalamud.Interface.Internal.Notifications
{ {
/// <summary> /// <summary>
/// Class handling notifications/toasts in ImGui. /// Class handling notifications/toasts in ImGui.
/// Ported from https://github.com/patrickcjk/imgui-notify. /// Ported from https://github.com/patrickcjk/imgui-notify.
/// </summary> /// </summary>
internal class Notifications internal class NotificationManager
{ {
/// <summary> /// <summary>
/// Value indicating the bottom-left X padding. /// Value indicating the bottom-left X padding.
@ -61,7 +61,7 @@ namespace Dalamud.Interface.Internal
/// <param name="title">The title of the notification.</param> /// <param name="title">The title of the notification.</param>
/// <param name="type">The type of the notification.</param> /// <param name="type">The type of the notification.</param>
/// <param name="msDelay">The time the notification should be displayed for.</param> /// <param name="msDelay">The time the notification should be displayed for.</param>
public void AddNotification(string content, string title = null, Notification.Type type = Notification.Type.None, int msDelay = NotifyDefaultDismiss) public void AddNotification(string content, string? title = null, NotificationType type = NotificationType.None, uint msDelay = NotifyDefaultDismiss)
{ {
this.notifications.Add(new Notification this.notifications.Add(new Notification
{ {
@ -170,37 +170,6 @@ namespace Dalamud.Interface.Internal
/// </summary> /// </summary>
internal class Notification internal class Notification
{ {
/// <summary>
/// Possible notification types.
/// </summary>
public enum Type
{
/// <summary>
/// No special type.
/// </summary>
None,
/// <summary>
/// Type indicating success.
/// </summary>
Success,
/// <summary>
/// Type indicating a warning.
/// </summary>
Warning,
/// <summary>
/// Type indicating an error.
/// </summary>
Error,
/// <summary>
/// Type indicating generic information.
/// </summary>
Info,
}
/// <summary> /// <summary>
/// Possible notification phases. /// Possible notification phases.
/// </summary> /// </summary>
@ -230,12 +199,12 @@ namespace Dalamud.Interface.Internal
/// <summary> /// <summary>
/// Gets the type of the notification. /// Gets the type of the notification.
/// </summary> /// </summary>
internal Type NotificationType { get; init; } internal NotificationType NotificationType { get; init; }
/// <summary> /// <summary>
/// Gets the title of the notification. /// Gets the title of the notification.
/// </summary> /// </summary>
internal string Title { get; init; } internal string? Title { get; init; }
/// <summary> /// <summary>
/// Gets the content of the notification. /// Gets the content of the notification.
@ -245,7 +214,7 @@ namespace Dalamud.Interface.Internal
/// <summary> /// <summary>
/// Gets the duration of the notification in milliseconds. /// Gets the duration of the notification in milliseconds.
/// </summary> /// </summary>
internal int DurationMs { get; init; } internal uint DurationMs { get; init; }
/// <summary> /// <summary>
/// Gets the creation time of the notification. /// Gets the creation time of the notification.
@ -258,11 +227,11 @@ namespace Dalamud.Interface.Internal
/// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="NotificationType"/> is set to an out-of-range value.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="NotificationType"/> is set to an out-of-range value.</exception>
internal Vector4 Color => this.NotificationType switch internal Vector4 Color => this.NotificationType switch
{ {
Type.None => ImGuiColors.DalamudWhite, NotificationType.None => ImGuiColors.DalamudWhite,
Type.Success => ImGuiColors.HealerGreen, NotificationType.Success => ImGuiColors.HealerGreen,
Type.Warning => ImGuiColors.DalamudOrange, NotificationType.Warning => ImGuiColors.DalamudOrange,
Type.Error => ImGuiColors.DalamudRed, NotificationType.Error => ImGuiColors.DalamudRed,
Type.Info => ImGuiColors.TankBlue, NotificationType.Info => ImGuiColors.TankBlue,
_ => throw new ArgumentOutOfRangeException(), _ => throw new ArgumentOutOfRangeException(),
}; };
@ -272,11 +241,11 @@ namespace Dalamud.Interface.Internal
/// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="NotificationType"/> is set to an out-of-range value.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="NotificationType"/> is set to an out-of-range value.</exception>
internal string? Icon => this.NotificationType switch internal string? Icon => this.NotificationType switch
{ {
Type.None => null, NotificationType.None => null,
Type.Success => FontAwesomeIcon.CheckCircle.ToIconString(), NotificationType.Success => FontAwesomeIcon.CheckCircle.ToIconString(),
Type.Warning => FontAwesomeIcon.ExclamationCircle.ToIconString(), NotificationType.Warning => FontAwesomeIcon.ExclamationCircle.ToIconString(),
Type.Error => FontAwesomeIcon.TimesCircle.ToIconString(), NotificationType.Error => FontAwesomeIcon.TimesCircle.ToIconString(),
Type.Info => FontAwesomeIcon.InfoCircle.ToIconString(), NotificationType.Info => FontAwesomeIcon.InfoCircle.ToIconString(),
_ => throw new ArgumentOutOfRangeException(), _ => throw new ArgumentOutOfRangeException(),
}; };
@ -286,11 +255,11 @@ namespace Dalamud.Interface.Internal
/// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="NotificationType"/> is set to an out-of-range value.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="NotificationType"/> is set to an out-of-range value.</exception>
internal string? DefaultTitle => this.NotificationType switch internal string? DefaultTitle => this.NotificationType switch
{ {
Type.None => null, NotificationType.None => null,
Type.Success => Type.Success.ToString(), NotificationType.Success => NotificationType.Success.ToString(),
Type.Warning => Type.Warning.ToString(), NotificationType.Warning => NotificationType.Warning.ToString(),
Type.Error => Type.Error.ToString(), NotificationType.Error => NotificationType.Error.ToString(),
Type.Info => Type.Info.ToString(), NotificationType.Info => NotificationType.Info.ToString(),
_ => throw new ArgumentOutOfRangeException(), _ => throw new ArgumentOutOfRangeException(),
}; };

View file

@ -0,0 +1,33 @@
namespace Dalamud.Interface.Internal.Notifications
{
/// <summary>
/// Possible notification types.
/// </summary>
public enum NotificationType
{
/// <summary>
/// No special type.
/// </summary>
None,
/// <summary>
/// Type indicating success.
/// </summary>
Success,
/// <summary>
/// Type indicating a warning.
/// </summary>
Warning,
/// <summary>
/// Type indicating an error.
/// </summary>
Error,
/// <summary>
/// Type indicating generic information.
/// </summary>
Info,
}
}

View file

@ -22,6 +22,7 @@ using Dalamud.Game.Gui;
using Dalamud.Game.Gui.FlyText; using Dalamud.Game.Gui.FlyText;
using Dalamud.Game.Gui.Toast; using Dalamud.Game.Gui.Toast;
using Dalamud.Game.Text; using Dalamud.Game.Text;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Windowing; using Dalamud.Interface.Windowing;
using Dalamud.Memory; using Dalamud.Memory;
using Dalamud.Plugin; using Dalamud.Plugin;
@ -1056,6 +1057,7 @@ namespace Dalamud.Interface.Internal.Windows
private void DrawImGui() private void DrawImGui()
{ {
var interfaceManager = Service<InterfaceManager>.Get(); var interfaceManager = Service<InterfaceManager>.Get();
var notifications = Service<NotificationManager>.Get();
ImGui.Text("Monitor count: " + ImGui.GetPlatformIO().Monitors.Size); ImGui.Text("Monitor count: " + ImGui.GetPlatformIO().Monitors.Size);
ImGui.Text("OverrideGameCursor: " + interfaceManager.OverrideGameCursor); ImGui.Text("OverrideGameCursor: " + interfaceManager.OverrideGameCursor);
@ -1082,17 +1084,17 @@ namespace Dalamud.Interface.Internal.Windows
var type = rand.Next(0, 4) switch var type = rand.Next(0, 4) switch
{ {
0 => Notifications.Notification.Type.Error, 0 => Notifications.NotificationType.Error,
1 => Notifications.Notification.Type.Warning, 1 => Notifications.NotificationType.Warning,
2 => Notifications.Notification.Type.Info, 2 => Notifications.NotificationType.Info,
3 => Notifications.Notification.Type.Success, 3 => Notifications.NotificationType.Success,
4 => Notifications.Notification.Type.None, 4 => Notifications.NotificationType.None,
_ => Notifications.Notification.Type.None, _ => Notifications.NotificationType.None,
}; };
var text = "Bla bla bla bla bla bla bla bla bla bla bla.\nBla bla bla bla bla bla bla bla bla bla bla bla bla bla."; var text = "Bla bla bla bla bla bla bla bla bla bla bla.\nBla bla bla bla bla bla bla bla bla bla bla bla bla bla.";
interfaceManager.Notifications.AddNotification(text, title, type); notifications.AddNotification(text, title, type);
} }
} }

View file

@ -15,6 +15,7 @@ using Dalamud.Configuration.Internal;
using Dalamud.Game.Command; using Dalamud.Game.Command;
using Dalamud.Interface.Colors; using Dalamud.Interface.Colors;
using Dalamud.Interface.Components; using Dalamud.Interface.Components;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Windowing; using Dalamud.Interface.Windowing;
using Dalamud.Logging.Internal; using Dalamud.Logging.Internal;
using Dalamud.Plugin; using Dalamud.Plugin;
@ -321,9 +322,8 @@ namespace Dalamud.Interface.Internal.Windows
private void DrawUpdatePluginsButton() private void DrawUpdatePluginsButton()
{ {
var interfaceManager = Service<InterfaceManager>.Get();
var pluginManager = Service<PluginManager>.Get(); var pluginManager = Service<PluginManager>.Get();
var notifications = Service<Notifications>.Get(); var notifications = Service<NotificationManager>.Get();
var ready = pluginManager.PluginsReady && pluginManager.ReposReady; var ready = pluginManager.PluginsReady && pluginManager.ReposReady;
@ -379,11 +379,11 @@ namespace Dalamud.Interface.Internal.Windows
if (this.updatePluginCount > 0) if (this.updatePluginCount > 0)
{ {
pluginManager.PrintUpdatedPlugins(this.updatedPlugins, Locs.PluginUpdateHeader_Chatbox); pluginManager.PrintUpdatedPlugins(this.updatedPlugins, Locs.PluginUpdateHeader_Chatbox);
notifications.AddNotification(Locs.Notifications_UpdatesInstalled(this.updatePluginCount), Locs.Notifications_UpdatesInstalledTitle, Notifications.Notification.Type.Success); notifications.AddNotification(Locs.Notifications_UpdatesInstalled(this.updatePluginCount), Locs.Notifications_UpdatesInstalledTitle, NotificationType.Success);
} }
else if (this.updatePluginCount == 0) else if (this.updatePluginCount == 0)
{ {
notifications.AddNotification(Locs.Notifications_NoUpdatesFound, Locs.Notifications_NoUpdatesFoundTitle, Notifications.Notification.Type.Info); notifications.AddNotification(Locs.Notifications_NoUpdatesFound, Locs.Notifications_NoUpdatesFoundTitle, NotificationType.Info);
} }
} }
}); });
@ -898,8 +898,7 @@ namespace Dalamud.Interface.Internal.Windows
private void DrawAvailablePlugin(RemotePluginManifest manifest, int index) private void DrawAvailablePlugin(RemotePluginManifest manifest, int index)
{ {
var configuration = Service<DalamudConfiguration>.Get(); var configuration = Service<DalamudConfiguration>.Get();
var interfaceManager = Service<InterfaceManager>.Get(); var notifications = Service<NotificationManager>.Get();
var notifications = Service<Notifications>.Get();
var pluginManager = Service<PluginManager>.Get(); var pluginManager = Service<PluginManager>.Get();
var useTesting = pluginManager.UseTesting(manifest); var useTesting = pluginManager.UseTesting(manifest);
@ -976,11 +975,11 @@ namespace Dalamud.Interface.Internal.Windows
{ {
if (task.Result.State == PluginState.Loaded) if (task.Result.State == PluginState.Loaded)
{ {
notifications.AddNotification(Locs.Notifications_PluginInstalled(manifest.Name), Locs.Notifications_PluginInstalledTitle, Notifications.Notification.Type.Success); notifications.AddNotification(Locs.Notifications_PluginInstalled(manifest.Name), Locs.Notifications_PluginInstalledTitle, NotificationType.Success);
} }
else else
{ {
notifications.AddNotification(Locs.Notifications_PluginNotInstalled(manifest.Name), Locs.Notifications_PluginNotInstalledTitle, Notifications.Notification.Type.Error); notifications.AddNotification(Locs.Notifications_PluginNotInstalled(manifest.Name), Locs.Notifications_PluginNotInstalledTitle, NotificationType.Error);
this.ShowErrorModal(Locs.ErrorModal_InstallFail(manifest.Name)); this.ShowErrorModal(Locs.ErrorModal_InstallFail(manifest.Name));
} }
} }
@ -1248,7 +1247,7 @@ namespace Dalamud.Interface.Internal.Windows
private void DrawPluginControlButton(LocalPlugin plugin) private void DrawPluginControlButton(LocalPlugin plugin)
{ {
var configuration = Service<DalamudConfiguration>.Get(); var configuration = Service<DalamudConfiguration>.Get();
var notifications = Service<Notifications>.Get(); var notifications = Service<NotificationManager>.Get();
var pluginManager = Service<PluginManager>.Get(); var pluginManager = Service<PluginManager>.Get();
var startInfo = Service<DalamudStartInfo>.Get(); var startInfo = Service<DalamudStartInfo>.Get();
@ -1293,7 +1292,7 @@ namespace Dalamud.Interface.Internal.Windows
pluginManager.RemovePlugin(plugin); pluginManager.RemovePlugin(plugin);
} }
notifications.AddNotification(Locs.Notifications_PluginDisabled(plugin.Manifest.Name), Locs.Notifications_PluginDisabledTitle, Notifications.Notification.Type.Success); notifications.AddNotification(Locs.Notifications_PluginDisabled(plugin.Manifest.Name), Locs.Notifications_PluginDisabledTitle, NotificationType.Success);
}); });
} }
} }

View file

@ -6,6 +6,7 @@ using Dalamud.Configuration.Internal;
using Dalamud.Game.ClientState.Conditions; using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.Gui; using Dalamud.Game.Gui;
using Dalamud.Interface.Internal; using Dalamud.Interface.Internal;
using Dalamud.Interface.Internal.Notifications;
using ImGuiNET; using ImGuiNET;
using ImGuiScene; using ImGuiScene;
using Serilog; using Serilog;
@ -201,6 +202,17 @@ namespace Dalamud.Interface
Service<InterfaceManager>.Get().RebuildFonts(); Service<InterfaceManager>.Get().RebuildFonts();
} }
/// <summary>
/// Add a notification to the notification queue.
/// </summary>
/// <param name="content">The content of the notification.</param>
/// <param name="title">The title of the notification.</param>
/// <param name="type">The type of the notification.</param>
/// <param name="msDelay">The time the notification should be displayed for.</param>
public void AddNotification(
string content, string? title = null, NotificationType type = NotificationType.None, uint msDelay = 3000) =>
Service<NotificationManager>.Get().AddNotification(content, title, type, msDelay);
/// <summary> /// <summary>
/// Unregister the UiBuilder. Do not call this in plugin code. /// Unregister the UiBuilder. Do not call this in plugin code.
/// </summary> /// </summary>