plottery_editor 0.7.3

Graphical Editor of Plottery, a creative coding framework for generative vector graphics and pen plotting.
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use crate::{
    components::{image::*, loading_spinner::Loading, navigation::Navigation},
    router::editor::{
        console::Console,
        console_messages::ConsoleMessages,
        external_editor_button::ExternalEditorButton,
        layer_editor::{layer_editor::LayerEditor, layer_tree_ref::LayerTreeReference},
        params_editor::params_editor::ParamsEditor,
        project_hot_reload::start_hot_reload,
        project_runner::ProjectRunner,
        running_state::RunningState,
    },
    util::format_svg,
};
use bincode::{deserialize, serialize};
use dioxus::prelude::*;
use dioxus_logger::tracing;
use notify::FsEventWatcher;
use plottery_lib::{Layer, LayerPropsInheritable, SampleSettings};
use plottery_project::{project_params_list_wrapper::ProjectParamsListWrapper, Project};
use plottery_server_lib::{plot_setting::PlotSettings, task::send_task};
use std::{path::PathBuf, sync::Arc};
use tokio::{sync::Mutex, task::JoinHandle};

fn is_gitkraken_installed() -> bool {
    which::which("GitKraken").is_ok()
}

fn is_vscode_installed() -> bool {
    which::which("code").is_ok()
}

fn is_zed_installed() -> bool {
    which::which("zed").is_ok()
}

fn is_cursor_installed() -> bool {
    which::which("cursor").is_ok()
}

fn is_sublime_installed() -> bool {
    which::which("subl").is_ok()
}

#[derive(Debug, Clone)]
pub struct LayerChangeWrapper {
    pub layer: Option<Layer>,
    pub change_counter: u32,
}
impl PartialEq for LayerChangeWrapper {
    fn eq(&self, other: &Self) -> bool {
        self.change_counter == other.change_counter
    }
}

