mid settings improvement attempt
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -28,8 +28,10 @@ using LightlessSync.WebAPI;
|
||||
using LightlessSync.WebAPI.SignalR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
@@ -191,6 +193,99 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase
|
||||
return addSuffix ? $"{dblSByte:0.00} {suffix[i]}" : $"{dblSByte:0.00}";
|
||||
}
|
||||
|
||||
public readonly struct TabOption<T>
|
||||
{
|
||||
public string Label { get; }
|
||||
public T Value { get; }
|
||||
public bool Enabled { get; }
|
||||
|
||||
public TabOption(string label, T value, bool enabled = true)
|
||||
{
|
||||
Label = label;
|
||||
Value = value;
|
||||
Enabled = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Tab<T>(string id, IReadOnlyList<TabOption<T>> options, ref T selectedValue) where T : struct
|
||||
{
|
||||
if (options.Count == 0)
|
||||
return false;
|
||||
|
||||
var pushIdValue = string.IsNullOrEmpty(id)
|
||||
? $"UiSharedTab_{RuntimeHelpers.GetHashCode(options):X}"
|
||||
: id;
|
||||
using var tabId = ImRaii.PushId(pushIdValue);
|
||||
|
||||
var selectedIndex = -1;
|
||||
for (var i = 0; i < options.Count; i++)
|
||||
{
|
||||
if (!EqualityComparer<T>.Default.Equals(options[i].Value, selectedValue))
|
||||
continue;
|
||||
|
||||
selectedIndex = i;
|
||||
break;
|
||||
}
|
||||
|
||||
if (selectedIndex == -1 || !options[selectedIndex].Enabled)
|
||||
selectedIndex = GetFirstEnabledTabIndex(options);
|
||||
|
||||
if (selectedIndex == -1)
|
||||
return false;
|
||||
|
||||
var changed = DrawTabsInternal(options, ref selectedIndex);
|
||||
selectedValue = options[Math.Clamp(selectedIndex, 0, options.Count - 1)].Value;
|
||||
return changed;
|
||||
}
|
||||
|
||||
private static int GetFirstEnabledTabIndex<T>(IReadOnlyList<TabOption<T>> options)
|
||||
{
|
||||
for (var i = 0; i < options.Count; i++)
|
||||
{
|
||||
if (options[i].Enabled)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static bool DrawTabsInternal<T>(IReadOnlyList<TabOption<T>> options, ref int selectedIndex)
|
||||
{
|
||||
selectedIndex = Math.Clamp(selectedIndex, 0, Math.Max(0, options.Count - 1));
|
||||
|
||||
var style = ImGui.GetStyle();
|
||||
var availableWidth = ImGui.GetContentRegionAvail().X;
|
||||
var spacingX = style.ItemSpacing.X;
|
||||
var buttonWidth = options.Count > 0 ? Math.Max(1f, (availableWidth - spacingX * (options.Count - 1)) / options.Count) : availableWidth;
|
||||
var buttonHeight = Math.Max(ImGui.GetFrameHeight() + style.FramePadding.Y, 28f * ImGuiHelpers.GlobalScale);
|
||||
var changed = false;
|
||||
|
||||
for (var i = 0; i < options.Count; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
ImGui.SameLine();
|
||||
|
||||
var tab = options[i];
|
||||
var isSelected = i == selectedIndex;
|
||||
|
||||
using (ImRaii.Disabled(!tab.Enabled))
|
||||
{
|
||||
using var tabIndexId = ImRaii.PushId(i);
|
||||
using var selectedButton = isSelected ? ImRaii.PushColor(ImGuiCol.Button, style.Colors[(int)ImGuiCol.TabActive]) : null;
|
||||
using var selectedHover = isSelected ? ImRaii.PushColor(ImGuiCol.ButtonHovered, style.Colors[(int)ImGuiCol.TabHovered]) : null;
|
||||
using var selectedActive = isSelected ? ImRaii.PushColor(ImGuiCol.ButtonActive, style.Colors[(int)ImGuiCol.TabActive]) : null;
|
||||
|
||||
if (ImGui.Button(tab.Label, new Vector2(buttonWidth, buttonHeight)))
|
||||
{
|
||||
selectedIndex = i;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
public static void CenterNextWindow(float width, float height, ImGuiCond cond = ImGuiCond.None)
|
||||
{
|
||||
var center = ImGui.GetMainViewport().GetCenter();
|
||||
|
||||
Reference in New Issue
Block a user