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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::fmt::Debug;
use kas::prelude::*;
#[derive(Clone, Debug, Default, Widget)]
pub struct ProgressBar<D: Directional> {
#[widget_core]
core: CoreData,
direction: D,
width: i32,
value: f32,
}
impl<D: Directional + Default> ProgressBar<D> {
#[inline]
pub fn new() -> Self {
ProgressBar::new_with_direction(D::default())
}
}
impl<D: Directional> ProgressBar<D> {
#[inline]
pub fn new_with_direction(direction: D) -> Self {
ProgressBar {
core: Default::default(),
direction,
width: 0,
value: 0.0,
}
}
#[inline]
pub fn with_value(mut self, value: f32) -> Self {
self.value = value.max(0.0).min(1.0);
self
}
#[inline]
pub fn value(&self) -> f32 {
self.value
}
#[allow(clippy::float_cmp)]
pub fn set_value(&mut self, value: f32) -> TkAction {
let value = value.max(0.0).min(1.0);
if value == self.value {
TkAction::empty()
} else {
self.value = value;
TkAction::REDRAW
}
}
}
impl<D: Directional> Layout for ProgressBar<D> {
fn size_rules(&mut self, size_handle: &mut dyn SizeHandle, axis: AxisInfo) -> SizeRules {
let mut size = size_handle.progress_bar();
if self.direction.is_vertical() {
size = size.transpose();
}
let margins = (0, 0);
if self.direction.is_vertical() == axis.is_vertical() {
SizeRules::new(size.0, size.0, margins, Stretch::High)
} else {
self.width = size.1;
SizeRules::fixed(size.1, margins)
}
}
fn set_rect(&mut self, _: &mut Manager, rect: Rect, align: AlignHints) {
let mut ideal_size = Size::splat(self.width);
ideal_size.set_component(self.direction, i32::MAX);
let rect = align
.complete(Align::Centre, Align::Centre)
.aligned_rect(ideal_size, rect);
self.core.rect = rect;
}
fn draw(&self, draw_handle: &mut dyn DrawHandle, mgr: &ManagerState, disabled: bool) {
let dir = self.direction.as_direction();
let state = self.input_state(mgr, disabled);
draw_handle.progress_bar(self.core.rect, dir, state, self.value);
}
}