Screenie/Ui/PluginUi.cs

73 lines
1.8 KiB
C#

using System.Numerics;
using ImGuiNET;
using Screenie.Ui.Tabs;
using Screenie.Util;
namespace Screenie.Ui;
internal class PluginUi : IDisposable {
private Plugin Plugin { get; }
private List<ITab> Tabs { get; } = [];
internal bool Visible;
internal PluginUi(Plugin plugin) {
this.Plugin = plugin;
this.Tabs.AddRange([
new SettingsTab(this.Plugin),
new DatabaseTab(this.Plugin),
]);
this.Plugin.Interface.UiBuilder.Draw += this.Draw;
this.Plugin.Interface.UiBuilder.OpenConfigUi += this.OpenConfigUi;
}
public void Dispose() {
this.Plugin.Interface.UiBuilder.OpenConfigUi -= this.OpenConfigUi;
this.Plugin.Interface.UiBuilder.Draw -= this.Draw;
foreach (var tab in this.Tabs) {
tab.Dispose();
}
}
private void OpenConfigUi() {
this.Visible = true;
}
private void Draw() {
if (!this.Visible) {
return;
}
using var end = new OnDispose(ImGui.End);
ImGui.SetNextWindowSize(new Vector2(420, 500), ImGuiCond.FirstUseEver);
if (!ImGui.Begin(Plugin.Name, ref this.Visible)) {
return;
}
ImGui.PushTextWrapPos();
using var popTextWrapPos = new OnDispose(ImGui.PopTextWrapPos);
if (!ImGui.BeginTabBar("##screenie-tabbar")) {
return;
}
using var endTabBar = new OnDispose(ImGui.EndTabBar);
foreach (var tab in this.Tabs) {
if (!ImGui.BeginTabItem(tab.Label)) {
continue;
}
using var endTabItem = new OnDispose(ImGui.EndTabItem);
try {
tab.Draw();
} catch (Exception ex) {
Plugin.Log.Error(ex, $"Error drawing tab \"{tab.Label}\"");
}
}
}
}