|
| 1 | +// "Therefore those skilled at the unorthodox |
| 2 | +// are infinite as heaven and earth, |
| 3 | +// inexhaustible as the great rivers. |
| 4 | +// When they come to an end, |
| 5 | +// they begin again, |
| 6 | +// like the days and months; |
| 7 | +// they die and are reborn, |
| 8 | +// like the four seasons." |
| 9 | +// |
| 10 | +// - Sun Tsu, |
| 11 | +// "The Art of War" |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Collections.Generic; |
| 15 | +using System.Diagnostics; |
| 16 | +using System.IO; |
| 17 | +using System.Threading.Tasks; |
| 18 | +using Microsoft.Graphics.Canvas; |
| 19 | +using Microsoft.UI.Xaml; |
| 20 | +using Microsoft.UI.Xaml.Controls; |
| 21 | +using TheArtOfDev.HtmlRenderer.Core.Entities; |
| 22 | +using TheArtOfDev.HtmlRenderer.Demo.Common; |
| 23 | +using Windows.Storage.Streams; |
| 24 | + |
| 25 | +namespace TheArtOfDev.HtmlRenderer.Demo.WinUI |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// Hosts a <see cref="TreeView"/> of the same showcase/test/performance samples the WPF and WinForms |
| 29 | + /// demos expose (via the shared, UI-framework-agnostic <see cref="SamplesLoader"/>/<see cref="HtmlSample"/> |
| 30 | + /// from <c>HtmlRenderer.Demo.Common</c>) next to a <see cref="TheArtOfDev.HtmlRenderer.WinUI.HtmlPanel"/> |
| 31 | + /// that renders whichever sample is selected. |
| 32 | + /// </summary> |
| 33 | + /// <remarks> |
| 34 | + /// Node content is a plain <see cref="string"/> (the sample/category name), matching Microsoft's own |
| 35 | + /// documented <c>RootNodes</c> pattern exactly (see |
| 36 | + /// https://learn.microsoft.com/en-us/windows/apps/develop/ui/controls/tree-view) - no |
| 37 | + /// <see cref="TreeView.ItemTemplate"/> is needed for that case, since a string displays directly. An |
| 38 | + /// earlier version wrapped each node's content in a custom object and bound it via |
| 39 | + /// <c>TreeView.ItemTemplate</c>'s <c>{Binding Name}</c>, which rendered no text and never expanded - |
| 40 | + /// switched to this simpler, documented shape instead of chasing the exact binding-context rules for |
| 41 | + /// that pattern. <see cref="_nodeToSample"/> maps each leaf node back to its <see cref="HtmlSample"/> |
| 42 | + /// since the node's own displayed Content is just its name. |
| 43 | + /// </remarks> |
| 44 | + public sealed partial class MainWindow : Window |
| 45 | + { |
| 46 | + private readonly Dictionary<TreeViewNode, HtmlSample> _nodeToSample = new Dictionary<TreeViewNode, HtmlSample>(); |
| 47 | + |
| 48 | + public MainWindow() |
| 49 | + { |
| 50 | + InitializeComponent(); |
| 51 | + |
| 52 | + Title = "HTML Renderer - WinUI 3 Demo"; |
| 53 | + |
| 54 | + SamplesLoader.Init("WinUI", typeof(TheArtOfDev.HtmlRenderer.WinUI.HtmlRender).Assembly.GetName().Version.ToString()); |
| 55 | + |
| 56 | + // Without this, showcase samples' <link rel="Stylesheet" href="StyleSheet"> never resolves, so |
| 57 | + // e.g. Intro.htm's `.whitehole { background-color:white; border-radius:10px }` class is simply |
| 58 | + // never applied at all - not a paint bug, the rule itself never loads. DemoUtils.OnStylesheetLoad |
| 59 | + // is the same platform-agnostic handler the WPF/WinForms demos use. |
| 60 | + RendererPanel.StylesheetLoad += DemoUtils.OnStylesheetLoad; |
| 61 | + |
| 62 | + // Same idea as StylesheetLoad above: samples reference icons by logical key (e.g. |
| 63 | + // src="HtmlIcon") that DemoUtils.GetImageStream resolves to an embedded resource - without |
| 64 | + // this, every such <img> falls through to the built-in "failed to load" icon instead. |
| 65 | + RendererPanel.ImageLoad += OnImageLoad; |
| 66 | + |
| 67 | + LoadSamples(); |
| 68 | + } |
| 69 | + |
| 70 | + private void LoadSamples() |
| 71 | + { |
| 72 | + var showcaseRoot = new TreeViewNode { Content = "HTML Renderer" }; |
| 73 | + SamplesTreeView.RootNodes.Add(showcaseRoot); |
| 74 | + foreach (var sample in SamplesLoader.ShowcaseSamples) |
| 75 | + AddSampleNode(showcaseRoot, sample); |
| 76 | + |
| 77 | + var testSamplesRoot = new TreeViewNode { Content = "Test Samples" }; |
| 78 | + SamplesTreeView.RootNodes.Add(testSamplesRoot); |
| 79 | + foreach (var sample in SamplesLoader.TestSamples) |
| 80 | + AddSampleNode(testSamplesRoot, sample); |
| 81 | + |
| 82 | + if (SamplesLoader.PerformanceSamples.Count > 0) |
| 83 | + { |
| 84 | + var perfSamplesRoot = new TreeViewNode { Content = "Performance Samples" }; |
| 85 | + SamplesTreeView.RootNodes.Add(perfSamplesRoot); |
| 86 | + foreach (var sample in SamplesLoader.PerformanceSamples) |
| 87 | + AddSampleNode(perfSamplesRoot, sample); |
| 88 | + } |
| 89 | + |
| 90 | + showcaseRoot.IsExpanded = true; |
| 91 | + if (showcaseRoot.Children.Count > 0) |
| 92 | + { |
| 93 | + var firstNode = showcaseRoot.Children[0]; |
| 94 | + SamplesTreeView.SelectedNode = firstNode; |
| 95 | + ShowSample(_nodeToSample[firstNode]); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void AddSampleNode(TreeViewNode root, HtmlSample sample) |
| 100 | + { |
| 101 | + var node = new TreeViewNode { Content = sample.Name }; |
| 102 | + _nodeToSample[node] = sample; |
| 103 | + root.Children.Add(node); |
| 104 | + } |
| 105 | + |
| 106 | + private void OnSamplesTreeViewSelectionChanged(TreeView sender, TreeViewSelectionChangedEventArgs args) |
| 107 | + { |
| 108 | + foreach (var node in sender.SelectedNodes) |
| 109 | + { |
| 110 | + if (_nodeToSample.TryGetValue(node, out var sample)) |
| 111 | + { |
| 112 | + ShowSample(sample); |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private void ShowSample(HtmlSample sample) |
| 119 | + { |
| 120 | + try |
| 121 | + { |
| 122 | + RendererPanel.AvoidImagesLateLoading = !sample.FullName.Contains("Many images"); |
| 123 | + RendererPanel.Text = sample.Html; |
| 124 | + } |
| 125 | + catch (Exception ex) |
| 126 | + { |
| 127 | + Debug.WriteLine(ex); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Resolves the demo's logical icon keys (e.g. <c>src="HtmlIcon"</c>) via |
| 133 | + /// <see cref="DemoUtils.GetImageStream"/> - real URLs/paths DemoUtils doesn't recognize fall |
| 134 | + /// through untouched (<see cref="HtmlImageLoadEventArgs.Handled"/> left false) so the default |
| 135 | + /// loader still handles them. |
| 136 | + /// </summary> |
| 137 | + private void OnImageLoad(object sender, HtmlImageLoadEventArgs e) |
| 138 | + { |
| 139 | + var stream = DemoUtils.GetImageStream(e.Src); |
| 140 | + if (stream == null) |
| 141 | + return; |
| 142 | + |
| 143 | + e.Handled = true; |
| 144 | + _ = LoadIconAsync(e, stream); |
| 145 | + } |
| 146 | + |
| 147 | + private static async Task LoadIconAsync(HtmlImageLoadEventArgs e, Stream stream) |
| 148 | + { |
| 149 | + try |
| 150 | + { |
| 151 | + byte[] bytes; |
| 152 | + using (stream) |
| 153 | + using (var memory = new MemoryStream()) |
| 154 | + { |
| 155 | + await stream.CopyToAsync(memory).ConfigureAwait(false); |
| 156 | + bytes = memory.ToArray(); |
| 157 | + } |
| 158 | + |
| 159 | + using var randomAccessStream = new InMemoryRandomAccessStream(); |
| 160 | + using (var writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0))) |
| 161 | + { |
| 162 | + writer.WriteBytes(bytes); |
| 163 | + await writer.StoreAsync(); |
| 164 | + await writer.FlushAsync(); |
| 165 | + writer.DetachStream(); |
| 166 | + } |
| 167 | + randomAccessStream.Seek(0); |
| 168 | + |
| 169 | + var bitmap = await CanvasBitmap.LoadAsync(CanvasDevice.GetSharedDevice(), randomAccessStream); |
| 170 | + e.Callback(bitmap); |
| 171 | + } |
| 172 | + catch (Exception ex) |
| 173 | + { |
| 174 | + Debug.WriteLine(ex); |
| 175 | + e.Callback(); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments