rstk/
progressbar.rs

1//! Progressbar widget - displays feedback on progress through a task.
2//!
3//! * also see the Tk [manual](https://www.tcl-lang.org/man/tcl8.6/TkCmd/ttk_progressbar.htm)
4//!
5
6use super::grid;
7use super::pack;
8use super::widget;
9use super::wish;
10
11/// Refers to a progressbar widget
12#[derive(Clone, Debug, PartialEq)]
13pub struct TkProgressbar {
14    pub id: String,
15    mode: widget::ProgressMode,
16}
17
18/// Creates an instance of a progressbar in given parent.
19pub fn make_progressbar(
20    parent: &impl widget::TkWidget,
21    orientation: widget::Orientation,
22    mode: widget::ProgressMode,
23) -> TkProgressbar {
24    let id = wish::next_wid(parent.id());
25    let msg = format!(
26        "ttk::progressbar {} -orient {} -mode {}",
27        id, orientation, mode
28    );
29    wish::tell_wish(&msg);
30
31    TkProgressbar { id, mode }
32}
33
34impl widget::TkWidget for TkProgressbar {
35    /// Returns the widget's id reference - used within tk
36    fn id(&self) -> &str {
37        &self.id
38    }
39}
40
41impl grid::TkGridLayout for TkProgressbar {}
42impl pack::TkPackLayout for TkProgressbar {}
43
44impl TkProgressbar {
45    /// Displayed length of progress bar in pixels.
46    pub fn length(&self, value: u64) {
47        widget::configure(&self.id, "length", &value.to_string());
48    }
49
50    /// Sets the maximum value for the progress bar - defaults to 100.0.
51    pub fn maximum(&self, value: f64) {
52        widget::configure(&self.id, "maximum", &value.to_string());
53    }
54
55    /// Starts auto-increment for the progress bar, updating
56    /// after every 'interval' milliseconds (50 is recommended).
57    pub fn start(&self, interval: u64) {
58        let msg = format!("{} start {}", &self.id, interval);
59        wish::tell_wish(&msg);
60    }
61
62    /// Sets the state of the widget (normal or disabled).
63    pub fn state(&self, value: widget::State) {
64        widget::configure(&self.id, "state", &value.to_string());
65    }
66
67    /// Steps the progress bar manually by given amount.
68    pub fn step(&self, value: f64) {
69        let msg = format!("{} step {}", &self.id, value);
70        wish::tell_wish(&msg);
71    }
72
73    /// Stops auto-increment for the progress bar.
74    pub fn stop(&self) {
75        let msg = format!("{} stop", &self.id);
76        wish::tell_wish(&msg);
77    }
78
79    /// Returns the current value of the progress bar.
80    pub fn value_get(&self) -> f64 {
81        let result = widget::TkWidget::cget(self, "value");
82        if let Ok(value) = result.parse::<f64>() {
83            value
84        } else {
85            0.0
86        }
87    }
88
89    /// Sets the value of the progress bar.
90    pub fn value(&self, value: f64) {
91        widget::configure(&self.id, "value", &value.to_string());
92    }
93}