|
| 1 | +use super::Control; |
| 2 | +use std::mem; |
| 3 | +use ui::UI; |
| 4 | +use ui_sys::{self, uiControl, uiProgressBar}; |
| 5 | + |
| 6 | +define_control! { |
| 7 | + /// A bar that fills up with a set percentage, used to show completion of a task |
| 8 | + rust_type: ProgressBar, |
| 9 | + sys_type: uiProgressBar, |
| 10 | +} |
| 11 | + |
| 12 | +pub enum ProgressBarStyle { |
| 13 | + Determinate(i32), |
| 14 | + Indeterminate, |
| 15 | +} |
| 16 | + |
| 17 | +impl ProgressBar { |
| 18 | + pub fn new() -> ProgressBar { |
| 19 | + unsafe { ProgressBar::from_raw(ui_sys::uiNewProgressBar()) } |
| 20 | + } |
| 21 | + |
| 22 | + pub fn indeterminate() -> ProgressBar { |
| 23 | + let mut pb = ProgressBar::new(); |
| 24 | + pb.set_value(ProgressBarStyle::Indeterminate); |
| 25 | + pb |
| 26 | + } |
| 27 | + |
| 28 | + pub fn set_value(&mut self, value: ProgressBarStyle) { |
| 29 | + let sys_value = match value { |
| 30 | + ProgressBarStyle::Determinate(value) => { |
| 31 | + // use !is_negative() because 0 is a valid value, but it |
| 32 | + // returns false for is_positive() |
| 33 | + assert!( |
| 34 | + !value.is_negative(), |
| 35 | + "determinate value for ProgressBar must not be negative" |
| 36 | + ); |
| 37 | + value |
| 38 | + } |
| 39 | + ProgressBarStyle::Indeterminate => -1, |
| 40 | + }; |
| 41 | + unsafe { ui_sys::uiProgressBarSetValue(self.uiProgressBar, sys_value) } |
| 42 | + } |
| 43 | + |
| 44 | + pub fn set_determinate(&mut self, value: i32) { |
| 45 | + self.set_value(ProgressBarStyle::Determinate(value)); |
| 46 | + } |
| 47 | + |
| 48 | + pub fn value(&self) -> ProgressBarStyle { |
| 49 | + let sys_value = unsafe { ui_sys::uiProgressBarValue(self.uiProgressBar) }; |
| 50 | + if sys_value.is_negative() { |
| 51 | + assert!( |
| 52 | + sys_value == -1, |
| 53 | + "if ProgressBar value is negative it should only be -1" |
| 54 | + ); |
| 55 | + ProgressBarStyle::Indeterminate |
| 56 | + } else { |
| 57 | + ProgressBarStyle::Determinate(sys_value) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments