1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: MPL-2.0
//! A horizontal bar that continuously fills as an action progresses.
use crate::prelude::*;
impl Ui {
/// Creates a new [`ProgressBar`].
pub fn create_progress_bar<'ui>(&'ui self) -> Result<&'ui mut ProgressBar, crate::Error> {
unsafe { call_libui_new_fn!(ui: self, fn: uiNewProgressBar() -> ProgressBar) }
}
}
/// A horizontal bar that continuously fills as an action progresses.
#[subcontrol(handle = "uiProgressBar")]
pub struct ProgressBar;
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum State {
Value(NonNegativeInt),
Indefinite,
}
impl ProgressBar<'_> {
/// The current state of this progress bar.
///
/// This returns `State::Value(0)` by default.
pub fn state(&self) -> State {
let value = unsafe { uiProgressBarValue(self.as_ptr()) };
if value >= 0 {
debug_assert!(value <= 100, "invalid progress bar value");
State::Value(unsafe { NonNegativeInt::from_libui(value) })
} else {
debug_assert_eq!(value, -1, "invalid progress bar value");
State::Indefinite
}
}
pub fn value(&self) -> Option<NonNegativeInt> {
match self.state() {
State::Value(it) => Some(it),
State::Indefinite => None,
}
}
pub fn is_indefinite(&self) -> bool {
matches!(self.state(), State::Indefinite)
}
/// Sets the value of this progress bar.
///
/// [`state`](Self::state) will return `State::Value(...)` after this is called.
///
/// # Panics
///
/// Panics if `value` is greater than 100.
pub fn set_value(&self, value: impl Into<NonNegativeInt>) {
let value = value.into().to_libui();
assert!(value <= 100);
unsafe { uiProgressBarSetValue(self.as_ptr(), value) };
}
/// Marks this progress bar as being indefinite.
///
/// By convention, this causes the progress bar to continuously rotate a colored inner bar,
/// indicating that an action is in progress but it is unknown when the action will complete.
///
/// To mark this progress bar as being definite again, call [`set_value`](Self::set_value) with
/// the progress.
pub fn set_as_indefinite(&self) {
unsafe { uiProgressBarSetValue(self.as_ptr(), -1) };
}
}