Skip to content

Commit 6e96f14

Browse files
committed
docs
1 parent aedf53e commit 6e96f14

File tree

3 files changed

+86
-104
lines changed

3 files changed

+86
-104
lines changed

README.md

+25-91
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,22 @@ fn color_background(ui: &mut Ui, color: egui::Color32) {
4545
impl eframe::App for MyApp {
4646
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
4747
egui::CentralPanel::default().show(ctx, |ui| {
48-
let dynamic_x = 5.0;
49-
let dynamic_if_separator = true;
50-
5148
load_layout!(
52-
<?xml version="1.0" encoding="utf-8"?>
53-
<Form>
54-
<Strip direction="north" gap="@dynamic_x" separator="@dynamic_if_separator">
55-
<Panel size="relative" value="0.4">
56-
<Strip direction="west">
57-
<Panel size="exact" value="250.0">
58-
color_background(ui, egui::Color32::from_rgb(255, 255, 0));
59-
</Panel>
60-
<Panel size="remainder">
61-
color_background(ui, egui::Color32::from_rgb(255, 0, 0));
62-
</Panel>
63-
</Strip>
64-
</Panel>
65-
<Panel size="remainder">
66-
<Strip direction="west">
67-
<Panel size="relative" value="0.3">
68-
color_background(ui, egui::Color32::from_rgb(0, 0, 255));
69-
</Panel>
70-
<Panel size="remainder">
71-
<Strip direction="north" gap="1.5">
72-
<Panel size="relative" value="0.3">
73-
color_background(ui, egui::Color32::from_rgb(0, 255, 255));
74-
</Panel>
75-
<Panel size="remainder">
76-
color_background(ui, egui::Color32::from_rgb(255, 0, 255));
77-
</Panel>
78-
</Strip>
79-
</Panel>
80-
</Strip>
81-
</Panel>
82-
</Strip>
83-
</Form>
49+
<Strip direction="west">
50+
<Panel size="relative" value="0.3">
51+
color_background(ui, egui::Color32::from_rgb(0, 0, 255));
52+
</Panel>
53+
<Panel size="remainder">
54+
<Strip direction="north">
55+
<Panel size="relative" value="0.3">
56+
color_background(ui, egui::Color32::from_rgb(0, 255, 255));
57+
</Panel>
58+
<Panel size="remainder">
59+
color_background(ui, egui::Color32::from_rgb(255, 0, 255));
60+
</Panel>
61+
</Strip>
62+
</Panel>
63+
</Strip>
8464
);
8565
});
8666
}
@@ -93,63 +73,38 @@ impl eframe::App for MyApp {
9373
use eframe::egui;
9474
use egui::{Rounding, Ui};
9575
use egui_xml::load_layout;
76+
9677
fn main() -> Result<(), eframe::Error> {
9778
let options = eframe::NativeOptions {
9879
..Default::default()
9980
};
10081
eframe::run_native("My egui App", options, Box::new(|_cc| Box::<MyApp>::new(MyApp)))
10182
}
83+
10284
struct MyApp;
85+
10386
fn color_background(ui: &mut Ui, color: egui::Color32) {
10487
ui.painter()
10588
.rect_filled(ui.available_rect_before_wrap(), Rounding::same(5.0), color);
10689
}
90+
10791
impl eframe::App for MyApp {
10892
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
10993
egui::CentralPanel::default()
11094
.show(
11195
ctx,
11296
|ui| {
113-
let dynamic_x = 5.0;
114-
let dynamic_if_separator = true;
11597
let mut macro_strip_builder = egui_extras::StripBuilder::new(ui);
11698
macro_strip_builder = macro_strip_builder
117-
.size(egui_extras::Size::relative(0.4));
118-
macro_strip_builder = macro_strip_builder
119-
.size(egui_extras::Size::exact(dynamic_x));
99+
.size(egui_extras::Size::relative(0.3));
120100
macro_strip_builder = macro_strip_builder
121101
.size(egui_extras::Size::remainder());
122102
let macro_strip_response = macro_strip_builder
123-
.vertical(|mut strip| {
103+
.horizontal(|mut strip| {
124104
strip
125105
.cell(|ui| {
126-
let mut macro_strip_builder = egui_extras::StripBuilder::new(
127-
ui,
128-
);
129-
macro_strip_builder = macro_strip_builder
130-
.size(egui_extras::Size::exact(250.0));
131-
macro_strip_builder = macro_strip_builder
132-
.size(egui_extras::Size::remainder());
133-
let macro_strip_response = macro_strip_builder
134-
.horizontal(|mut strip| {
135-
strip
136-
.cell(|ui| {
137-
color_background(ui, egui::Color32::from_rgb(255, 255, 0));
138-
});
139-
strip
140-
.cell(|ui| {
141-
color_background(ui, egui::Color32::from_rgb(255, 0, 0));
142-
});
143-
});
106+
color_background(ui, egui::Color32::from_rgb(0, 0, 255));
144107
});
145-
if dynamic_if_separator {
146-
strip
147-
.cell(|ui| {
148-
ui.separator();
149-
});
150-
} else {
151-
strip.empty();
152-
}
153108
strip
154109
.cell(|ui| {
155110
let mut macro_strip_builder = egui_extras::StripBuilder::new(
@@ -160,33 +115,14 @@ impl eframe::App for MyApp {
160115
macro_strip_builder = macro_strip_builder
161116
.size(egui_extras::Size::remainder());
162117
let macro_strip_response = macro_strip_builder
163-
.horizontal(|mut strip| {
118+
.vertical(|mut strip| {
164119
strip
165120
.cell(|ui| {
166-
color_background(ui, egui::Color32::from_rgb(0, 0, 255));
121+
color_background(ui, egui::Color32::from_rgb(0, 255, 255));
167122
});
168123
strip
169124
.cell(|ui| {
170-
let mut macro_strip_builder = egui_extras::StripBuilder::new(
171-
ui,
172-
);
173-
macro_strip_builder = macro_strip_builder
174-
.size(egui_extras::Size::relative(0.3));
175-
macro_strip_builder = macro_strip_builder
176-
.size(egui_extras::Size::exact(1.5));
177-
macro_strip_builder = macro_strip_builder
178-
.size(egui_extras::Size::remainder());
179-
let macro_strip_response = macro_strip_builder
180-
.vertical(|mut strip| {
181-
strip
182-
.cell(|ui| {
183-
color_background(ui, egui::Color32::from_rgb(0, 255, 255));
184-
});
185-
strip
186-
.cell(|ui| {
187-
color_background(ui, egui::Color32::from_rgb(255, 0, 255));
188-
});
189-
});
125+
color_background(ui, egui::Color32::from_rgb(255, 0, 255));
190126
});
191127
});
192128
});
@@ -197,8 +133,6 @@ impl eframe::App for MyApp {
197133
}
198134
```
199135

200-
201-
202136
In this example, the load_layout! macro takes an XML string that defines the structure and style of the UI. Dynamic values and conditions can be injected directly into the XML, allowing for a flexible and dynamic UI creation process.
203137
Getting Started
204138

macros/src/lib.rs

+61
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
//! `load_layout!` macro for loading layout from XML for egui
2+
//!
3+
//! The `load_layout!` macro allows for loading layout configurations from XML for use with the egui GUI framework. It takes an XML representation of the layout structure and generates Rust code to construct the layout within an egui UI.
4+
//!
5+
//! # Example
6+
//!
7+
//! ```rust
8+
//! # use egui_macros::load_layout;
9+
//! let layout_code = load_layout!(
10+
//! <Strip direction="west">
11+
//! <Panel size="relative" value="0.3">
12+
//! color_background(ui, egui::Color32::from_rgb(0, 0, 255));
13+
//! </Panel>
14+
//! <Panel size="remainder">
15+
//! <Strip direction="north">
16+
//! <Panel size="relative" value="0.3">
17+
//! color_background(ui, egui::Color32::from_rgb(0, 255, 255));
18+
//! </Panel>
19+
//! <Panel size="remainder">
20+
//! color_background(ui, egui::Color32::from_rgb(255, 0, 255));
21+
//! </Panel>
22+
//! </Strip>
23+
//! </Panel>
24+
//! </Strip>
25+
//! );
26+
//! ```
27+
128
extern crate proc_macro;
229

330
use std::{cell::RefCell, rc::Rc};
@@ -41,6 +68,31 @@ fn expand_node(
4168
}
4269
}
4370

71+
/// Macro for loading layout from XML.
72+
///
73+
/// This macro parses an XML layout representation and generates Rust code to construct the layout within an egui UI.
74+
///
75+
/// # Example
76+
///
77+
/// ```rust
78+
/// load_layout!(
79+
/// <Strip direction="west">
80+
/// <Panel size="relative" value="0.3">
81+
/// color_background(ui, egui::Color32::from_rgb(0, 0, 255));
82+
/// </Panel>
83+
/// <Panel size="remainder">
84+
/// <Strip direction="north">
85+
/// <Panel size="relative" value="0.3">
86+
/// color_background(ui, egui::Color32::from_rgb(0, 255, 255));
87+
/// </Panel>
88+
/// <Panel size="remainder">
89+
/// color_background(ui, egui::Color32::from_rgb(255, 0, 255));
90+
/// </Panel>
91+
/// </Strip>
92+
/// </Panel>
93+
/// </Strip>
94+
/// );
95+
/// ```
4496
#[proc_macro]
4597
pub fn load_layout(input: TokenStream) -> TokenStream {
4698
let xml = input.to_string();
@@ -60,6 +112,15 @@ pub fn load_layout(input: TokenStream) -> TokenStream {
60112
expanded.into()
61113
}
62114

115+
/// Macro for loading layout from a file.
116+
///
117+
/// This macro reads the content of the specified file and passes it to the `load_layout` macro for parsing and code generation.
118+
///
119+
/// # Example
120+
///
121+
/// ```rust
122+
/// load_layout_file!("layout.xml");
123+
/// ```
63124
#[proc_macro]
64125
pub fn load_layout_file(input: TokenStream) -> TokenStream {
65126
// Parse the input tokens into a syntax tree

src/lib.rs

-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,2 @@
11
pub use macros::load_layout;
22
pub use macros::load_layout_file;
3-
4-
mod test {
5-
#[test]
6-
fn test() {
7-
// use macros::load_layout;
8-
9-
//form!(
10-
// println!("Hello from test module");
11-
//);
12-
13-
println!("Hello from test module");
14-
}
15-
}

0 commit comments

Comments
 (0)