1use crate::*;
2
3impl Frame<'_> {
4 pub fn rating(&mut self, id: u64, value: &mut usize, max: usize, enabled: bool) {
6 let enabled = enabled && self.enabled;
7 let max = max.clamp(1, 20);
8 *value = (*value).min(max);
9 let row = self.reserve(40.0);
10 let width = (row.width / max as f32).min(36.0);
11 let hovered = (0..max).find(|i| {
12 self.pointer_in(Rect {
13 x: row.x + *i as f32 * width,
14 width,
15 ..row
16 })
17 });
18 let preview = if enabled {
19 hovered.map_or(*value, |i| i + 1)
20 } else {
21 *value
22 };
23 let mut keyboard_handled = false;
24 for i in 0..max {
25 let bounds = Rect {
26 x: row.x + i as f32 * width,
27 width,
28 ..row
29 };
30 let mut r = self.interact(id + i as u64, bounds, enabled);
31 if r.clicked {
32 *value = if *value == i + 1 { 0 } else { i + 1 };
33 r.changed = true;
34 }
35 if r.focused && enabled && !keyboard_handled {
36 let next = if self.pressed_key(Key::Right) || self.pressed_key(Key::Up) {
37 Some((i + 2).min(max))
38 } else if self.pressed_key(Key::Left) || self.pressed_key(Key::Down) {
39 Some(i.max(1))
40 } else if self.pressed_key(Key::Home) {
41 Some(1)
42 } else if self.pressed_key(Key::End) {
43 Some(max)
44 } else {
45 None
46 };
47 if let Some(next) = next {
48 *value = next;
49 self.request_focus(id + (next - 1) as u64);
50 keyboard_handled = true;
51 }
52 }
53 self.focus_ring(r);
54 self.styled_text(
55 bounds,
56 if i < preview { "★" } else { "☆" },
57 if enabled {
58 self.ui.theme.primary
59 } else {
60 self.ui.theme.text.opacity(0.38)
61 },
62 TextStyle {
63 size: 30.0,
64 align: 1,
65 ..TextStyle::default()
66 },
67 );
68 self.semantic(
69 r,
70 Role::Radio,
71 &format!("{} of {} stars", i + 1, max),
72 *value == i + 1,
73 Some(if *value == i + 1 {
74 CheckState::Checked
75 } else {
76 CheckState::Unchecked
77 }),
78 None,
79 None,
80 );
81 }
82 }
83 pub fn rating_fractional(&mut self, id: u64, value: &mut f32, stars: usize, step: f32, enabled: bool) -> Response {
85 self.rating_fractional_with_indicator(id, value, stars, step, enabled, false)
86 }
87 pub fn rating_fractional_with_indicator(&mut self, id: u64, value: &mut f32, stars: usize, step: f32, enabled: bool, indicator: bool) -> Response {
88 self.rating_fractional_directional(id, value, stars, step, enabled, indicator, false)
89 }
90 #[allow(clippy::too_many_arguments)]
92 pub fn rating_fractional_directional(&mut self, id: u64, value: &mut f32, stars: usize, step: f32, enabled: bool, indicator: bool, rtl: bool) -> Response {
93 self.rating_fractional_tinted(id,value,stars,step,enabled,indicator,rtl,[None;3])
94 }
95 #[allow(clippy::too_many_arguments)]
96 pub(crate) fn rating_fractional_tinted(&mut self, id:u64,value:&mut f32,stars:usize,step:f32,enabled:bool,indicator:bool,rtl:bool,colors:[Option<Color>;3])->Response {
97 let stars = stars.clamp(1, 20);
98 let step = if step.is_finite() && step > 0.0 { step.min(stars as f32) } else { 0.5 };
99 *value = if value.is_finite() { value.clamp(0.0, stars as f32) } else { 0.0 };
100 let old = *value;
101 let mut row = self.reserve(40.0);
102 let width = (row.width / stars as f32).min(48.0);
103 if rtl { row.x += row.width - width * stars as f32; }
104 row.width = width * stars as f32;
105 let mut response = self.interact(id, row, enabled && !indicator);
106 if response.enabled && self.ui.active == Some(id) && self.ui.capture == Capture::Pointer
107 && let Some(point) = self.input.pointer {
108 let distance = if rtl { row.x + row.width - point[0] } else { point[0] - row.x };
109 let raw = (distance / width.max(1.0)).clamp(0.0, stars as f32);
110 *value = ((raw / step).ceil() * step).clamp(0.0, stars as f32);
111 }
112 if let Some(key) = self.key_for(response) {
113 *value = match key {
114 Key::Left if rtl => (*value + step).min(stars as f32),
115 Key::Right if rtl => (*value - step).max(0.0),
116 Key::Left | Key::Down => (*value - step).max(0.0),
117 Key::Right | Key::Up => (*value + step).min(stars as f32),
118 Key::Home => 0.0,
119 Key::End => stars as f32,
120 _ => *value,
121 };
122 }
123 response.changed = (*value - old).abs() > 0.0001;
124 let foreground = colors[0].unwrap_or(self.ui.theme.primary.opacity(if enabled && self.enabled { 1.0 } else { 0.38 }));
125 let background = colors[1].unwrap_or(self.ui.theme.on_surface_variant.opacity(if enabled && self.enabled { 0.38 } else { 0.12 }));
126 let index = (crate::beercss_shapes::SHAPES.len() + crate::icons::material_navigation::ICONS.len() + crate::icons::material_symbols::ICONS.len()) as u8;
127 for i in 0..stars {
128 let physical_index = if rtl { stars - 1 - i } else { i };
129 let star = Rect { x: row.x + physical_index as f32 * width, width, ..row };
130 let icon_size = star.width.min(star.height);
131 let icon = Rect { x: star.x + (star.width - icon_size) / 2.0, y: star.y + (star.height - icon_size) / 2.0, width: icon_size, height: icon_size };
132 self.commands.push(DrawCommand::Shape { bounds: icon, index, color: background, rotation: 0.0 });
133 let fill = (*value - i as f32).clamp(0.0, 1.0);
134 if fill > 0.0 && let Some(secondary)=colors[2] {
135 self.commands.push(DrawCommand::Shape { bounds: icon, index, color: secondary, rotation: 0.0 });
136 }
137 if fill > 0.0 {
138 let fill_width = star.width * fill;
139 let fill_x = if rtl { star.x + star.width - fill_width } else { star.x };
140 self.commands.push(DrawCommand::PushClip(Rect { x: fill_x, width: fill_width, ..star }.intersect(self.clip)));
141 self.commands.push(DrawCommand::Shape { bounds: icon, index, color: foreground, rotation: 0.0 });
142 self.commands.push(DrawCommand::PopClip);
143 }
144 }
145 self.focus_ring(response);
146 response
147 }
148}
149
150#[cfg(test)]
151#[path = "rating.spec.rs"]
152mod tests;