2021-08-23 16:50:18 +02:00
|
|
|
|
using Flowframes.Data;
|
2022-07-23 14:43:57 +02:00
|
|
|
|
using Flowframes.Forms;
|
2021-08-23 16:50:18 +02:00
|
|
|
|
using Flowframes.IO;
|
|
|
|
|
|
using Flowframes.Main;
|
|
|
|
|
|
using Flowframes.Os;
|
|
|
|
|
|
using System;
|
2025-11-09 20:34:37 +01:00
|
|
|
|
using System.Collections.Generic;
|
2024-11-25 18:51:43 +01:00
|
|
|
|
using System.Text.RegularExpressions;
|
2021-08-23 16:50:18 +02:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Flowframes.Ui
|
|
|
|
|
|
{
|
|
|
|
|
|
class UiUtils
|
|
|
|
|
|
{
|
2022-04-07 04:59:34 +02:00
|
|
|
|
public static void InitCombox(ComboBox box, int index)
|
2021-08-23 16:50:18 +02:00
|
|
|
|
{
|
2022-04-07 04:59:34 +02:00
|
|
|
|
if (box.Items.Count >= 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
box.SelectedIndex = index;
|
|
|
|
|
|
box.Text = box.Items[index].ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2022-04-07 04:59:34 +02:00
|
|
|
|
public static bool AssignComboxIndexFromText(ComboBox box, string text) // Set index to corresponding text
|
|
|
|
|
|
{
|
|
|
|
|
|
int index = box.Items.IndexOf(text);
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2022-04-07 04:59:34 +02:00
|
|
|
|
if (index == -1) // custom value, index not found
|
|
|
|
|
|
return false;
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2022-04-07 04:59:34 +02:00
|
|
|
|
box.SelectedIndex = index;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-21 14:45:46 +02:00
|
|
|
|
public static ComboBox LoadAiModelsIntoGui(ComboBox combox, AiInfo ai)
|
2021-08-23 16:50:18 +02:00
|
|
|
|
{
|
2022-04-07 04:59:34 +02:00
|
|
|
|
combox.Items.Clear();
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-04-07 04:59:34 +02:00
|
|
|
|
ModelCollection modelCollection = AiModels.GetModels(ai);
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2022-07-27 15:18:37 +02:00
|
|
|
|
if (modelCollection.Models == null || modelCollection.Models.Count < 1)
|
2022-04-07 04:59:34 +02:00
|
|
|
|
return combox;
|
2021-08-31 15:57:48 +02:00
|
|
|
|
|
2022-07-27 15:18:37 +02:00
|
|
|
|
for (int i = 0; i < modelCollection.Models.Count; i++)
|
2022-04-07 04:59:34 +02:00
|
|
|
|
{
|
2022-07-27 15:18:37 +02:00
|
|
|
|
ModelCollection.ModelInfo modelInfo = modelCollection.Models[i];
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2022-07-27 15:18:37 +02:00
|
|
|
|
if (string.IsNullOrWhiteSpace(modelInfo.Name))
|
2022-04-07 04:59:34 +02:00
|
|
|
|
continue;
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2022-04-07 04:59:34 +02:00
|
|
|
|
combox.Items.Add(modelInfo.GetUiString());
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2022-07-27 15:18:37 +02:00
|
|
|
|
if (modelInfo.IsDefault)
|
2022-04-07 04:59:34 +02:00
|
|
|
|
combox.SelectedIndex = i;
|
|
|
|
|
|
}
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2022-04-07 04:59:34 +02:00
|
|
|
|
if (combox.SelectedIndex < 0)
|
|
|
|
|
|
combox.SelectedIndex = 0;
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2022-04-07 04:59:34 +02:00
|
|
|
|
SelectNcnnIfNoCudaAvail(combox);
|
|
|
|
|
|
}
|
2021-08-23 16:50:18 +02:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2022-07-21 10:08:53 +02:00
|
|
|
|
Logger.Log($"Failed to load available AI models for {ai.NameInternal}! {e.Message}");
|
2022-04-07 04:59:34 +02:00
|
|
|
|
Logger.Log($"Stack Trace: {e.StackTrace}", true);
|
2021-08-23 16:50:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-07 04:59:34 +02:00
|
|
|
|
return combox;
|
|
|
|
|
|
}
|
2021-08-23 16:50:18 +02:00
|
|
|
|
|
2022-04-07 04:59:34 +02:00
|
|
|
|
static void SelectNcnnIfNoCudaAvail(ComboBox combox)
|
2021-08-23 16:50:18 +02:00
|
|
|
|
{
|
2024-11-21 01:22:31 +01:00
|
|
|
|
if (NvApi.NvGpus.Count < 1)
|
2021-08-23 16:50:18 +02:00
|
|
|
|
{
|
2022-04-07 04:59:34 +02:00
|
|
|
|
for (int i = 0; i < combox.Items.Count; i++)
|
|
|
|
|
|
{
|
2024-09-03 22:08:38 +02:00
|
|
|
|
if (((string)combox.Items[i]).Upper().Contains("NCNN"))
|
2022-04-07 04:59:34 +02:00
|
|
|
|
combox.SelectedIndex = i;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public enum MessageType { Message, Warning, Error };
|
|
|
|
|
|
|
2025-11-09 16:07:03 +01:00
|
|
|
|
public static DialogResult ShowMessageBox(string text, MessageType type = MessageType.Message, bool monospace = false)
|
2022-04-07 04:59:34 +02:00
|
|
|
|
{
|
2022-05-03 16:00:55 +02:00
|
|
|
|
Logger.Log($"MessageBox: {text} ({type}){(BatchProcessing.busy ? "[Batch Mode - Will not display messagebox]" : "")}", true);
|
2022-04-07 04:59:34 +02:00
|
|
|
|
|
2024-11-21 01:22:31 +01:00
|
|
|
|
if(Program.CmdMode)
|
2024-09-03 22:01:32 +02:00
|
|
|
|
return DialogResult.OK;
|
|
|
|
|
|
|
2022-07-20 21:48:30 +02:00
|
|
|
|
if (BatchProcessing.busy)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.Log(text);
|
|
|
|
|
|
return new DialogResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-07 04:59:34 +02:00
|
|
|
|
MessageBoxIcon icon = MessageBoxIcon.Information;
|
|
|
|
|
|
if (type == MessageType.Warning) icon = MessageBoxIcon.Warning;
|
|
|
|
|
|
else if (type == MessageType.Error) icon = MessageBoxIcon.Error;
|
|
|
|
|
|
|
2025-11-09 16:07:03 +01:00
|
|
|
|
var msgForm = new MessageForm(text, type.ToString(), monospace: monospace) { TopMost = true };
|
|
|
|
|
|
Program.mainForm.Invoke(() => msgForm.ShowDialog());
|
2022-07-23 14:43:57 +02:00
|
|
|
|
return DialogResult.OK;
|
2022-04-07 04:59:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-24 22:30:30 +02:00
|
|
|
|
public static DialogResult ShowMessageBox(string text, string title, MessageBoxButtons btns)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageForm form = new MessageForm(text, title, btns);
|
|
|
|
|
|
return form.ShowDialog();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-07 04:59:34 +02:00
|
|
|
|
public enum MoveDirection { Up = -1, Down = 1 };
|
|
|
|
|
|
|
|
|
|
|
|
public static void MoveListViewItem(ListView listView, MoveDirection direction)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (listView.SelectedItems.Count != 1)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
ListViewItem selected = listView.SelectedItems[0];
|
|
|
|
|
|
int index = selected.Index;
|
|
|
|
|
|
int count = listView.Items.Count;
|
|
|
|
|
|
|
|
|
|
|
|
if (direction == MoveDirection.Up)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (index == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
listView.Items.Remove(selected);
|
|
|
|
|
|
listView.Items.Insert(count - 1, selected);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
listView.Items.Remove(selected);
|
|
|
|
|
|
listView.Items.Insert(index - 1, selected);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (index == count - 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
listView.Items.Remove(selected);
|
|
|
|
|
|
listView.Items.Insert(0, selected);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2021-08-23 16:50:18 +02:00
|
|
|
|
{
|
2022-04-07 04:59:34 +02:00
|
|
|
|
listView.Items.Remove(selected);
|
|
|
|
|
|
listView.Items.Insert(index + 1, selected);
|
2021-08-23 16:50:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-25 18:51:43 +01:00
|
|
|
|
|
|
|
|
|
|
// TODO: Move to NmkdUtils once implemented
|
|
|
|
|
|
public static string PascalCaseToText(string s)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (s.IsEmpty())
|
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
|
|
return Regex.Replace(s, "([A-Z])", " $1").Trim();
|
|
|
|
|
|
}
|
2025-11-09 20:34:37 +01:00
|
|
|
|
|
|
|
|
|
|
private static Dictionary<string, bool> _fontAvailabilityCache = new Dictionary<string, bool>(StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> Checks if a <paramref name="font"/> is installed on the system, results are cached. </summary>
|
|
|
|
|
|
public static bool IsFontInstalled(string font)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_fontAvailabilityCache.TryGetValue(font, out bool isInstalledCached))
|
|
|
|
|
|
return isInstalledCached;
|
|
|
|
|
|
|
|
|
|
|
|
using (var testFont = new System.Drawing.Font(font, 8))
|
|
|
|
|
|
{
|
|
|
|
|
|
bool isInstalled = string.Equals(font, testFont.Name, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
|
Logger.Log($"Font is {(isInstalled ? "" : "not ")}available: {font}", true);
|
|
|
|
|
|
_fontAvailabilityCache[font] = isInstalled;
|
|
|
|
|
|
return isInstalled;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> Gets a font or tries one of the fallbacks (if provided). If none are found, returns the default font. </summary>
|
|
|
|
|
|
public static System.Drawing.Font GetFontOrFallback(string preferredFont, float emSize, System.Drawing.FontStyle? style = null, params string[] fallbackFonts)
|
|
|
|
|
|
{
|
|
|
|
|
|
style = style ?? System.Drawing.FontStyle.Regular;
|
|
|
|
|
|
if (IsFontInstalled(preferredFont))
|
|
|
|
|
|
return new System.Drawing.Font(preferredFont, emSize, style.Value);
|
|
|
|
|
|
foreach (var fallback in fallbackFonts)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (IsFontInstalled(fallback))
|
|
|
|
|
|
return new System.Drawing.Font(fallback, emSize, style.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
// Last resort: use default font
|
|
|
|
|
|
return new System.Drawing.Font(System.Drawing.SystemFonts.DefaultFont.FontFamily, emSize, style.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static System.Drawing.Font GetMonospaceFont(float emSize, System.Drawing.FontStyle? style = null)
|
|
|
|
|
|
=> GetFontOrFallback("Cascadia Mono", emSize, style ?? System.Drawing.FontStyle.Regular, "Cascadia Code", "Consolas", "Courier New");
|
2022-04-07 04:59:34 +02:00
|
|
|
|
}
|
2021-08-23 16:50:18 +02:00
|
|
|
|
}
|