@@ -3,7 +3,37 @@ use std::mem;
33use ui:: UI ;
44use ui_sys:: { self , uiControl, uiProgressBar} ;
55
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+ /// ```
737pub enum ProgressBarValue {
838 /// Represents a set, consistent percentage of the bar to be filled
939 ///
@@ -15,6 +45,25 @@ pub enum ProgressBarValue {
1545 Indeterminate ,
1646}
1747
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+
1867define_control ! {
1968 /// A bar that fills up with a set percentage, used to show completion of a
2069 ///
@@ -34,27 +83,16 @@ impl ProgressBar {
3483 }
3584
3685 /// Create a new indeterminate progress bar
37- pub fn indeterminate ( ) -> ProgressBar {
86+ pub fn indeterminate ( ctx : & UI ) -> ProgressBar {
3887 let mut pb = ProgressBar :: new ( ) ;
39- pb. set_indeterminate ( ) ;
88+ pb. set_value ( ctx , ProgressBarValue :: Indeterminate ) ;
4089 pb
4190 }
4291
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 ( ) {
5896 ProgressBarValue :: Determinate ( value) => {
5997 let value = if value > 100 { 100 } else { value } ;
6098 value as i32
@@ -65,7 +103,7 @@ impl ProgressBar {
65103 }
66104
67105 /// Get the value of the progress bar
68- pub fn value ( & self ) -> ProgressBarValue {
106+ pub fn value ( & self , _ctx : & UI ) -> ProgressBarValue {
69107 let sys_value = unsafe { ui_sys:: uiProgressBarValue ( self . uiProgressBar ) } ;
70108 if sys_value. is_negative ( ) {
71109 assert ! (
0 commit comments