Skip to content

Commit

Permalink
Basic layout of calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Jan 9, 2024
1 parent d272f54 commit 0b4a47b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 61 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "eframe_template"
name = "calculator"
version = "0.1.0"
authors = ["Emil Ernerfeldt <[email protected]>"]
edition = "2021"
rust-version = "1.72"

Expand Down
5 changes: 2 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<head>
<!-- change this to your project name -->
<title>eframe template</title>
<title>Calculator</title>

<!-- config for our rust wasm binary. go to https://trunkrs.dev/assets/#rust for more customization -->
<link data-trunk rel="rust" data-wasm-opt="2" />
Expand Down Expand Up @@ -114,7 +114,6 @@
transform: rotate(360deg);
}
}

</style>
</head>

Expand All @@ -137,4 +136,4 @@

</html>

<!-- Powered by egui: https://github.com/emilk/egui/ -->
<!-- Powered by egui: https://github.com/emilk/egui/ -->
95 changes: 55 additions & 40 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
pub struct TemplateApp {
// Example stuff:
label: String,

#[serde(skip)] // This how you opt-out of serialization of a field
value: f32,
pub struct CalculatorApp {
value: f64,
answer: f64,
}

impl Default for TemplateApp {
impl Default for CalculatorApp {
fn default() -> Self {
Self {
// Example stuff:
label: "Hello World!".to_owned(),
value: 2.7,
value: 0.0,
answer: 0.0,
}
}
}

impl TemplateApp {
impl CalculatorApp {
/// Called once before the first frame.
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
// This is also where you can customize the look and feel of egui using
Expand All @@ -35,7 +32,7 @@ impl TemplateApp {
}
}

impl eframe::App for TemplateApp {
impl eframe::App for CalculatorApp {
/// Called by the frame work to save state before shutdown.
fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, eframe::APP_KEY, self);
Expand Down Expand Up @@ -67,43 +64,61 @@ impl eframe::App for TemplateApp {

egui::CentralPanel::default().show(ctx, |ui| {
// The central panel the region left after adding TopPanel's and SidePanel's
ui.heading("eframe template");
ui.heading("calculator");

ui.horizontal(|ui| {
ui.label("Write something: ");
ui.text_edit_singleline(&mut self.label);
ui.label(&format!("{}", self.answer));
});

ui.add(egui::Slider::new(&mut self.value, 0.0..=10.0).text("value"));
if ui.button("Increment").clicked() {
self.value += 1.0;
}
ui.horizontal(|ui| {
if ui.button("7").clicked() {
self.answer = 7.0
};
if ui.button("8").clicked() {
self.answer = 8.0
};
if ui.button("9").clicked() {
self.answer = 9.0
};
if ui.button("/").clicked() {};
});

ui.separator();
ui.horizontal(|ui| {
if ui.button("4").clicked() {
self.answer = 4.0
};
if ui.button("5").clicked() {
self.answer = 5.0
};
if ui.button("6").clicked() {
self.answer = 6.0
};
if ui.button("x").clicked() {};
});

ui.add(egui::github_link_file!(
"https://github.com/emilk/eframe_template/blob/master/",
"Source code."
));
ui.horizontal(|ui| {
if ui.button("1").clicked() {
self.answer = 1.0
};
if ui.button("2").clicked() {
self.answer = 2.0
};
if ui.button("3").clicked() {
self.answer = 3.0
};
if ui.button("-").clicked() {};
});

ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
powered_by_egui_and_eframe(ui);
egui::warn_if_debug_build(ui);
ui.horizontal(|ui| {
if ui.button("0").clicked() {
self.answer = 0.0
};
if ui.button("C").clicked() {
self.answer = 0.0
};
if ui.button("=").clicked() {};
if ui.button("+").clicked() {};
});
});
}
}

fn powered_by_egui_and_eframe(ui: &mut egui::Ui) {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.label("Powered by ");
ui.hyperlink_to("egui", "https://github.com/emilk/egui");
ui.label(" and ");
ui.hyperlink_to(
"eframe",
"https://github.com/emilk/egui/tree/master/crates/eframe",
);
ui.label(".");
});
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(clippy::all, rust_2018_idioms)]

mod app;
pub use app::TemplateApp;
pub use app::CalculatorApp;
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fn main() -> eframe::Result<()> {
..Default::default()
};
eframe::run_native(
"eframe template",
"Calculator",
native_options,
Box::new(|cc| Box::new(eframe_template::TemplateApp::new(cc))),
Box::new(|cc| Box::new(calculator::CalculatorApp::new(cc))),
)
}

Expand All @@ -32,7 +32,7 @@ fn main() {
.start(
"the_canvas_id", // hardcode it
web_options,
Box::new(|cc| Box::new(eframe_template::TemplateApp::new(cc))),
Box::new(|cc| Box::new(calculator::CalculatorApp::new(cc))),
)
.await
.expect("failed to start eframe");
Expand Down

0 comments on commit 0b4a47b

Please sign in to comment.