init2
This commit is contained in:
parent
88b05d196f
commit
ad96685ce2
1 changed files with 66 additions and 6 deletions
72
src/app.rs
72
src/app.rs
|
@ -1,11 +1,12 @@
|
||||||
use eframe::egui::{Context, TextEdit, Ui};
|
use eframe::egui::{Context, Id, TextEdit, Ui};
|
||||||
use eframe::{egui, Frame};
|
use eframe::{egui, Frame};
|
||||||
use eframe::egui::WidgetType::TextEdit;
|
use eframe::egui::text_edit::TextCursorState;
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
prompt: String,
|
prompt: String,
|
||||||
output: String,
|
output: String,
|
||||||
evaluated: String,
|
evaluated: String,
|
||||||
|
cursor: TextCursorState
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for App {
|
impl Default for App {
|
||||||
|
@ -14,19 +15,74 @@ impl Default for App {
|
||||||
prompt: String::new(),
|
prompt: String::new(),
|
||||||
output: String::new(),
|
output: String::new(),
|
||||||
evaluated: String::new(),
|
evaluated: String::new(),
|
||||||
|
cursor: TextCursorState::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl eframe::App for App {
|
impl eframe::App for App {
|
||||||
fn update(&mut self, ctx: &Context, frame: &mut Frame) {
|
fn update(&mut self, ctx: &Context, frame: &mut Frame) {
|
||||||
|
ctx.set_pixels_per_point(3.0);
|
||||||
egui::TopBottomPanel::top("top").show(ctx, |ui| {
|
egui::TopBottomPanel::top("top").show(ctx, |ui| {
|
||||||
// TODO: get cursor position from multiline
|
let res = ui.add_sized([ui.available_width(), 20.0], TextEdit::multiline(&mut self.prompt));
|
||||||
TextEdit::multiline(&mut self.prompt).show(ui);
|
if let Some(state) = TextEdit::load_state(ctx, res.id) {
|
||||||
TextEdit::singleline(&mut self.output).show(ui);
|
self.cursor = state.cursor;
|
||||||
|
}
|
||||||
|
ui.add_sized([ui.available_width(), 20.0], TextEdit::singleline(&mut self.output));
|
||||||
});
|
});
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
ui.vertical_centered_justified(|ui| {
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
self.btn(ui, "1".to_string(), "1".to_string());
|
||||||
|
self.btn(ui, "2".to_string(), "2".to_string());
|
||||||
|
self.btn(ui, "3".to_string(), "3".to_string());
|
||||||
|
});
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
self.btn(ui, "4".to_string(), "4".to_string());
|
||||||
|
self.btn(ui, "5".to_string(), "5".to_string());
|
||||||
|
self.btn(ui, "6".to_string(), "6".to_string());
|
||||||
|
});
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
self.btn(ui, "7".to_string(), "7".to_string());
|
||||||
|
self.btn(ui, "8".to_string(), "8".to_string());
|
||||||
|
self.btn(ui, "9".to_string(), "9".to_string());
|
||||||
|
});
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
self.btn(ui, "0".to_string(), "0".to_string());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
ui.label(" ");
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
self.btn(ui, "+".to_string(), "+".to_string());
|
||||||
|
self.btn(ui, "-".to_string(), "-".to_string());
|
||||||
|
});
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
self.btn(ui, "*".to_string(), "*".to_string());
|
||||||
|
self.btn(ui, "/".to_string(), "/".to_string());
|
||||||
|
});
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
self.btn(ui, "(".to_string(), "(".to_string());
|
||||||
|
self.btn(ui, ")".to_string(), ")".to_string());
|
||||||
|
});
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
self.btn(ui, "sin".to_string(), "sin(".to_string());
|
||||||
|
self.btn(ui, "cos".to_string(), "cos(".to_string());
|
||||||
|
self.btn(ui, "tan".to_string(), "tan(".to_string());
|
||||||
|
self.btn(ui, "cot".to_string(), "cot(".to_string());
|
||||||
|
});
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
self.btn(ui, "log".to_string(), "log(".to_string());
|
||||||
|
self.btn(ui, "ln".to_string(), "ln(".to_string());
|
||||||
|
});
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
self.btn(ui, "sqrt".to_string(), "sqrt(".to_string());
|
||||||
|
self.btn(ui, "exp".to_string(), "^".to_string());
|
||||||
|
});
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
self.btn(ui, "pi".to_string(), "pi".to_string());
|
||||||
|
self.btn(ui, "e".to_string(), "e".to_string());
|
||||||
|
self.btn(ui, "i".to_string(), "i".to_string());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
// reevaluate if prompt changes
|
// reevaluate if prompt changes
|
||||||
if self.evaluated != self.prompt {
|
if self.evaluated != self.prompt {
|
||||||
|
@ -35,9 +91,13 @@ impl eframe::App for App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
fn btn(&mut self, ui: &mut Ui, label: String, expr: String) {
|
fn btn(&mut self, ui: &mut Ui, label: String, expr: String) {
|
||||||
if ui.button(label).clicked() {
|
if ui.button(label).clicked() {
|
||||||
// TODO: insert expr at cursor position in prompt
|
// TODO: insert expr at cursor position in prompt
|
||||||
|
self.prompt.push_str(&expr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue