1pub fn truncate_to_fit(s: &str, available_px: f32, px_per_char: f32) -> String {
27 if available_px <= 0.0 || px_per_char <= 0.0 {
28 return String::new();
29 }
30
31 let max_chars = (available_px / px_per_char).floor() as usize;
32 let char_count = s.chars().count();
33
34 if char_count <= max_chars {
35 s.to_string()
37 } else if max_chars <= 1 {
38 "…".to_string()
40 } else {
41 let mut out: String = s.chars().take(max_chars - 1).collect();
43 out.push('…');
44 out
45 }
46}
47
48pub fn thin_scrollbar() -> iced::widget::scrollable::Direction {
54 iced::widget::scrollable::Direction::Vertical(
55 iced::widget::scrollable::Scrollbar::new()
56 .width(6)
57 .scroller_width(4),
58 )
59}
60
61pub fn context_menu_separator<'a, M: 'a>() -> iced::Element<'a, M> {
65 iced::widget::container(iced::widget::Space::new(0, 1))
66 .padding(iced::Padding {
67 top: 4.0,
68 right: 0.0,
69 bottom: 4.0,
70 left: 0.0,
71 })
72 .width(iced::Length::Fill)
73 .into()
74}
75
76pub fn context_menu_header<'a, M: 'a>(label: String, muted: iced::Color) -> iced::Element<'a, M> {
78 iced::widget::container(iced::widget::text(label).size(12).color(muted))
79 .padding(iced::Padding {
80 top: 8.0,
81 right: 14.0,
82 bottom: 6.0,
83 left: 14.0,
84 })
85 .width(iced::Length::Fill)
86 .into()
87}
88
89pub fn centered_placeholder<'a>(
93 icon_char: char,
94 icon_size: u16,
95 label_text: &str,
96 muted: iced::Color,
97) -> iced::Element<'a, crate::message::Message> {
98 use iced::widget::{column, container, text, Space};
99 use iced::{Alignment, Length};
100
101 let icon_widget = icon!(icon_char, icon_size, muted);
102 let label = text(label_text.to_string()).size(14).color(muted);
103
104 container(
105 column![icon_widget, Space::new(0, 8), label]
106 .spacing(4)
107 .align_x(Alignment::Center),
108 )
109 .width(Length::Fill)
110 .height(Length::Fill)
111 .center_x(Length::Fill)
112 .center_y(Length::Fill)
113 .into()
114}
115
116pub fn on_press_maybe<'a>(
123 btn: iced::widget::Button<'a, crate::message::Message>,
124 msg: Option<crate::message::Message>,
125) -> iced::widget::Button<'a, crate::message::Message> {
126 match msg {
127 Some(m) => btn.on_press(m),
128 None => btn,
129 }
130}
131
132pub fn collapsible_header<'a>(
137 expanded: bool,
138 label: &'a str,
139 count: usize,
140 on_toggle: crate::message::Message,
141 muted: iced::Color,
142) -> iced::Element<'a, crate::message::Message> {
143 use iced::widget::{button, row, text, Space};
144 use iced::Alignment;
145
146 let chevron_char = if expanded {
147 crate::icons::CHEVRON_DOWN
148 } else {
149 crate::icons::CHEVRON_RIGHT
150 };
151 let chevron = icon!(chevron_char, 11, muted);
152
153 button(
154 row![
155 chevron,
156 Space::new(4, 0),
157 text(label).size(11).color(muted),
158 Space::new(4, 0),
159 text(format!("({count})")).size(10).color(muted),
160 ]
161 .align_y(Alignment::Center),
162 )
163 .padding([4, 8])
164 .width(iced::Length::Fill)
165 .style(crate::theme::ghost_button)
166 .on_press(on_toggle)
167 .into()
168}
169
170pub fn toolbar_btn<'a>(
175 icon_widget: impl Into<iced::Element<'a, crate::message::Message>>,
176 label: &'a str,
177 msg: crate::message::Message,
178) -> iced::widget::Button<'a, crate::message::Message> {
179 use iced::widget::{button, row, text, Space};
180 use iced::Alignment;
181
182 button(
183 row![icon_widget.into(), Space::new(4, 0), text(label).size(12)].align_y(Alignment::Center),
184 )
185 .padding([4, 10])
186 .style(crate::theme::toolbar_button)
187 .on_press(msg)
188}
189
190pub fn surface_panel<'a>(
194 content: impl Into<iced::Element<'a, crate::message::Message>>,
195 width: iced::Length,
196) -> iced::Element<'a, crate::message::Message> {
197 iced::widget::container(content)
198 .width(width)
199 .height(iced::Length::Fill)
200 .style(crate::theme::surface_style)
201 .into()
202}
203
204pub fn empty_list_hint<'a>(
208 label: &'a str,
209 muted: iced::Color,
210) -> iced::Element<'a, crate::message::Message> {
211 iced::widget::container(iced::widget::text(label.to_string()).size(12).color(muted))
212 .padding([12, 8])
213 .width(iced::Length::Fill)
214 .center_x(iced::Length::Fill)
215 .into()
216}
217
218#[cfg(test)]
221mod tests {
222 use super::truncate_to_fit;
223
224 #[test]
227 fn short_string_returned_unchanged() {
228 assert_eq!(truncate_to_fit("hi", 100.0, 7.0), "hi");
229 }
230
231 #[test]
232 fn string_exactly_at_limit_is_not_truncated() {
233 assert_eq!(truncate_to_fit("abc", 30.0, 10.0), "abc");
235 }
236
237 #[test]
238 fn empty_string_returned_unchanged() {
239 assert_eq!(truncate_to_fit("", 100.0, 7.0), "");
240 }
241
242 #[test]
245 fn long_string_truncated_with_ellipsis() {
246 let result = truncate_to_fit("hello world", 30.0, 7.0);
248 assert_eq!(result, "hel…");
249 assert!(result.ends_with('…'));
250 }
251
252 #[test]
253 fn result_respects_max_char_budget() {
254 let result = truncate_to_fit("abcdefghij", 50.0, 10.0);
256 assert_eq!(result.chars().count(), 5);
257 assert!(result.ends_with('…'));
258 }
259
260 #[test]
261 fn one_char_over_limit_gives_ellipsis_only() {
262 let result = truncate_to_fit("ab", 10.0, 10.0);
264 assert_eq!(result, "…");
265 }
266
267 #[test]
268 fn branch_name_with_slash_truncated_correctly() {
269 let name = "mario/MARIO-3924_global_design_system_library_publishing";
272 let result = truncate_to_fit(name, 120.0, 7.5);
273 assert_eq!(result.chars().count(), 16);
274 assert!(result.ends_with('…'));
275 assert!(result.starts_with("mario/MARIO-392"));
276 }
277
278 #[test]
279 fn commit_summary_short_enough_shows_fully() {
280 let summary = "Fix typo in README";
281 let result = truncate_to_fit(summary, 500.0, 7.0);
283 assert_eq!(result, summary);
284 }
285
286 #[test]
287 fn commit_summary_too_long_gets_ellipsis() {
288 let summary =
289 "CARTS-2149: Serialize MediaPickerOptions nav args as URI-encoded JSON strings";
290 let result = truncate_to_fit(summary, 300.0, 7.0);
292 assert_eq!(result.chars().count(), 42);
293 assert!(result.ends_with('…'));
294 }
295
296 #[test]
297 fn stash_message_truncated_correctly() {
298 let msg = "WIP on mario/MARIO-3869_fix_icons_svg_parsing: f51116a10d4";
299 let result = truncate_to_fit(msg, 200.0, 6.5);
301 assert_eq!(result.chars().count(), 30);
302 assert!(result.ends_with('…'));
303 assert!(result.starts_with("WIP on mario/MARIO-3869_fix_i"));
304 }
305
306 #[test]
309 fn zero_available_px_returns_empty() {
310 assert_eq!(truncate_to_fit("hello", 0.0, 7.0), "");
311 }
312
313 #[test]
314 fn negative_available_px_returns_empty() {
315 assert_eq!(truncate_to_fit("hello", -10.0, 7.0), "");
316 }
317
318 #[test]
319 fn zero_px_per_char_returns_empty() {
320 assert_eq!(truncate_to_fit("hello", 100.0, 0.0), "");
321 }
322
323 #[test]
324 fn single_char_string_fits_in_one_char_budget() {
325 assert_eq!(truncate_to_fit("a", 10.0, 10.0), "a");
327 }
328
329 #[test]
330 fn unicode_multibyte_chars_counted_by_char_not_byte() {
331 let result = truncate_to_fit("héllo", 40.0, 10.0);
333 assert_eq!(result.chars().count(), 4);
334 assert_eq!(result, "hél…");
335 }
336
337 #[test]
338 fn unicode_ellipsis_in_source_not_duplicated() {
339 let s = "short…";
341 let result = truncate_to_fit(s, 200.0, 7.0);
342 assert_eq!(result, s);
343 }
344
345 #[test]
346 fn very_small_px_per_char_truncates_to_many_chars() {
347 let result = truncate_to_fit("helloworld", 200.0, 1.0);
349 assert_eq!(result, "helloworld");
350 }
351
352 #[test]
353 fn fractional_px_per_char_floors_correctly() {
354 let result = truncate_to_fit("abcd", 25.0, 7.5);
356 assert_eq!(result, "ab…");
357 }
358}