Screenie/Ui/PluginUi.cs

170 lines
5.5 KiB
C#

using System.Diagnostics;
using System.Numerics;
using Dalamud.Interface;
using Dalamud.Interface.Components;
using Dalamud.Interface.ImGuiFileDialog;
using Dalamud.Interface.Utility;
using ImGuiNET;
using Screenie.Util;
namespace Screenie.Ui;
internal class PluginUi : IDisposable {
private Plugin Plugin { get; }
private FileDialogManager FileDialogManager { get; }
private ScreenshotMetadata Metadata { get; set; }
internal bool Visible;
private const int RefreshSeconds = 5;
private readonly Stopwatch _metaUpdate = Stopwatch.StartNew();
internal PluginUi(Plugin plugin) {
this.Plugin = plugin;
this.FileDialogManager = new FileDialogManager();
this.Metadata = ScreenshotMetadata.Capture(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;
}
private void OpenConfigUi() {
this.Visible = true;
}
private void Draw() {
try {
this.FileDialogManager.Draw();
} catch (Exception e) {
Plugin.Log.Error(e, "Error in FileDialogManager.Draw");
}
if (!this.Visible) {
return;
}
if (this._metaUpdate.Elapsed >= TimeSpan.FromSeconds(RefreshSeconds)) {
this.Metadata = ScreenshotMetadata.Capture(this.Plugin);
this._metaUpdate.Restart();
}
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);
var anyChanged = false;
anyChanged |= this.DrawScreenshotsFolderInput();
ImGui.TextUnformatted("Save format");
ImGui.SetNextItemWidth(-1);
if (ImGui.BeginCombo("##file-format", Enum.GetName(this.Plugin.Config.SaveFormat))) {
using var endCombo = new OnDispose(ImGui.EndCombo);
foreach (var format in Enum.GetValues<Format>()) {
if (ImGui.Selectable(Enum.GetName(format), this.Plugin.Config.SaveFormat == format)) {
this.Plugin.Config.SaveFormat = format;
anyChanged = true;
}
}
}
var label = this.Plugin.Config.SaveFormat switch {
Format.Jpg => "Quality",
Format.WebpLossless => null,
Format.WebpLossy => "Quality",
Format.Png => "Compression level",
_ => "Unknown",
};
if (label != null) {
ImGui.TextUnformatted(label);
ImGui.SetNextItemWidth(-1);
anyChanged |= ImGui.SliderInt("##format-data", ref this.Plugin.Config.SaveFormatData, 0, 100);
}
ImGui.TextUnformatted("Filename format");
ImGui.SetNextItemWidth(-1);
ImGui.PushFont(UiBuilder.MonoFont);
using (new OnDispose(ImGui.PopFont)) {
anyChanged |= ImGui.InputTextMultiline(
"##filename-format",
ref this.Plugin.Config.SaveFileNameFormat,
2048,
new Vector2(-1, 150)
);
}
var template = this.Plugin.Config.SaveFileNameTemplate;
if (template.HasErrors) {
ImGui.TextUnformatted("Invalid template");
foreach (var error in template.Messages) {
ImGui.Bullet();
ImGui.SameLine();
ImGui.TextUnformatted(error.Message);
}
} else {
try {
ImGui.TextUnformatted(this.Plugin.Config.SaveFileNameTemplate.Render(this.Metadata).ReplaceLineEndings(" "));
} catch (Exception ex) {
ImGui.TextUnformatted($"Failed to evaluate: {ex.Message}");
}
}
ImGui.ProgressBar(
(float) (TimeSpan.FromSeconds(RefreshSeconds) - this._metaUpdate.Elapsed).TotalSeconds / RefreshSeconds,
new Vector2(-1, 1)
);
if (anyChanged) {
this.Plugin.SaveConfig();
}
}
private bool DrawScreenshotsFolderInput() {
ImGui.TextUnformatted("Screenshots folder");
var changed = false;
ImGui.PushFont(UiBuilder.IconFont);
Vector2 size;
using (new OnDispose(ImGui.PopFont)) {
size = ImGuiHelpers.GetButtonSize(FontAwesomeIcon.Folder.ToIconString());
}
var availWidth = ImGui.GetContentRegionAvail().X;
ImGui.SetNextItemWidth(availWidth - size.X - ImGui.GetStyle().ItemSpacing.X);
if (ImGui.InputText("##save-directory", ref this.Plugin.Config.SaveDirectory, 1024, ImGuiInputTextFlags.EnterReturnsTrue)) {
changed = true;
}
ImGui.SameLine();
if (ImGuiComponents.IconButton(FontAwesomeIcon.Folder)) {
this.FileDialogManager.OpenFolderDialog(
"Choose screenshots folder",
(b, s) => {
if (!b) {
return;
}
this.Plugin.Config.SaveDirectory = s;
this.Plugin.SaveConfig();
},
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
);
}
return changed;
}
}