aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 9af3e48bb7f069b9c1a6e2765d7c8637390494a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
mod eval;
mod expr;
mod lexer;
mod parser;

use gdk4::{Key, ModifierType};
use gtk4::glib::{clone, ExitCode};
use gtk4::prelude::{
    ApplicationExt, ApplicationExtManual, EditableExt, GridExt, GtkWindowExt, WidgetExt,
};
use gtk4::{glib, Application, ApplicationWindow, Entry, EventControllerKey, Grid};

fn main() -> ExitCode {
    let app = Application::builder()
        .application_id("com.github.rhogenson.qc")
        .build();
    app.connect_activate(|app| {
        let window = ApplicationWindow::builder()
            .application(app)
            .decorated(false)
            .resizable(false)
            .build();

        let grid = Grid::new();
        window.set_child(Some(&grid));

        let input = Entry::builder().width_request(300).build();
        grid.attach(&input, 0, 0, 1, 1);

        let output = Entry::builder().editable(false).build();
        grid.attach(&output, 0, 1, 1, 1);

        let key_controller = EventControllerKey::new();
        key_controller.connect_key_released(
            clone!(@weak window, @weak input, @weak output => move |_, k, _, modifiers| {
                if k == Key::Escape
                    || k == Key::Return
                    || k == Key::bracketleft && modifiers.contains(ModifierType::CONTROL_MASK)
                {
                    window.destroy();
                }
                if let Some(n) = parser::parse(&input.text()) {
                    output.set_text(&format!("{}", n.eval()));
                }
            }),
        );
        window.add_controller(key_controller);

        window.present();
    });

    app.run()
}