ced/src/ui/tab_bar.rs

95 lines
4.2 KiB
Rust
Raw Normal View History

2025-07-05 14:42:45 -04:00
use crate::app::{state::UnsavedAction, TextEditor};
use eframe::egui::{self, Frame};
pub(crate) fn tab_bar(app: &mut TextEditor, ctx: &egui::Context) {
let frame = Frame::NONE.fill(ctx.style().visuals.panel_fill);
let tab_bar = egui::TopBottomPanel::top("tab_bar")
2025-07-05 14:42:45 -04:00
.frame(frame)
.show(ctx, |ui| {
egui::ScrollArea::horizontal()
.auto_shrink([false, true])
.scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysHidden)
.scroll_source(egui::scroll_area::ScrollSource::DRAG)
.show(ui, |ui| {
ui.horizontal(|ui| {
let mut tab_to_close_unmodified = None;
let mut tab_to_close_modified = None;
let mut tab_to_switch = None;
let mut add_new_tab = false;
2025-07-05 14:42:45 -04:00
let tabs_len = app.tabs.len();
let active_tab_index = app.active_tab_index;
2025-07-05 14:42:45 -04:00
let tabs_info: Vec<(String, bool)> = app
.tabs
.iter()
.map(|tab| (tab.get_display_title(), tab.is_modified))
.collect();
2025-07-05 14:42:45 -04:00
for (i, (title, is_modified)) in tabs_info.iter().enumerate() {
let is_active = i == active_tab_index;
2025-07-05 14:42:45 -04:00
let mut label_text = if is_active {
egui::RichText::new(title).strong()
} else {
egui::RichText::new(title).color(ui.visuals().weak_text_color())
};
2025-07-05 14:42:45 -04:00
if *is_modified {
label_text = label_text.italics();
}
2025-07-05 14:42:45 -04:00
let tab_response = ui.add(egui::Label::new(label_text).selectable(false).sense(egui::Sense::click()));
if tab_response.clicked() {
tab_to_switch = Some(i);
}
if tabs_len > 1 {
let visuals = ui.visuals();
let close_button = egui::Button::new("×")
.small()
.fill(visuals.panel_fill)
.stroke(egui::Stroke::new(0.0, egui::Color32::from_rgb(0, 0, 0)));
let close_response = ui.add(close_button);
if close_response.clicked() {
if *is_modified {
tab_to_close_modified = Some(i);
} else {
tab_to_close_unmodified = Some(i);
}
}
}
ui.separator();
}
2025-07-05 14:42:45 -04:00
let visuals = ui.visuals();
let add_button = egui::Button::new("+")
2025-07-05 14:42:45 -04:00
.small()
.fill(visuals.panel_fill)
.stroke(egui::Stroke::new(0.0, egui::Color32::from_rgb(0, 0, 0)));
if ui.add(add_button).clicked() {
add_new_tab = true;
2025-07-05 14:42:45 -04:00
}
if let Some(tab_index) = tab_to_switch {
app.switch_to_tab(tab_index);
}
if let Some(tab_index) = tab_to_close_unmodified {
app.close_tab(tab_index);
}
if let Some(tab_index) = tab_to_close_modified {
app.switch_to_tab(tab_index);
app.pending_unsaved_action = Some(UnsavedAction::CloseTab(tab_index));
}
if add_new_tab {
app.add_new_tab();
}
});
});
2025-07-05 14:42:45 -04:00
});
app.tab_bar_rect = Some(tab_bar.response.rect);
2025-07-05 14:42:45 -04:00
}