it loops instead of having massive code repetition is i good yet?
This commit is contained in:
parent
87c7f0cefb
commit
de7ffbe70e
157
src/main.rs
157
src/main.rs
|
@ -73,132 +73,43 @@ fn main() {
|
|||
// row 0
|
||||
let cal0 = calc.clone();
|
||||
container.add(&cal0.borrow().label);
|
||||
// row 1
|
||||
let row1 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
||||
let button1 = Button::new_with_label("1");
|
||||
row1.add(&button1);
|
||||
let cal1 = calc.clone();
|
||||
button1.connect_clicked(move |but| {
|
||||
cal1.borrow_mut().handle_number(but.get_label().unwrap());
|
||||
});
|
||||
|
||||
let button2 = Button::new_with_label("2");
|
||||
row1.add(&button2);
|
||||
let cal2 = calc.clone();
|
||||
button2.connect_clicked(move |but| {
|
||||
cal2.borrow_mut().handle_number(but.get_label().unwrap());
|
||||
let keys: [[&'static str; 3]; 5] = [
|
||||
["1", "2", "3"],
|
||||
["4", "5", "6"],
|
||||
["7", "8", "9"],
|
||||
["0", "-", "+"],
|
||||
["*", "/", "="],
|
||||
];
|
||||
for row in &keys {
|
||||
let gui_row = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
||||
for button in row {
|
||||
let gui_button = Button::new_with_label(button);
|
||||
gui_row.add(&gui_button);
|
||||
let cal_copy = calc.clone();
|
||||
gui_button.connect_clicked(move |but| {
|
||||
let label = but.get_label().unwrap();
|
||||
match label.chars().next().unwrap() {
|
||||
'0'...'9' => cal_copy.borrow_mut().handle_number(label),
|
||||
'-' | '+' | '*' | '/' => {
|
||||
let mut cal_ops_borrowed = cal_copy.borrow_mut();
|
||||
cal_ops_borrowed.operator = Some(match label.as_ref() {
|
||||
"-" => Operators::MINUS,
|
||||
"+" => Operators::PLUS,
|
||||
"*" => Operators::MULTIPLY,
|
||||
"/" => Operators::DIVIDE,
|
||||
_ => panic!("oh noes, unknown operator"),
|
||||
});
|
||||
cal_ops_borrowed.display();
|
||||
}
|
||||
'=' => cal_copy.borrow_mut().eval(),
|
||||
_ => panic!("oh noes unknowen button label"),
|
||||
};
|
||||
});
|
||||
}
|
||||
container.add(&gui_row);
|
||||
}
|
||||
|
||||
let button3 = Button::new_with_label("3");
|
||||
row1.add(&button3);
|
||||
let cal3 = calc.clone();
|
||||
button3.connect_clicked(move |but| {
|
||||
cal3.borrow_mut().handle_number(but.get_label().unwrap());
|
||||
});
|
||||
|
||||
// row 2
|
||||
let row2 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
||||
let button4 = Button::new_with_label("4");
|
||||
row2.add(&button4);
|
||||
let cal4 = calc.clone();
|
||||
button4.connect_clicked(move |but| {
|
||||
cal4.borrow_mut().handle_number(but.get_label().unwrap());
|
||||
});
|
||||
|
||||
let button5 = Button::new_with_label("5");
|
||||
row2.add(&button5);
|
||||
let cal5 = calc.clone();
|
||||
button5.connect_clicked(move |but| {
|
||||
cal5.borrow_mut().handle_number(but.get_label().unwrap());
|
||||
});
|
||||
|
||||
let button6 = Button::new_with_label("6");
|
||||
row2.add(&button6);
|
||||
let cal6 = calc.clone();
|
||||
button6.connect_clicked(move |but| {
|
||||
cal6.borrow_mut().handle_number(but.get_label().unwrap());
|
||||
});
|
||||
|
||||
// row 3
|
||||
let row3 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
||||
let button7 = Button::new_with_label("7");
|
||||
row3.add(&button7);
|
||||
let cal7 = calc.clone();
|
||||
button7.connect_clicked(move |but| {
|
||||
cal7.borrow_mut().handle_number(but.get_label().unwrap());
|
||||
});
|
||||
|
||||
let button8 = Button::new_with_label("8");
|
||||
row3.add(&button8);
|
||||
let cal8 = calc.clone();
|
||||
button8.connect_clicked(move |but| {
|
||||
cal8.borrow_mut().handle_number(but.get_label().unwrap());
|
||||
});
|
||||
|
||||
let button9 = Button::new_with_label("9");
|
||||
row3.add(&button9);
|
||||
let cal9 = calc.clone();
|
||||
button9.connect_clicked(move |but| {
|
||||
cal9.borrow_mut().handle_number(but.get_label().unwrap());
|
||||
});
|
||||
|
||||
// row 4
|
||||
let row4 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
||||
let button0 = Button::new_with_label("0");
|
||||
row4.add(&button0);
|
||||
let cal0 = calc.clone();
|
||||
button0.connect_clicked(move |but| {
|
||||
cal0.borrow_mut().handle_number(but.get_label().unwrap());
|
||||
});
|
||||
|
||||
let button_sub = Button::new_with_label("-");
|
||||
row4.add(&button_sub);
|
||||
let cal_sub = calc.clone();
|
||||
button_sub.connect_clicked(move |_| {
|
||||
let mut cal_sub_borrowed = cal_sub.borrow_mut();
|
||||
cal_sub_borrowed.operator = Some(Operators::MINUS);
|
||||
cal_sub_borrowed.display();
|
||||
});
|
||||
|
||||
let button_add = Button::new_with_label("+");
|
||||
row4.add(&button_add);
|
||||
let cal_add = calc.clone();
|
||||
button_add.connect_clicked(move |_| {
|
||||
let mut cal_add_borrowed = cal_add.borrow_mut();
|
||||
cal_add_borrowed.operator = Some(Operators::PLUS);
|
||||
cal_add_borrowed.display();
|
||||
});
|
||||
// row 5
|
||||
let row5 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
||||
let button_mult = Button::new_with_label("*");
|
||||
row5.add(&button_mult);
|
||||
let cal_mult = calc.clone();
|
||||
button_mult.connect_clicked(move |_| {
|
||||
let mut cal_mult_borrowed = cal_mult.borrow_mut();
|
||||
cal_mult_borrowed.operator = Some(Operators::MULTIPLY);
|
||||
cal_mult_borrowed.display();
|
||||
});
|
||||
let button_div = Button::new_with_label("/");
|
||||
row5.add(&button_div);
|
||||
let cal_div = calc.clone();
|
||||
button_div.connect_clicked(move |_| {
|
||||
let mut cal_div_borrowed = cal_div.borrow_mut();
|
||||
cal_div_borrowed.operator = Some(Operators::DIVIDE);
|
||||
cal_div_borrowed.display();
|
||||
});
|
||||
let button_equals = Button::new_with_label("=");
|
||||
row5.add(&button_equals);
|
||||
let cal_eq = calc.clone();
|
||||
button_equals.connect_clicked(move |_| {
|
||||
let mut cal_eq_borrowed = cal_eq.borrow_mut();
|
||||
cal_eq_borrowed.eval();
|
||||
});
|
||||
// add rows to window
|
||||
container.add(&row1);
|
||||
container.add(&row2);
|
||||
container.add(&row3);
|
||||
container.add(&row4);
|
||||
container.add(&row5);
|
||||
window.add(&container);
|
||||
window.show_all();
|
||||
window.connect_delete_event(|_, _| {
|
||||
|
|
Loading…
Reference in New Issue