temp_converter2/
temp_converter2.rs

1use fltk::{enums::*, prelude::*, *};
2
3fn main() {
4    MyApp::new().run()
5}
6
7#[derive(Copy, Clone)]
8pub enum Message {
9    CelciusChanged,
10    FahrenheitChanged,
11}
12
13fn make_label(label: &str) -> frame::Frame {
14    let mut f = frame::Frame::default()
15        .with_label(label)
16        .with_align(Align::Inside | Align::Bottom);
17    f.set_label_type(LabelType::Engraved);
18    f
19}
20
21fn c_to_f(val: f64) -> f64 {
22    (val * 9.0 / 5.0) + 32.0
23}
24
25fn f_to_c(val: f64) -> f64 {
26    (val - 32.0) * 5.0 / 9.0
27}
28
29struct MyApp {
30    a: app::App,
31    inp1: input::FloatInput,
32    inp2: input::FloatInput,
33    r: app::Receiver<Message>,
34}
35
36impl MyApp {
37    pub fn new() -> Self {
38        let a = app::App::default();
39        MyApp::init_styles();
40        let (s, r) = app::channel();
41
42        let (inp1, inp2) = {
43            let mut win = window::Window::default().with_size(150, 200);
44            let mut flex = group::Flex::default()
45                .with_size(130, 180)
46                .center_of(&win)
47                .column();
48            make_label("Celcius");
49            let mut inp1 = input::FloatInput::default().with_size(0, 40);
50            make_label("Fahrenheit");
51            let mut inp2 = input::FloatInput::default().with_size(0, 40);
52            flex.fixed(&inp1, 30);
53            flex.fixed(&inp2, 30);
54            flex.end();
55            win.end();
56            win.make_resizable(true);
57            win.show();
58
59            inp1.set_value(&format!("{}", 0.0));
60            inp2.set_value(&format!("{}", 32.0));
61
62            inp1.set_when(When::Changed);
63            inp2.set_when(When::Changed);
64
65            inp1.emit(s, Message::CelciusChanged);
66            inp2.emit(s, Message::FahrenheitChanged);
67
68            (inp1, inp2)
69        };
70        Self { a, inp1, inp2, r }
71    }
72
73    fn init_styles() {
74        app::set_scheme(app::Scheme::Gleam);
75        app::set_background_color(170, 189, 206);
76        app::set_background2_color(255, 255, 255);
77        app::set_foreground_color(0, 0, 0);
78        app::set_selection_color(255, 160, 63);
79        app::set_inactive_color(130, 149, 166);
80        app::set_font_size(16);
81    }
82
83    pub fn run(&mut self) {
84        while self.a.wait() {
85            if let Some(msg) = self.r.recv() {
86                match msg {
87                    Message::CelciusChanged => {
88                        self.inp2.set_value(&format!(
89                            "{:.4}",
90                            c_to_f(self.inp1.value().parse().unwrap_or(0.0))
91                        ));
92                    }
93                    Message::FahrenheitChanged => {
94                        self.inp1.set_value(&format!(
95                            "{:.4}",
96                            f_to_c(self.inp2.value().parse().unwrap_or(0.0))
97                        ));
98                    }
99                }
100            }
101        }
102    }
103}