Skip to content

Commit 1ec241c

Browse files
committed
docs(components): add getting-started guide for tessera-ui-basic-components
1 parent 38b97e3 commit 1ec241c

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

docs/en/components/getting-started.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,107 @@ order: 0
44
---
55

66
# Getting Started
7+
8+
`tessera-ui-basic-components` is the official component library for `tessera-ui`. It provides a set of common foundational components. It is completely optional and is decoupled from the `tessera-ui` core library.
9+
10+
## Installation
11+
12+
We assume you have already added and configured `tessera-ui` as a dependency for your project. If not, please refer to [Getting Started](../guide/getting-started.md).
13+
14+
Next, add `tessera-ui-basic-components` as a dependency:
15+
16+
```bash
17+
cargo add tessera-ui-basic-components
18+
```
19+
20+
## Registering with the renderer
21+
22+
::: warning ⚠
23+
If you do not register the corresponding pipelines with the renderer, the renderer will crash.
24+
:::
25+
26+
`tessera-ui` uses a decentralized renderer architecture (see ../guide/what-is-tessera.md#pluggable-shaders), so you need to register `tessera-ui-basic-components` with the renderer.
27+
28+
Assume your original renderer initialization looks like:
29+
30+
```rust
31+
use tessera_ui::{Renderer, renderer::TesseraConfig};
32+
33+
fn main() {
34+
let config = TesseraConfig {
35+
window_title: "Tessera".to_string(), // Window title
36+
..Default::default()
37+
};
38+
Renderer::run_with_config(
39+
|| {}, // UI entry function
40+
|app| {},
41+
config,
42+
)
43+
.unwrap();
44+
}
45+
```
46+
47+
You need to modify it as follows:
48+
49+
```rust
50+
use tessera_ui::{Renderer, renderer::TesseraConfig};
51+
52+
fn main() {
53+
let config = TesseraConfig {
54+
window_title: "Tessera".to_string(), // Window title
55+
..Default::default()
56+
};
57+
Renderer::run_with_config(
58+
|| {}, // UI entry function
59+
|app| {
60+
tessera_ui_basic_components::pipelines::register_pipelines(app); // Register pipelines required by the basic components
61+
},
62+
config,
63+
)
64+
.unwrap();
65+
}
66+
```
67+
68+
## Using components
69+
70+
Now you can use components provided by `tessera-ui-basic-components` inside `#[tessera]` functions. Here is an example that uses `surface` as a background:
71+
72+
```rust
73+
use tessera_ui::{Color, DimensionValue, Renderer, renderer::TesseraConfig, tessera};
74+
use tessera_ui_basic_components::surface::{SurfaceArgsBuilder, surface};
75+
76+
fn main() {
77+
let config = TesseraConfig {
78+
window_title: "Tessera Example".to_string(), // Window title
79+
..Default::default()
80+
};
81+
Renderer::run_with_config(
82+
|| app(), // UI entry, the top-level tessera
83+
|app| {
84+
tessera_ui_basic_components::pipelines::register_pipelines(app); // Register pipelines
85+
},
86+
config,
87+
)
88+
.unwrap();
89+
}
90+
91+
#[tessera]
92+
fn app() {
93+
surface(
94+
SurfaceArgsBuilder::default()
95+
.color(Color::WHITE) // Set surface background color to white
96+
.width(DimensionValue::FILLED) // Fill parent width
97+
.height(DimensionValue::FILLED) // Fill parent height
98+
.build()
99+
.unwrap(),
100+
None, // The second argument is the ripple state for click animations; background surface doesn't need clicks so pass None
101+
|| {
102+
// surface is a container component that can hold other components; here we pass an empty closure as placeholder
103+
},
104+
);
105+
}
106+
```
107+
108+
Run `cargo run` to see a white window.
109+
110+
![White window](/getting-start-2.png)

docs/zhHans/components/getting-started.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,107 @@ order: 0
44
---
55

66
# 快速开始
7+
8+
`tessera-ui-basic-components``tessera-ui` 的官方组件库,包含了一些常用的基础组件。它完全可选,和 `tessera-ui` 核心库解耦。
9+
10+
## 安装
11+
12+
我们假设你已经添加并配置了 `tessera-ui` 作为你项目的依赖,如果没有,请参考 [快速开始](../guide/getting-started.md)
13+
14+
接下来添加 `tessera-ui-basic-components` 作为依赖:
15+
16+
```bash
17+
cargo add tessera-ui-basic-components
18+
```
19+
20+
## 注册到渲染器
21+
22+
::: warning ⚠
23+
如果不注册对应管线到渲染器中,渲染器将直接崩溃。
24+
:::
25+
26+
`tessera-ui` [使用去中心化的渲染器架构](../guide/what-is-tessera.md#可插拔着色器),所以你需要将 `tessera-ui-basic-components` 注册到渲染器中。
27+
28+
假设你原来的渲染器初始化代码如下:
29+
30+
```rust
31+
use tessera_ui::{Renderer, renderer::TesseraConfig};
32+
33+
fn main() {
34+
let config = TesseraConfig {
35+
window_title: "Tessera".to_string(), // 设置窗口标题
36+
..Default::default()
37+
};
38+
Renderer::run_with_config(
39+
|| {}, // UI入口函数
40+
|app| {},
41+
config,
42+
)
43+
.unwrap();
44+
}
45+
```
46+
47+
那么你需要做如下修改:
48+
49+
```rust
50+
use tessera_ui::{Renderer, renderer::TesseraConfig};
51+
52+
fn main() {
53+
let config = TesseraConfig {
54+
window_title: "Tessera".to_string(), // 设置窗口标题
55+
..Default::default()
56+
};
57+
Renderer::run_with_config(
58+
|| {}, // UI入口函数
59+
|app| {
60+
tessera_ui_basic_components::pipelines::register_pipelines(app); // 注册基本组件库的渲染管线
61+
},
62+
config,
63+
)
64+
.unwrap();
65+
}
66+
```
67+
68+
## 使用组件
69+
70+
现在你可以在`#[tessera]`函数中使用 `tessera-ui-basic-components` 提供的组件了。这里展示一个使用`surface`做背景的例子
71+
72+
```rust
73+
use tessera_ui::{Color, DimensionValue, Renderer, renderer::TesseraConfig, tessera};
74+
use tessera_ui_basic_components::surface::{SurfaceArgsBuilder, surface};
75+
76+
fn main() {
77+
let config = TesseraConfig {
78+
window_title: "Tessera Example".to_string(), // 设置窗口标题
79+
..Default::default()
80+
};
81+
Renderer::run_with_config(
82+
|| app(), // UI入口,也就是顶层的tessera
83+
|app| {
84+
tessera_ui_basic_components::pipelines::register_pipelines(app); // 注册渲染管线
85+
},
86+
config,
87+
)
88+
.unwrap();
89+
}
90+
91+
#[tessera]
92+
fn app() {
93+
surface(
94+
SurfaceArgsBuilder::default()
95+
.color(Color::WHITE) // 设置surface的背景颜色为白色
96+
.width(DimensionValue::FILLED) // 这代表对surface的约束是填满父组件宽度
97+
.height(DimensionValue::FILLED) // 这代表对surface的约束是填满父组件高度
98+
.build()
99+
.unwrap(),
100+
None, // 第二个参数传入的是ripple动画状态,这是用于点击的水波纹动画的,我们的背景surface不需要点击,所以传None
101+
|| {
102+
// surface是一个经典的容器组件,可以用来放置其他组件,目前我们放一个空闭包占位
103+
},
104+
);
105+
}
106+
```
107+
108+
运行`cargo run`,将会看到一个白色的窗口。
109+
110+
![白色的窗口](/getting-start-2.png)

0 commit comments

Comments
 (0)