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
|
pub mod function_cache;
pub mod math;
pub mod ui;
use function_cache::FunctionCache;
use iced::{executor, Renderer};
use iced::widget::{canvas, container, pane_grid, responsive, text, text_input, PaneGrid};
use iced::{Application, Command, Element, Length, Settings, Theme};
use math::expression_function::ExpressionFunction;
use std::sync::Mutex;
use ui::graph::GraphCanvas;
use ui::Message;
use ui::pane::{Pane, PaneKind};
pub fn main() -> iced::Result {
Graph::run(Settings {
antialiasing: true,
..Settings::default()
})
}
struct Graph {
panes: pane_grid::State<Pane>,
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<Message>) {
let (mut panes, pane) = pane_grid::State::new(Pane::new(PaneKind::FunctionPane));
panes.split(
pane_grid::Axis::Vertical,
&pane,
Pane::new(PaneKind::GraphPane),
);
let default_func = "f(x) = x^2".to_string();
(
Graph {
graph_canvas: GraphCanvas::new(&default_func),
input_state: default_func,
panes,
},
Command::none(),
)
}
fn title(&self) -> String {
String::from("MathEval")
}
fn update(&mut self, message: Message) -> Command<Message> {
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();
}
Message::Resized(pane_grid::ResizeEvent { split, ratio }) => {
self.panes.resize(&split, ratio);
}
}
Command::none()
}
fn view(&self) -> Element<Message> {
let pane_grid = PaneGrid::new(&self.panes, |_id, pane, _| {
pane_grid::Content::new(responsive(move |_size| {
let child : Element<'_, Message, Renderer> = match pane.kind {
PaneKind::GraphPane => {
canvas(&self.graph_canvas)
.width(Length::Fill)
.height(Length::Fill).into()
}
PaneKind::FunctionPane => {
text_input("x", &self.input_state)
.on_input(Message::InputChanged)
.on_submit(Message::UpdateFunction)
.padding(15)
.size(30).into()
},
_ => text("").into(),
};
container(child)
.width(Length::Fill)
.height(Length::Fill)
.into()
}))
})
.width(Length::Fill)
.height(Length::Fill)
.spacing(10)
.on_resize(10, Message::Resized);
container(pane_grid)
.width(Length::Fill)
.height(Length::Fill)
.padding(10)
.into()
}
}
|