Use ImRaii in DatePicker

This commit is contained in:
Infi
2024-05-25 07:41:21 +02:00
parent 0f6556472a
commit 1eed122e7a
+78 -78
View File
@@ -6,6 +6,7 @@ using Dalamud.Interface.Colors;
using Dalamud.Interface.ImGuiNotification; using Dalamud.Interface.ImGuiNotification;
using Dalamud.Interface.Internal.Notifications; using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Utility; using Dalamud.Interface.Utility;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Utility; using Dalamud.Utility;
using ImGuiNET; using ImGuiNET;
@@ -28,17 +29,17 @@ public static class DateWidget
static DateWidget() static DateWidget()
{ {
if (MaxMonthWidthIndex == -1) if (MaxMonthWidthIndex != -1)
return;
float maxMonthWidth = 0;
for (var i = 0; i < 12; i++)
{ {
float maxMonthWidth = 0; var mw = ImGui.CalcTextSize(MonthNames[i]).X;
for (var i = 0; i < 12; i++) if (maxMonthWidth < mw)
{ {
var mw = ImGui.CalcTextSize(MonthNames[i]).X; maxMonthWidth = mw;
if (maxMonthWidth < mw) MaxMonthWidthIndex = i;
{
maxMonthWidth = mw;
MaxMonthWidthIndex = i;
}
} }
} }
} }
@@ -105,12 +106,13 @@ public static class DateWidget
ImGui.SetNextWindowSize(new Vector2(widthRequiredByCalendar, widthRequiredByCalendar)); ImGui.SetNextWindowSize(new Vector2(widthRequiredByCalendar, widthRequiredByCalendar));
ImGui.SetNextWindowSizeConstraints(new Vector2(widthRequiredByCalendar, popupHeight + 40), new Vector2(widthRequiredByCalendar, popupHeight + 40)); ImGui.SetNextWindowSizeConstraints(new Vector2(widthRequiredByCalendar, popupHeight + 40), new Vector2(widthRequiredByCalendar, popupHeight + 40));
if (!ImGui.BeginPopupContextItem(label, ImGuiPopupFlags.None)) using var popupItem = ImRaii.ContextPopupItem(label, ImGuiPopupFlags.None);
if (!popupItem.Success)
return valueChanged; return valueChanged;
if (ImGui.GetIO().MouseClicked[1]) if (ImGui.GetIO().MouseClicked[1])
{ {
// reset date when user right clicks the date chooser header when the dialog is open // reset date when user right-clicks the date chooser header when the dialog is open
dateOut = DateTime.Now; dateOut = DateTime.Now;
} }
else if (LastOpenComboID != id) else if (LastOpenComboID != id)
@@ -120,10 +122,11 @@ public static class DateWidget
dateOut = DateTime.Now; dateOut = DateTime.Now;
} }
ImGui.PushFont(UiBuilder.MonoFont); using var mono = ImRaii.PushFont(UiBuilder.MonoFont);
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, style.FramePadding); using var windowPadding = ImRaii.PushStyle(ImGuiStyleVar.WindowPadding, style.FramePadding);
using var buttonColor = ImRaii.PushColor(ImGuiCol.Button, Transparent);
ImGui.Spacing(); ImGui.Spacing();
ImGui.PushStyleColor(ImGuiCol.Button, Transparent);
var yearString = $"{dateOut.Year}"; var yearString = $"{dateOut.Year}";
var yearPartWidth = arrowLeftWidth + arrowRightWidth + ImGui.CalcTextSize(yearString).X; var yearPartWidth = arrowLeftWidth + arrowRightWidth + ImGui.CalcTextSize(yearString).X;
@@ -131,35 +134,38 @@ public static class DateWidget
var oldWindowRounding = style.WindowRounding; var oldWindowRounding = style.WindowRounding;
style.WindowRounding = 0; style.WindowRounding = 0;
ImGui.PushID(1234); using (ImRaii.PushId(1234))
if (ImGui.SmallButton(arrowLeft)) {
dateOut = dateOut.AddMonths(-1); if (ImGui.SmallButton(arrowLeft))
ImGui.SameLine(); dateOut = dateOut.AddMonths(-1);
ImGui.SameLine();
ImGui.TextUnformatted($"{Center(MonthNames[dateOut.Month - 1], 9)}"); ImGui.TextUnformatted($"{Center(MonthNames[dateOut.Month - 1], 9)}");
ImGui.SameLine(); ImGui.SameLine();
if (ImGui.SmallButton(arrowRight)) if (ImGui.SmallButton(arrowRight))
dateOut = dateOut.AddMonths(1); dateOut = dateOut.AddMonths(1);
ImGui.PopID(); }
ImGui.SameLine(ImGui.GetWindowWidth() - yearPartWidth - style.WindowPadding.X - style.ItemSpacing.X * 4.0f); ImGui.SameLine(ImGui.GetWindowWidth() - yearPartWidth - style.WindowPadding.X - style.ItemSpacing.X * 4.0f);
ImGui.PushID(1235); using (ImRaii.PushId(1235))
if (ImGui.SmallButton(arrowLeft)) {
dateOut = dateOut.AddYears(-1); if (ImGui.SmallButton(arrowLeft))
ImGui.SameLine(); dateOut = dateOut.AddYears(-1);
ImGui.SameLine();
ImGui.Text($"{dateOut.Year}"); ImGui.Text($"{dateOut.Year}");
ImGui.SameLine(); ImGui.SameLine();
if (ImGui.SmallButton(arrowRight)) if (ImGui.SmallButton(arrowRight))
dateOut = dateOut.AddYears(1); dateOut = dateOut.AddYears(1);
ImGui.PopID(); }
ImGui.Spacing(); ImGui.Spacing();
var maxDayOfCurMonth = NumDaysPerMonth[dateOut.Month - 1]; // This could be calculated only when needed (but I guess it's fast in any case...) // This could be calculated only when needed (but I guess it's fast in any case...)
var maxDayOfCurMonth = NumDaysPerMonth[dateOut.Month - 1];
if (maxDayOfCurMonth == 28) if (maxDayOfCurMonth == 28)
{ {
var year = dateOut.Year; var year = dateOut.Year;
@@ -168,8 +174,8 @@ public static class DateWidget
maxDayOfCurMonth = 29; maxDayOfCurMonth = 29;
} }
ImGui.PushStyleColor(ImGuiCol.ButtonHovered, ImGuiColors.DalamudOrange); using var buttonHovered = ImRaii.PushColor(ImGuiCol.ButtonHovered, ImGuiColors.DalamudOrange);
ImGui.PushStyleColor(ImGuiCol.ButtonActive, ImGuiColors.DalamudYellow); using var buttonActive = ImRaii.PushColor(ImGuiCol.ButtonActive, ImGuiColors.DalamudYellow);
ImGui.Separator(); ImGui.Separator();
@@ -177,58 +183,47 @@ public static class DateWidget
var dayOfWeek = (int)new DateTime(dateOut.Year, dateOut.Month, 1).DayOfWeek; var dayOfWeek = (int)new DateTime(dateOut.Year, dateOut.Month, 1).DayOfWeek;
for (var dw = 0; dw < 7; dw++) for (var dw = 0; dw < 7; dw++)
{ {
ImGui.BeginGroup(); using (ImRaii.Group())
if (dw == 0)
{ {
var textColor = ImGuiColors.DalamudGrey; using var textColor = ImRaii.PushColor(ImGuiCol.Text, CalculateTextColor(), dw == 0);
var l = (textColor.X + textColor.Y + textColor.Z) * 0.33334f;
ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(l * 2.0f > 1 ? 1 : l * 2.0f, l * .5f, l * .5f, textColor.W));
}
ImGui.Text($"{(dw == 0 ? "" : " ")}{DayNames[dw]}"); ImGui.Text($"{(dw == 0 ? "" : " ")}{DayNames[dw]}");
if (dw == 0) if (dw == 0)
ImGui.Separator(); ImGui.Separator();
else
ImGui.Spacing();
var curDay = dw - dayOfWeek; // Use dayOfWeek for spacing
for (var row = 0; row < 7; row++)
{
var cday = curDay + (7 * row);
if (cday >= 0 && cday < maxDayOfCurMonth)
{
ImGui.PushID(row * 10 + dw);
if (ImGui.SmallButton(string.Format(cday < 9 ? " {0}" : "{0}", cday + 1)))
{
valueChanged = true;
ImGui.SetItemDefaultFocus();
dateOut = new DateTime(dateOut.Year, dateOut.Month, cday + 1);
}
ImGui.PopID();
}
else else
ImGui.Spacing();
// Use dayOfWeek for spacing
var curDay = dw - dayOfWeek;
for (var row = 0; row < 7; row++)
{ {
ImGui.TextUnformatted(" "); var cday = curDay + (7 * row);
if (cday >= 0 && cday < maxDayOfCurMonth)
{
using var rowId = ImRaii.PushId(row * 10 + dw);
if (ImGui.SmallButton(string.Format(cday < 9 ? " {0}" : "{0}", cday + 1)))
{
ImGui.SetItemDefaultFocus();
valueChanged = true;
dateOut = new DateTime(dateOut.Year, dateOut.Month, cday + 1);
}
}
else
{
ImGui.TextUnformatted(" ");
}
} }
}
if (dw == 0) if (dw == 0)
{ ImGui.Separator();
ImGui.Separator();
ImGui.PopStyleColor();
} }
ImGui.EndGroup();
if (dw != 6) if (dw != 6)
ImGui.SameLine(ImGui.GetWindowWidth() - ((6 - dw) * (ImGui.GetWindowWidth() / 7.0f))); ImGui.SameLine(ImGui.GetWindowWidth() - (6 - dw) * (ImGui.GetWindowWidth() / 7.0f));
} }
style.WindowRounding = oldWindowRounding; style.WindowRounding = oldWindowRounding;
ImGui.PopStyleColor(2);
ImGui.PopStyleColor();
ImGui.PopStyleVar();
var mustCloseCombo = valueChanged; var mustCloseCombo = valueChanged;
if (closeWhenMouseLeavesIt && !mustCloseCombo) if (closeWhenMouseLeavesIt && !mustCloseCombo)
@@ -245,19 +240,24 @@ public static class DateWidget
mustCloseCombo = true; mustCloseCombo = true;
} }
ImGui.PopFont();
// ImGui issue #273849, children keep popups from closing automatically // ImGui issue #273849, children keep popups from closing automatically
if (mustCloseCombo) if (mustCloseCombo)
ImGui.CloseCurrentPopup(); ImGui.CloseCurrentPopup();
ImGui.EndPopup();
return valueChanged; return valueChanged;
} }
private static Vector4 CalculateTextColor()
{
var textColor = ImGuiColors.DalamudGrey;
var l = (textColor.X + textColor.Y + textColor.Z) * 0.33334f;
return new Vector4(l * 2.0f > 1 ? 1 : l * 2.0f, l * .5f, l * .5f, textColor.W);
}
private static string Center(string source, int length) private static string Center(string source, int length)
{ {
var spaces = length - source.Length; var spaces = length - source.Length;
var padLeft = (spaces / 2) + source.Length; var padLeft = spaces / 2 + source.Length;
return source.PadLeft(padLeft).PadRight(length); return source.PadLeft(padLeft).PadRight(length);
} }
} }