diff --git a/MainUI.cs b/MainUI.cs index 5084933..5813fce 100644 --- a/MainUI.cs +++ b/MainUI.cs @@ -20,7 +20,6 @@ static class MainUI public static unsafe void AddFontFromResource(string resource, Action f) { - var io = ImGui.GetIO(); using(var m = new MemoryStream()) { Assembly.GetExecutingAssembly().GetManifestResourceStream(resource).CopyTo(m); @@ -32,7 +31,7 @@ static class MainUI } } - public static unsafe bool Load() + public static bool Load(string[] args) { var io = ImGui.GetIO(); ImGui.StyleColorsDark(); @@ -48,6 +47,10 @@ static class MainUI MonospaceFont = io.Fonts.AddFontFromMemoryTTF(buf, len, 16f); }); + if(args.Length > 0) + foreach (var arg in args) + NewTab(arg); + return true; } diff --git a/Media/PlainMediaHandler.cs b/Media/PlainMediaHandler.cs index 83b00f9..ccc0f30 100644 --- a/Media/PlainMediaHandler.cs +++ b/Media/PlainMediaHandler.cs @@ -26,7 +26,9 @@ class PlainMediaHandler : MediaHandler public override void Render() { + ImGui.PushFont(MainUI.MonospaceFont); foreach(var line in lines) ImGui.TextUnformatted(line); + ImGui.PopFont(); } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index c1c9592..a549e71 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,3 @@ -using ImGuiNET; using Raylib_cs; using rlImGui_cs; @@ -8,17 +7,18 @@ class Program { static void Main(string[] args) { - Raylib.SetConfigFlags(ConfigFlags.FLAG_WINDOW_RESIZABLE | ConfigFlags.FLAG_WINDOW_MAXIMIZED); - Raylib.InitWindow(1024, 768, "Shoko"); + Raylib.SetConfigFlags( + ConfigFlags.FLAG_MSAA_4X_HINT | + ConfigFlags.FLAG_WINDOW_HIGHDPI | + ConfigFlags.FLAG_WINDOW_MAXIMIZED | + ConfigFlags.FLAG_WINDOW_RESIZABLE | + ConfigFlags.FLAG_VSYNC_HINT); + Raylib.InitWindow(0, 0, "Shoko"); rlImGui.BeginInitImGui(); - bool quit = MainUI.Load(); + bool quit = MainUI.Load(args); rlImGui.EndInitImGui(); - if(args.Length > 0) - foreach (var arg in args) - MainUI.NewTab(arg); - while(!Raylib.WindowShouldClose() && quit) { Raylib.BeginDrawing(); diff --git a/Protocols/FileProtoHandler.cs b/Protocols/FileProtoHandler.cs index c6c3121..9d525ec 100644 --- a/Protocols/FileProtoHandler.cs +++ b/Protocols/FileProtoHandler.cs @@ -1,3 +1,6 @@ +using System.Text; +using System.Web; + namespace Shoko; [Protocol("file")] @@ -10,13 +13,39 @@ class FileProtoHandler : ProtoHandler public override void Load() { - var file = new UriBuilder(URL).Path; + var file = HttpUtility.UrlDecode(new UriBuilder(URL).Path); - var stream = new FileStream(file, FileMode.Open); + if(URL.Host.Length > 0) + file = "//" + URL.Host + file; - Content = stream; MediaType = "text/plain"; // TODO: magic numbers Status = "OK"; + + if(File.Exists(file)) + { + var stream = new FileStream(file, FileMode.Open); + Content = stream; + } + else if(Directory.Exists(file)) + { + MediaType = "text/gemini"; + var str = "# "+file+"\r\n"; + var entries = Directory.EnumerateFileSystemEntries(file); + + foreach(var entry in entries) + { + var path = Path.GetFileName(entry); + str += string.Format("=>{0}/{1} {2}\r\n", URL.ToString(), HttpUtility.UrlEncode(path), path); + } + + Content = new MemoryStream(Encoding.UTF8.GetBytes(str)); + } + else + { + Content = new MemoryStream(Encoding.UTF8.GetBytes("file not found")); + Status = "not found"; + } + Loaded = true; } public override void Render() diff --git a/Protocols/ProtoHandler.cs b/Protocols/ProtoHandler.cs index 78e440b..22ed65e 100644 --- a/Protocols/ProtoHandler.cs +++ b/Protocols/ProtoHandler.cs @@ -22,6 +22,8 @@ class ProtoHandler public Stream Content; public string Status; public bool Loaded = false; + public int LoadedBytes = 0; + public int TotalBytes = 0; public IEnumerable>> Headers; public IEnumerable> MediaTypeParams; public ProtoHandler() @@ -36,6 +38,7 @@ class ProtoHandler { Content = new MemoryStream(Encoding.UTF8.GetBytes("error: no handler for this scheme")); MediaType = "text/plain"; + Loaded = true; } public virtual void Render() diff --git a/Protocols/ResProtoHandler.cs b/Protocols/ResProtoHandler.cs new file mode 100644 index 0000000..e90d33e --- /dev/null +++ b/Protocols/ResProtoHandler.cs @@ -0,0 +1,33 @@ +using System.Reflection; +using System.Web; + +namespace Shoko; + +[Protocol("res")] +class ResProtoHandler : ProtoHandler +{ + public ResProtoHandler(Uri url) + { + URL = url; + } + + public override void Load() + { + var path = HttpUtility.HtmlDecode(new UriBuilder(URL).Path); + Status = "OK"; + Loaded = true; + MediaType = "text/plain"; + try + { + Content = Assembly.GetExecutingAssembly().GetManifestResourceStream(path); + } + catch + { + Status = "not found"; + Loaded = false; + } + } + public override void Render() + { + } +} \ No newline at end of file diff --git a/Tab.cs b/Tab.cs index 6ad87a0..e20f52f 100644 --- a/Tab.cs +++ b/Tab.cs @@ -66,7 +66,8 @@ class Tab if(Error is null) { Handler.MenuBar(); - Document.MenuBar(); + if(Handler.Loaded) + Document.MenuBar(); } Gui.Button("<<", ()=>{ @@ -78,6 +79,8 @@ class Tab Gui.Button("Go", ()=>{ Load(txtURL); }); + if(!Handler.Loaded && Handler.TotalBytes != 0) + ImGui.ProgressBar(Handler.LoadedBytes / Handler.TotalBytes); }); if(Error is not null) { @@ -86,7 +89,8 @@ class Tab } else { - Document.Render(); + if(Handler.Loaded) + Document.Render(); Handler.Render(); } });