@@ -3,7 +3,37 @@ use std::mem;
3
3
use ui:: UI ;
4
4
use ui_sys:: { self , uiControl, uiProgressBar} ;
5
5
6
- /// An enum representing the value of a `ProgressBar`
6
+ /// An enum representing the value of a `ProgressBar`.
7
+ ///
8
+ /// # Values
9
+ ///
10
+ /// A `ProgressBarValue` can be either `Determinate`, a number from 0 up to 100, or
11
+ /// `Indeterminate`, representing a process that is still in progress but has no
12
+ /// completeness metric availble.
13
+ ///
14
+ /// # Conversions
15
+ ///
16
+ /// A `ProgressBarValue` can be made from a `u32` or an `Option<u32>`, and the relevant functions
17
+ /// take a type that is generic over this behavior, so it's easy to set the progress of a bar.
18
+ ///
19
+ /// ```
20
+ /// # use iui::prelude::*;
21
+ /// # use iui::controls::{ProgressBar, ProgressBarValue};
22
+ /// # let ui = UI::init().unwrap();
23
+ /// # let mut window = Window::new(&ui, "Test Window", 0, 0, WindowType::NoMenubar);
24
+ /// let mut progressbar = ProgressBar::indeterminate(&ui);
25
+ /// progressbar.set_value(&ui, 54);
26
+ ///
27
+ /// // Perhaps this is the result of some fallible progress-checking function.
28
+ /// let maybe_progress: Option<u32> = None;
29
+ /// progressbar.set_value(&ui, maybe_progress);
30
+ ///
31
+ /// // And of course, you can always set it by hand.
32
+ /// progressbar.set_value(&ui, ProgressBarValue::Indeterminate);
33
+ /// # window.set_child(&ui, progressbar);
34
+ /// # ui.quit();
35
+ /// # ui.main();
36
+ /// ```
7
37
pub enum ProgressBarValue {
8
38
/// Represents a set, consistent percentage of the bar to be filled
9
39
///
@@ -15,6 +45,25 @@ pub enum ProgressBarValue {
15
45
Indeterminate ,
16
46
}
17
47
48
+ impl From < u32 > for ProgressBarValue {
49
+ fn from ( value : u32 ) -> ProgressBarValue {
50
+ if value <= 100 {
51
+ ProgressBarValue :: Determinate ( value)
52
+ } else {
53
+ ProgressBarValue :: Determinate ( 100 )
54
+ }
55
+ }
56
+ }
57
+
58
+ impl From < Option < u32 > > for ProgressBarValue {
59
+ fn from ( value : Option < u32 > ) -> ProgressBarValue {
60
+ match value {
61
+ Some ( v) => v. into ( ) ,
62
+ None => ProgressBarValue :: Indeterminate
63
+ }
64
+ }
65
+ }
66
+
18
67
define_control ! {
19
68
/// A bar that fills up with a set percentage, used to show completion of a
20
69
///
@@ -34,27 +83,16 @@ impl ProgressBar {
34
83
}
35
84
36
85
/// Create a new indeterminate progress bar
37
- pub fn indeterminate ( ) -> ProgressBar {
86
+ pub fn indeterminate ( ctx : & UI ) -> ProgressBar {
38
87
let mut pb = ProgressBar :: new ( ) ;
39
- pb. set_indeterminate ( ) ;
88
+ pb. set_value ( ctx , ProgressBarValue :: Indeterminate ) ;
40
89
pb
41
90
}
42
91
43
- /// Set the value of the progress bar to a determinate value
44
- ///
45
- /// If `value` is larger than 100, than the value will be set to 100.
46
- pub fn set_determinate ( & mut self , value : u32 ) {
47
- self . set_value ( ProgressBarValue :: Determinate ( value) ) ;
48
- }
49
-
50
- /// Set the value of the progress bar to be indeterminate
51
- pub fn set_indeterminate ( & mut self ) {
52
- self . set_value ( ProgressBarValue :: Indeterminate ) ;
53
- }
54
-
55
- /// Set the value of the progress bar
56
- pub fn set_value ( & mut self , value : ProgressBarValue ) {
57
- let sys_value = match value {
92
+ /// Set the value of the progress bar. See [`ProgressBarValue`] for the values that can be passed in.
93
+ /// [`ProgressBarValue`]: enum.ProgressBarValue.html
94
+ pub fn set_value < V : Into < ProgressBarValue > > ( & mut self , _ctx : & UI , value : V ) {
95
+ let sys_value = match value. into ( ) {
58
96
ProgressBarValue :: Determinate ( value) => {
59
97
let value = if value > 100 { 100 } else { value } ;
60
98
value as i32
@@ -65,7 +103,7 @@ impl ProgressBar {
65
103
}
66
104
67
105
/// Get the value of the progress bar
68
- pub fn value ( & self ) -> ProgressBarValue {
106
+ pub fn value ( & self , _ctx : & UI ) -> ProgressBarValue {
69
107
let sys_value = unsafe { ui_sys:: uiProgressBarValue ( self . uiProgressBar ) } ;
70
108
if sys_value. is_negative ( ) {
71
109
assert ! (
0 commit comments