custom_dial/
custom_dial.rs

1use fltk::{enums::*, prelude::*, *};
2use std::cell::RefCell;
3use std::rc::Rc;
4
5#[derive(Debug, Clone)]
6pub struct MyDial {
7    main_wid: group::Group,
8    value: Rc<RefCell<i32>>,
9    value_frame: frame::Frame,
10}
11
12impl MyDial {
13    pub fn new(x: i32, y: i32, w: i32, h: i32, label: &str) -> Self {
14        let value = Rc::from(RefCell::from(0));
15        let mut main_wid = group::Group::new(x, y, w, h, None)
16            .with_label(label)
17            .with_align(Align::Top);
18        let mut value_frame =
19            frame::Frame::new(main_wid.x(), main_wid.y() + 80, main_wid.w(), 40, "0");
20        value_frame.set_label_size(26);
21        main_wid.end();
22        let value_c = value.clone();
23        main_wid.draw(move |w| {
24            draw::set_draw_rgb_color(230, 230, 230);
25            draw::draw_pie(w.x(), w.y(), w.w(), w.h(), 0., 180.);
26            draw::set_draw_hex_color(0xb0bf1a);
27            draw::draw_pie(
28                w.x(),
29                w.y(),
30                w.w(),
31                w.h(),
32                (100 - *value_c.borrow()) as f64 * 1.8,
33                180.,
34            );
35            draw::set_draw_color(Color::White);
36            draw::draw_pie(
37                w.x() - 50 + w.w() / 2,
38                w.y() - 50 + w.h() / 2,
39                100,
40                100,
41                0.,
42                360.,
43            );
44            w.draw_children();
45        });
46        Self {
47            main_wid,
48            value,
49            value_frame,
50        }
51    }
52    pub fn value(&self) -> i32 {
53        *self.value.borrow()
54    }
55    pub fn set_value(&mut self, val: i32) {
56        *self.value.borrow_mut() = val;
57        self.value_frame.set_label(&val.to_string());
58        self.main_wid.redraw();
59    }
60}
61
62widget_extends!(MyDial, group::Group, main_wid);
63
64fn main() {
65    let app = app::App::default();
66    app::background(255, 255, 255);
67    let mut win = window::Window::default().with_size(400, 300);
68    let mut dial = MyDial::new(100, 100, 200, 200, "CPU Load %");
69    dial.set_label_size(22);
70    dial.set_label_color(Color::from_u32(0x797979));
71    win.end();
72    win.show();
73
74    // get the cpu load value from somewhere, then call dial.set_value() in a callback or event loop
75    dial.set_value(10);
76
77    app.run().unwrap();
78}