shoko/MainUI.cs

95 lines
2.4 KiB
C#
Raw Normal View History

2023-10-02 18:49:24 +00:00
using System.Reflection;
using ImGuiNET;
namespace Shoko;
static class MainUI
{
static string txtURL = "";
public static List<Tab> tabs = new List<Tab>();
2023-10-03 19:52:26 +00:00
public static ImFontPtr Font;
public static List<ImFontPtr> HeadingFonts = new List<ImFontPtr>();
public static ImFontPtr MonospaceFont;
2023-10-02 18:49:24 +00:00
public static void NewTab(string url)
{
tabs.Add(new Tab(url));
}
2023-10-03 19:52:26 +00:00
public static unsafe void AddFontFromResource(string resource, Action<IntPtr, int> f)
{
using(var m = new MemoryStream())
{
Assembly.GetExecutingAssembly().GetManifestResourceStream(resource).CopyTo(m);
var fontBuffer = m.ToArray();
fixed(byte* buffer = fontBuffer)
{
f(new IntPtr(buffer), fontBuffer.Length);
}
}
}
public static bool Load(string[] args)
2023-10-03 19:52:26 +00:00
{
var io = ImGui.GetIO();
ImGui.StyleColorsDark();
io.ConfigFlags |= ImGuiConfigFlags.DockingEnable;
AddFontFromResource("RobotoFlex.ttf", (buf, len)=>{
Font = io.Fonts.AddFontFromMemoryTTF(buf, len, 16f);
HeadingFonts.Add(io.Fonts.AddFontFromMemoryTTF(buf, len, 32f));
HeadingFonts.Add(io.Fonts.AddFontFromMemoryTTF(buf, len, 24f));
HeadingFonts.Add(io.Fonts.AddFontFromMemoryTTF(buf, len, 20f));
});
AddFontFromResource("RobotoMono.ttf", (buf, len)=>{
MonospaceFont = io.Fonts.AddFontFromMemoryTTF(buf, len, 16f);
});
if(args.Length > 0)
foreach (var arg in args)
NewTab(arg);
2023-10-03 19:52:26 +00:00
return true;
}
2023-10-02 18:49:24 +00:00
public static bool Render()
{
bool quit = true;
2023-10-03 19:52:26 +00:00
ImGui.PushFont(Font);
2023-10-02 18:49:24 +00:00
ImGui.DockSpaceOverViewport();
Gui.MainMenuBar(()=>
{
Gui.Menu("File", ()=>
{
Gui.MenuItem("Quit", null, ()=> quit = false);
});
Gui.Menu("Help", ()=>
{
Gui.MenuItem("About", null, ()=> NewTab("about:"));
});
ImGui.InputText("##url", ref txtURL, 1024);
Gui.Button("Go", ()=>{
NewTab(txtURL);
txtURL = "";
});
});
tabs = tabs.Where(x=>x.IsOpen).ToList();
foreach (var tab in tabs)
{
tab.Render();
}
2023-10-03 19:52:26 +00:00
ImGui.PopFont();
2023-10-02 18:49:24 +00:00
return quit;
}
}