Skip to content

Commit

Permalink
finish loop widget
Browse files Browse the repository at this point in the history
  • Loading branch information
syf20020816 committed Mar 26, 2024
1 parent 6ee752d commit 8204e25
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 37 deletions.
3 changes: 2 additions & 1 deletion src/gen/design/converter/converter.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Of course, I believe that my framework is not a replacement for Makepad, but rat

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

For those who pursue higher goals, Makepad can be directly used in DSL
For those who pursue higher goals, Makepad can be directly used in DSL

43 changes: 22 additions & 21 deletions src/gen/design/parser/parser.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
# Convert RSX DSL to AST
## How to convert
# Parser

## Convert RSX DSL to AST

```shell
Real AST
----------- --------------- Strategy ---------------
| RSX DSL | --> | ParseTarget | ----------> | ParseResult |
----------- --------------- ---------------
```
## RSX

- Template
- Script
- Style

### Template

1. Unrestricted tags (tag name is not constrained)
2. There are no styles, only properties, or in other words, all styles are properties
3. Nesting strings in tags is not allowed (example: <view>this is a view</view> ❌)

### Script
1. Allow Rust syntax

### Style

1. bind to tag by name
2. nesting allowed
3. function and bind allowed
## Convert To Makead

```shell
---------------------------
| |
------------------------ | MakepadConvertResult |
| AST (in ParseResult) | --------> | |
------------------------ | -------------------- |
------------------------ | | MakepadConverter | |
| File Meta Data | --------> | -------------------- |
------------------------ | |
---------------------------
|
|------------------→ |
|
------------ | ----------------
| Strategy | --------------| | Makepad Code |
------------ ----------------
```
12 changes: 6 additions & 6 deletions src/makepad/code/widgets/define.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

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.

## Starting with a New Module
## Init a New Module

First, create a new module for our custom widget:

```rust
pub mod my_label;
```

## Crafting a Live Design
## Design Live Design

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

Expand All @@ -36,7 +36,7 @@ live_design!{
}
```

## Defining the Widget Structure
## Define the Widget Structure

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

Expand All @@ -49,7 +49,7 @@ pub struct MyLabel{
}
```

## Implementing the Widget Trait
## Impl the Widget Trait

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

Expand All @@ -61,7 +61,7 @@ impl Widget for MyLabel {
}
```

## Registering in the Application
## Register in the Application

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

Expand All @@ -74,7 +74,7 @@ impl LiveRegister for App {
}
```

## Utilizing in the App's Live Design
## Use in the App's Live Design

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

Expand Down
8 changes: 4 additions & 4 deletions src/makepad/code/widgets/define2.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

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.

## Initiating with a New Module
## Init a New Module

Begin by creating a module specifically for our enhanced widget:

```rust
pub mod deref_label;
```

## Designing a Live Layout
## Design Live Design

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

Expand All @@ -35,7 +35,7 @@ live_design!{
}
```

## Creating a Deref Structure
## Define a Deref Structure

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

Expand All @@ -53,7 +53,7 @@ pub struct DerefLabel{
}
```

## Implementing the Widget Trait
## Impl the Widget Trait

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

Expand Down
102 changes: 99 additions & 3 deletions src/makepad/code/widgets/for.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

## Writing Live Design
## Write Live Design

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

Expand All @@ -27,7 +27,7 @@ live_design!{
}
```

## Writing the Widget
## Define the Widget Struct

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).

Expand Down Expand Up @@ -57,7 +57,7 @@ pub struct Footer {
}
```

## Drawing the Widget
## Draw the Widget

Implement the `draw_walk` method to render the widget.

Expand Down Expand Up @@ -89,4 +89,100 @@ impl Widget for Footer {
DrawStep::done()
}
}
```
---

## Impl handle_event

In order for all buttons to receive events, we need to implement the `handle_event` function

```rust
impl Widget for Footer{

// ... draw_walk() fn

fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
// Handle events for the current widget
for btn in self.btns.values_mut(){
// btn.handle_event(cx, event, scope);
for action in cx.capture_actions(|cx| btn.handle_event(cx, event, scope)){
match action.as_widget_action().cast() {
// Handle button actions
ButtonAction::Clicked => {
log!("Button clicked:{}", btn.text());
}
_ => {}
}
}
}
}
}
```

![](../../../static/for_loop_1.png)

## Pass Event

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

### Define Footer Action

Define a Focus event which get LiveId(Button's LiveId)

```rust
#[derive(Clone, Debug, DefaultNone)]
pub enum FooterAction{
None,
// LiveId can specialize the clicked button
Focus(LiveId),
}
```

### Change handle_event

```rust
impl Widget for Footer{

// ... draw_walk() fn

fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
let uid = self.widget_uid();
// Handle events for the current widget
for (btn_id, btn) in self.btns.iter_mut(){
// btn.handle_event(cx, event, scope);
for action in cx.capture_actions(|cx| btn.handle_event(cx, event, scope)){
match action.as_widget_action().cast() {
// Handle button actions
ButtonAction::Clicked => {
log!("Button clicked:{}", btn.text());
// pass event
cx.widget_action(uid, &scope.path, FooterAction::Focus(*btn_id))
}
_ => {}
}
}
}
}
}
```

### Impl MatchEvent for Parent Widget

impl `handle_actions` function, match the action we need to do and then handle.

```rust
impl MatchEvent for App{
fn handle_actions(&mut self, cx: &mut Cx, actions: &Actions) {
for action in actions{
match action.as_widget_action().cast(){
FooterAction::Focus(btn_id) => {
log!("Button focused:{}", btn_id);
}
_ => {
log!("other");
}
};
}
}
}
```
28 changes: 26 additions & 2 deletions src/makepad/syntax/import.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@

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.

## Importing Images
> widgets: `import `
>
> font, images, ...other static resources: `dep()`
## Import Widgets

use keyword: `import` can import widget in `live_design!`

```rust
live_design!{
import you_crate_name::widget_mod::widget_name;
}

// example
live_design!{
import hello::header::Header;
}

//import all
live_design!{
import hello::header::*;
}
```

## Import Images

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

Expand Down Expand Up @@ -36,7 +60,7 @@ live_design!{
}
```

## Importing Fonts
## Import Fonts

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:

Expand Down
Binary file added src/static/for_loop_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/static/for_loop_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8204e25

Please sign in to comment.