Skip to content

Commit

Permalink
feat: update small display in top left
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Mar 5, 2024
1 parent 380e6d2 commit 7a55a98
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct CalculatorApp {
error_message: Option<String>,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Copy, Debug)]
#[derive(serde::Deserialize, serde::Serialize, Clone, Copy, Debug, PartialEq, Eq)]
enum Operator {
Add,
Subtract,
Expand Down Expand Up @@ -183,6 +183,27 @@ impl CalculatorApp {
SpecialButton::Clear => *self = CalculatorApp::default(),
}
}

/// Answer if it is Some followed by last_operator if it is Some unless last operator is "Equal"
fn partial_result_display(&self) -> String {
if Some(Operator::Equal) == self.last_operation {
"".to_string()
} else {
format!(
"{} {}",
if let Some(answer) = self.answer {
answer.to_string()
} else {
String::new()
},
if let Some(operator) = self.last_operation {
operator.to_string()
} else {
String::new()
}
)
}
}
}

impl eframe::App for CalculatorApp {
Expand Down Expand Up @@ -216,12 +237,9 @@ impl eframe::App for CalculatorApp {
});

egui::CentralPanel::default().show(ctx, |ui| {
// The central panel the region left after adding TopPanel's and SidePanel's
ui.label(if let Some(operator) = self.last_operation {
format!("{operator}")
} else {
String::from("")
});
// Small display top left
ui.label(self.partial_result_display());

ui.allocate_ui_with_layout(
Vec2::new(250., 40.),
Layout::right_to_left(egui::Align::BOTTOM),
Expand Down

0 comments on commit 7a55a98

Please sign in to comment.