fix: allow Window to be instantiated without Dalamud services

This commit is contained in:
goat 2023-07-11 20:54:59 +02:00
parent 4dc43b7ed3
commit a82096c671
No known key found for this signature in database
GPG key ID: 49E2AA8C6A76498B
2 changed files with 21 additions and 15 deletions

View file

@ -17,7 +17,6 @@ public abstract class Window
private bool internalLastIsOpen = false; private bool internalLastIsOpen = false;
private bool internalIsOpen = false; private bool internalIsOpen = false;
private bool nextFrameBringToFront = false; private bool nextFrameBringToFront = false;
private DalamudConfiguration Configuration;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Window"/> class. /// Initializes a new instance of the <see cref="Window"/> class.
@ -33,7 +32,6 @@ public abstract class Window
this.WindowName = name; this.WindowName = name;
this.Flags = flags; this.Flags = flags;
this.ForceMainWindow = forceMainWindow; this.ForceMainWindow = forceMainWindow;
this.Configuration = Service<DalamudConfiguration>.Get();
} }
/// <summary> /// <summary>
@ -225,10 +223,12 @@ public abstract class Window
/// <summary> /// <summary>
/// Draw the window via ImGui. /// Draw the window via ImGui.
/// </summary> /// </summary>
internal void DrawInternal() internal void DrawInternal(DalamudConfiguration? configuration)
{ {
this.PreOpenCheck(); this.PreOpenCheck();
var doSoundEffects = configuration?.EnablePluginUISoundEffects ?? false;
if (!this.IsOpen) if (!this.IsOpen)
{ {
if (this.internalIsOpen != this.internalLastIsOpen) if (this.internalIsOpen != this.internalLastIsOpen)
@ -238,7 +238,7 @@ public abstract class Window
this.IsFocused = false; this.IsFocused = false;
if (this.Configuration.EnablePluginUISoundEffects && !this.DisableWindowSounds) UIModule.PlaySound(this.OnCloseSfxId, 0, 0, 0); if (doSoundEffects && !this.DisableWindowSounds) UIModule.PlaySound(this.OnCloseSfxId, 0, 0, 0);
} }
return; return;
@ -264,7 +264,7 @@ public abstract class Window
this.internalLastIsOpen = this.internalIsOpen; this.internalLastIsOpen = this.internalIsOpen;
this.OnOpen(); this.OnOpen();
if (this.Configuration.EnablePluginUISoundEffects && !this.DisableWindowSounds) UIModule.PlaySound(this.OnOpenSfxId, 0, 0, 0); if (doSoundEffects && !this.DisableWindowSounds) UIModule.PlaySound(this.OnOpenSfxId, 0, 0, 0);
} }
var wasFocused = this.IsFocused; var wasFocused = this.IsFocused;
@ -294,16 +294,19 @@ public abstract class Window
this.IsFocused = ImGui.IsWindowFocused(ImGuiFocusedFlags.RootAndChildWindows); this.IsFocused = ImGui.IsWindowFocused(ImGuiFocusedFlags.RootAndChildWindows);
var escapeDown = Service<KeyState>.Get()[VirtualKey.ESCAPE]; var isAllowed = configuration?.IsFocusManagementEnabled ?? false;
var isAllowed = Service<DalamudConfiguration>.Get().IsFocusManagementEnabled; if (isAllowed)
if (escapeDown && this.IsFocused && isAllowed && !wasEscPressedLastFrame && this.RespectCloseHotkey)
{ {
this.IsOpen = false; var escapeDown = Service<KeyState>.Get()[VirtualKey.ESCAPE];
wasEscPressedLastFrame = true; if (escapeDown && this.IsFocused && !wasEscPressedLastFrame && this.RespectCloseHotkey)
} {
else if (!escapeDown && wasEscPressedLastFrame) this.IsOpen = false;
{ wasEscPressedLastFrame = true;
wasEscPressedLastFrame = false; }
else if (!escapeDown && wasEscPressedLastFrame)
{
wasEscPressedLastFrame = false;
}
} }
ImGui.End(); ImGui.End();

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Dalamud.Configuration.Internal;
using Dalamud.Interface.Internal.ManagedAsserts; using Dalamud.Interface.Internal.ManagedAsserts;
using ImGuiNET; using ImGuiNET;
using Serilog; using Serilog;
@ -111,6 +112,8 @@ public class WindowSystem
if (hasNamespace) if (hasNamespace)
ImGui.PushID(this.Namespace); ImGui.PushID(this.Namespace);
var config = Service<DalamudConfiguration>.GetNullable();
// Shallow clone the list of windows so that we can edit it without modifying it while the loop is iterating // Shallow clone the list of windows so that we can edit it without modifying it while the loop is iterating
foreach (var window in this.windows.ToArray()) foreach (var window in this.windows.ToArray())
{ {
@ -119,7 +122,7 @@ public class WindowSystem
#endif #endif
var snapshot = ImGuiManagedAsserts.GetSnapshot(); var snapshot = ImGuiManagedAsserts.GetSnapshot();
window.DrawInternal(); window.DrawInternal(config);
var source = ($"{this.Namespace}::" ?? string.Empty) + window.WindowName; var source = ($"{this.Namespace}::" ?? string.Empty) + window.WindowName;
ImGuiManagedAsserts.ReportProblems(source, snapshot); ImGuiManagedAsserts.ReportProblems(source, snapshot);