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") .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; let tabs_len = app.tabs.len(); let active_tab_index = app.active_tab_index; let tabs_info: Vec<(String, bool)> = app .tabs .iter() .map(|tab| (tab.get_display_title(), tab.is_modified)) .collect(); for (i, (title, is_modified)) in tabs_info.iter().enumerate() { let is_active = i == active_tab_index; let mut label_text = if is_active { egui::RichText::new(title).strong() } else { egui::RichText::new(title).color(ui.visuals().weak_text_color()) }; if *is_modified { label_text = label_text.italics(); } 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(); } let visuals = ui.visuals(); let add_button = egui::Button::new("+") .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; } 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(); } }); }); }); app.tab_bar_rect = Some(tab_bar.response.rect); }