temp_converter/
temp_converter.rs1use fltk::{prelude::*, *};
2
3fn main() {
4 let app = app::App::default().with_scheme(app::Scheme::Gleam);
5 let mut win = window::Window::default().with_size(150, 200);
6 let mut col = group::Flex::default()
7 .column()
8 .with_size(130, 180)
9 .center_of(&win);
10 col.set_pad(5);
11 frame::Frame::default()
12 .with_size(0, 40)
13 .with_label("Celcius")
14 .with_align(enums::Align::Inside | enums::Align::Bottom);
15 let mut inp1 = input::FloatInput::default().with_size(0, 40);
16 frame::Frame::default()
17 .with_size(0, 40)
18 .with_label("Farenheit")
19 .with_align(enums::Align::Inside | enums::Align::Bottom);
20 let mut inp2 = input::FloatInput::default().with_size(0, 40);
21 col.end();
22 win.end();
23 win.show();
24
25 inp1.set_value(&format!("{}", 0.0));
26 inp2.set_value(&format!("{}", 32.0));
27
28 inp1.set_trigger(enums::CallbackTrigger::Changed);
29 inp2.set_trigger(enums::CallbackTrigger::Changed);
30
31 inp1.set_callback({
32 let mut inp2 = inp2.clone();
33 move |i| {
34 let inp1_val: f64 = if i.value().is_empty() {
35 0.0
36 } else {
37 i.value().parse().unwrap_or(0.0)
38 };
39 inp2.set_value(&format!("{:.4}", c_to_f(inp1_val)));
40 }
41 });
42 inp2.set_callback(move |i| {
43 let inp2_val: f64 = if i.value().is_empty() {
44 0.0
45 } else {
46 i.value().parse().unwrap_or(0.0)
47 };
48 inp1.set_value(&format!("{:.4}", f_to_c(inp2_val)));
49 });
50
51 app.run().unwrap();
52}
53
54fn c_to_f(val: f64) -> f64 {
55 (val * 9.0 / 5.0) + 32.0
56}
57
58fn f_to_c(val: f64) -> f64 {
59 (val - 32.0) * 5.0 / 9.0
60}