Skip to content

Instantly share code, notes, and snippets.

@davassi
Created October 2, 2023 06:44
Show Gist options
  • Save davassi/1122f84110ea4598db4478fe2686f472 to your computer and use it in GitHub Desktop.
Save davassi/1122f84110ea4598db4478fe2686f472 to your computer and use it in GitHub Desktop.
calc
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use eframe::egui;
use egui::{Vec2, Rounding, TextEdit, FontId, FontSelection, FontFamily, Separator, Button};
use yarer::session::Session;
use egui::TextStyle::*;
macro_rules! butt {
($t:expr) => {
Button::new($t).min_size(Vec2::new(64.,64.)).rounding(Rounding::same(4.)).frame(false)
};
}
fn main() -> Result<(), eframe::Error> {
env_logger::init();
let options = eframe::NativeOptions {
initial_window_size: Some(egui::vec2(360.0, 380.0)),
resizable: false,
active: true,
centered: true,
..Default::default()
};
// Our application state:
let mut expression = "".to_owned();
let session = Session::init();
eframe::run_simple_native("Yarer Calculator", options, move |ctx, _frame| {
egui::CentralPanel::default().show(ctx, |ui| {
ui.style_mut().text_styles = [
(Body, FontId::new(24.0, FontFamily::Proportional)),
(Button, FontId::new(24.0, FontFamily::Proportional)),
].into();
let radius = 4.;
let button_size = Vec2::new(64.,64.);
let mut new_line : bool = true;
let b1 = butt!("1");
let b2 = butt!("2");
let b3 = butt!("3");
let b4 = butt!("4");
let b5 = butt!("5");
let b6 = butt!("6");
let b7 = butt!("7");
let b8 = butt!("8");
let b9 = butt!("9");
let b0 = butt!("0");
let buttonDot = Button::new(".").min_size(button_size).rounding(Rounding::same(radius));
let buttonMul = egui::Button::new("X").min_size(button_size).rounding(Rounding::same(radius));
let buttonMin = egui::Button::new("-").min_size(button_size).rounding(Rounding::same(radius));
let buttonDiv = egui::Button::new("/").min_size(button_size).rounding(Rounding::same(radius));
let buttonAdd = egui::Button::new("+").min_size(button_size).rounding(Rounding::same(radius));
let buttonResult = egui::Button::new("=").min_size(button_size).rounding(Rounding::same(radius));
let buttonAC = egui::Button::new("AC").min_size(button_size).rounding(Rounding::same(radius));
let buttonPI = egui::Button::new("π").min_size(button_size).rounding(Rounding::same(radius));
let buttonE = egui::Button::new("e").min_size(button_size).rounding(Rounding::same(radius));
let buttonLog = egui::Button::new("Log").min_size(button_size).rounding(Rounding::same(radius));
let buttonLn = egui::Button::new("Ln").min_size(button_size).rounding(Rounding::same(radius));
let buttonOpenBracket = egui::Button::new("(").min_size(button_size).rounding(Rounding::same(radius));
let buttonClosBracket = egui::Button::new(")").min_size(button_size).rounding(Rounding::same(radius));
let font = FontSelection::FontId(FontId {
size: 24.0,
family: FontFamily::Monospace,
});
let expression_text = TextEdit::singleline(&mut expression).horizontal_align(egui::Align::Max).font(font);
ui.horizontal(|ui| {
ui.add(expression_text)
});
ui.separator();
ui.horizontal(|ui| {
if ui.add_sized(button_size,b7).clicked() {
expression += "7";
}
if ui.add(b8).clicked() {
expression += "8";
}
if ui.add(b9).clicked() {
expression += "9";
}
if ui.add(buttonMul).clicked() {
expression += "*";
}
});
ui.horizontal(|ui| {
let b = ui.add(b4);
if b.clicked() {
expression += "4";
}
b.on_hover_ui(add_contents)
if ui.add(b5).clicked() {
expression += "5";
}
if ui.add(b6).clicked() {
expression += "6";
}
if ui.add(buttonMin).clicked() {
expression += "-";
}
});
ui.horizontal(|ui| {
if ui.add(b3).clicked() {
expression += "3";
}
if ui.add(b2).clicked() {
expression += "2";
}
if ui.add(b1).clicked() {
expression += "1";
}
if ui.add(buttonAdd).clicked() {
expression += "+";
}
});
ui.horizontal(|ui| {
if ui.add(buttonAC).clicked() {
expression.clear();
}
if ui.add(b0).clicked() {
expression += "0";
}
if ui.add(buttonDot).clicked() {
expression += ".";
}
if ui.add(buttonResult).clicked() {
new_line = true;
let mut resolver = session.build_resolver_for(&expression);
match resolver.resolve() {
Ok(res) => {
expression.clear();
expression += &format!("{}",res)
},
Err(err) => {
println!("Error: {}", err);
expression.clear();
expression += "Error!"
},
}
}
});
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment