Created
September 4, 2020 10:28
-
-
Save zesterer/e27d8dc80e6395d4d05564809c58e165 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use gui::{ | |
widget::{Toggle, Button, Label, List}, | |
layout::Direction, | |
event::Click, | |
Window, Widget, | |
}; | |
fn main() { | |
enum Op { | |
Add, | |
Sub, | |
Mul, | |
Div, | |
} | |
struct Data { | |
screen: String, | |
second: f64, | |
op: Option<Op>, | |
} | |
impl Data { | |
fn start_op(&mut self, op: Op) { | |
self.second = std::mem::take(&mut self.screen).parse().unwrap(); | |
self.op = Some(op); | |
} | |
fn calc(&mut self) { | |
let screen = self.screen.parse().unwrap_or(0.0); | |
self.screen = match self.op.take() { | |
Some(Op::Add) => self.second + screen, | |
Some(Op::Sub) => self.second - screen, | |
Some(Op::Mul) => self.second * screen, | |
Some(Op::Div) => self.second / screen, | |
None => return, | |
}.to_string(); | |
} | |
} | |
let num_button = |n: &'static str| Button::<Data>::default_state() | |
.containing(Label::<Data>::with_state(n).padded(16.0)) | |
.on(Click, move |ctx| ctx.data.screen.push_str(&n)) | |
.padded(8.0); | |
let ui = List::<Data>::vertical() | |
.push(Button::<Data>::default_state() | |
.containing(Label::<Data>::bind_state(|d| &mut d.screen).padded(16.0)) | |
.padded(8.0)) | |
.push(List::<Data>::horizontal() | |
.push(num_button("1")) | |
.push(num_button("2")) | |
.push(num_button("3")) | |
.push(Button::<Data>::default_state() | |
.containing(Label::<Data>::with_state("+").padded(16.0)) | |
.on(Click, |ctx| ctx.data.start_op(Op::Add)) | |
.padded(8.0))) | |
.push(List::<Data>::horizontal() | |
.push(num_button("4")) | |
.push(num_button("5")) | |
.push(num_button("6")) | |
.push(Button::<Data>::default_state() | |
.containing(Label::<Data>::with_state("-").padded(16.0)) | |
.on(Click, |ctx| ctx.data.start_op(Op::Sub)) | |
.padded(8.0))) | |
.push(List::<Data>::horizontal() | |
.push(num_button("7")) | |
.push(num_button("8")) | |
.push(num_button("9")) | |
.push(Button::<Data>::default_state() | |
.containing(Label::<Data>::with_state("*").padded(16.0)) | |
.on(Click, |ctx| ctx.data.start_op(Op::Mul)) | |
.padded(8.0))) | |
.push(List::<Data>::horizontal() | |
.push(Button::<Data>::default_state() | |
.containing(Label::<Data>::with_state("C").padded(16.0)) | |
.on(Click, |ctx| ctx.data.screen.clear()) | |
.padded(8.0)) | |
.push(num_button("0")) | |
.push(Button::<Data>::default_state() | |
.containing(Label::<Data>::with_state("=").padded(16.0)) | |
.on(Click, |ctx| ctx.data.calc()) | |
.padded(8.0)) | |
.push(Button::<Data>::default_state() | |
.containing(Label::<Data>::with_state("/").padded(16.0)) | |
.on(Click, |ctx| ctx.data.start_op(Op::Div)) | |
.padded(8.0))) | |
.padded(8.0); | |
Window::new(ui) | |
.run(Data { | |
screen: String::new(), | |
second: 0.0, | |
op: None, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment