pub mod function_cache; pub mod math; pub mod ui; use function_cache::FunctionCache; use iced::executor; use iced::widget::column; use iced::widget::text_input; use iced::widget::{canvas, container}; use iced::{Application, Command, Element, Length, Settings, Theme}; use math::expression_function::ExpressionFunction; use std::sync::Mutex; use ui::graph::GraphCanvas; use ui::Message; pub fn main() -> iced::Result { Graph::run(Settings { antialiasing: true, ..Settings::default() }) } #[derive(Default)] struct Graph { input_state: String, graph_canvas: GraphCanvas, } impl Application for Graph { type Executor = executor::Default; type Message = Message; type Theme = Theme; type Flags = (); fn new(_flags: ()) -> (Self, Command) { let default_func = "f(x) = x^2".to_string(); ( Graph { graph_canvas: GraphCanvas::new(&default_func), input_state: default_func, ..Default::default() }, Command::none(), ) } fn title(&self) -> String { String::from("MathEval") } fn update(&mut self, message: Message) -> Command { match message { Message::InputChanged(s) => { self.input_state = s; } Message::UpdateFunction => { let func = ExpressionFunction::from_string( self.input_state.clone(), self.graph_canvas.ctx.operations(), ); self.graph_canvas.func = Mutex::new(FunctionCache::new(func)); self.graph_canvas.clear(); } Message::UpdateScreen => { self.graph_canvas.clear(); } } Command::none() } fn view(&self) -> Element { let input = text_input("x", &self.input_state) .on_input(Message::InputChanged) .on_submit(Message::UpdateFunction) .padding(15) .size(30); let canvas = canvas(&self.graph_canvas) .width(Length::Fill) .height(Length::Fill); let container = container(canvas) .width(Length::Fill) .height(Length::Fill) .padding(20); column![input, container].into() } }