index.html 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. <!DOCTYPE HTML>
  2. <html lang="en" class="sidebar-visible no-js light">
  3. <head>
  4. <!-- Book generated using mdBook -->
  5. <meta charset="UTF-8">
  6. <title>Custom Renderer</title>
  7. <!-- Custom HTML head -->
  8. <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  9. <meta name="description" content="">
  10. <meta name="viewport" content="width=device-width, initial-scale=1">
  11. <meta name="theme-color" content="#ffffff" />
  12. <link rel="icon" href="../favicon.svg">
  13. <link rel="shortcut icon" href="../favicon.png">
  14. <link rel="stylesheet" href="../css/variables.css">
  15. <link rel="stylesheet" href="../css/general.css">
  16. <link rel="stylesheet" href="../css/chrome.css">
  17. <link rel="stylesheet" href="../css/print.css" media="print">
  18. <!-- Fonts -->
  19. <link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
  20. <link rel="stylesheet" href="../fonts/fonts.css">
  21. <!-- Highlight.js Stylesheets -->
  22. <link rel="stylesheet" href="../highlight.css">
  23. <link rel="stylesheet" href="../tomorrow-night.css">
  24. <link rel="stylesheet" href="../ayu-highlight.css">
  25. <!-- Custom theme stylesheets -->
  26. <!-- MathJax -->
  27. <script async type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  28. </head>
  29. <body>
  30. <!-- Provide site root to javascript -->
  31. <script type="text/javascript">
  32. var path_to_root = "../";
  33. var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
  34. </script>
  35. <!-- Work around some values being stored in localStorage wrapped in quotes -->
  36. <script type="text/javascript">
  37. try {
  38. var theme = localStorage.getItem('mdbook-theme');
  39. var sidebar = localStorage.getItem('mdbook-sidebar');
  40. if (theme.startsWith('"') && theme.endsWith('"')) {
  41. localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
  42. }
  43. if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
  44. localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
  45. }
  46. } catch (e) { }
  47. </script>
  48. <!-- Set the theme before any content is loaded, prevents flash -->
  49. <script type="text/javascript">
  50. var theme;
  51. try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
  52. if (theme === null || theme === undefined) { theme = default_theme; }
  53. var html = document.querySelector('html');
  54. html.classList.remove('no-js')
  55. html.classList.remove('light')
  56. html.classList.add(theme);
  57. html.classList.add('js');
  58. </script>
  59. <!-- Hide / unhide sidebar before it is displayed -->
  60. <script type="text/javascript">
  61. var html = document.querySelector('html');
  62. var sidebar = 'hidden';
  63. if (document.body.clientWidth >= 1080) {
  64. try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
  65. sidebar = sidebar || 'visible';
  66. }
  67. html.classList.remove('sidebar-visible');
  68. html.classList.add("sidebar-" + sidebar);
  69. </script>
  70. <nav id="sidebar" class="sidebar" aria-label="Table of contents">
  71. <div class="sidebar-scrollbox">
  72. <ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Introduction</a></li><li class="chapter-item expanded "><a href="../getting_started/index.html"><strong aria-hidden="true">1.</strong> Getting Started</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../getting_started/desktop.html"><strong aria-hidden="true">1.1.</strong> Desktop</a></li><li class="chapter-item expanded "><a href="../getting_started/web.html"><strong aria-hidden="true">1.2.</strong> Web</a></li><li class="chapter-item expanded "><a href="../getting_started/ssr.html"><strong aria-hidden="true">1.3.</strong> Server-Side Rendering</a></li><li class="chapter-item expanded "><a href="../getting_started/fullstack.html"><strong aria-hidden="true">1.4.</strong> Fullstack</a></li><li class="chapter-item expanded "><a href="../getting_started/liveview.html"><strong aria-hidden="true">1.5.</strong> Liveview</a></li><li class="chapter-item expanded "><a href="../getting_started/tui.html"><strong aria-hidden="true">1.6.</strong> Terminal UI</a></li><li class="chapter-item expanded "><a href="../getting_started/mobile.html"><strong aria-hidden="true">1.7.</strong> Mobile</a></li><li class="chapter-item expanded "><a href="../getting_started/hot_reload.html"><strong aria-hidden="true">1.8.</strong> Hot Reloading</a></li></ol></li><li class="chapter-item expanded "><a href="../describing_ui/index.html"><strong aria-hidden="true">2.</strong> Describing the UI</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../describing_ui/special_attributes.html"><strong aria-hidden="true">2.1.</strong> Special Attributes</a></li><li class="chapter-item expanded "><a href="../describing_ui/components.html"><strong aria-hidden="true">2.2.</strong> Components</a></li><li class="chapter-item expanded "><a href="../describing_ui/component_props.html"><strong aria-hidden="true">2.3.</strong> Props</a></li><li class="chapter-item expanded "><a href="../describing_ui/component_children.html"><strong aria-hidden="true">2.4.</strong> Component Children</a></li></ol></li><li class="chapter-item expanded "><a href="../interactivity/index.html"><strong aria-hidden="true">3.</strong> Interactivity</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../interactivity/event_handlers.html"><strong aria-hidden="true">3.1.</strong> Event Listeners</a></li><li class="chapter-item expanded "><a href="../interactivity/hooks.html"><strong aria-hidden="true">3.2.</strong> Hooks &amp; Component State</a></li><li class="chapter-item expanded "><a href="../interactivity/user_input.html"><strong aria-hidden="true">3.3.</strong> User Input</a></li><li class="chapter-item expanded "><a href="../interactivity/sharing_state.html"><strong aria-hidden="true">3.4.</strong> Sharing State</a></li><li class="chapter-item expanded "><a href="../interactivity/custom_hooks.html"><strong aria-hidden="true">3.5.</strong> Custom Hooks</a></li><li class="chapter-item expanded "><a href="../interactivity/dynamic_rendering.html"><strong aria-hidden="true">3.6.</strong> Dynamic Rendering</a></li><li class="chapter-item expanded "><a href="../interactivity/router.html"><strong aria-hidden="true">3.7.</strong> Routing</a></li></ol></li><li class="chapter-item expanded "><a href="../async/index.html"><strong aria-hidden="true">4.</strong> Async</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../async/use_future.html"><strong aria-hidden="true">4.1.</strong> UseFuture</a></li><li class="chapter-item expanded "><a href="../async/use_coroutine.html"><strong aria-hidden="true">4.2.</strong> UseCoroutine</a></li><li class="chapter-item expanded "><a href="../async/spawn.html"><strong aria-hidden="true">4.3.</strong> Spawning Futures</a></li></ol></li><li class="chapter-item expanded "><a href="../best_practices/index.html"><strong aria-hidden="true">5.</strong> Best Practices</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../best_practices/error_handling.html"><strong aria-hidden="true">5.1.</strong> Error Handling</a></li><li class="chapter-item expanded "><a href="../best_practices/antipatterns.html"><strong aria-hidden="true">5.2.</strong> Antipatterns</a></li></ol></li><li class="chapter-item expanded "><a href="../publishing/index.html"><strong aria-hidden="true">6.</strong> Publishing</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../publishing/desktop.html"><strong aria-hidden="true">6.1.</strong> Desktop</a></li><li class="chapter-item expanded "><a href="../publishing/web.html"><strong aria-hidden="true">6.2.</strong> Web</a></li><li class="spacer"></li></ol></li><li class="chapter-item expanded "><a href="../fullstack/index.html"><strong aria-hidden="true">7.</strong> Fullstack</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../fullstack/getting_started.html"><strong aria-hidden="true">7.1.</strong> Getting Started</a></li><li class="chapter-item expanded "><a href="../fullstack/server_functions.html"><strong aria-hidden="true">7.2.</strong> Communicating with the Server</a></li><li class="spacer"></li></ol></li><li class="chapter-item expanded "><a href="../custom_renderer/index.html" class="active"><strong aria-hidden="true">8.</strong> Custom Renderer</a></li><li class="spacer"></li><li class="chapter-item expanded "><a href="../contributing/index.html"><strong aria-hidden="true">9.</strong> Contributing</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../contributing/project_structure.html"><strong aria-hidden="true">9.1.</strong> Project Structure</a></li><li class="chapter-item expanded "><a href="../contributing/walkthrough_readme.html"><strong aria-hidden="true">9.2.</strong> Walkthrough of Internals</a></li><li class="chapter-item expanded "><a href="../contributing/guiding_principles.html"><strong aria-hidden="true">9.3.</strong> Guiding Principles</a></li><li class="chapter-item expanded "><a href="../contributing/roadmap.html"><strong aria-hidden="true">9.4.</strong> Roadmap</a></li></ol></li></ol>
  73. </div>
  74. <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
  75. </nav>
  76. <div id="page-wrapper" class="page-wrapper">
  77. <div class="page">
  78. <div id="menu-bar-hover-placeholder"></div>
  79. <div id="menu-bar" class="menu-bar sticky bordered">
  80. <div class="left-buttons">
  81. <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
  82. <i class="fa fa-bars"></i>
  83. </button>
  84. <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
  85. <i class="fa fa-paint-brush"></i>
  86. </button>
  87. <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
  88. <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
  89. <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
  90. <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
  91. <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
  92. <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
  93. </ul>
  94. <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
  95. <i class="fa fa-search"></i>
  96. </button>
  97. <button id="language-toggle" class="icon-button" type="button" title="Select language" aria-label="Select language" aria-haspopup="true" aria-expanded="false" aria-controls="language-list">
  98. <i class="fa fa-globe"></i>
  99. </button>
  100. <ul id="language-list" class="language-popup" aria-label="Languages" role="menu">
  101. <li role="none"><a href="../../en/custom_renderer/index.html"><button role="menuitem" class="language" id="light">English</button></a></li>
  102. <li role="none"><a href="../../pt-br/custom_renderer/index.html"><button role="menuitem" class="language" id="light">Português Brasileiro</button></a></li>
  103. </ul>
  104. </div>
  105. <h1 class="menu-title"></h1>
  106. <div class="right-buttons">
  107. <a href="../print.html" title="Print this book" aria-label="Print this book">
  108. <i id="print-button" class="fa fa-print"></i>
  109. </a>
  110. <a href="https://github.com/DioxusLabs/dioxus/edit/master/docs/guide" title="Git repository" aria-label="Git repository">
  111. <i id="git-repository-button" class="fa fa-github"></i>
  112. </a>
  113. <a href="https://github.com/DioxusLabs/dioxus/edit/master/docs/guide/src/custom_renderer/index.md" title="Suggest an edit" aria-label="Suggest an edit">
  114. <i id="git-edit-button" class="fa fa-edit"></i>
  115. </a>
  116. </div>
  117. </div>
  118. <div id="search-wrapper" class="hidden">
  119. <form id="searchbar-outer" class="searchbar-outer">
  120. <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
  121. </form>
  122. <div id="searchresults-outer" class="searchresults-outer hidden">
  123. <div id="searchresults-header" class="searchresults-header"></div>
  124. <ul id="searchresults">
  125. </ul>
  126. </div>
  127. </div>
  128. <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
  129. <script type="text/javascript">
  130. document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
  131. document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
  132. Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
  133. link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
  134. });
  135. </script>
  136. <div id="content" class="content">
  137. <main>
  138. <h1 id="custom-renderer"><a class="header" href="#custom-renderer">Custom Renderer</a></h1>
  139. <p>Dioxus is an incredibly portable framework for UI development. The lessons, knowledge, hooks, and components you acquire over time can always be used for future projects. However, sometimes those projects cannot leverage a supported renderer or you need to implement your own better renderer.</p>
  140. <p>Great news: the design of the renderer is entirely up to you! We provide suggestions and inspiration with the 1st party renderers, but only really require processing <code>Mutations</code> and sending <code>UserEvents</code>.</p>
  141. <h2 id="the-specifics"><a class="header" href="#the-specifics">The specifics:</a></h2>
  142. <p>Implementing the renderer is fairly straightforward. The renderer needs to:</p>
  143. <ol>
  144. <li>Handle the stream of edits generated by updates to the virtual DOM</li>
  145. <li>Register listeners and pass events into the virtual DOM's event system</li>
  146. </ol>
  147. <p>Essentially, your renderer needs to process edits and generate events to update the VirtualDOM. From there, you'll have everything needed to render the VirtualDOM to the screen.</p>
  148. <p>Internally, Dioxus handles the tree relationship, diffing, memory management, and the event system, leaving as little as possible required for renderers to implement themselves.</p>
  149. <p>For reference, check out the <a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/interpreter">javascript interpreter</a> or <a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/dioxus-tui">tui renderer</a> as a starting point for your custom renderer.</p>
  150. <h2 id="templates"><a class="header" href="#templates">Templates</a></h2>
  151. <p>Dioxus is built around the concept of <a href="https://docs.rs/dioxus-core/latest/dioxus_core/prelude/struct.Template.html">Templates</a>. Templates describe a UI tree known at compile time with dynamic parts filled at runtime. This is useful internally to make skip diffing static nodes, but it is also useful for the renderer to reuse parts of the UI tree. This can be useful for things like a list of items. Each item could contain some static parts and some dynamic parts. The renderer can use the template to create a static part of the UI once, clone it for each element in the list, and then fill in the dynamic parts.</p>
  152. <h2 id="mutations"><a class="header" href="#mutations">Mutations</a></h2>
  153. <p>The <code>Mutation</code> type is a serialized enum that represents an operation that should be applied to update the UI. The variants roughly follow this set:</p>
  154. <pre><pre class="playground"><code class="language-rust edition2018">
  155. <span class="boring">#![allow(unused)]
  156. </span><span class="boring">fn main() {
  157. </span>enum Mutation {
  158. AppendChildren,
  159. AssignId,
  160. CreatePlaceholder,
  161. CreateTextNode,
  162. HydrateText,
  163. LoadTemplate,
  164. ReplaceWith,
  165. ReplacePlaceholder,
  166. InsertAfter,
  167. InsertBefore,
  168. SetAttribute,
  169. SetText,
  170. NewEventListener,
  171. RemoveEventListener,
  172. Remove,
  173. PushRoot,
  174. }
  175. <span class="boring">}
  176. </span></code></pre></pre>
  177. <p>The Dioxus diffing mechanism operates as a <a href="https://en.wikipedia.org/wiki/Stack_machine">stack machine</a> where the <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.LoadTemplate">LoadTemplate</a>, <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.CreatePlaceholder">CreatePlaceholder</a>, and <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.CreateTextNode">CreateTextNode</a> mutations pushes a new &quot;real&quot; DOM node onto the stack and <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.AppendChildren">AppendChildren</a>, <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.InsertAfter">InsertAfter</a>, <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.InsertBefore">InsertBefore</a>, <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.ReplacePlaceholder">ReplacePlaceholder</a>, and <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.ReplaceWith">ReplaceWith</a> all remove nodes from the stack.</p>
  178. <h2 id="node-storage"><a class="header" href="#node-storage">Node storage</a></h2>
  179. <p>Dioxus saves and loads elements with IDs. Inside the VirtualDOM, this is just tracked as as a u64.</p>
  180. <p>Whenever a <code>CreateElement</code> edit is generated during diffing, Dioxus increments its node counter and assigns that new element its current NodeCount. The RealDom is responsible for remembering this ID and pushing the correct node when id is used in a mutation. Dioxus reclaims the IDs of elements when removed. To stay in sync with Dioxus you can use a sparse Vec (Vec&lt;Option<T>&gt;) with possibly unoccupied items. You can use the ids as indexes into the Vec for elements, and grow the Vec when an id does not exist.</p>
  181. <h3 id="an-example"><a class="header" href="#an-example">An Example</a></h3>
  182. <p>For the sake of understanding, let's consider this example – a very simple UI declaration:</p>
  183. <pre><pre class="playground"><code class="language-rust edition2018">
  184. <span class="boring">#![allow(unused)]
  185. </span><span class="boring">fn main() {
  186. </span>rsx!( h1 {&quot;count: {x}&quot;} )
  187. <span class="boring">}
  188. </span></code></pre></pre>
  189. <h4 id="building-templates"><a class="header" href="#building-templates">Building Templates</a></h4>
  190. <p>The above rsx will create a template that contains one static h1 tag and a placeholder for a dynamic text node. The template contains the static parts of the UI, and ids for the dynamic parts along with the paths to access them.</p>
  191. <p>The template will look something like this:</p>
  192. <pre><pre class="playground"><code class="language-rust edition2018">
  193. <span class="boring">#![allow(unused)]
  194. </span><span class="boring">fn main() {
  195. </span>Template {
  196. // Some id that is unique for the entire project
  197. name: &quot;main.rs:1:1:0&quot;,
  198. // The root nodes of the template
  199. roots: &amp;[
  200. TemplateNode::Element {
  201. tag: &quot;h1&quot;,
  202. namespace: None,
  203. attrs: &amp;[],
  204. children: &amp;[
  205. TemplateNode::DynamicText {
  206. id: 0
  207. },
  208. ],
  209. }
  210. ],
  211. // the path to each of the dynamic nodes
  212. node_paths: &amp;[
  213. // the path to dynamic node with a id of 0
  214. &amp;[
  215. // on the first root node
  216. 0,
  217. // the first child of the root node
  218. 0,
  219. ]
  220. ],
  221. // the path to each of the dynamic attributes
  222. attr_paths: &amp;'a [&amp;'a [u8]],
  223. }
  224. <span class="boring">}
  225. </span></code></pre></pre>
  226. <blockquote>
  227. <p>For more detailed docs about the struture of templates see the <a href="https://docs.rs/dioxus-core/latest/dioxus_core/prelude/struct.Template.html">Template api docs</a></p>
  228. </blockquote>
  229. <p>This template will be sent to the renderer in the <a href="https://docs.rs/dioxus-core/latest/dioxus_core/struct.Mutations.html#structfield.templates">list of templates</a> supplied with the mutations the first time it is used. Any time the renderer encounters a <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.LoadTemplate">LoadTemplate</a> mutation after this, it should clone the template and store it in the given id.</p>
  230. <p>For dynamic nodes and dynamic text nodes, a placeholder node should be created and inserted into the UI so that the node can be modified later.</p>
  231. <p>In HTML renderers, this template could look like this:</p>
  232. <pre><code class="language-html">&lt;h1&gt;&quot;&quot;&lt;/h1&gt;
  233. </code></pre>
  234. <h4 id="applying-mutations"><a class="header" href="#applying-mutations">Applying Mutations</a></h4>
  235. <p>After the renderer has created all of the new templates, it can begin to process the mutations.</p>
  236. <p>When the renderer starts, it should contain the Root node on the stack and store the Root node with an id of 0. The Root node is the top-level node of the UI. In HTML, this is the <code>&lt;div id=&quot;main&quot;&gt;</code> element.</p>
  237. <pre><pre class="playground"><code class="language-rust edition2018">
  238. <span class="boring">#![allow(unused)]
  239. </span><span class="boring">fn main() {
  240. </span>instructions: []
  241. stack: [
  242. RootNode,
  243. ]
  244. nodes: [
  245. RootNode,
  246. ]
  247. <span class="boring">}
  248. </span></code></pre></pre>
  249. <p>The first mutation is a <code>LoadTemplate</code> mutation. This tells the renderer to load a root from the template with the given id. The renderer will then push the root node of the template onto the stack and store it with an id for later. In this case, the root node is an h1 element.</p>
  250. <pre><pre class="playground"><code class="language-rust edition2018">
  251. <span class="boring">#![allow(unused)]
  252. </span><span class="boring">fn main() {
  253. </span>instructions: [
  254. LoadTemplate {
  255. // the id of the template
  256. name: &quot;main.rs:1:1:0&quot;,
  257. // the index of the root node in the template
  258. index: 0,
  259. // the id to store
  260. id: ElementId(1),
  261. }
  262. ]
  263. stack: [
  264. RootNode,
  265. &lt;h1&gt;&quot;&quot;&lt;/h1&gt;,
  266. ]
  267. nodes: [
  268. RootNode,
  269. &lt;h1&gt;&quot;&quot;&lt;/h1&gt;,
  270. ]
  271. <span class="boring">}
  272. </span></code></pre></pre>
  273. <p>Next, Dioxus will create the dynamic text node. The diff algorithm decides that this node needs to be created, so Dioxus will generate the Mutation <code>HydrateText</code>. When the renderer receives this instruction, it will navigate to the placeholder text node in the template and replace it with the new text.</p>
  274. <pre><pre class="playground"><code class="language-rust edition2018">
  275. <span class="boring">#![allow(unused)]
  276. </span><span class="boring">fn main() {
  277. </span>instructions: [
  278. LoadTemplate {
  279. name: &quot;main.rs:1:1:0&quot;,
  280. index: 0,
  281. id: ElementId(1),
  282. },
  283. HydrateText {
  284. // the id to store the text node
  285. id: ElementId(2),
  286. // the text to set
  287. text: &quot;count: 0&quot;,
  288. }
  289. ]
  290. stack: [
  291. RootNode,
  292. &lt;h1&gt;&quot;count: 0&quot;&lt;/h1&gt;,
  293. ]
  294. nodes: [
  295. RootNode,
  296. &lt;h1&gt;&quot;count: 0&quot;&lt;/h1&gt;,
  297. &quot;count: 0&quot;,
  298. ]
  299. <span class="boring">}
  300. </span></code></pre></pre>
  301. <p>Remember, the h1 node is not attached to anything (it is unmounted) so Dioxus needs to generate an Edit that connects the h1 node to the Root. It depends on the situation, but in this case, we use <code>AppendChildren</code>. This pops the text node off the stack, leaving the Root element as the next element on the stack.</p>
  302. <pre><pre class="playground"><code class="language-rust edition2018">
  303. <span class="boring">#![allow(unused)]
  304. </span><span class="boring">fn main() {
  305. </span>instructions: [
  306. LoadTemplate {
  307. name: &quot;main.rs:1:1:0&quot;,
  308. index: 0,
  309. id: ElementId(1),
  310. },
  311. HydrateText {
  312. id: ElementId(2),
  313. text: &quot;count: 0&quot;,
  314. },
  315. AppendChildren {
  316. // the id of the parent node
  317. id: ElementId(0),
  318. // the number of nodes to pop off the stack and append
  319. m: 1
  320. }
  321. ]
  322. stack: [
  323. RootNode,
  324. ]
  325. nodes: [
  326. RootNode,
  327. &lt;h1&gt;&quot;count: 0&quot;&lt;/h1&gt;,
  328. &quot;count: 0&quot;,
  329. ]
  330. <span class="boring">}
  331. </span></code></pre></pre>
  332. <p>Over time, our stack looked like this:</p>
  333. <pre><pre class="playground"><code class="language-rust edition2018">
  334. <span class="boring">#![allow(unused)]
  335. </span><span class="boring">fn main() {
  336. </span>[Root]
  337. [Root, &lt;h1&gt;&quot;&quot;&lt;/h1&gt;]
  338. [Root, &lt;h1&gt;&quot;count: 0&quot;&lt;/h1&gt;]
  339. [Root]
  340. <span class="boring">}
  341. </span></code></pre></pre>
  342. <p>Conveniently, this approach completely separates the Virtual DOM and the Real DOM. Additionally, these edits are serializable, meaning we can even manage UIs across a network connection. This little stack machine and serialized edits make Dioxus independent of platform specifics.</p>
  343. <p>Dioxus is also really fast. Because Dioxus splits the diff and patch phase, it's able to make all the edits to the RealDOM in a very short amount of time (less than a single frame) making rendering very snappy. It also allows Dioxus to cancel large diffing operations if higher priority work comes in while it's diffing.</p>
  344. <p>This little demo serves to show exactly how a Renderer would need to process a mutation stream to build UIs.</p>
  345. <h2 id="event-loop"><a class="header" href="#event-loop">Event loop</a></h2>
  346. <p>Like most GUIs, Dioxus relies on an event loop to progress the VirtualDOM. The VirtualDOM itself can produce events as well, so it's important for your custom renderer can handle those too.</p>
  347. <p>The code for the WebSys implementation is straightforward, so we'll add it here to demonstrate how simple an event loop is:</p>
  348. <pre><code class="language-rust ignore">pub async fn run(&amp;mut self) -&gt; dioxus_core::error::Result&lt;()&gt; {
  349. // Push the body element onto the WebsysDom's stack machine
  350. let mut websys_dom = crate::new::WebsysDom::new(prepare_websys_dom());
  351. websys_dom.stack.push(root_node);
  352. // Rebuild or hydrate the virtualdom
  353. let mutations = self.internal_dom.rebuild();
  354. websys_dom.apply_mutations(mutations);
  355. // Wait for updates from the real dom and progress the virtual dom
  356. loop {
  357. let user_input_future = websys_dom.wait_for_event();
  358. let internal_event_future = self.internal_dom.wait_for_work();
  359. match select(user_input_future, internal_event_future).await {
  360. Either::Left((_, _)) =&gt; {
  361. let mutations = self.internal_dom.work_with_deadline(|| false);
  362. websys_dom.apply_mutations(mutations);
  363. },
  364. Either::Right((event, _)) =&gt; websys_dom.handle_event(event),
  365. }
  366. // render
  367. }
  368. }
  369. </code></pre>
  370. <p>It's important to decode what the real events are for your event system into Dioxus' synthetic event system (synthetic meaning abstracted). This simply means matching your event type and creating a Dioxus <code>UserEvent</code> type. Right now, the virtual event system is modeled almost entirely around the HTML spec, but we are interested in slimming it down.</p>
  371. <pre><code class="language-rust ignore">fn virtual_event_from_websys_event(event: &amp;web_sys::Event) -&gt; VirtualEvent {
  372. match event.type_().as_str() {
  373. &quot;keydown&quot; =&gt; {
  374. let event: web_sys::KeyboardEvent = event.clone().dyn_into().unwrap();
  375. UserEvent::KeyboardEvent(UserEvent {
  376. scope_id: None,
  377. priority: EventPriority::Medium,
  378. name: &quot;keydown&quot;,
  379. // This should be whatever element is focused
  380. element: Some(ElementId(0)),
  381. data: Arc::new(KeyboardData{
  382. char_code: event.char_code(),
  383. key: event.key(),
  384. key_code: event.key_code(),
  385. alt_key: event.alt_key(),
  386. ctrl_key: event.ctrl_key(),
  387. meta_key: event.meta_key(),
  388. shift_key: event.shift_key(),
  389. location: event.location(),
  390. repeat: event.repeat(),
  391. which: event.which(),
  392. })
  393. })
  394. }
  395. _ =&gt; todo!()
  396. }
  397. }
  398. </code></pre>
  399. <h2 id="custom-raw-elements"><a class="header" href="#custom-raw-elements">Custom raw elements</a></h2>
  400. <p>If you need to go as far as relying on custom elements/attributes for your renderer – you totally can. This still enables you to use Dioxus' reactive nature, component system, shared state, and other features, but will ultimately generate different nodes. All attributes and listeners for the HTML and SVG namespace are shuttled through helper structs that essentially compile away. You can drop in your elements any time you want, with little hassle. However, you must be sure your renderer can handle the new namespace.</p>
  401. <p>For more examples and information on how to create custom namespaces, see the <a href="https://github.com/DioxusLabs/dioxus/blob/master/packages/html/README.md#how-to-extend-it"><code>dioxus_html</code> crate</a>.</p>
  402. <h1 id="native-core"><a class="header" href="#native-core">Native Core</a></h1>
  403. <p>If you are creating a renderer in rust, the <a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/native-core">native-core</a> crate provides some utilities to implement a renderer. It provides an abstraction over Mutations and Templates and contains helpers that can handle the layout and text editing for you.</p>
  404. <h2 id="the-realdom"><a class="header" href="#the-realdom">The RealDom</a></h2>
  405. <p>The <code>RealDom</code> is a higher-level abstraction over updating the Dom. It uses an entity component system to manage the state of nodes. This system allows you to modify insert and modify arbitrary components on nodes. On top of this, the RealDom provides a way to manage a tree of nodes, and the State trait provides a way to automatically add and update these components when the tree is modified. It also provides a way to apply <code>Mutations</code> to the RealDom.</p>
  406. <h3 id="example"><a class="header" href="#example">Example</a></h3>
  407. <p>Let's build a toy renderer with borders, size, and text color.
  408. Before we start let's take a look at an example element we can render:</p>
  409. <pre><pre class="playground"><code class="language-rust edition2018">
  410. <span class="boring">#![allow(unused)]
  411. </span><span class="boring">fn main() {
  412. </span>cx.render(rsx!{
  413. div{
  414. color: &quot;red&quot;,
  415. p{
  416. border: &quot;1px solid black&quot;,
  417. &quot;hello world&quot;
  418. }
  419. }
  420. })
  421. <span class="boring">}
  422. </span></code></pre></pre>
  423. <p>In this tree, the color depends on the parent's color. The layout depends on the children's layout, the current text, and the text size. The border depends on only the current node.</p>
  424. <p>In the following diagram arrows represent dataflow:</p>
  425. <p><a href="https://mermaid.live/edit#pako:eNqllV1vgjAUhv8K6W4wkQVa2QdLdrHsdlfukmSptEhjoaSWqTH-9xVwONAKst70g5739JzzlO5BJAgFAYi52EQJlsr6fAszS7d1sVhKnCdWJDJFt6peLVs5-9owohK7HFrVcFJ_pxnpmK8VVvRkTJikkWIiaxy1dhP23bUwW1WW5WbPrrqJ4ziR4EJ6dtVN2ls5y1ZztePUcrWZFCvqVEcPPDffvlyS1XoLIQnVgnVvVPR6FU9Zc-6dV453ojjOPbuetRJ57gIeXQR3cez7rjtteZyZQ2j5MqmjqwE0ZW0VKx9RKtgpFewp1aw3sXXFy6TWgiYlv8mfq1scD8ofbBCAfQg8_AMBOAyBxzEIwA4CxgQ99QbQkjnD2KT7_CfxGF8_9WXQEsq5sDZCcjICOXRCri4h6r3NA38Q6Jdi1EOx5w3DGDYYI6MUvJFjM3VoGHUeGoMd6mBnDmh2E3fo7O4Yhf0x4OkBmIKUyhQzol_GfbkcApXQlIYg0EOC5SoEYXbQ-3ChxHyXRSBQsqBTUOREx_7OsAY3BUGM-VqvUsKUkB_1U6vf05gtweEHTk4_HQ"><img src="https://mermaid.ink/img/pako:eNqllV1vgjAUhv8K6W4wkQVa2QdLdrHsdlfukmSptEhjoaSWqTH-9xVwONAKst70g5739JzzlO5BJAgFAYi52EQJlsr6fAszS7d1sVhKnCdWJDJFt6peLVs5-9owohK7HFrVcFJ_pxnpmK8VVvRkTJikkWIiaxy1dhP23bUwW1WW5WbPrrqJ4ziR4EJ6dtVN2ls5y1ZztePUcrWZFCvqVEcPPDffvlyS1XoLIQnVgnVvVPR6FU9Zc-6dV453ojjOPbuetRJ57gIeXQR3cez7rjtteZyZQ2j5MqmjqwE0ZW0VKx9RKtgpFewp1aw3sXXFy6TWgiYlv8mfq1scD8ofbBCAfQg8_AMBOAyBxzEIwA4CxgQ99QbQkjnD2KT7_CfxGF8_9WXQEsq5sDZCcjICOXRCri4h6r3NA38Q6Jdi1EOx5w3DGDYYI6MUvJFjM3VoGHUeGoMd6mBnDmh2E3fo7O4Yhf0x4OkBmIKUyhQzol_GfbkcApXQlIYg0EOC5SoEYXbQ-3ChxHyXRSBQsqBTUOREx_7OsAY3BUGM-VqvUsKUkB_1U6vf05gtweEHTk4_HQ?type=png" alt="" /></a></p>
  426. <p>To help in building a Dom, native-core provides the State trait and a RealDom struct. The State trait provides a way to describe how states in a node depend on other states in its relatives. By describing how to update a single node from its relations, native-core will derive a way to update the states of all nodes for you. Once you have a state you can provide it as a generic to RealDom. RealDom provides all of the methods to interact and update your new dom.</p>
  427. <p>Native Core cannot create all of the required methods for the State trait, but it can derive some of them. To implement the State trait, you must implement the following methods and let the <code>#[partial_derive_state]</code> macro handle the rest:</p>
  428. <pre><code class="language-rust ignore">// All states must derive Component (https://docs.rs/shipyard/latest/shipyard/derive.Component.html)
  429. // They also must implement Default or provide a custom implementation of create in the State trait
  430. #[derive(Default, Component)]
  431. struct MyState;
  432. /// Derive some of the boilerplate for the State implementation
  433. #[partial_derive_state]
  434. impl State for MyState {
  435. // The states of the parent nodes this state depends on
  436. type ParentDependencies = ();
  437. // The states of the child nodes this state depends on
  438. type ChildDependencies = (Self,);
  439. // The states of the current node this state depends on
  440. type NodeDependencies = ();
  441. // The parts of the current text, element, or placeholder node in the tree that this state depends on
  442. const NODE_MASK: NodeMaskBuilder&lt;'static&gt; = NodeMaskBuilder::new();
  443. // How to update the state of the current node based on the state of the parent nodes, child nodes, and the current node
  444. // Returns true if the node was updated and false if the node was not updated
  445. fn update&lt;'a&gt;(
  446. &amp;mut self,
  447. // The view of the current node limited to the parts this state depends on
  448. _node_view: NodeView&lt;()&gt;,
  449. // The state of the current node that this state depends on
  450. _node: &lt;Self::NodeDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;,
  451. // The state of the parent nodes that this state depends on
  452. _parent: Option&lt;&lt;Self::ParentDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  453. // The state of the child nodes that this state depends on
  454. _children: Vec&lt;&lt;Self::ChildDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  455. // The context of the current node used to pass global state into the tree
  456. _context: &amp;SendAnyMap,
  457. ) -&gt; bool {
  458. todo!()
  459. }
  460. // partial_derive_state will generate a default implementation of all the other methods
  461. }
  462. </code></pre>
  463. <p>Lets take a look at how to implement the State trait for a simple renderer.</p>
  464. <pre><pre class="playground"><code class="language-rust edition2018">
  465. <span class="boring">#![allow(unused)]
  466. </span><span class="boring">fn main() {
  467. </span>struct FontSize(f64);
  468. // All states need to derive Component
  469. #[derive(Default, Debug, Copy, Clone, Component)]
  470. struct Size(f64, f64);
  471. /// Derive some of the boilerplate for the State implementation
  472. #[partial_derive_state]
  473. impl State for Size {
  474. type ParentDependencies = ();
  475. // The size of the current node depends on the size of its children
  476. type ChildDependencies = (Self,);
  477. type NodeDependencies = ();
  478. // Size only cares about the width, height, and text parts of the current node
  479. const NODE_MASK: NodeMaskBuilder&lt;'static&gt; = NodeMaskBuilder::new()
  480. // Get access to the width and height attributes
  481. .with_attrs(AttributeMaskBuilder::Some(&amp;[&quot;width&quot;, &quot;height&quot;]))
  482. // Get access to the text of the node
  483. .with_text();
  484. fn update&lt;'a&gt;(
  485. &amp;mut self,
  486. node_view: NodeView&lt;()&gt;,
  487. _node: &lt;Self::NodeDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;,
  488. _parent: Option&lt;&lt;Self::ParentDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  489. children: Vec&lt;&lt;Self::ChildDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  490. context: &amp;SendAnyMap,
  491. ) -&gt; bool {
  492. let font_size = context.get::&lt;FontSize&gt;().unwrap().0;
  493. let mut width;
  494. let mut height;
  495. if let Some(text) = node_view.text() {
  496. // if the node has text, use the text to size our object
  497. width = text.len() as f64 * font_size;
  498. height = font_size;
  499. } else {
  500. // otherwise, the size is the maximum size of the children
  501. width = children
  502. .iter()
  503. .map(|(item,)| item.0)
  504. .reduce(|accum, item| if accum &gt;= item { accum } else { item })
  505. .unwrap_or(0.0);
  506. height = children
  507. .iter()
  508. .map(|(item,)| item.1)
  509. .reduce(|accum, item| if accum &gt;= item { accum } else { item })
  510. .unwrap_or(0.0);
  511. }
  512. // if the node contains a width or height attribute it overrides the other size
  513. for a in node_view.attributes().into_iter().flatten() {
  514. match &amp;*a.attribute.name {
  515. &quot;width&quot; =&gt; width = a.value.as_float().unwrap(),
  516. &quot;height&quot; =&gt; height = a.value.as_float().unwrap(),
  517. // because Size only depends on the width and height, no other attributes will be passed to the member
  518. _ =&gt; panic!(),
  519. }
  520. }
  521. // to determine what other parts of the dom need to be updated we return a boolean that marks if this member changed
  522. let changed = (width != self.0) || (height != self.1);
  523. *self = Self(width, height);
  524. changed
  525. }
  526. }
  527. #[derive(Debug, Clone, Copy, PartialEq, Default, Component)]
  528. struct TextColor {
  529. r: u8,
  530. g: u8,
  531. b: u8,
  532. }
  533. #[partial_derive_state]
  534. impl State for TextColor {
  535. // TextColor depends on the TextColor part of the parent
  536. type ParentDependencies = (Self,);
  537. type ChildDependencies = ();
  538. type NodeDependencies = ();
  539. // TextColor only cares about the color attribute of the current node
  540. const NODE_MASK: NodeMaskBuilder&lt;'static&gt; =
  541. // Get access to the color attribute
  542. NodeMaskBuilder::new().with_attrs(AttributeMaskBuilder::Some(&amp;[&quot;color&quot;]));
  543. fn update&lt;'a&gt;(
  544. &amp;mut self,
  545. node_view: NodeView&lt;()&gt;,
  546. _node: &lt;Self::NodeDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;,
  547. parent: Option&lt;&lt;Self::ParentDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  548. _children: Vec&lt;&lt;Self::ChildDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  549. _context: &amp;SendAnyMap,
  550. ) -&gt; bool {
  551. // TextColor only depends on the color tag, so getting the first tag is equivilent to looking through all tags
  552. let new = match node_view
  553. .attributes()
  554. .and_then(|mut attrs| attrs.next())
  555. .and_then(|attr| attr.value.as_text())
  556. {
  557. // if there is a color tag, translate it
  558. Some(&quot;red&quot;) =&gt; TextColor { r: 255, g: 0, b: 0 },
  559. Some(&quot;green&quot;) =&gt; TextColor { r: 0, g: 255, b: 0 },
  560. Some(&quot;blue&quot;) =&gt; TextColor { r: 0, g: 0, b: 255 },
  561. Some(color) =&gt; panic!(&quot;unknown color {color}&quot;),
  562. // otherwise check if the node has a parent and inherit that color
  563. None =&gt; match parent {
  564. Some((parent,)) =&gt; *parent,
  565. None =&gt; Self::default(),
  566. },
  567. };
  568. // check if the member has changed
  569. let changed = new != *self;
  570. *self = new;
  571. changed
  572. }
  573. }
  574. #[derive(Debug, Clone, Copy, PartialEq, Default, Component)]
  575. struct Border(bool);
  576. #[partial_derive_state]
  577. impl State for Border {
  578. // TextColor depends on the TextColor part of the parent
  579. type ParentDependencies = (Self,);
  580. type ChildDependencies = ();
  581. type NodeDependencies = ();
  582. // Border does not depended on any other member in the current node
  583. const NODE_MASK: NodeMaskBuilder&lt;'static&gt; =
  584. // Get access to the border attribute
  585. NodeMaskBuilder::new().with_attrs(AttributeMaskBuilder::Some(&amp;[&quot;border&quot;]));
  586. fn update&lt;'a&gt;(
  587. &amp;mut self,
  588. node_view: NodeView&lt;()&gt;,
  589. _node: &lt;Self::NodeDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;,
  590. _parent: Option&lt;&lt;Self::ParentDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  591. _children: Vec&lt;&lt;Self::ChildDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  592. _context: &amp;SendAnyMap,
  593. ) -&gt; bool {
  594. // check if the node contians a border attribute
  595. let new = Self(
  596. node_view
  597. .attributes()
  598. .and_then(|mut attrs| attrs.next().map(|a| a.attribute.name == &quot;border&quot;))
  599. .is_some(),
  600. );
  601. // check if the member has changed
  602. let changed = new != *self;
  603. *self = new;
  604. changed
  605. }
  606. }
  607. <span class="boring">}
  608. </span></code></pre></pre>
  609. <p>Now that we have our state, we can put it to use in our RealDom. We can update the RealDom with apply_mutations to update the structure of the dom (adding, removing, and changing properties of nodes) and then update_state to update the States for each of the nodes that changed.</p>
  610. <pre><pre class="playground"><code class="language-rust edition2018">fn main() -&gt; Result&lt;(), Box&lt;dyn std::error::Error&gt;&gt; {
  611. fn app(cx: Scope) -&gt; Element {
  612. let count = use_state(cx, || 0);
  613. use_future(cx, (count,), |(count,)| async move {
  614. loop {
  615. tokio::time::sleep(std::time::Duration::from_secs(1)).await;
  616. count.set(*count + 1);
  617. }
  618. });
  619. cx.render(rsx! {
  620. div{
  621. color: &quot;red&quot;,
  622. &quot;{count}&quot;
  623. }
  624. })
  625. }
  626. // create the vdom, the real_dom, and the binding layer between them
  627. let mut vdom = VirtualDom::new(app);
  628. let mut rdom: RealDom = RealDom::new([
  629. Border::to_type_erased(),
  630. TextColor::to_type_erased(),
  631. Size::to_type_erased(),
  632. ]);
  633. let mut dioxus_intigration_state = DioxusState::create(&amp;mut rdom);
  634. let mutations = vdom.rebuild();
  635. // update the structure of the real_dom tree
  636. dioxus_intigration_state.apply_mutations(&amp;mut rdom, mutations);
  637. let mut ctx = SendAnyMap::new();
  638. // set the font size to 3.3
  639. ctx.insert(FontSize(3.3));
  640. // update the State for nodes in the real_dom tree
  641. let _to_rerender = rdom.update_state(ctx);
  642. // we need to run the vdom in a async runtime
  643. tokio::runtime::Builder::new_current_thread()
  644. .enable_all()
  645. .build()?
  646. .block_on(async {
  647. loop {
  648. // wait for the vdom to update
  649. vdom.wait_for_work().await;
  650. // get the mutations from the vdom
  651. let mutations = vdom.render_immediate();
  652. // update the structure of the real_dom tree
  653. dioxus_intigration_state.apply_mutations(&amp;mut rdom, mutations);
  654. // update the state of the real_dom tree
  655. let mut ctx = SendAnyMap::new();
  656. // set the font size to 3.3
  657. ctx.insert(FontSize(3.3));
  658. let _to_rerender = rdom.update_state(ctx);
  659. // render...
  660. rdom.traverse_depth_first(|node| {
  661. let indent = &quot; &quot;.repeat(node.height() as usize);
  662. let color = *node.get::&lt;TextColor&gt;().unwrap();
  663. let size = *node.get::&lt;Size&gt;().unwrap();
  664. let border = *node.get::&lt;Border&gt;().unwrap();
  665. let id = node.id();
  666. let node = node.node_type();
  667. let node_type = &amp;*node;
  668. println!(&quot;{indent}{id:?} {color:?} {size:?} {border:?} {node_type:?}&quot;);
  669. });
  670. }
  671. })
  672. }
  673. </code></pre></pre>
  674. <h2 id="layout"><a class="header" href="#layout">Layout</a></h2>
  675. <p>For most platforms, the layout of the Elements will stay the same. The <a href="https://docs.rs/dioxus-native-core/latest/dioxus_native_core/layout_attributes/index.html">layout_attributes</a> module provides a way to apply HTML attributes a <a href="https://docs.rs/taffy/latest/taffy/index.html">Taffy</a> layout style.</p>
  676. <h2 id="text-editing"><a class="header" href="#text-editing">Text Editing</a></h2>
  677. <p>To make it easier to implement text editing in rust renderers, <code>native-core</code> also contains a renderer-agnostic cursor system. The cursor can handle text editing, selection, and movement with common keyboard shortcuts integrated.</p>
  678. <pre><pre class="playground"><code class="language-rust edition2018">
  679. <span class="boring">#![allow(unused)]
  680. </span><span class="boring">fn main() {
  681. </span>fn text_editing() {
  682. let mut cursor = Cursor::default();
  683. let mut text = String::new();
  684. // handle keyboard input with a max text length of 10
  685. cursor.handle_input(
  686. &amp;Code::ArrowRight,
  687. &amp;Key::ArrowRight,
  688. &amp;Modifiers::empty(),
  689. &amp;mut text,
  690. 10,
  691. );
  692. // mannually select text between characters 0-5 on the first line (this could be from dragging with a mouse)
  693. cursor.start = Pos::new(0, 0);
  694. cursor.end = Some(Pos::new(5, 0));
  695. // delete the selected text and move the cursor to the start of the selection
  696. cursor.delete_selection(&amp;mut text);
  697. }
  698. <span class="boring">}
  699. </span></code></pre></pre>
  700. <h2 id="conclusion"><a class="header" href="#conclusion">Conclusion</a></h2>
  701. <p>That should be it! You should have nearly all the knowledge required on how to implement your renderer. We're super interested in seeing Dioxus apps brought to custom desktop renderers, mobile renderers, video game UI, and even augmented reality! If you're interested in contributing to any of these projects, don't be afraid to reach out or join the <a href="https://discord.gg/XgGxMSkvUM">community</a>.</p>
  702. </main>
  703. <nav class="nav-wrapper" aria-label="Page navigation">
  704. <!-- Mobile navigation buttons -->
  705. <a rel="prev" href="../fullstack/server_functions.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
  706. <i class="fa fa-angle-left"></i>
  707. </a>
  708. <a rel="next" href="../contributing/index.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
  709. <i class="fa fa-angle-right"></i>
  710. </a>
  711. <div style="clear: both"></div>
  712. </nav>
  713. </div>
  714. </div>
  715. <nav class="nav-wide-wrapper" aria-label="Page navigation">
  716. <a rel="prev" href="../fullstack/server_functions.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
  717. <i class="fa fa-angle-left"></i>
  718. </a>
  719. <a rel="next" href="../contributing/index.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
  720. <i class="fa fa-angle-right"></i>
  721. </a>
  722. </nav>
  723. </div>
  724. <script type="text/javascript">
  725. window.playground_line_numbers = true;
  726. </script>
  727. <script type="text/javascript">
  728. window.playground_copyable = true;
  729. </script>
  730. <script src="../ace.js" type="text/javascript" charset="utf-8"></script>
  731. <script src="../editor.js" type="text/javascript" charset="utf-8"></script>
  732. <script src="../mode-rust.js" type="text/javascript" charset="utf-8"></script>
  733. <script src="../theme-dawn.js" type="text/javascript" charset="utf-8"></script>
  734. <script src="../theme-tomorrow_night.js" type="text/javascript" charset="utf-8"></script>
  735. <script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
  736. <script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
  737. <script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
  738. <script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
  739. <script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
  740. <script src="../book.js" type="text/javascript" charset="utf-8"></script>
  741. <!-- Custom JS scripts -->
  742. </body>
  743. </html>