Skip to content

Commit 4fa5cea

Browse files
committed
update doc
1 parent 2d3ac94 commit 4fa5cea

20 files changed

Lines changed: 423 additions & 56 deletions

astro.config.mjs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export default defineConfig({
5151
// label: '安装',
5252
// slug: 'components/install'
5353
// },
54+
{
55+
label: '组件速览',
56+
slug: "components/gallery",
57+
},
5458
{
5559
label: '基类 Element',
5660
slug: 'components/element',
@@ -68,21 +72,37 @@ export default defineConfig({
6872
slug: 'components/image',
6973
},
7074
{
71-
label: '输入框 Entry',
72-
slug: 'components/entry',
75+
label: '单行输入框 TextInput',
76+
slug: 'components/text-input',
7377
},
7478
{
75-
label: '段落 Paragraph',
76-
slug: 'components/paragraph',
79+
label: '多行输入框 TextEdit',
80+
slug: 'components/text-edit',
7781
},
7882
{
79-
label: '容器 Container',
80-
slug: 'components/container',
83+
label: '单选按钮 Radio',
84+
slug: 'components/radio',
85+
},
86+
{
87+
label: '复选框 Checkbox',
88+
slug: 'components/checkbox',
89+
},
90+
{
91+
label: '下拉框 Select',
92+
slug: 'components/select',
8193
},
8294
{
83-
label: '滚动容器 Scroll',
84-
slug: 'components/scroll',
95+
label: '富文本 RichText',
96+
slug: 'components/rich-text',
8597
},
98+
{
99+
label: '容器 Container',
100+
slug: 'components/container',
101+
},
102+
// {
103+
// label: '滚动容器 Scroll',
104+
// slug: 'components/scroll',
105+
// },
86106
{
87107
label: '视频 Video',
88108
slug: 'components/video',

src/assets/gallery-dark.png

72.3 KB
Loading

src/assets/gallery.png

71.2 KB
Loading

src/assets/hello-element/init.png

7.18 KB
Loading
7.79 KB
Loading

src/content/docs/advanced/custom-component.md

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
title: 自定义组件(Rust)
3+
---
4+
5+
import { Image } from 'astro:assets';
6+
import initImg from "../../../assets/hello-element/init.png";
7+
import renderImg from "../../../assets/hello-element/render.png";
8+
9+
10+
Deft内置了一些常用的基础组件,同时允许你创建自定义组件,以满足一些复杂需求。
11+
12+
_备注:这里的自定义组件指的是类似于Label,Button这些基础组件。如果你需要创建自定义React/Vue/Solid组件,应该参考对应框架的自定义组件教程,本教程不适用。_
13+
14+
### 创建自定义组件
15+
16+
要创建一个自定义组件非常简单,只需要实现`ElementBackend` trait即可。
17+
18+
`ElementBackend`有几个方法,其中只有`create`方法是必须实现的,其他方法根据需求,选择性去实现。
19+
20+
首先,我们需要创建一个结构体用于表示我们将要创建自定义组件,这里以`HelloBackend`为例。
21+
22+
```rust
23+
pub struct HelloBackend {
24+
element_weak: ElementWeak,
25+
}
26+
27+
impl ElementBackend for HelloBackend {
28+
fn create(element: &mut Element) -> Self where Self: Sized {
29+
Self {
30+
// 保存元素弱引用,后面会用到
31+
element_weak: element.as_weak(),
32+
}
33+
}
34+
}
35+
```
36+
37+
### 注册自定义组件
38+
39+
创建完自定义组件后,需要注册一下,可以在初始化JS引擎的时候注册。
40+
41+
```rust
42+
impl IApp for AppImpl {
43+
44+
fn init_js_engine(&mut self, _js_engine: &mut JsEngine) {
45+
// 注册我们的自定义组件,tag为hello
46+
register_component::<HelloBackend>("hello");
47+
}
48+
...
49+
}
50+
```
51+
52+
这里我们把这个组件注册为`hello`组件。
53+
54+
### 创建自定义组件的JS绑定
55+
56+
为了能在JS中使用这个自定义组件,我们还需要为其创建一个JS绑定: `HelloElement`
57+
58+
```javascript
59+
class HelloElement extends Element {
60+
constructor() {
61+
/// hello为元素的tag,需和注册时声明的tag保持一致
62+
super("hello");
63+
}
64+
}
65+
```
66+
67+
HelloElement继承于`Element`类,这是所有组件的基类。
68+
69+
### 使用自定义组件
70+
71+
经过`创建`,`注册`,`绑定`这3个步骤后,就可以在JS代码中直接使用啦。
72+
73+
首先,我们为其定义一些基础样式。
74+
75+
```css
76+
body {
77+
justify-content: center;
78+
align-items: center;
79+
}
80+
hello {
81+
width: 100px;
82+
height: 100px;
83+
background: #ccc;
84+
}
85+
```
86+
87+
然后,我们实例化一个hello组件,并添加到文档流中。
88+
89+
```javascript
90+
const helloElement = new HelloElement();
91+
window.body.addChild(helloElement);
92+
```
93+
94+
运行效果如下:
95+
96+
<Image src={initImg} alt="hello element" width="300" />
97+
98+
### 绘制组件
99+
100+
可以看到,我们的自定义组件在窗口中显示出来了,但是这个组件一点用也没有,因为它什么也没有做。现在我们接着来完善这个组件,为其添加一些自定义绘制逻辑,这通常是自定义组件需要实现的。
101+
102+
要增加自定义绘制逻辑,实现`ElementBackend::render`方法即可。
103+
104+
```rust
105+
impl ElementBackend for HelloBackend {
106+
...
107+
fn render(&mut self) -> RenderFn {
108+
// 升级元素弱引用为强引用
109+
let element = self.element_weak.upgrade_mut().unwrap();
110+
// 获取元素尺寸信息
111+
let bounds = element.get_bounds();
112+
// 获取元素中心坐标
113+
let center = (bounds.width / 2.0, bounds.height / 2.0);
114+
// 计算半径
115+
let radius = f32::min(center.0, center.1);
116+
RenderFn::new(move |painter| {
117+
let mut paint = Paint::default();
118+
// 使用填充样式
119+
paint.set_style(PaintStyle::Fill);
120+
paint.set_color(Color::from_rgb(0, 80, 0));
121+
// 绘制圆形
122+
painter.canvas.draw_circle(center, radius, &paint);
123+
})
124+
}
125+
}
126+
```
127+
128+
`render`方法返回了一个`RenderFn`对象,里面包含了我们组件的绘制逻辑,在以上示例中,我们绘制了一个实心圆,重新运行,看看最终效果:
129+
130+
<Image src={renderImg} alt="custom element with renderer" width="300" />
131+
132+
### 相关代码和参考示例
133+
134+
本文完整代码可在Deft仓库中找到:https://github.com/deft-ui/deft/blob/main/examples/custom_element.rs
135+
136+
更多参考示例:
137+
138+
1. 视频播放(ffmpeg):https://github.com/deft-ui/deft-video
139+
2. 远程桌面(SPICE):https://github.com/kasonyang/tiny-spice
140+
141+

src/content/docs/components/button.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ title: 按钮 Button
66

77
**使用**
88

9-
```
9+
```javascript
1010
import {Button} from 'deft-react';
1111
function App() {
1212
return <Button>确定</Button>
1313
}
14-
```
14+
```
15+
16+
**属性**
17+
18+
| 属性 | 说明 | 类型 | 默认值 |
19+
|------------|-------|-----------|--------|
20+
| disabled | 是否禁用 | boolean | false |
21+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: 复选框 Checkbox
3+
---
4+
5+
复选框用于多项选择
6+
7+
**使用**
8+
9+
```javascript
10+
import {Checkbox} from 'deft-react';
11+
12+
function App() {
13+
const javascriptRef = useRef();
14+
const rustRef = useRef();
15+
16+
function onCheckChange(e) {
17+
console.log('is javascript checked', javascriptRef.current.value);
18+
console.log('is rust checked', rustRef.current.value);
19+
}
20+
21+
return <Container>
22+
<Checkbox ref={javascriptRef} label="JavaScript" onChange={onCheckChange}/>
23+
<Checkbox ref={rustRef} label="Rust" onChange={onCheckChange}/>
24+
</Container>
25+
}
26+
```
27+
28+
**属性**
29+
30+
| 属性 | 说明 | 类型 | 默认值 |
31+
|----------|------|---------|-------|
32+
| label | 标签 | string | - |
33+
| checked | 是否勾选 | boolean | - |
34+
| disabled | 是否禁用 | boolean | false |
35+
36+
37+
**事件**
38+
39+
| 事件 | 说明 | 事件类型 |
40+
|---------|----------|--------------------|
41+
| change | 勾选状态发生变化 | |

src/content/docs/components/container.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: 容器 Container
66

77
**使用**
88

9-
```
9+
```javascript
1010
import {Container} from 'deft-react';
1111
function App() {
1212
return <Container>

0 commit comments

Comments
 (0)