Skip to content

Commit 8204e25

Browse files
committed
finish loop widget
1 parent 6ee752d commit 8204e25

File tree

8 files changed

+159
-37
lines changed

8 files changed

+159
-37
lines changed

src/gen/design/converter/converter.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ Of course, I believe that my framework is not a replacement for Makepad, but rat
1010

1111
For most people, they may not need very fancy special effects, but rather quickly start to implement a product.
1212

13-
For those who pursue higher goals, Makepad can be directly used in DSL
13+
For those who pursue higher goals, Makepad can be directly used in DSL
14+

src/gen/design/parser/parser.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
# Convert RSX DSL to AST
2-
## How to convert
1+
# Parser
2+
3+
## Convert RSX DSL to AST
4+
35
```shell
46
Real AST
57
----------- --------------- Strategy ---------------
68
| RSX DSL | --> | ParseTarget | ----------> | ParseResult |
79
----------- --------------- ---------------
810
```
9-
## RSX
10-
11-
- Template
12-
- Script
13-
- Style
14-
15-
### Template
1611

17-
1. Unrestricted tags (tag name is not constrained)
18-
2. There are no styles, only properties, or in other words, all styles are properties
19-
3. Nesting strings in tags is not allowed (example: <view>this is a view</view> ❌)
20-
21-
### Script
22-
1. Allow Rust syntax
23-
24-
### Style
25-
26-
1. bind to tag by name
27-
2. nesting allowed
28-
3. function and bind allowed
12+
## Convert To Makead
2913

14+
```shell
15+
---------------------------
16+
| |
17+
------------------------ | MakepadConvertResult |
18+
| AST (in ParseResult) | --------> | |
19+
------------------------ | -------------------- |
20+
------------------------ | | MakepadConverter | |
21+
| File Meta Data | --------> | -------------------- |
22+
------------------------ | |
23+
---------------------------
24+
|
25+
|------------------→ |
26+
|
27+
------------ | ----------------
28+
| Strategy | --------------| | Makepad Code |
29+
------------ ----------------
30+
```

src/makepad/code/widgets/define.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
In this chapter, we'll delve into the creation of a `MyLabel` Widget, which extends the capabilities of the built-in `Label` widget in Makepad.
44

5-
## Starting with a New Module
5+
## Init a New Module
66

77
First, create a new module for our custom widget:
88

99
```rust
1010
pub mod my_label;
1111
```
1212

13-
## Crafting a Live Design
13+
## Design Live Design
1414

1515
The `live_design!` macro allows for an effortless and succinct definition of custom widgets. Here, we define `MyLabel` with unique styling:
1616

@@ -36,7 +36,7 @@ live_design!{
3636
}
3737
```
3838

39-
## Defining the Widget Structure
39+
## Define the Widget Structure
4040

4141
`MyLabel` will utilize the `Widget` trait and directly reference (`deref`) the `Label` widget:
4242

@@ -49,7 +49,7 @@ pub struct MyLabel{
4949
}
5050
```
5151

52-
## Implementing the Widget Trait
52+
## Impl the Widget Trait
5353

5454
Next, we implement the `Widget` trait for `MyLabel` to ensure it behaves as intended:
5555

@@ -61,7 +61,7 @@ impl Widget for MyLabel {
6161
}
6262
```
6363

64-
## Registering in the Application
64+
## Register in the Application
6565

6666
It's crucial to register our custom widget in the application to make it available for use:
6767

@@ -74,7 +74,7 @@ impl LiveRegister for App {
7474
}
7575
```
7676

77-
## Utilizing in the App's Live Design
77+
## Use in the App's Live Design
7878

7979
Finally, incorporate `MyLabel` into the application's live design, setting it up for use:
8080

src/makepad/code/widgets/define2.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
Following our previous exploration where we introduced the `MyLabel` widget, extending the `Label`, this chapter ventures into more sophisticated use cases. While `Label` provides a robust foundation, there might be instances where its granular control over internal properties isn't necessary for your project's needs.
44

5-
## Initiating with a New Module
5+
## Init a New Module
66

77
Begin by creating a module specifically for our enhanced widget:
88

99
```rust
1010
pub mod deref_label;
1111
```
1212

13-
## Designing a Live Layout
13+
## Design Live Design
1414

1515
In this step, we define `DerefLabel`, focusing on simplicity and reusability:
1616

@@ -35,7 +35,7 @@ live_design!{
3535
}
3636
```
3737

38-
## Creating a Deref Structure
38+
## Define a Deref Structure
3939

4040
`DerefLabel` is designed with simplicity and flexibility in mind, allowing for easy customization of text, color, and font size:
4141

@@ -53,7 +53,7 @@ pub struct DerefLabel{
5353
}
5454
```
5555

56-
## Implementing the Widget Trait
56+
## Impl the Widget Trait
5757

5858
With the widget's structure defined, we implement the `Widget` trait to specify how `DerefLabel` behaves and is rendered:
5959

src/makepad/code/widgets/for.md

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This example demonstrates how to generate components from iterable data in Makepad.
44