#[component]
pub fn Editor(project_path: Vec<String>) -> Element {
    let project = use_signal(|| {
        let path_str = format!("/{}", project_path.join("/"));
        let p = PathBuf::from(path_str);
        Project::load_from_file(p).expect("Failed to load project from file")
    });

    let release = true;

    let gitkraken_installed = use_signal(is_gitkraken_installed);
    let vscode_installed = use_signal(is_vscode_installed);
    let zed_installed = use_signal(is_zed_installed);
    let cursor_installed = use_signal(is_cursor_installed);
    let sublime_installed = use_signal(is_sublime_installed);

    // console
    let console_change_counter = use_signal_sync(|| 0);
    let console: Signal<ConsoleMessages, SyncStorage> =
        use_signal_sync(|| ConsoleMessages::new(console_change_counter));
    let mut console_expanded = use_signal(|| false);

    // ui state
    let project_params = use_signal_sync(|| {
        // read params from binary file
        let params_file_path = project().get_editor_params_path();
        match std::fs::read(params_file_path) {
            Ok(params_binary) => match deserialize(&params_binary) {
                Ok(params) => ProjectParamsListWrapper::new(params),
                Err(err) => {
                    console()
                        .error(format!("failed to deserialize project params: {:?}", err).as_str());
                    ProjectParamsListWrapper::new(vec![])
                }
            },
            Err(_) => ProjectParamsListWrapper::new(vec![]),
        }
    });
    let layer = use_signal_sync(|| {
        let layer_from_file = {
            match std::fs::read(project().get_editor_layer_path()) {
                Ok(layer_binary) => match Layer::new_from_binary(&layer_binary) {
                    Ok(layer) => Some(layer),
                    Err(err) => {
                        console().error(
                            format!("failed to deserialize layer from binary: {:?}", err).as_str(),
                        );
                        None
                    }
                },
                Err(err) => {
                    console().error(format!("failed to read layer from file: {:?}", err).as_str());
                    None
                }
            }
        };
        LayerChangeWrapper {
            layer: layer_from_file,
            change_counter: 0,
        }
    });
    let mut layer_tree_ref = use_signal_sync(move || None);
    use_effect(move || {
        layer_tree_ref.set(
            layer()
                .layer
                .map(|layer| LayerTreeReference::new(&layer, &LayerPropsInheritable::default())),
        )
    });
    let mut layer_only_visible_change_counter = use_signal(|| 0);
    let layer_only_visible = use_memo(move || {
        let mut cc = layer_only_visible_change_counter.write();
        let new_count = *cc + 1;
        *cc = new_count;
        let layer = layer().clone();
        match layer_tree_ref() {
            Some(layer_tree_ref) => LayerChangeWrapper {
                layer: Some(layer_tree_ref.filter_layer_by_visibility(&layer.layer.unwrap())),
                change_counter: new_count,
            },
            None => LayerChangeWrapper {
                layer: None,
                change_counter: new_count,
            },
        }
    });

    // hooks for changes in project
    // params
    use_effect(move || {
        let params_binary =
            serialize(&(project_params())).expect("Failed to serialize project params");
        let params_file_path = project().get_editor_params_path();
        std::fs::write(params_file_path, params_binary)
            .expect("Failed to write project params to file");
    });
    // layer
    let svg = use_memo(move || {
        layer_only_visible()
            .layer
            .as_ref()
            .map(|layer| layer.to_svg(1.0).to_string())
    });
    use_effect(move || {
        let layer_path = project().get_editor_layer_path();
        if let Some(layer) = &layer().layer {
            let binary = layer.to_binary().expect("Failed to serialize layer");
            match std::fs::write(layer_path, binary) {
                Ok(_) => (),
                Err(e) => tracing::error!("Failed to write layer to file: {:?}", e),
            }
        }
        let svg_path = project().get_editor_preview_image_path();
        if let Some(svg) = svg() {
            match std::fs::write(svg_path, svg) {
                Ok(_) => (),
                Err(e) => tracing::error!("Failed to write .svg to file: {:?}", e),
            }
        }
    });

    // running project
    let mut running_state = use_signal_sync(|| RunningState::Idle);
    let project_runner = use_signal_sync(|| {
        Arc::new(Mutex::new(ProjectRunner::new(
            project().clone(),
            layer,
            project_params,
        )))
    });

    // hot reload
    let mut hot_reload_watcher = use_signal_sync(|| None as Option<FsEventWatcher>);
    let mut hot_reload_join_handle = use_signal_sync(|| None as Option<JoinHandle<()>>);
    let hot_reload_path_to_watch = project().get_cargo_path().unwrap().join("src");
    let hot_reload_is_enabled = move || hot_reload_watcher.read().is_some();

    let hot_reload_button_class = if hot_reload_is_enabled() {
        "active_button"
    } else {
        ""
    };

    let running_state_class = if running_state().is_error() {
        "running_state err_box"
    } else {
        "running_state"
    };

    let icon_folder = if cfg!(target_os = "windows") {
        format_svg(include_bytes!("../../../public/icons/explorer.svg"))
    } else if cfg!(target_os = "macos") {
        format_svg(include_bytes!("../../../public/icons/finder.svg"))
    } else {
        format_svg(include_bytes!("../../../public/icons/linux_folder.svg"))
    };

    rsx! {
        style { { include_str!("./editor.css") } }
        Navigation { page_name: "{project().config.name.clone()}", body: rsx! {} }

        div { class: "Editor",
            div { class: "plot_header",
                div { class: "open_actions",
                    {
                        let cargo_dir = use_memo(move || project().get_cargo_path().unwrap());
                        let project_dir = use_memo(move || project().get_dir());
                        rsx! {
                            if vscode_installed() {
                                ExternalEditorButton {
                                    editor_name: "VS Code".to_string(),
                                    editor_command: "code".to_string(),
                                    icon_svg: include_bytes!("../../../public/icons/vscode.svg"),
                                    project_dir: cargo_dir,
                                }
                            }
                            if zed_installed() {
                                ExternalEditorButton {
                                    editor_name: "Zed".to_string(),
                                    editor_command: "zed".to_string(),
                                    icon_svg: include_bytes!("../../../public/icons/zed.svg"),
                                    project_dir: cargo_dir,
                                }
                            }
                            if cursor_installed() {
                                ExternalEditorButton {
                                    editor_name: "Cursor".to_string(),
                                    editor_command: "cursor".to_string(),
                                    icon_svg: include_bytes!("../../../public/icons/cursor.svg"),
                                    project_dir: cargo_dir,
                                }
                            }
                            if sublime_installed() {
                                ExternalEditorButton {
                                    editor_name: "Sublime Text".to_string(),
                                    editor_command: "subl".to_string(),
                                    icon_svg: include_bytes!("../../../public/icons/sublime-text.svg"),
                                    project_dir: cargo_dir,
                                }
                            }
                            button { class: "icon_button",
                                onclick: move |event| {
                                    open::that_in_background(project().dir).join().unwrap().unwrap();
                                    event.stop_propagation();
                                },
                                img { src: "{icon_folder}" }
                            }
                            if gitkraken_installed() {
                                ExternalEditorButton {
                                    editor_name: "GitKraken".to_string(),
                                    editor_command: "GitKraken".to_string(),
                                    icon_svg: include_bytes!("../../../public/icons/gitkraken.svg"),
                                    project_dir: project_dir,
                                    extra_args: vec!["-p".to_string()],
                                }
                            }
                        }
                    }
                }
                div { class: "run_actions",
                    if !matches!(running_state(), RunningState::Idle) {
                        div { class: "{running_state_class}",
                            p { "{running_state().get_msg()}" }
                        }
                    }
                    if !hot_reload_is_enabled() {
                        button { class: "img_button",
                            onclick: move |event| {
                                running_state.set(RunningState::Preparing { msg: "preparing".to_string() });
                                match project_runner().try_lock() {
                                    Ok(mut runner) => runner.trigger_run_project(release, running_state, console),
                                    Err(e) => {
                                        tracing::error!("Error preparing to run: {:?}", e);
                                        running_state.set(RunningState::RunFailed { msg: format!("Error preparing to run: {}", e) });
                                    },
                                }
                                event.stop_propagation();
                            },
                            img { src: "{format_svg(include_bytes!(\"../../../public/icons/play.svg\"))}" }
                        }
                    }
                    button { class: "img_button {hot_reload_button_class}",
                        onclick: move |event| {
                            if hot_reload_is_enabled() {
                                // Disable hot reload
                                hot_reload_watcher.set(None);
                                hot_reload_join_handle.set(None);
                            } else {
                                // Enable hot reload
                                let (handle, watcher) = start_hot_reload(
                                    hot_reload_path_to_watch.clone(),
                                    release,
                                    project_runner().clone(),
                                    running_state,
                                    console,
                                );
                                hot_reload_join_handle.set(Some(handle));
                                hot_reload_watcher.set(Some(watcher));
                            }
                            event.stop_propagation();
                        },
                        p { "hot reload" }
                    }
                }
                div { class: "output_actions",
                    button { class: "img_button",
                        onclick: move |event| {
                            match layer_only_visible().layer {
                                Some(layer) => {
                                    tokio::spawn(async move {
                                        console().info("...sending plot");
                                        let plot_result = send_task(plottery_server_lib::task::Task::Plot {
                                            layer: layer.clone(),
                                            sample_settings: SampleSettings::default(),
                                            plot_settings: PlotSettings::default()
                                        }).await;
                                        if plot_result.is_err() {
                                            console().error(format!("failed to send plot: {:?}", plot_result.err().unwrap()).as_str());
                                        }
                                    });
                                },
                                None => {
                                    console().error("cannot send plot: no layer available");
                                }
                            }
                            event.stop_propagation();
                        },
                        p { "send plot" }
                    }
                    button { class: "img_button",
                        onclick: move |event| {
                            if let Some(svg) = svg() {
                                let path = std::env::temp_dir().join("temp_editor.svg");
                                std::fs::write(&path, svg).unwrap();
                                if let Err(err) = open::that_in_background(path).join() {
                                    tracing::error!("Failed to open svg: {:?}", err);
                                }
                            }
                            event.stop_propagation();
                        },
                        p { "open svg" }
                    }
                }
            }

            div { class: "plot_and_params",
                div { class: "params",
                    ParamsEditor {
                        project_params,
                        project_runner,
                        running_state,
                        console,
                        release,
                    }
                }
                div { class: "plot_and_console",
                    div { class: "plot",
                        if project().get_editor_preview_image_path().exists() {
                            if running_state().is_busy() {
                                Loading {}
                            }
                            if let Some(svg) = svg() {
                                SVGImage { svg }
                            }
                            else {
                                Image {
                                    img_path: project().get_editor_preview_image_path().to_str().unwrap().to_string(),
                                    redraw_counter: layer().change_counter,
                                }
                            }
                        } else {
                            div { class: "err_box",
                                p { ".svg could not be found!" }
                            }
                        }
                    }
                    div { class: "console_container",
                        {
                            let button_text = if console_expanded() { "" } else { "" };
                            rsx! {
                                button {
                                    class: "console_toggle",
                                    onclick: move |_| {
                                        let current = console_expanded();
                                        console_expanded.set(!current);
                                    },
                                    "{button_text}"
                                }
                            }
                        }
                        div { class: if console_expanded() { "console expanded" } else { "console" },
                            Console {
                                console: console,
                            }
                        }
                    }
                }
                LayerEditor {
                    layer_tree_ref,
                    running_state: running_state,
                }
            }
        }
    }
}