use egui::{Layout, RichText, Ui};
#[derive(Clone, Debug)]
pub struct StatusItem {
pub text: String,
pub tooltip: String,
pub warning: bool,
}
impl StatusItem {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
tooltip: String::new(),
warning: false,
}
}
pub fn tooltip(mut self, tooltip: impl Into<String>) -> Self {
self.tooltip = tooltip.into();
self
}
pub fn warning(mut self) -> Self {
self.warning = true;
self
}
fn show(&self, ui: &mut Ui) {
let mut text = RichText::new(&self.text).small();
if self.warning {
text = text.color(ui.visuals().warn_fg_color);
}
let response = ui.label(text);
if !self.tooltip.is_empty() {
response.on_hover_text(&self.tooltip);
}
}
}
pub fn show_status_bar(ui: &mut Ui, left: &[StatusItem], right: &[StatusItem]) {
ui.horizontal_centered(|ui| {
for (index, item) in left.iter().enumerate() {
if index > 0 {
ui.separator();
}
item.show(ui);
}
ui.with_layout(Layout::right_to_left(egui::Align::Center), |ui| {
for (index, item) in right.iter().enumerate() {
if index > 0 {
ui.separator();
}
item.show(ui);
}
});
});
}
pub fn ellipsize_start(text: &str, max: usize) -> String {
let count = text.chars().count();
if count <= max {
return text.to_owned();
}
if max == 0 {
return String::new();
}
let tail: String = text.chars().skip(count - (max - 1)).collect();
format!("…{tail}")
}
pub fn show_empty_state(ui: &mut Ui, heading: &str, subtitle: &str, button: &str, hint: &str) -> bool {
let mut clicked = false;
ui.vertical_centered(|ui| {
ui.add_space((ui.available_height() * 0.24).max(24.0));
ui.heading(heading);
ui.label(subtitle);
ui.add_space(16.0);
clicked = ui.button(button).clicked();
ui.add_space(8.0);
ui.label(hint);
});
clicked
}
pub fn editor_scroll_style(base: &egui::style::ScrollStyle) -> egui::style::ScrollStyle {
egui::style::ScrollStyle {
floating: true,
floating_width: base.floating_width.max(6.0),
bar_width: base.bar_width.max(10.0),
handle_min_length: base.handle_min_length.max(24.0),
dormant_background_opacity: 0.0,
active_background_opacity: 0.3,
interact_background_opacity: 0.5,
dormant_handle_opacity: 0.55,
active_handle_opacity: 0.75,
interact_handle_opacity: 1.0,
..*base
}
}
pub fn with_editor_scrollbars<R>(ui: &mut Ui, add: impl FnOnce(&mut Ui) -> R) -> R {
let parent = ui.spacing().scroll;
ui.spacing_mut().scroll = editor_scroll_style(&parent);
let result = add(ui);
ui.spacing_mut().scroll = parent;
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn long_paths_keep_their_tail() {
assert_eq!(ellipsize_start("short", 10), "short");
assert_eq!(ellipsize_start("/home/user/project/file.md", 10), "…t/file.md");
}
#[test]
fn editor_scrollbars_stay_visible_at_rest() {
let style = editor_scroll_style(&egui::style::ScrollStyle::default());
assert!(style.floating && style.dormant_handle_opacity > 0.5);
}
}