GTK calculator on Rust

Antonov Mike - Nov 21 '22 - - Dev Community

(Almost done)

Made my first application with user interface and built it on Linux. Have to improve few issues. Also need to know how to build it for Windows and Mac.

Some issues I have to solve to consider the project complete:

  • Listen for keyboard events (the program have to accept keyboard input even if the cursor is not in the text entry field)
  • Scrollable entry screen with saved data of previous operations
  • Set rounding precision (what is the best way to round calculation results?)
  • Negative numbers issue (now it only works if you remove space between negative sign ant number manually)

I rewrote the part of the code in which buttons are created. Now all the numeric buttons are created inside the for loop and placed in the correct places.

// NUM BUTTONS
for iterator in 0..=9 {
    let button = gtk::Button::with_label(&iterator.to_string());
    let mut column = 0;
    let mut raw = 1;

    button.connect_clicked(clone!( @strong entry => move |_| {
        entry.insert_text(&iterator.to_string(), &mut -1);
    }));

    if iterator == 1 || iterator == 4 || iterator == 7 { column = 0 }
    if iterator == 2 || iterator == 5 || iterator == 8 { column = 1 }
    if iterator == 3 || iterator == 6 || iterator == 9 { column = 2 }
    if iterator == 0 { column = 1 }

    if (1..=3).contains(&iterator) { raw = 1 }
    else if (4..=6).contains(&iterator) { raw = 2 }
    else if (7..=9).contains(&iterator) { raw = 3 }
    else if iterator == 0 { raw = 4 }

    grid.attach(&button, column, raw, 1, 1);
}
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .