|
| 1 | +# Router configuration |
| 2 | + |
| 3 | +Starting from GenUI v0.1.2, the framework has introduced the routing configuration function. However, it should be noted that GenUI's routing mechanism is different from the "front-end routing" in traditional Web front-ends. There is no real URL routing in GUI applications. GenUI's routing is essentially a page switching mechanism for state management and navigation between multiple UI interfaces. |
| 4 | + |
| 5 | +GenUI's routing mechanism is based on a dedicated component and is enabled through the project configuration file. You can declare the routing structure in `router.toml`, enable the configuration in `gen_ui.toml`, and then introduce it to a specific page through the `route!` macro. |
| 6 | + |
| 7 | +For more examples, see: |
| 8 | + |
| 9 | +- [Route component example](https://github.com/Privoce/made_with_GenUI/tree/main/tests/router) |
| 10 | +- [Root route example](https://github.com/Privoce/made_with_GenUI/tree/main/tests/router_tabbar) |
| 11 | + |
| 12 | +## Configuration and usage process |
| 13 | + |
| 14 | +### Step 1: Configure `router.toml` |
| 15 | + |
| 16 | +You can define a complete route configuration through `router.toml`, including route mode, default activation page, whether to enable Tabbar, page path, etc.: |
| 17 | + |
| 18 | +```toml title="router.toml" |
| 19 | +name = "MyRouter" |
| 20 | +id = "app_router" |
| 21 | +mode = "History" # Optional value: History / Switch |
| 22 | +active = "login" # Default page ID |
| 23 | + |
| 24 | +[tabbar] |
| 25 | +theme = "Dark" |
| 26 | +active = false |
| 27 | + |
| 28 | +[tabbar.bars] |
| 29 | +login = { icon = "crate://self/resources/login.svg", text = "Login Page" } |
| 30 | +register = { icon = "crate://self/resources/register.svg", text = "Register Page" } |
| 31 | + |
| 32 | +[bar_pages] |
| 33 | +login = { path = "crate::views::login::*", component = "Login" } |
| 34 | +register = "crate::views::register::Register" |
| 35 | + |
| 36 | +[nav_pages] |
| 37 | +nav_about = { path = "crate::views::about::*", component = "About" } |
| 38 | +``` |
| 39 | + |
| 40 | +### Step 2: Enable routing configuration in `gen_ui.toml` |
| 41 | + |
| 42 | +```toml title="gen_ui.toml" |
| 43 | + |
| 44 | +[compiler] |
| 45 | +#... |
| 46 | + |
| 47 | +[makepad] |
| 48 | +router = "./router.toml" |
| 49 | + |
| 50 | +[plugins] |
| 51 | +#... |
| 52 | +``` |
| 53 | + |
| 54 | +### Step 3: Import routing in components |
| 55 | + |
| 56 | +In the `.gen` file where routing is needed, use the `route!` macro to import the declared routing component: |
| 57 | + |
| 58 | +```rust |
| 59 | +<script> |
| 60 | +route!(app_router); |
| 61 | +</script> |
| 62 | +``` |
| 63 | + |
| 64 | +> [!WARNING] |
| 65 | +> If the `route!` macro is used, the `.gen` file cannot contain other elements and must be used only for routing. |
| 66 | +
|
| 67 | +### Step 4: Use the Router component in other components |
| 68 | + |
| 69 | +You can embed the Router component in other pages just like using a normal component: |
| 70 | + |
| 71 | +```html |
| 72 | +<template> |
| 73 | + <component name="Home"> |
| 74 | + <view> |
| 75 | + <button @clicked="to_page('login')"></button> |
| 76 | + </view> |
| 77 | + <MyRouter id="my_router"></MyRouter> |
| 78 | + </component> |
| 79 | +</template> |
| 80 | +``` |
| 81 | + |
| 82 | +## Use the Router as the root component |
| 83 | + |
| 84 | +If you want to use the Router component as the root component of your application, use the `route!` macro in the `root.gen` file. |
| 85 | + |
| 86 | +> At this time, `name` in `router.toml` must be set to `"UiRoot"` to ensure that the compiler recognizes it as the main entry. |
| 87 | +
|
| 88 | +## Page jump operation |
| 89 | + |
| 90 | +### Control jump in parent component |
| 91 | + |
| 92 | +You can get the router instance through the `c_ref!` macro and call the method it provides: |
| 93 | + |
| 94 | +```rust |
| 95 | +fn to_page(&mut self, page: &str) { |
| 96 | +let mut my_router = c_ref!(my_router); |
| 97 | + |
| 98 | +match page { |
| 99 | +"login" => my_router.nav_to(id!(login)), |
| 100 | +"register" => my_router.nav_to(id!(register)), |
| 101 | +"about" => my_router.nav_to(id!(nav_about)), |
| 102 | +"back" => my_router.nav_back(), |
| 103 | +_ => {} |
| 104 | +} |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Jump in child component |
| 109 | + |
| 110 | +For child components, GenUI provides convenient macros `nav_to!` and `nav_back!` to handle jumps: |
| 111 | + |
| 112 | +```rust |
| 113 | +pub fn to_register(&mut self) { |
| 114 | +nav_to!(register); |
| 115 | +} |
| 116 | + |
| 117 | +pub fn back(&mut self) { |
| 118 | +nav_back!(); |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## Routing configuration structure description |
| 123 | + |
| 124 | +### Data structure definition in Rust: |
| 125 | + |
| 126 | +```rust |
| 127 | +pub struct RouterBuilder { |
| 128 | + pub name: String, |
| 129 | + pub id: String, |
| 130 | + pub mode: NavMode, |
| 131 | + pub active: Option<String>, |
| 132 | + pub tabbar: Option<TabbarBuilder>, |
| 133 | + pub bar_pages: Vec<(String, Page)>, |
| 134 | + pub nav_pages: HashMap<String, Page>, |
| 135 | +} |
| 136 | + |
| 137 | +pub struct TabbarBuilder { |
| 138 | + pub theme: Option<Themes>, |
| 139 | + pub active: bool, |
| 140 | + pub bars: HashMap<String, TabbarItem>, |
| 141 | +} |
| 142 | + |
| 143 | +pub struct TabbarItem { |
| 144 | + pub icon: Option<LiveDependency>, |
| 145 | + pub text: Option<String>, |
| 146 | +} |
| 147 | + |
| 148 | +pub enum Page { |
| 149 | + Path(Import), |
| 150 | + Component { path: Import, component: String }, |
| 151 | +} |
| 152 | + |
| 153 | +pub enum NavMode { |
| 154 | + History, |
| 155 | + Switch, |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +### Field description table: |
| 160 | + |
| 161 | +| Key | Type | Description | |
| 162 | +| ----------------- | ------------------------ | --------------------------------------------------------------------------- | |
| 163 | +| `name` | `String` | Routing component name | |
| 164 | +| `id` | `String` | Routing component unique identifier | |
| 165 | +| `mode` | `NavMode` | Routing mode, supports `History` (history stack) and `Switch` (switch mode) | |
| 166 | +| `active` | `Option<String>` | Default activated page ID | |
| 167 | +| `tabbar` | `Option<TabbarBuilder>` | Tabbar configuration items (optional) | |
| 168 | +| `bar_pages` | `Vec<(String, Page)>` | Primary page configuration, supports component specification | |
| 169 | +| `nav_pages` | `HashMap<String, Page>` | Secondary page configuration, not displayed in Tabbar | |
| 170 | +| `TabbarItem.icon` | `Option<LiveDependency>` | Icon resource path | |
| 171 | +| `TabbarItem.text` | `Option<String>` | Tabbar display text | |
0 commit comments