106 lines
3.0 KiB
Rust
106 lines
3.0 KiB
Rust
extern crate gtk;
|
|
use gtk::prelude::*;
|
|
use gtk::{Button, Window, WindowType};
|
|
enum Operators {
|
|
PLUS,
|
|
MINUS,
|
|
DIVIDE,
|
|
MULTIPLY,
|
|
}
|
|
struct calc {
|
|
left: Option<u32>,
|
|
right: Option<u32>,
|
|
operator: Option<Operators>,
|
|
}
|
|
impl calc {
|
|
fn handle_number(&mut self, button: gtk::Button) {
|
|
match self {
|
|
calc {
|
|
left: l,
|
|
right: r,
|
|
operator: None,
|
|
} => match l {
|
|
Some(n) => {let s = n.to_string() + &button.get_label().unwrap();
|
|
self.left = Some(s.parse::<u32>().unwrap());
|
|
()
|
|
}
|
|
},
|
|
calc {
|
|
left: Some(l),
|
|
right: r,
|
|
operator: Some(o),
|
|
} => {}
|
|
}
|
|
}
|
|
fn eval(&mut self) -> Self {
|
|
calc {
|
|
left: None,
|
|
right: None,
|
|
operator: None,
|
|
}
|
|
}
|
|
}
|
|
fn main() {
|
|
if gtk::init().is_err() {
|
|
println!("failed to init GTK");
|
|
return;
|
|
}
|
|
let window: Window = Window::new(WindowType::Toplevel);
|
|
window.set_title("Rust calc");
|
|
let container = gtk::Box::new(gtk::Orientation::Vertical, 1);
|
|
// row 1
|
|
let row1 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
|
let button1 = Button::new_with_label("1");
|
|
row1.add(&button1);
|
|
button1.connect_clicked(|but| println!("{}", but.get_label().unwrap()));
|
|
let button2 = Button::new_with_label("2");
|
|
row1.add(&button2);
|
|
let button3 = Button::new_with_label("3");
|
|
row1.add(&button3);
|
|
// row 2
|
|
let row2 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
|
let button4 = Button::new_with_label("4");
|
|
row2.add(&button4);
|
|
let button5 = Button::new_with_label("5");
|
|
row2.add(&button5);
|
|
let button6 = Button::new_with_label("6");
|
|
row2.add(&button6);
|
|
// row 3
|
|
let row3 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
|
let button7 = Button::new_with_label("7");
|
|
row3.add(&button7);
|
|
let button8 = Button::new_with_label("8");
|
|
row3.add(&button8);
|
|
let button9 = Button::new_with_label("9");
|
|
row3.add(&button9);
|
|
// row 4
|
|
let row4 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
|
let button0 = Button::new_with_label("0");
|
|
row4.add(&button0);
|
|
let button_sub = Button::new_with_label("-");
|
|
row4.add(&button_sub);
|
|
let button_add = Button::new_with_label("+");
|
|
row4.add(&button_add);
|
|
// row 5
|
|
let row5 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
|
let button_mult = Button::new_with_label("*");
|
|
row5.add(&button_mult);
|
|
let button_div = Button::new_with_label("/");
|
|
row5.add(&button_div);
|
|
let button_equals = Button::new_with_label("=");
|
|
row5.add(&button_equals);
|
|
// 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(|_, _| {
|
|
gtk::main_quit();
|
|
Inhibit(false)
|
|
});
|
|
gtk::main();
|
|
}
|