Skip to content

Commit

Permalink
dyn widget name, lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
syf20020816 committed Aug 14, 2024
1 parent 7d522fb commit 9e2b1b0
Showing 1 changed file with 104 additions and 1 deletion.
105 changes: 104 additions & 1 deletion src/gen/tutorials/syntex/dyn_widget.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,108 @@
# Dyn Widget

## 动态Widget类型

1. A: 没有使用`gen_macros::Prop`标注的且没有声明id
2. B: 没有使用`gen_macros::Prop`标注但声明id
3. C: 使用`gen_macros::Prop`不声明id

> Note!:
> 1. 同时使用`gen_macros::Prop`且声明id时,若`${prop_struct_name} == ${id}`,属于C类
> 2. 但若`${prop_struct_name} != ${id}`时,panic!
### A类

当没有使用`Prop`进行标注且没有在`<component></component>`中声明组件id时:

```rust
<template>
<component inherits="view"></component>
</template>
```

Dyn Widget的结构体名为: `${source_name}_${inherits}`

### B类

当没有使用`Prop`进行标注但在`<component></component>`中声明组件id时:

```rust
<template>
<component id="MyView" inherits="view"></component>
</template>
```

Dyn Widget的结构体名为: `${id}`, 在这个例子中就是`MyView`

### C类

使用`Prop`进行标注但不声明id

```rust
<template>
<component inherits="view"></component>
</template>

<script>
use gen_macros::{Prop} // 可以省略

#[derive(Prop)]
pub struct MyView{}
</script>
```

Dyn Widget的结构体名为`${prop_struct_name}`, 在这个例子中就是`MyView`

---

## Script脚本原则

### LifeTime

- before: 最早被调用, 对PropStruct的初始化会在这里处理
- after: 在before之后调用,声明式修改PropStruct Value

#### Example

```rust
<script>
use gen_macros::{Prop} // 可以省略

#[derive(Prop)]
pub struct MyView{
pub label_value1: String
}
---------------------------------------------------------------|
impl Default for MyView{ |
fn default() -> Self { |
Self{ |
label_value1: "Click The Button".to_string() |
} before
} |
} |
|
let mut prop = MyView::default(); |
---------------------------------------------------------------|

---------------------------------------------------------------|
prop.label_value1 = "Click Here!".to_string(); after
---------------------------------------------------------------|
</script>
```
#### After Compiled
```rust
impl LiveHook for MyView {
fn before_apply(&mut self, _cx: &mut Cx, _apply: &mut Apply, _index: usize, _nodes: &[LiveNode]) {
self.label_value1 = "Click The Button".to_string();
}
fn after_apply(&mut self, _cx: &mut Cx, _apply: &mut Apply, _index: usize, _nodes: &[LiveNode]) {
self.label_value1 = "Click Here!".to_string();
}
}
```

## Example

动态Widget采用`<component>`标签作为单根标签,使用`inherits`属性指定某个需要继承的widget,
当前推荐继承view widget

Expand Down Expand Up @@ -33,7 +136,7 @@ pub struct ButtonView{
impl Default for ButtonView{
fn default() -> Self {
Self{
label1: "Click The Button"
label1: "Click The Button".to_string()
}
}
}
Expand Down

0 comments on commit 9e2b1b0

Please sign in to comment.