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);
|
2025-07-22 22:26:48 -04:00
|
|
|
|
let tab_bar = egui::TopBottomPanel::top("tab_bar")
|
2025-07-05 14:42:45 -04:00
|
|
|
|
.frame(frame)
|
|
|
|
|
|
.show(ctx, |ui| {
|
2025-07-22 22:26:48 -04:00
|
|
|
|
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
|
|
|
|
|
2025-07-22 22:26:48 -04:00
|
|
|
|
let tabs_len = app.tabs.len();
|
|
|
|
|
|
let active_tab_index = app.active_tab_index;
|
2025-07-05 14:42:45 -04:00
|
|
|
|
|
2025-07-22 22:26:48 -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
|
|
|
|
|
2025-07-22 22:26:48 -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
|
|
|
|
|
2025-07-22 22:26:48 -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
|
|
|
|
|
2025-07-22 22:26:48 -04:00
|
|
|
|
if *is_modified {
|
|
|
|
|
|
label_text = label_text.italics();
|
|
|
|
|
|
}
|
2025-07-05 14:42:45 -04:00
|
|
|
|
|
2025-07-23 13:17:10 -04:00
|
|
|
|
let tab_response = ui.add(
|
|
|
|
|
|
egui::Label::new(label_text)
|
|
|
|
|
|
.selectable(false)
|
|
|
|
|
|
.sense(egui::Sense::click()),
|
|
|
|
|
|
);
|
2025-07-22 22:26:48 -04:00
|
|
|
|
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)
|
2025-07-23 13:17:10 -04:00
|
|
|
|
.stroke(egui::Stroke::new(
|
|
|
|
|
|
0.0,
|
|
|
|
|
|
egui::Color32::from_rgb(0, 0, 0),
|
|
|
|
|
|
));
|
2025-07-22 22:26:48 -04:00
|
|
|
|
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();
|
2025-07-22 22:26:48 -04:00
|
|
|
|
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)));
|
2025-07-22 22:26:48 -04:00
|
|
|
|
if ui.add(add_button).clicked() {
|
|
|
|
|
|
add_new_tab = true;
|
2025-07-05 14:42:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-22 22:26:48 -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
|
|
|
|
});
|
|
|
|
|
|
|
2025-07-22 22:26:48 -04:00
|
|
|
|
app.tab_bar_rect = Some(tab_bar.response.rect);
|
2025-07-05 14:42:45 -04:00
|
|
|
|
}
|