1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
use std::path::PathBuf;
use iced::theme::{Base, Mode, Theme};
use iced::{Task, window};
use rust_format::{Formatter, PostProcess, PrettyPlease};
use serde::{Deserialize, Serialize};
use smol::fs;
use super::rendered_element::RenderedElement;
use crate::Error;
use crate::appearance::iced_theme_from_str;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Project {
pub title: Option<String>,
pub theme: Option<String>,
pub element_tree: Option<RenderedElement>,
}
impl Default for Project {
fn default() -> Self {
Self::new()
}
}
impl Project {
pub fn new() -> Self {
Self {
title: None,
theme: None,
element_tree: None,
}
}
pub fn get_theme(&self, mode: Mode) -> Theme {
self.theme
.as_ref()
.and_then(|theme| iced_theme_from_str(theme))
.unwrap_or_else(|| Theme::default(mode))
}
pub async fn from_path(path: PathBuf) -> Result<(PathBuf, Self), Error> {
let contents = fs::read_to_string(&path).await?;
let project: Self = serde_json::from_str(&contents)?;
Ok((path, project))
}
pub fn from_file() -> Task<Result<(PathBuf, Self), Error>> {
window::latest()
.and_then(|id| {
window::run(id, |window| {
rfd::AsyncFileDialog::new()
.set_parent(window)
.set_title("Open a JSON file...")
.add_filter("*.json, *.JSON", &["json", "JSON"])
.pick_file()
})
})
.then(Task::future)
.map(|picked_file| picked_file.ok_or(Error::DialogClosed))
.and_then(|picked_file| {
Task::future(Self::from_path(picked_file.path().to_owned()))
})
}
pub fn write_to_file(
&self,
path: Option<PathBuf>,
) -> Task<Result<PathBuf, Error>> {
let path = if let Some(p) = path {
Task::done(Ok(p))
} else {
window::latest()
.and_then(|id| {
window::run(id, |window| {
rfd::AsyncFileDialog::new()
.set_parent(window)
.set_title("Save to JSON file...")
.add_filter("*.json, *.JSON", &["json", "JSON"])
.save_file()
})
})
.then(Task::future)
.map(|picked_file| picked_file.ok_or(Error::DialogClosed))
.and_then(|picked_file| {
Task::done(Ok(picked_file.path().to_owned()))
})
};
let contents = serde_json::to_string(self).map_err(Error::from);
path.and_then(move |path| {
Task::done(contents.clone().map(|contents| {
let path = path.clone();
async move {
fs::write(path.clone(), contents)
.await
.map(|_| path)
.map_err(Error::from)
}
}))
})
.and_then(Task::future)
.and_then(move |path| Task::done(Ok(path)))
}
pub fn app_code(&mut self, theme_mode: Mode) -> Result<String, Error> {
use iced::debug;
let codegen = debug::time("Code Generation");
let result = match self.element_tree {
Some(ref element_tree) => {
let (imports, view) = element_tree.codegen();
let theme = self.get_theme(theme_mode);
let app_code = format!(
r#"// Automatically generated by iced Builder
use iced::{{widget::{{{imports}}},Element}};
_blank_!();
fn main() -> iced::Result {{
iced::application(State::default, State::update, State::view).title("{title}").theme(State::theme).run()
}}
_blank_!();
#[derive(Default)]
struct State;
_blank_!();
#[derive(Debug, Clone)]
enum Message {{}}
_blank_!();
impl State {{
fn update(&mut self, _message: Message) {{
// Insert your desired update logic here
}}
_blank_!();
fn theme(&self) -> iced::Theme {{
iced::Theme::{theme}
}}
_blank_!();
fn view(&self) -> Element<Message> {{
{view}.into()
}}
}}"#,
title = match self.title {
Some(ref t) => t,
None => "New app",
},
theme = theme.to_string().replace(" ", "")
);
let config = rust_format::Config::new_str()
.post_proc(PostProcess::ReplaceMarkers);
Ok(PrettyPlease::from_config(config).format_str(&app_code)?)
}
None => Err("No element tree present".into()),
};
codegen.finish();
result
}
}
|