Working application
This commit is contained in:
1454
reaction_systems_gui/src/app.rs
Normal file
1454
reaction_systems_gui/src/app.rs
Normal file
File diff suppressed because it is too large
Load Diff
1943
reaction_systems_gui/src/app_logic.rs
Normal file
1943
reaction_systems_gui/src/app_logic.rs
Normal file
File diff suppressed because it is too large
Load Diff
163
reaction_systems_gui/src/helper.rs
Normal file
163
reaction_systems_gui/src/helper.rs
Normal file
@ -0,0 +1,163 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use eframe::egui::{self, Color32, TextFormat, TextStyle};
|
||||
use egui::text::LayoutJob;
|
||||
use grammar_separated::user_error::{UserError, UserErrorTypes};
|
||||
use lalrpop_util::ParseError;
|
||||
|
||||
fn create_error<T>(
|
||||
input_str: &str,
|
||||
l: usize,
|
||||
t: T,
|
||||
r: usize,
|
||||
expected: Option<Vec<String>>,
|
||||
error: Option<UserErrorTypes>,
|
||||
ctx: &eframe::egui::Context,
|
||||
) -> LayoutJob
|
||||
where
|
||||
T: Display,
|
||||
{
|
||||
let style = ctx.style();
|
||||
let monospace_text = TextStyle::Monospace.resolve(&style);
|
||||
let monospace = TextFormat {
|
||||
font_id: monospace_text.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
let monospace_red = TextFormat {
|
||||
font_id: monospace_text.clone(),
|
||||
color: Color32::RED,
|
||||
..Default::default()
|
||||
};
|
||||
let monospace_blue = TextFormat {
|
||||
font_id: monospace_text.clone(),
|
||||
color: Color32::BLUE,
|
||||
..Default::default()
|
||||
};
|
||||
let monospace_green = TextFormat {
|
||||
font_id: monospace_text,
|
||||
color: Color32::GREEN,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let mut err = LayoutJob::default();
|
||||
if let Some(error) = error {
|
||||
err.append(&format!("{error} "), 0., Default::default());
|
||||
err.append(&format!("\"{t}\""), 0., monospace_red.clone());
|
||||
err.append(
|
||||
&format!(" between positions {l} and {r}."),
|
||||
0.,
|
||||
Default::default(),
|
||||
);
|
||||
} else {
|
||||
err.append("Unrecognized token ", 0., Default::default());
|
||||
err.append(&format!("\"{t}\""), 0., monospace_red.clone());
|
||||
err.append(
|
||||
&format!(" between positions {l} and {r}."),
|
||||
0.,
|
||||
Default::default(),
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
if let Some(expected) = expected {
|
||||
// Temporary debug.
|
||||
err.append("\nExpected: ", 0., Default::default());
|
||||
let mut it = expected.iter().peekable();
|
||||
while let Some(s) = it.next() {
|
||||
err.append("(", 0., monospace.clone());
|
||||
err.append(s, 0., monospace_green.clone());
|
||||
err.append(")", 0., monospace.clone());
|
||||
if it.peek().is_some() {
|
||||
err.append(", ", 0., monospace.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let right_new_line = input_str[l..]
|
||||
.find("\n")
|
||||
.map(|pos| pos + l)
|
||||
.unwrap_or(input_str.len());
|
||||
let left_new_line = input_str[..r]
|
||||
.rfind("\n")
|
||||
.map(|pos| pos + 1)
|
||||
.unwrap_or_default();
|
||||
|
||||
let line_number = input_str[..l].match_indices('\n').count() + 1;
|
||||
let pre = format!("{line_number} |");
|
||||
|
||||
let line_pos_l = l - left_new_line;
|
||||
let line_pos_r = r - left_new_line;
|
||||
|
||||
err.append(
|
||||
&format!(
|
||||
"\nLine {} position {} to {}:\n",
|
||||
line_number, line_pos_l, line_pos_r,
|
||||
),
|
||||
0.,
|
||||
Default::default(),
|
||||
);
|
||||
err.append(&pre, 0., monospace_blue.clone());
|
||||
err.append(&input_str[left_new_line..l], 0., monospace_green);
|
||||
err.append(&input_str[l..r], 0., monospace_red.clone());
|
||||
err.append(&input_str[r..right_new_line], 0., monospace.clone());
|
||||
|
||||
err.append("\n", 0., monospace.clone());
|
||||
err.append(&" ".repeat(pre.len() - 1), 0., monospace.clone());
|
||||
err.append("|", 0., monospace_blue);
|
||||
err.append(&" ".repeat(l - left_new_line), 0., monospace.clone());
|
||||
err.append("^", 0., monospace_red.clone());
|
||||
|
||||
if r - l > 1 {
|
||||
err.append(&" ".repeat(r - l - 2), 0., monospace);
|
||||
err.append("^", 0., monospace_red);
|
||||
}
|
||||
|
||||
err
|
||||
}
|
||||
|
||||
pub fn reformat_error<T>(
|
||||
e: ParseError<usize, T, UserError>,
|
||||
input_str: &str,
|
||||
ctx: &eframe::egui::Context,
|
||||
) -> LayoutJob
|
||||
where
|
||||
T: Display,
|
||||
{
|
||||
let mut job = LayoutJob::default();
|
||||
match e {
|
||||
| ParseError::ExtraToken { token: (l, t, r) } => job.append(
|
||||
&format!(
|
||||
"Unexpected extra token \"{t}\" between positions {l} \
|
||||
and {r}."
|
||||
),
|
||||
0.,
|
||||
Default::default(),
|
||||
),
|
||||
| ParseError::UnrecognizedEof {
|
||||
location: _,
|
||||
expected: _,
|
||||
} => job.append(
|
||||
"End of file encountered while parsing.",
|
||||
0.,
|
||||
Default::default(),
|
||||
),
|
||||
| ParseError::InvalidToken { location } => job.append(
|
||||
&format!("Invalid token at position {location}."),
|
||||
0.,
|
||||
Default::default(),
|
||||
),
|
||||
| ParseError::UnrecognizedToken {
|
||||
token: (l, t, r),
|
||||
expected,
|
||||
} => job = create_error(input_str, l, t, r, Some(expected), None, ctx),
|
||||
| ParseError::User {
|
||||
error:
|
||||
UserError {
|
||||
token: (l, t, r),
|
||||
error,
|
||||
},
|
||||
} => job = create_error(input_str, l, t, r, None, Some(error), ctx),
|
||||
};
|
||||
|
||||
job
|
||||
}
|
||||
18
reaction_systems_gui/src/lib.rs
Normal file
18
reaction_systems_gui/src/lib.rs
Normal file
@ -0,0 +1,18 @@
|
||||
#![forbid(unsafe_code)]
|
||||
#![warn(clippy::all, rust_2018_idioms)]
|
||||
// Forbid warnings in release builds
|
||||
#![cfg_attr(not(debug_assertions), deny(warnings))]
|
||||
|
||||
mod app;
|
||||
mod app_logic;
|
||||
mod helper;
|
||||
|
||||
pub use app::AppHandle;
|
||||
|
||||
// If compiling for web
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
mod web;
|
||||
|
||||
// Export endpoints for wasm
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub use web::*;
|
||||
29
reaction_systems_gui/src/main.rs
Normal file
29
reaction_systems_gui/src/main.rs
Normal file
@ -0,0 +1,29 @@
|
||||
#![cfg_attr(not(debug_assertions), deny(warnings))]
|
||||
#![warn(clippy::all, rust_2018_idioms)]
|
||||
// hide console window on Windows in release
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use reaction_systems_gui::AppHandle;
|
||||
|
||||
// When compiling natively:
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn main() {
|
||||
use eframe::egui::Visuals;
|
||||
|
||||
eframe::run_native(
|
||||
"Reaction Systems",
|
||||
eframe::NativeOptions::default(),
|
||||
Box::new(|cc| {
|
||||
cc.egui_ctx.set_visuals(Visuals::dark());
|
||||
#[cfg(feature = "persistence")]
|
||||
{
|
||||
Ok(Box::new(AppHandle::new(cc)))
|
||||
}
|
||||
#[cfg(not(feature = "persistence"))]
|
||||
{
|
||||
Ok(Box::<AppHandle>::default())
|
||||
}
|
||||
}),
|
||||
)
|
||||
.expect("Failed to run native example");
|
||||
}
|
||||
77
reaction_systems_gui/src/web.rs
Normal file
77
reaction_systems_gui/src/web.rs
Normal file
@ -0,0 +1,77 @@
|
||||
#![allow(clippy::mem_forget)] // False positives from #[wasm_bindgen] macro
|
||||
|
||||
use eframe::wasm_bindgen::prelude::*;
|
||||
use eframe::wasm_bindgen::{self};
|
||||
|
||||
use crate::app::NodeGraphExample;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[wasm_bindgen()]
|
||||
pub struct WebHandle {
|
||||
runner: eframe::WebRunner,
|
||||
}
|
||||
|
||||
#[wasm_bindgen()]
|
||||
impl WebHandle {
|
||||
/// Installs a panic hook, then returns.
|
||||
#[allow(clippy::new_without_default, clippy::allow_attributes)]
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new() -> Self {
|
||||
// Redirect [`log`] message to `console.log` and friends:
|
||||
let log_level = if cfg!(debug_assertions) {
|
||||
log::LevelFilter::Trace
|
||||
} else {
|
||||
log::LevelFilter::Debug
|
||||
};
|
||||
eframe::WebLogger::init(log_level).ok();
|
||||
|
||||
Self {
|
||||
runner: eframe::WebRunner::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Call this once from JavaScript to start the app.
|
||||
#[wasm_bindgen]
|
||||
pub async fn start(
|
||||
&self,
|
||||
canvas: eframe::web_sys::HtmlCanvasElement,
|
||||
) -> Result<(), wasm_bindgen::JsValue> {
|
||||
self.runner
|
||||
.start(
|
||||
canvas,
|
||||
eframe::WebOptions::default(),
|
||||
Box::new(|_cc| {
|
||||
#[cfg(feature = "persistence")]
|
||||
{
|
||||
Ok(Box::new(NodeGraphExample::new(_cc)))
|
||||
}
|
||||
#[cfg(not(feature = "persistence"))]
|
||||
{
|
||||
Ok(Box::<NodeGraphExample>::default())
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn destroy(&self) {
|
||||
self.runner.destroy();
|
||||
}
|
||||
|
||||
/// The JavaScript can check whether or not the app has crashed:
|
||||
#[wasm_bindgen]
|
||||
pub fn has_panicked(&self) -> bool {
|
||||
self.runner.has_panicked()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn panic_message(&self) -> Option<String> {
|
||||
self.runner.panic_summary().map(|s| s.message())
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn panic_callstack(&self) -> Option<String> {
|
||||
self.runner.panic_summary().map(|s| s.callstack())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user