Skip to content

Commit 88262b0

Browse files
committed
add proc-macro
1 parent 883163a commit 88262b0

File tree

21 files changed

+230
-252
lines changed

21 files changed

+230
-252
lines changed

README.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,54 @@ Then run:
1818
$ fl2rust <fl file>.fl > <output file>.rs
1919
```
2020

21-
### As a library
21+
### As a proc-macro
22+
Add fl2rust-macro to your list of dependencies:
23+
```toml
24+
# Cargo.toml
25+
[dependencies]
26+
fltk = "1"
27+
fl2rust-macro = "0.5"
28+
```
29+
30+
The ui file that's generated by fluid, we'll name it myuifile.fl and keep it in our src directory:
31+
```
32+
# data file for the Fltk User Interface Designer (fluid)
33+
version 1.0400
34+
header_name {.h}
35+
code_name {.cxx}
36+
class UserInterface {open
37+
} {
38+
Function {make_window()} {open
39+
} {
40+
Fl_Window {} {open selected
41+
xywh {138 161 440 355} type Double visible
42+
} {
43+
Fl_Button but {
44+
label {Click me}
45+
xywh {175 230 95 45}
46+
}
47+
}
48+
}
49+
}
50+
```
51+
52+
In our main source file:
53+
```rust
54+
use fltk::{prelude::*, *};
55+
56+
mod ui {
57+
fl2rust_macro::include_ui!("src/myui.fl");
58+
}
59+
60+
fn main() {
61+
let a = app::App::default();
62+
let mut ui = ui::UserInterface::make_window();
63+
ui.but.set_callback(|b| println!("Button clicked!"));
64+
a.run().unwrap();
65+
}
66+
```
67+
68+
### As a dev-dependency
2269

2370
(A template repo usable via [cargo-generate](https://crates.io/crates/cargo-generate) can be found [here](https://github.com/fltk-rs/fl2rust-template))
2471

@@ -45,8 +92,8 @@ fn main() {
4592
}
4693
```
4794

95+
The ui file that's generated by fluid, we'll name it myuifile.fl and keep it in our src directory:
4896
```
49-
# src/myuifile.fl -> generated via fluid
5097
# data file for the Fltk User Interface Designer (fluid)
5198
version 1.0400
5299
header_name {.h}
@@ -113,7 +160,6 @@ extern crate tr;
113160
- Initialize tr as described in the tr crate's documentation.
114161

115162
## Known limitations
116-
- Adding arbitrary code or declaring global/member variables is unsupported.
117163
- Only constructor methods are supported.
118164
- fl2rust doesn't check the generated Rust code for correctness.
119165

examples/devdep-example/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "simple"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
fltk = "1.3.20"
10+
11+
[build-dependencies]
12+
fl2rust = { path = "../../fl2rust" }
File renamed without changes.

examples/devdep-example/src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use fltk::{prelude::*, *};
2+
3+
mod myuifile;
4+
5+
fn main() {
6+
let app = app::App::default();
7+
let _ui = myuifile::UserInterface::make_window();
8+
app.run().unwrap();
9+
}

examples/fltkform-example/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ fltk-form = "0.1"
1111
fltk-form-derive = "0.1"
1212

1313
[build-dependencies]
14-
fl2rust = { path = "../.." }
14+
fl2rust = { path = "../../fl2rust" }

examples/simple/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ edition = "2021"
77

88
[dependencies]
99
fltk = "1.3.20"
10-
11-
[build-dependencies]
12-
fl2rust = { path = "../.." }
10+
fl2rust-macro = { path = "../../fl2rust-macro" }

examples/simple/src/main.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use fltk::{prelude::*, *};
22

3-
mod myuifile;
3+
mod ui {
4+
fl2rust_macro::include_ui!("src/myui.fl");
5+
}
46

57
fn main() {
6-
let app = app::App::default();
7-
let _ui = myuifile::UserInterface::make_window();
8-
app.run().unwrap();
8+
let a = app::App::default();
9+
let mut ui = ui::UserInterface::make_window();
10+
ui.but.set_callback(|b| println!("Button clicked!"));
11+
a.run().unwrap();
912
}

examples/simple/src/myui.fl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# data file for the Fltk User Interface Designer (fluid)
2+
version 1.0400
3+
header_name {.h}
4+
code_name {.cxx}
5+
class UserInterface {open
6+
} {
7+
Function {make_window()} {open
8+
} {
9+
Fl_Window {} {open selected
10+
xywh {138 161 440 355} type Double visible
11+
} {
12+
Fl_Button but {
13+
label {Click me}
14+
xywh {175 230 95 45} color 25
15+
}
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)