5-
## Writing Live Design
5+
## Write Live Design
66

77
Note that we use `btn: <Button>` instead of `btn = <Button>`. This implies that the button is treated as a prop here.
88

@@ -27,7 +27,7 @@ live_design!{
2727
}
2828
```
2929

30-
## Writing the Widget
30+
## Define the Widget Struct
3131

3232
Next, we implement a widget that conforms to the Widget trait. Here, we use `Area + LivePtr + ComponentMap`. You can roughly consider these as a standard combination (though that's not entirely accurate).
3333

@@ -57,7 +57,7 @@ pub struct Footer {
5757
}
5858
```
5959

60-
## Drawing the Widget
60+
## Draw the Widget
6161

6262
Implement the `draw_walk` method to render the widget.
6363

@@ -89,4 +89,100 @@ impl Widget for Footer {
8989
DrawStep::done()
9090
}
9191
}
92+
```
93+
---
94+
95+
## Impl handle_event
96+
97+
In order for all buttons to receive events, we need to implement the `handle_event` function
98+
99+
```rust
100+
impl Widget for Footer{
101+
102+
// ... draw_walk() fn
103+
104+
fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
105+
// Handle events for the current widget
106+
for btn in self.btns.values_mut(){
107+
// btn.handle_event(cx, event, scope);
108+
for action in cx.capture_actions(|cx| btn.handle_event(cx, event, scope)){
109+
match action.as_widget_action().cast() {
110+
// Handle button actions
111+
ButtonAction::Clicked => {
112+
log!("Button clicked:{}", btn.text());
113+
}
114+
_ => {}
115+
}
116+
}
117+
}
118+
}
119+
}
120+
```
121+
122+
![](../../../static/for_loop_1.png)
123+
124+
## Pass Event
125+
126+
Now we need to implement event passing for the Footer widget, so that when any sub button is clicked, the Footer widget will trigger a Focus event
127+
128+
### Define Footer Action
129+
130+
Define a Focus event which get LiveId(Button's LiveId)
131+
132+
```rust
133+
#[derive(Clone, Debug, DefaultNone)]
134+
pub enum FooterAction{
135+
None,
136+
// LiveId can specialize the clicked button
137+
Focus(LiveId),
138+
}
139+
```
140+
141+
### Change handle_event
142+
143+
```rust
144+
impl Widget for Footer{
145+
146+
// ... draw_walk() fn
147+
148+
fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
149+
let uid = self.widget_uid();
150+
// Handle events for the current widget
151+
for (btn_id, btn) in self.btns.iter_mut(){
152+
// btn.handle_event(cx, event, scope);
153+
for action in cx.capture_actions(|cx| btn.handle_event(cx, event, scope)){
154+
match action.as_widget_action().cast() {
155+
// Handle button actions
156+
ButtonAction::Clicked => {
157+
log!("Button clicked:{}", btn.text());
158+
// pass event
159+
cx.widget_action(uid, &scope.path, FooterAction::Focus(*btn_id))
160+
}
161+
_ => {}
162+
}
163+
}
164+
}
165+
}
166+
}
167+
```
168+
169+
### Impl MatchEvent for Parent Widget
170+
171+
impl `handle_actions` function, match the action we need to do and then handle.
172+
173+
```rust
174+
impl MatchEvent for App{
175+
fn handle_actions(&mut self, cx: &mut Cx, actions: &Actions) {
176+
for action in actions{
177+
match action.as_widget_action().cast(){
178+
FooterAction::Focus(btn_id) => {
179+
log!("Button focused:{}", btn_id);
180+
}
181+
_ => {
182+
log!("other");
183+
}
184+
};
185+
}
186+
}
187+
}
92188
```

src/makepad/syntax/import.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,31 @@
22

33
This section details how to incorporate external resources such as images and fonts into your Makepad projects. Makepad supports various formats, allowing you to enhance the visual aspect of your applications.
44

5-
## Importing Images
5+
> widgets: `import `
6+
>
7+
> font, images, ...other static resources: `dep()`
8+
9+
## Import Widgets
10+
11+
use keyword: `import` can import widget in `live_design!`
12+
13+
```rust
14+
live_design!{
15+
import you_crate_name::widget_mod::widget_name;
16+
}
17+
18+
// example
19+
live_design!{
20+
import hello::header::Header;
21+
}
22+
23+
//import all
24+
live_design!{
25+
import hello::header::*;
26+
}
27+
```
28+
29+
## Import Images
630

731
Makepad allows the importation of various image formats to enrich your user interface. Supported formats include:
832

@@ -36,7 +60,7 @@ live_design!{
3660
}
3761
```
3862

39-
## Importing Fonts
63+
## Import Fonts
4064

4165
Importing fonts in Makepad is very similar to importing images. Makepad supports various font formats to allow you to customize the typography of your application. Supported font formats include:
4266

src/static/for_loop_1.png

25.6 KB
Loading

src/static/for_loop_2.png

199 KB
Loading

0 commit comments

Comments
 (0)