Multi-platform .NET bindings to the Ultralight project.
1using System;
2using System.IO;
3using System.Runtime.CompilerServices;
4using System.Runtime.InteropServices;
5using System.Text;
6using SixLabors.ImageSharp;
7using SixLabors.ImageSharp.PixelFormats;
8using SixLabors.ImageSharp.Processing;
9using SixLabors.ImageSharp.Processing.Processors.Transforms;
10using ImpromptuNinjas.UltralightSharp;
11using ImpromptuNinjas.UltralightSharp.Enums;
12using String = ImpromptuNinjas.UltralightSharp.String;
13
14namespace ImpromptuNinjas.UltralightSharp.Demo {
15
16 public static partial class DemoProgram {
17
18 public static unsafe void Main(string[] args) {
19 // setup logging
20 LoggerLogMessageCallback cb = LoggerCallback;
21 Ultralight.SetLogger(new Logger {LogMessage = cb});
22
23 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
24 Console.OutputEncoding = Encoding.UTF8;
25 Ansi.WindowsConsole.TryEnableVirtualTerminalProcessing();
26 }
27
28 var asmPath = new Uri(typeof(DemoProgram).Assembly.CodeBase!).LocalPath;
29 var asmDir = Path.GetDirectoryName(asmPath)!;
30 var tempDir = Path.GetTempPath();
31 // find a place to stash instance storage
32 string storagePath;
33 do {
34 storagePath = Path.Combine(tempDir, Guid.NewGuid().ToString());
35 } while (Directory.Exists(storagePath) || File.Exists(storagePath));
36
37 var cfg = Config.Create();
38
39 {
40 var cachePath = String.Create(Path.Combine(storagePath, "Cache"));
41 cfg->SetCachePath(cachePath);
42 cachePath->Destroy();
43 }
44
45 {
46 var resourcePath = String.Create(Path.Combine(asmDir, "resources"));
47 cfg->SetResourcePath(resourcePath);
48 resourcePath->Destroy();
49 }
50
51 cfg->SetUseGpuRenderer(false);
52 cfg->SetEnableImages(true);
53 cfg->SetEnableJavaScript(false);
54
55 AppCore.EnablePlatformFontLoader();
56
57 {
58 var assetsPath = String.Create(Path.Combine(asmDir, "assets"));
59 AppCore.EnablePlatformFileSystem(assetsPath);
60 assetsPath->Destroy();
61 }
62
63 var renderer = Renderer.Create(cfg);
64 var sessionName = String.Create("Demo");
65 var session = Session.Create(renderer, false, sessionName);
66
67 var view = View.Create(renderer, 640, 480, false, session);
68
69 {
70 var htmlString = String.Create("<i>Loading...</i>");
71 Console.WriteLine($"Loading HTML: {htmlString->Read()}");
72 view->LoadHtml(htmlString);
73 htmlString->Destroy();
74 }
75
76 var loaded = false;
77
78 view->SetFinishLoadingCallback((data, caller, frameId, isMainFrame, url) => {
79 Console.WriteLine($"Loading Finished, URL: 0x{(ulong) url:X8} {url->Read()}");
80
81 loaded = true;
82 }, null);
83
84 while (!loaded) {
85 Ultralight.Update(renderer);
86 Ultralight.Render(renderer);
87 }
88
89 /*
90 {
91 var surface = view->GetSurface();
92 var bitmap = surface->GetBitmap();
93 var pixels = bitmap->LockPixels();
94 RenderAnsi<Bgra32>(pixels, bitmap->GetWidth(), bitmap->GetHeight(), bitmap->GetBpp());
95 Console.WriteLine();
96 bitmap->UnlockPixels();
97 bitmap->SwapRedBlueChannels();
98 //bitmap->WritePng("Loading.png");
99 }
100 */
101
102 loaded = false;
103
104 {
105 var urlString = String.Create("file:///index.html");
106 Console.WriteLine($"Loading URL: {urlString->Read()}");
107 view->LoadUrl(urlString);
108 urlString->Destroy();
109 }
110
111 while (!loaded) {
112 //Ultralight.Update(renderer);
113 renderer->Update();
114 //Ultralight.Render(renderer);
115 renderer->Render();
116 }
117
118 {
119 var urlStrPtr = view->GetUrl();
120 Console.WriteLine($"After Loaded View GetURL: 0x{(ulong) urlStrPtr:X8} {urlStrPtr->Read()}");
121 }
122
123 {
124 var surface = view->GetSurface();
125 var bitmap = surface->GetBitmap();
126 var pixels = bitmap->LockPixels();
127 RenderAnsi<Bgra32>(pixels, bitmap->GetWidth(), bitmap->GetHeight());
128 bitmap->UnlockPixels();
129 bitmap->SwapRedBlueChannels();
130 //bitmap->WritePng("Loaded.png");
131 }
132
133 view->Destroy();
134
135 session->Destroy();
136 renderer->Destroy();
137 cfg->Destroy();
138
139 try {
140 Directory.Delete(storagePath, true);
141 }
142 catch {
143 /* ok */
144 }
145
146 //Console.WriteLine("Press any key to exit.");
147 //Console.ReadKey(true);
148 }
149
150 private static unsafe void LoggerCallback(LogLevel logLevel, String* msg)
151 => Console.WriteLine($"{logLevel.ToString()}: {msg->Read()}");
152
153 }
154
155}