Skip to content

Commit 9551081

Browse files
coolreader18NoraCodes
authored andcommitted
Add ProgressBar
1 parent 2e3343b commit 9551081

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

iui/src/controls/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Available user interface controls and related functionality.
2-
//!
2+
//!
33
//! Note that `Control` and all specific control types are references to memory which is owned by the UI library.
44
55
use ui::UI;
@@ -17,9 +17,11 @@ mod layout;
1717
pub use self::layout::*;
1818
mod entry;
1919
pub use self::entry::*;
20+
mod progressbar;
21+
pub use self::progressbar::*;
2022

2123
/// A generic UI control. Any UI control can be turned into this type.
22-
///
24+
///
2325
/// Note that `Control` and all specific control types are references
2426
/// whose memory is owned by the UI library.
2527
pub struct Control {

iui/src/controls/progressbar.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)