Skip to content

Commit 6ee752d

Browse files
committed
for loop widget
1 parent 85b028b commit 6ee752d

File tree

5 files changed

+104
-2
lines changed

5 files changed

+104
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ Cargo.lock
1313
# MSVC Windows builds of rustc generate these, which store debugging information
1414
*.pdb
1515

16-
./book/*
16+
/book/*

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888

8989
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
9090
<div class="sidebar-scrollbox">
91-
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html" class="active">Introduction</a></li><li class="chapter-item expanded "><a href="gen/quickstart.html"><strong aria-hidden="true">1.</strong> QuickStart</a></li><li class="chapter-item expanded "><a href="makepad/makepad.html"><strong aria-hidden="true">2.</strong> Makepad</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/quickstart.html"><strong aria-hidden="true">2.1.</strong> QuickStart</a></li><li class="chapter-item expanded "><a href="makepad/code/code.html"><strong aria-hidden="true">2.2.</strong> Code Guide</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/code/widgets/define.html"><strong aria-hidden="true">2.2.1.</strong> Define Easy Widget</a></li><li class="chapter-item expanded "><a href="makepad/code/widgets/define2.html"><strong aria-hidden="true">2.2.2.</strong> Define Deref Widget</a></li></ol></li><li class="chapter-item expanded "><a href="makepad/built-in/built-in.html"><strong aria-hidden="true">2.3.</strong> Built-In</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/built-in/types/types.html"><strong aria-hidden="true">2.3.1.</strong> Types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/built-in/types/walk.html"><strong aria-hidden="true">2.3.1.1.</strong> Walk</a></li></ol></li><li class="chapter-item expanded "><a href="makepad/built-in/widgets/widgets.html"><strong aria-hidden="true">2.3.2.</strong> Widgets</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/built-in/widgets/window.html"><strong aria-hidden="true">2.3.2.1.</strong> Window</a></li><li class="chapter-item expanded "><a href="makepad/built-in/widgets/view.html"><strong aria-hidden="true">2.3.2.2.</strong> View</a></li></ol></li></ol></li><li class="chapter-item expanded "><a href="makepad/syntax/syntax.html"><strong aria-hidden="true">2.4.</strong> Syntax</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/syntax/global.html"><strong aria-hidden="true">2.4.1.</strong> Global</a></li><li class="chapter-item expanded "><a href="makepad/syntax/import.html"><strong aria-hidden="true">2.4.2.</strong> Import</a></li><li class="chapter-item expanded "><a href="makepad/syntax/macro/macro.html"><strong aria-hidden="true">2.4.3.</strong> Macro</a></li><li class="chapter-item expanded "><a href="makepad/syntax/redraw.html"><strong aria-hidden="true">2.4.4.</strong> Redraw</a></li><li class="chapter-item expanded "><a href="makepad/event/event.html"><strong aria-hidden="true">2.4.5.</strong> Event</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/event/match_event.html"><strong aria-hidden="true">2.4.5.1.</strong> MatchEvent</a></li></ol></li></ol></li></ol></li><li class="chapter-item expanded "><a href="gen/design/gen-ui-design.html"><strong aria-hidden="true">3.</strong> GenUI-Design</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="gen/design/parser/parser.html"><strong aria-hidden="true">3.1.</strong> Parser</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="gen/design/parser/result.html"><strong aria-hidden="true">3.1.1.</strong> Result</a></li><li class="chapter-item expanded "><a href="gen/design/parser/all.html"><strong aria-hidden="true">3.1.2.</strong> All</a></li></ol></li><li class="chapter-item expanded "><a href="gen/design/converter/converter.html"><strong aria-hidden="true">3.2.</strong> Converter</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="gen/design/converter/rsx-makepad.html"><strong aria-hidden="true">3.2.1.</strong> Makepad</a></li></ol></li><li class="chapter-item expanded "><a href="gen/design/issues/issues.html"><strong aria-hidden="true">3.3.</strong> issues</a></li><li class="chapter-item expanded "><a href="gen/design/future.html"><strong aria-hidden="true">3.4.</strong> future</a></li></ol></li></ol>
91+
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html" class="active">Introduction</a></li><li class="chapter-item expanded "><a href="gen/quickstart.html"><strong aria-hidden="true">1.</strong> QuickStart</a></li><li class="chapter-item expanded "><a href="makepad/makepad.html"><strong aria-hidden="true">2.</strong> Makepad</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/quickstart.html"><strong aria-hidden="true">2.1.</strong> QuickStart</a></li><li class="chapter-item expanded "><a href="makepad/code/code.html"><strong aria-hidden="true">2.2.</strong> Code Guide</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/code/widgets/define.html"><strong aria-hidden="true">2.2.1.</strong> Define Easy Widget</a></li><li class="chapter-item expanded "><a href="makepad/code/widgets/define2.html"><strong aria-hidden="true">2.2.2.</strong> Define Deref Widget</a></li><li class="chapter-item expanded "><a href="makepad/code/widgets/for.html"><strong aria-hidden="true">2.2.3.</strong> For Loop Widget</a></li></ol></li><li class="chapter-item expanded "><a href="makepad/built-in/built-in.html"><strong aria-hidden="true">2.3.</strong> Built-In</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/built-in/types/types.html"><strong aria-hidden="true">2.3.1.</strong> Types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/built-in/types/walk.html"><strong aria-hidden="true">2.3.1.1.</strong> Walk</a></li></ol></li><li class="chapter-item expanded "><a href="makepad/built-in/widgets/widgets.html"><strong aria-hidden="true">2.3.2.</strong> Widgets</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/built-in/widgets/window.html"><strong aria-hidden="true">2.3.2.1.</strong> Window</a></li><li class="chapter-item expanded "><a href="makepad/built-in/widgets/view.html"><strong aria-hidden="true">2.3.2.2.</strong> View</a></li></ol></li></ol></li><li class="chapter-item expanded "><a href="makepad/syntax/syntax.html"><strong aria-hidden="true">2.4.</strong> Syntax</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/syntax/global.html"><strong aria-hidden="true">2.4.1.</strong> Global</a></li><li class="chapter-item expanded "><a href="makepad/syntax/import.html"><strong aria-hidden="true">2.4.2.</strong> Import</a></li><li class="chapter-item expanded "><a href="makepad/syntax/macro/macro.html"><strong aria-hidden="true">2.4.3.</strong> Macro</a></li><li class="chapter-item expanded "><a href="makepad/syntax/redraw.html"><strong aria-hidden="true">2.4.4.</strong> Redraw</a></li><li class="chapter-item expanded "><a href="makepad/event/event.html"><strong aria-hidden="true">2.4.5.</strong> Event</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="makepad/event/match_event.html"><strong aria-hidden="true">2.4.5.1.</strong> MatchEvent</a></li></ol></li></ol></li></ol></li><li class="chapter-item expanded "><a href="gen/design/gen-ui-design.html"><strong aria-hidden="true">3.</strong> GenUI-Design</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="gen/design/parser/parser.html"><strong aria-hidden="true">3.1.</strong> Parser</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="gen/design/parser/result.html"><strong aria-hidden="true">3.1.1.</strong> Result</a></li><li class="chapter-item expanded "><a href="gen/design/parser/all.html"><strong aria-hidden="true">3.1.2.</strong> All</a></li></ol></li><li class="chapter-item expanded "><a href="gen/design/converter/converter.html"><strong aria-hidden="true">3.2.</strong> Converter</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="gen/design/converter/rsx-makepad.html"><strong aria-hidden="true">3.2.1.</strong> Makepad</a></li></ol></li><li class="chapter-item expanded "><a href="gen/design/issues/issues.html"><strong aria-hidden="true">3.3.</strong> issues</a></li><li class="chapter-item expanded "><a href="gen/design/future.html"><strong aria-hidden="true">3.4.</strong> future</a></li></ol></li></ol>
9292
</div>
9393
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
9494
<div class="sidebar-resize-indicator"></div>

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [Code Guide](./makepad/code/code.md)
99
- [Define Easy Widget](./makepad/code/widgets/define.md)
1010
- [Define Deref Widget](./makepad/code/widgets/define2.md)
11+
- [For Loop Widget](./makepad/code/widgets/for.md)
1112
- [Built-In](./makepad/built-in/built-in.md)
1213
- [Types](./makepad/built-in/types/types.md)
1314
- [Walk](./makepad/built-in/types/walk.md)

src/makepad/built-in/types/area.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Area
2+
3+
界面元素的布局:Area 可以代表界面上的各种元素,如按钮、文本框、图像等。通过对 Area 进行布局和管理,可以构建复杂的用户界面。
4+
5+
事件处理:Area 可以响应和处理各种用户事件,如鼠标点击、滑动、键盘输入等。开发者可以为特定的 Area 定义事件处理逻辑,实现交互功能。
6+
7+
绘制和动画:Area 不仅可以管理静态的界面元素,还可以用来绘制图形和实现动画效果。通过更新 Area 的属性和状态,可以在屏幕上渲染动态的内容。
8+
9+
性能优化:在 Makepad 中,Area 的设计有助于优化应用程序的性能。只有当 Area 可见并且需要更新时,系统才会对其进行重新绘制,这有助于减少不必要的计算和渲染,提高应用程序的响应速度和效率。

src/makepad/code/widgets/for.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# For Loop Widget
2+
3+
This example demonstrates how to generate components from iterable data in Makepad.
4+
5+
## Writing Live Design
6+
7+
Note that we use `btn: <Button>` instead of `btn = <Button>`. This implies that the button is treated as a prop here.
8+
9+
```rust
10+
use makepad_widgets::*;
11+
12+
live_design!{
13+
// Import the essential base and theme components from makepad_widgets
14+
import makepad_widgets::base::*;
15+
import makepad_widgets::theme_desktop_dark::*;
16+
17+
// Define a new component named Footer
18+
Footer = {{Footer}}{
19+
// Set the height and width to fill their container
20+
height: Fill,
21+
width: Fill,
22+
// Define a button with a text prop
23+
btn: <Button>{
24+
text: "Click me",
25+
}
26+
}
27+
}
28+
```
29+
30+
## Writing the Widget
31+
32+
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).
33+
34+
- `Area`: Provides a renderable area.
35+
- `LivePtr`: A special pointer type used to reference `#[live]` data or resources.
36+
- `ComponentMap`: A container for managing widgets.
37+
38+
```rust
39+
#[derive(Live, LiveHook, Widget)]
40+
pub struct Footer {
41+
// The area property provides a specific region for rendering
42+
#[rust]
43+
#[redraw]
44+
area: Area,
45+
// Layout for arranging child widgets or components
46+
#[layout]
47+
layout: Layout,
48+
// Walk specifies how child components are positioned and sized
49+
#[walk]
50+
walk: Walk,
51+
// Optional property to hold a reference to the button's live data
52+
#[live]
53+
btn: Option<LivePtr>,
54+
// Maps button identifiers to their corresponding ButtonRef objects
55+
#[rust]
56+
btns: ComponentMap<LiveId, ButtonRef>
57+
}
58+
```
59+
60+
## Drawing the Widget
61+
62+
Implement the `draw_walk` method to render the widget.
63+
64+
```rust
65+
impl Widget for Footer {
66+
fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, walk: Walk) -> DrawStep {
67+
// Start drawing the widget using the specified layout
68+
cx.begin_turtle(walk, self.layout);
69+
// Example data to iterate over
70+
let data = vec!["1","2","3"];
71+
for (d_id, name) in data.iter().enumerate() {
72+
let id = LiveId(d_id as u64);
73+
// Retrieve or insert a new button based on the id
74+
let c_btn = self.btns.get_or_insert(cx, id, |cx|{
75+
// Create a new button from the live pointer
76+
WidgetRef::new_from_ptr(cx, self.btn).as_button()
77+
});
78+
// Set the button's text
79+
c_btn.set_text(name);
80+
// Draw the button
81+
c_btn.draw_all(cx, scope);
82+
}
83+
84+
// End the drawing process for the current widget
85+
cx.end_turtle();
86+
// Retain only the buttons that are visible
87+
self.btns.retain_visible();
88+
// Indicate that the drawing step is complete
89+
DrawStep::done()
90+
}
91+
}
92+
```

0 commit comments

Comments
 (0)